MagickCore 6.9.13-50
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
draw.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% DDDD RRRR AAA W W %
7% D D R R A A W W %
8% D D RRRR AAAAA W W W %
9% D D R RN A A WW WW %
10% DDDD R R A A W W %
11% %
12% %
13% MagickCore Image Drawing Methods %
14% %
15% %
16% Software Design %
17% Cristy %
18% July 1998 %
19% %
20% %
21% Copyright 1999 ImageMagick Studio LLC, a non-profit organization %
22% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% https://imagemagick.org/license/ %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37% Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38% rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39% Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40% (www.appligent.com) contributed the dash pattern, linecap stroking
41% algorithm, and minor rendering improvements.
42%
43*/
44
45/*
46 Include declarations.
47*/
48#include "magick/studio.h"
49#include "magick/annotate.h"
50#include "magick/artifact.h"
51#include "magick/blob.h"
52#include "magick/cache.h"
53#include "magick/cache-private.h"
54#include "magick/cache-view.h"
55#include "magick/channel.h"
56#include "magick/color.h"
57#include "magick/color-private.h"
58#include "magick/colorspace.h"
59#include "magick/colorspace-private.h"
60#include "magick/composite.h"
61#include "magick/composite-private.h"
62#include "magick/constitute.h"
63#include "magick/draw.h"
64#include "magick/draw-private.h"
65#include "magick/enhance.h"
66#include "magick/exception.h"
67#include "magick/exception-private.h"
68#include "magick/gem.h"
69#include "magick/geometry.h"
70#include "magick/image-private.h"
71#include "magick/list.h"
72#include "magick/log.h"
73#include "magick/magick.h"
74#include "magick/memory-private.h"
75#include "magick/monitor.h"
76#include "magick/monitor-private.h"
77#include "magick/option.h"
78#include "magick/paint.h"
79#include "magick/pixel-accessor.h"
80#include "magick/pixel-private.h"
81#include "magick/property.h"
82#include "magick/resample.h"
83#include "magick/resample-private.h"
84#include "magick/resource_.h"
85#include "magick/splay-tree.h"
86#include "magick/string_.h"
87#include "magick/string-private.h"
88#include "magick/thread-private.h"
89#include "magick/token.h"
90#include "magick/transform.h"
91#include "magick/utility.h"
92
93/*
94 Define declarations.
95*/
96#define AntialiasThreshold (1.0/3.0)
97#define BezierQuantum 200
98#define PrimitiveExtentPad 4296.0
99#define MaxBezierCoordinates 67108864
100#define ThrowPointExpectedException(image,token) \
101{ \
102 (void) ThrowMagickException(&(image)->exception,GetMagickModule(),DrawError, \
103 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
104 status=MagickFalse; \
105 break; \
106}
107
108/*
109 Typedef declarations.
110*/
111typedef struct _EdgeInfo
112{
113 SegmentInfo
114 bounds;
115
116 double
117 scanline;
118
119 PointInfo
120 *points;
121
122 size_t
123 number_points;
124
125 ssize_t
126 direction;
127
128 MagickBooleanType
129 ghostline;
130
131 size_t
132 highwater;
133} EdgeInfo;
134
135typedef struct _ElementInfo
136{
137 double
138 cx,
139 cy,
140 major,
141 minor,
142 angle;
143} ElementInfo;
144
145typedef struct _MVGInfo
146{
147 PrimitiveInfo
148 **primitive_info;
149
150 size_t
151 *extent;
152
153 ssize_t
154 offset;
155
156 PointInfo
157 point;
158
159 ExceptionInfo
160 *exception;
161} MVGInfo;
162
163typedef struct _PolygonInfo
164{
165 EdgeInfo
166 *edges;
167
168 size_t
169 number_edges;
170} PolygonInfo;
171
172typedef enum
173{
174 MoveToCode,
175 OpenCode,
176 GhostlineCode,
177 LineToCode,
178 EndCode
179} PathInfoCode;
180
181typedef struct _PathInfo
182{
183 PointInfo
184 point;
185
186 PathInfoCode
187 code;
188} PathInfo;
189
190/*
191 Forward declarations.
192*/
193static Image
194 *DrawClippingMask(Image *,const DrawInfo *,const char *,const char *,
195 ExceptionInfo *);
196
197static MagickBooleanType
198 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *),
199 RenderMVGContent(Image *,const DrawInfo *,const size_t),
200 TraceArc(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
201 TraceArcPath(MVGInfo *,const PointInfo,const PointInfo,const PointInfo,
202 const double,const MagickBooleanType,const MagickBooleanType),
203 TraceBezier(MVGInfo *,const size_t),
204 TraceCircle(MVGInfo *,const PointInfo,const PointInfo),
205 TraceEllipse(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
206 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
207 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
208 TraceRoundRectangle(MVGInfo *,const PointInfo,const PointInfo,PointInfo),
209 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
210
211static PrimitiveInfo
212 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *,ExceptionInfo *);
213
214static ssize_t
215 TracePath(Image *,MVGInfo *,const char *);
216
217/*
218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219% %
220% %
221% %
222% A c q u i r e D r a w I n f o %
223% %
224% %
225% %
226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227%
228% AcquireDrawInfo() returns a DrawInfo structure properly initialized.
229%
230% The format of the AcquireDrawInfo method is:
231%
232% DrawInfo *AcquireDrawInfo(void)
233%
234*/
235MagickExport DrawInfo *AcquireDrawInfo(void)
236{
237 DrawInfo
238 *draw_info;
239
240 draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
241 GetDrawInfo((ImageInfo *) NULL,draw_info);
242 return(draw_info);
243}
244
245/*
246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247% %
248% %
249% %
250% C l o n e D r a w I n f o %
251% %
252% %
253% %
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255%
256% CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
257% is specified, a new DrawInfo structure is created initialized to default
258% values.
259%
260% The format of the CloneDrawInfo method is:
261%
262% DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
263% const DrawInfo *draw_info)
264%
265% A description of each parameter follows:
266%
267% o image_info: the image info.
268%
269% o draw_info: the draw info.
270%
271*/
272MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
273 const DrawInfo *draw_info)
274{
275 DrawInfo
276 *clone_info;
277
278 clone_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*clone_info));
279 GetDrawInfo(image_info,clone_info);
280 if (draw_info == (DrawInfo *) NULL)
281 return(clone_info);
282 if (draw_info->id != (char *) NULL)
283 (void) CloneString(&clone_info->id,draw_info->id);
284 if (draw_info->primitive != (char *) NULL)
285 (void) CloneString(&clone_info->primitive,draw_info->primitive);
286 if (draw_info->geometry != (char *) NULL)
287 (void) CloneString(&clone_info->geometry,draw_info->geometry);
288 clone_info->compliance=draw_info->compliance;
289 clone_info->viewbox=draw_info->viewbox;
290 clone_info->affine=draw_info->affine;
291 clone_info->gravity=draw_info->gravity;
292 clone_info->fill=draw_info->fill;
293 clone_info->stroke=draw_info->stroke;
294 clone_info->stroke_width=draw_info->stroke_width;
295 if (draw_info->fill_pattern != (Image *) NULL)
296 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
297 &draw_info->fill_pattern->exception);
298 else
299 if (draw_info->tile != (Image *) NULL)
300 clone_info->fill_pattern=CloneImage(draw_info->tile,0,0,MagickTrue,
301 &draw_info->tile->exception);
302 clone_info->tile=NewImageList(); /* tile is deprecated */
303 if (draw_info->stroke_pattern != (Image *) NULL)
304 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
305 MagickTrue,&draw_info->stroke_pattern->exception);
306 clone_info->stroke_antialias=draw_info->stroke_antialias;
307 clone_info->text_antialias=draw_info->text_antialias;
308 clone_info->fill_rule=draw_info->fill_rule;
309 clone_info->linecap=draw_info->linecap;
310 clone_info->linejoin=draw_info->linejoin;
311 clone_info->miterlimit=draw_info->miterlimit;
312 clone_info->dash_offset=draw_info->dash_offset;
313 clone_info->decorate=draw_info->decorate;
314 clone_info->compose=draw_info->compose;
315 if (draw_info->text != (char *) NULL)
316 (void) CloneString(&clone_info->text,draw_info->text);
317 if (draw_info->font != (char *) NULL)
318 (void) CloneString(&clone_info->font,draw_info->font);
319 if (draw_info->metrics != (char *) NULL)
320 (void) CloneString(&clone_info->metrics,draw_info->metrics);
321 if (draw_info->family != (char *) NULL)
322 (void) CloneString(&clone_info->family,draw_info->family);
323 clone_info->style=draw_info->style;
324 clone_info->stretch=draw_info->stretch;
325 clone_info->weight=draw_info->weight;
326 if (draw_info->encoding != (char *) NULL)
327 (void) CloneString(&clone_info->encoding,draw_info->encoding);
328 clone_info->pointsize=draw_info->pointsize;
329 clone_info->kerning=draw_info->kerning;
330 clone_info->interline_spacing=draw_info->interline_spacing;
331 clone_info->interword_spacing=draw_info->interword_spacing;
332 clone_info->direction=draw_info->direction;
333 if (draw_info->density != (char *) NULL)
334 (void) CloneString(&clone_info->density,draw_info->density);
335 clone_info->align=draw_info->align;
336 clone_info->undercolor=draw_info->undercolor;
337 clone_info->border_color=draw_info->border_color;
338 if (draw_info->server_name != (char *) NULL)
339 (void) CloneString(&clone_info->server_name,draw_info->server_name);
340 if (draw_info->dash_pattern != (double *) NULL)
341 {
342 ssize_t
343 x;
344
345 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
346 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) (2*x+2),
347 sizeof(*clone_info->dash_pattern));
348 if (clone_info->dash_pattern == (double *) NULL)
349 ThrowFatalException(ResourceLimitFatalError,
350 "UnableToAllocateDashPattern");
351 (void) memset(clone_info->dash_pattern,0,(size_t) (2*x+2)*
352 sizeof(*clone_info->dash_pattern));
353 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(size_t)
354 (x+1)*sizeof(*clone_info->dash_pattern));
355 }
356 clone_info->gradient=draw_info->gradient;
357 if (draw_info->gradient.stops != (StopInfo *) NULL)
358 {
359 size_t
360 number_stops;
361
362 number_stops=clone_info->gradient.number_stops;
363 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
364 number_stops,sizeof(*clone_info->gradient.stops));
365 if (clone_info->gradient.stops == (StopInfo *) NULL)
366 ThrowFatalException(ResourceLimitFatalError,
367 "UnableToAllocateDashPattern");
368 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
369 (size_t) number_stops*sizeof(*clone_info->gradient.stops));
370 }
371 clone_info->bounds=draw_info->bounds;
372 clone_info->fill_opacity=draw_info->fill_opacity;
373 clone_info->stroke_opacity=draw_info->stroke_opacity;
374 clone_info->element_reference=draw_info->element_reference;
375 clone_info->clip_path=draw_info->clip_path;
376 clone_info->clip_units=draw_info->clip_units;
377 if (draw_info->clip_mask != (char *) NULL)
378 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
379 if (draw_info->clipping_mask != (Image *) NULL)
380 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
381 MagickTrue,&draw_info->clipping_mask->exception);
382 if (draw_info->composite_mask != (Image *) NULL)
383 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
384 MagickTrue,&draw_info->composite_mask->exception);
385 clone_info->render=draw_info->render;
386 clone_info->debug=draw_info->debug;
387 return(clone_info);
388}
389
390/*
391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392% %
393% %
394% %
395+ C o n v e r t P a t h T o P o l y g o n %
396% %
397% %
398% %
399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400%
401% ConvertPathToPolygon() converts a path to the more efficient sorted
402% rendering form.
403%
404% The format of the ConvertPathToPolygon method is:
405%
406% PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info,
407% ExceptionInfo *exception)
408%
409% A description of each parameter follows:
410%
411% o ConvertPathToPolygon() returns the path in a more efficient sorted
412% rendering form of type PolygonInfo.
413%
414% o draw_info: Specifies a pointer to an DrawInfo structure.
415%
416% o path_info: Specifies a pointer to an PathInfo structure.
417%
418% o exception: return any errors or warnings in this structure.
419%
420*/
421
422static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
423{
424 ssize_t
425 i;
426
427 if (polygon_info->edges != (EdgeInfo *) NULL)
428 {
429 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
430 if (polygon_info->edges[i].points != (PointInfo *) NULL)
431 polygon_info->edges[i].points=(PointInfo *)
432 RelinquishMagickMemory(polygon_info->edges[i].points);
433 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
434 polygon_info->edges);
435 }
436 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
437}
438
439#if defined(__cplusplus) || defined(c_plusplus)
440extern "C" {
441#endif
442
443static int DrawCompareEdges(const void *p_edge,const void *q_edge)
444{
445#define DrawCompareEdge(p,q) \
446{ \
447 if (((p)-(q)) < 0.0) \
448 return(-1); \
449 if (((p)-(q)) > 0.0) \
450 return(1); \
451}
452
453 const PointInfo
454 *p,
455 *q;
456
457 /*
458 Edge sorting for right-handed coordinate system.
459 */
460 p=((const EdgeInfo *) p_edge)->points;
461 q=((const EdgeInfo *) q_edge)->points;
462 DrawCompareEdge(p[0].y,q[0].y);
463 DrawCompareEdge(p[0].x,q[0].x);
464 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
465 (q[1].x-q[0].x));
466 DrawCompareEdge(p[1].y,q[1].y);
467 DrawCompareEdge(p[1].x,q[1].x);
468 return(0);
469}
470
471#if defined(__cplusplus) || defined(c_plusplus)
472}
473#endif
474
475static void LogPolygonInfo(const PolygonInfo *polygon_info)
476{
477 EdgeInfo
478 *p;
479
480 ssize_t
481 i,
482 j;
483
484 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
485 p=polygon_info->edges;
486 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
487 {
488 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
489 (double) i);
490 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
491 p->direction != MagickFalse ? "down" : "up");
492 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
493 p->ghostline != MagickFalse ? "transparent" : "opaque");
494 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
495 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
496 p->bounds.x2,p->bounds.y2);
497 for (j=0; j < (ssize_t) p->number_points; j++)
498 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
499 p->points[j].x,p->points[j].y);
500 p++;
501 }
502 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
503}
504
505static void ReversePoints(PointInfo *points,const size_t number_points)
506{
507 PointInfo
508 point;
509
510 ssize_t
511 i;
512
513 for (i=0; i < (ssize_t) (number_points >> 1); i++)
514 {
515 point=points[i];
516 points[i]=points[number_points-(i+1)];
517 points[number_points-(i+1)]=point;
518 }
519}
520
521static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info,
522 ExceptionInfo *exception)
523{
524 long
525 direction,
526 next_direction;
527
528 PointInfo
529 point,
530 *points;
531
532 PolygonInfo
533 *polygon_info;
534
535 SegmentInfo
536 bounds;
537
538 ssize_t
539 i,
540 n;
541
542 MagickBooleanType
543 ghostline;
544
545 size_t
546 edge,
547 number_edges,
548 number_points;
549
550 /*
551 Convert a path to the more efficient sorted rendering form.
552 */
553 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
554 if (polygon_info == (PolygonInfo *) NULL)
555 {
556 (void) ThrowMagickException(exception,GetMagickModule(),
557 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
558 return((PolygonInfo *) NULL);
559 }
560 number_edges=16;
561 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
562 sizeof(*polygon_info->edges));
563 if (polygon_info->edges == (EdgeInfo *) NULL)
564 {
565 (void) ThrowMagickException(exception,GetMagickModule(),
566 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
567 return(DestroyPolygonInfo(polygon_info));
568 }
569 (void) memset(polygon_info->edges,0,number_edges*
570 sizeof(*polygon_info->edges));
571 direction=0;
572 edge=0;
573 ghostline=MagickFalse;
574 n=0;
575 number_points=0;
576 points=(PointInfo *) NULL;
577 (void) memset(&point,0,sizeof(point));
578 (void) memset(&bounds,0,sizeof(bounds));
579 polygon_info->edges[edge].number_points=(size_t) n;
580 polygon_info->edges[edge].scanline=0.0;
581 polygon_info->edges[edge].highwater=0;
582 polygon_info->edges[edge].ghostline=ghostline;
583 polygon_info->edges[edge].direction=(ssize_t) direction;
584 polygon_info->edges[edge].points=points;
585 polygon_info->edges[edge].bounds=bounds;
586 polygon_info->number_edges=0;
587 for (i=0; path_info[i].code != EndCode; i++)
588 {
589 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
590 (path_info[i].code == GhostlineCode))
591 {
592 /*
593 Move to.
594 */
595 if ((points != (PointInfo *) NULL) && (n >= 2))
596 {
597 if (edge == number_edges)
598 {
599 number_edges<<=1;
600 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
601 polygon_info->edges,(size_t) number_edges,
602 sizeof(*polygon_info->edges));
603 if (polygon_info->edges == (EdgeInfo *) NULL)
604 {
605 (void) ThrowMagickException(exception,GetMagickModule(),
606 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
607 points=(PointInfo *) RelinquishMagickMemory(points);
608 return(DestroyPolygonInfo(polygon_info));
609 }
610 }
611 polygon_info->edges[edge].number_points=(size_t) n;
612 polygon_info->edges[edge].scanline=(-1.0);
613 polygon_info->edges[edge].highwater=0;
614 polygon_info->edges[edge].ghostline=ghostline;
615 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
616 if (direction < 0)
617 ReversePoints(points,(size_t) n);
618 polygon_info->edges[edge].points=points;
619 polygon_info->edges[edge].bounds=bounds;
620 polygon_info->edges[edge].bounds.y1=points[0].y;
621 polygon_info->edges[edge].bounds.y2=points[n-1].y;
622 points=(PointInfo *) NULL;
623 ghostline=MagickFalse;
624 edge++;
625 polygon_info->number_edges=edge;
626 }
627 if (points == (PointInfo *) NULL)
628 {
629 number_points=16;
630 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
631 sizeof(*points));
632 if (points == (PointInfo *) NULL)
633 {
634 (void) ThrowMagickException(exception,GetMagickModule(),
635 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
636 return(DestroyPolygonInfo(polygon_info));
637 }
638 }
639 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
640 point=path_info[i].point;
641 points[0]=point;
642 bounds.x1=point.x;
643 bounds.x2=point.x;
644 direction=0;
645 n=1;
646 continue;
647 }
648 /*
649 Line to.
650 */
651 next_direction=((path_info[i].point.y > point.y) ||
652 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
653 (path_info[i].point.x > point.x))) ? 1 : -1;
654 if ((points != (PointInfo *) NULL) && (direction != 0) &&
655 (direction != next_direction))
656 {
657 /*
658 New edge.
659 */
660 point=points[n-1];
661 if (edge == number_edges)
662 {
663 number_edges<<=1;
664 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
665 polygon_info->edges,(size_t) number_edges,
666 sizeof(*polygon_info->edges));
667 if (polygon_info->edges == (EdgeInfo *) NULL)
668 {
669 (void) ThrowMagickException(exception,GetMagickModule(),
670 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
671 points=(PointInfo *) RelinquishMagickMemory(points);
672 return(DestroyPolygonInfo(polygon_info));
673 }
674 }
675 polygon_info->edges[edge].number_points=(size_t) n;
676 polygon_info->edges[edge].scanline=(-1.0);
677 polygon_info->edges[edge].highwater=0;
678 polygon_info->edges[edge].ghostline=ghostline;
679 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
680 if (direction < 0)
681 ReversePoints(points,(size_t) n);
682 polygon_info->edges[edge].points=points;
683 polygon_info->edges[edge].bounds=bounds;
684 polygon_info->edges[edge].bounds.y1=points[0].y;
685 polygon_info->edges[edge].bounds.y2=points[n-1].y;
686 polygon_info->number_edges=edge+1;
687 points=(PointInfo *) NULL;
688 number_points=16;
689 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
690 sizeof(*points));
691 if (points == (PointInfo *) NULL)
692 {
693 (void) ThrowMagickException(exception,GetMagickModule(),
694 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
695 return(DestroyPolygonInfo(polygon_info));
696 }
697 n=1;
698 ghostline=MagickFalse;
699 points[0]=point;
700 bounds.x1=point.x;
701 bounds.x2=point.x;
702 edge++;
703 }
704 direction=next_direction;
705 if (points == (PointInfo *) NULL)
706 continue;
707 if (n == (ssize_t) number_points)
708 {
709 number_points<<=1;
710 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
711 sizeof(*points));
712 if (points == (PointInfo *) NULL)
713 {
714 (void) ThrowMagickException(exception,GetMagickModule(),
715 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
716 return(DestroyPolygonInfo(polygon_info));
717 }
718 }
719 point=path_info[i].point;
720 points[n]=point;
721 if (point.x < bounds.x1)
722 bounds.x1=point.x;
723 if (point.x > bounds.x2)
724 bounds.x2=point.x;
725 n++;
726 }
727 if (points != (PointInfo *) NULL)
728 {
729 if (n < 2)
730 points=(PointInfo *) RelinquishMagickMemory(points);
731 else
732 {
733 if (edge == number_edges)
734 {
735 number_edges<<=1;
736 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
737 polygon_info->edges,(size_t) number_edges,
738 sizeof(*polygon_info->edges));
739 if (polygon_info->edges == (EdgeInfo *) NULL)
740 {
741 (void) ThrowMagickException(exception,GetMagickModule(),
742 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
743 return(DestroyPolygonInfo(polygon_info));
744 }
745 }
746 polygon_info->edges[edge].number_points=(size_t) n;
747 polygon_info->edges[edge].scanline=(-1.0);
748 polygon_info->edges[edge].highwater=0;
749 polygon_info->edges[edge].ghostline=ghostline;
750 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
751 if (direction < 0)
752 ReversePoints(points,(size_t) n);
753 polygon_info->edges[edge].points=points;
754 polygon_info->edges[edge].bounds=bounds;
755 polygon_info->edges[edge].bounds.y1=points[0].y;
756 polygon_info->edges[edge].bounds.y2=points[n-1].y;
757 points=(PointInfo *) NULL;
758 ghostline=MagickFalse;
759 edge++;
760 polygon_info->number_edges=edge;
761 }
762 }
763 polygon_info->number_edges=edge;
764 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
765 polygon_info->number_edges,sizeof(*polygon_info->edges));
766 if (polygon_info->edges == (EdgeInfo *) NULL)
767 {
768 (void) ThrowMagickException(exception,GetMagickModule(),
769 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
770 return(DestroyPolygonInfo(polygon_info));
771 }
772 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
773 {
774 EdgeInfo
775 *edge_info;
776
777 edge_info=polygon_info->edges+i;
778 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
779 edge_info->number_points,sizeof(*edge_info->points));
780 if (edge_info->points == (PointInfo *) NULL)
781 {
782 (void) ThrowMagickException(exception,GetMagickModule(),
783 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
784 return(DestroyPolygonInfo(polygon_info));
785 }
786 }
787 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
788 sizeof(*polygon_info->edges),DrawCompareEdges);
789 if ((GetLogEventMask() & DrawEvent) != 0)
790 LogPolygonInfo(polygon_info);
791 return(polygon_info);
792}
793
794/*
795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796% %
797% %
798% %
799+ C o n v e r t P r i m i t i v e T o P a t h %
800% %
801% %
802% %
803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804%
805% ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
806% path structure.
807%
808% The format of the ConvertPrimitiveToPath method is:
809%
810% PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
811% const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
812%
813% A description of each parameter follows:
814%
815% o ConvertPrimitiveToPath() returns a vector path structure of type
816% PathInfo.
817%
818% o draw_info: a structure of type DrawInfo.
819%
820% o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
821%
822%
823*/
824
825static void LogPathInfo(const PathInfo *path_info)
826{
827 const PathInfo
828 *p;
829
830 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
831 for (p=path_info; p->code != EndCode; p++)
832 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
833 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
834 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
835 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
836 "?");
837 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
838}
839
840static PathInfo *ConvertPrimitiveToPath(
841 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info,
842 ExceptionInfo *exception)
843{
844 MagickBooleanType
845 closed_subpath;
846
847 PathInfo
848 *path_info;
849
850 PathInfoCode
851 code;
852
853 PointInfo
854 p,
855 q;
856
857 ssize_t
858 i,
859 n;
860
861 ssize_t
862 coordinates,
863 start;
864
865 magick_unreferenced(draw_info);
866
867 /*
868 Converts a PrimitiveInfo structure into a vector path structure.
869 */
870 switch (primitive_info->primitive)
871 {
872 case PointPrimitive:
873 case ColorPrimitive:
874 case MattePrimitive:
875 case TextPrimitive:
876 case ImagePrimitive:
877 return((PathInfo *) NULL);
878 default:
879 break;
880 }
881 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
882 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (3UL*i+1UL),
883 sizeof(*path_info));
884 if (path_info == (PathInfo *) NULL)
885 {
886 (void) ThrowMagickException(exception,GetMagickModule(),
887 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
888 return((PathInfo *) NULL);
889 }
890 coordinates=0;
891 closed_subpath=MagickFalse;
892 n=0;
893 p.x=(-1.0);
894 p.y=(-1.0);
895 q.x=(-1.0);
896 q.y=(-1.0);
897 start=0;
898 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
899 {
900 code=LineToCode;
901 if (coordinates <= 0)
902 {
903 /*
904 New subpath.
905 */
906 coordinates=(ssize_t) primitive_info[i].coordinates;
907 p=primitive_info[i].point;
908 start=n;
909 code=MoveToCode;
910 closed_subpath=primitive_info[i].closed_subpath;
911 }
912 coordinates--;
913 if ((code == MoveToCode) || (coordinates <= 0) ||
914 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
915 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
916 {
917 /*
918 Eliminate duplicate points.
919 */
920 path_info[n].code=code;
921 path_info[n].point=primitive_info[i].point;
922 q=primitive_info[i].point;
923 n++;
924 }
925 if (coordinates > 0)
926 continue; /* next point in current subpath */
927 if (closed_subpath != MagickFalse)
928 {
929 closed_subpath=MagickFalse;
930 continue;
931 }
932 /*
933 Mark the p point as open if the subpath is not closed.
934 */
935 path_info[start].code=OpenCode;
936 path_info[n].code=GhostlineCode;
937 path_info[n].point=primitive_info[i].point;
938 n++;
939 path_info[n].code=LineToCode;
940 path_info[n].point=p;
941 n++;
942 }
943 path_info[n].code=EndCode;
944 path_info[n].point.x=0.0;
945 path_info[n].point.y=0.0;
946 if (IsEventLogging() != MagickFalse)
947 LogPathInfo(path_info);
948 path_info=(PathInfo *) ResizeQuantumMemory(path_info,(size_t) (n+1),
949 sizeof(*path_info));
950 return(path_info);
951}
952
953/*
954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955% %
956% %
957% %
958% D e s t r o y D r a w I n f o %
959% %
960% %
961% %
962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963%
964% DestroyDrawInfo() deallocates memory associated with an DrawInfo structure.
965%
966% The format of the DestroyDrawInfo method is:
967%
968% DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
969%
970% A description of each parameter follows:
971%
972% o draw_info: the draw info.
973%
974*/
975MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
976{
977 assert(draw_info != (DrawInfo *) NULL);
978 assert(draw_info->signature == MagickCoreSignature);
979 if (IsEventLogging() != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
981 if (draw_info->id != (char *) NULL)
982 draw_info->id=DestroyString(draw_info->id);
983 if (draw_info->primitive != (char *) NULL)
984 draw_info->primitive=DestroyString(draw_info->primitive);
985 if (draw_info->text != (char *) NULL)
986 draw_info->text=DestroyString(draw_info->text);
987 if (draw_info->geometry != (char *) NULL)
988 draw_info->geometry=DestroyString(draw_info->geometry);
989 if (draw_info->tile != (Image *) NULL)
990 draw_info->tile=DestroyImage(draw_info->tile);
991 if (draw_info->fill_pattern != (Image *) NULL)
992 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
993 if (draw_info->stroke_pattern != (Image *) NULL)
994 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
995 if (draw_info->font != (char *) NULL)
996 draw_info->font=DestroyString(draw_info->font);
997 if (draw_info->metrics != (char *) NULL)
998 draw_info->metrics=DestroyString(draw_info->metrics);
999 if (draw_info->family != (char *) NULL)
1000 draw_info->family=DestroyString(draw_info->family);
1001 if (draw_info->encoding != (char *) NULL)
1002 draw_info->encoding=DestroyString(draw_info->encoding);
1003 if (draw_info->density != (char *) NULL)
1004 draw_info->density=DestroyString(draw_info->density);
1005 if (draw_info->server_name != (char *) NULL)
1006 draw_info->server_name=(char *)
1007 RelinquishMagickMemory(draw_info->server_name);
1008 if (draw_info->dash_pattern != (double *) NULL)
1009 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
1010 draw_info->dash_pattern);
1011 if (draw_info->gradient.stops != (StopInfo *) NULL)
1012 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1013 draw_info->gradient.stops);
1014 if (draw_info->clip_mask != (char *) NULL)
1015 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1016 if (draw_info->clipping_mask != (Image *) NULL)
1017 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1018 if (draw_info->composite_mask != (Image *) NULL)
1019 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1020 if (draw_info->image_info != (ImageInfo *) NULL)
1021 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1022 draw_info->signature=(~MagickCoreSignature);
1023 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1024 return(draw_info);
1025}
1026
1027/*
1028%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029% %
1030% %
1031% %
1032% D r a w A f f i n e I m a g e %
1033% %
1034% %
1035% %
1036%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037%
1038% DrawAffineImage() composites the source over the destination image as
1039% dictated by the affine transform.
1040%
1041% The format of the DrawAffineImage method is:
1042%
1043% MagickBooleanType DrawAffineImage(Image *image,const Image *source,
1044% const AffineMatrix *affine)
1045%
1046% A description of each parameter follows:
1047%
1048% o image: the image.
1049%
1050% o source: the source image.
1051%
1052% o affine: the affine transform.
1053%
1054*/
1055
1056static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
1057 const double y,const SegmentInfo *edge)
1058{
1059 double
1060 intercept,
1061 z;
1062
1063 double
1064 x;
1065
1066 SegmentInfo
1067 inverse_edge;
1068
1069 /*
1070 Determine left and right edges.
1071 */
1072 inverse_edge.x1=edge->x1;
1073 inverse_edge.y1=edge->y1;
1074 inverse_edge.x2=edge->x2;
1075 inverse_edge.y2=edge->y2;
1076 z=affine->ry*y+affine->tx;
1077 if (affine->sx >= MagickEpsilon)
1078 {
1079 intercept=(-z/affine->sx);
1080 x=intercept;
1081 if (x > inverse_edge.x1)
1082 inverse_edge.x1=x;
1083 intercept=(-z+(double) image->columns)/affine->sx;
1084 x=intercept;
1085 if (x < inverse_edge.x2)
1086 inverse_edge.x2=x;
1087 }
1088 else
1089 if (affine->sx < -MagickEpsilon)
1090 {
1091 intercept=(-z+(double) image->columns)/affine->sx;
1092 x=intercept;
1093 if (x > inverse_edge.x1)
1094 inverse_edge.x1=x;
1095 intercept=(-z/affine->sx);
1096 x=intercept;
1097 if (x < inverse_edge.x2)
1098 inverse_edge.x2=x;
1099 }
1100 else
1101 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1102 {
1103 inverse_edge.x2=edge->x1;
1104 return(inverse_edge);
1105 }
1106 /*
1107 Determine top and bottom edges.
1108 */
1109 z=affine->sy*y+affine->ty;
1110 if (affine->rx >= MagickEpsilon)
1111 {
1112 intercept=(-z/affine->rx);
1113 x=intercept;
1114 if (x > inverse_edge.x1)
1115 inverse_edge.x1=x;
1116 intercept=(-z+(double) image->rows)/affine->rx;
1117 x=intercept;
1118 if (x < inverse_edge.x2)
1119 inverse_edge.x2=x;
1120 }
1121 else
1122 if (affine->rx < -MagickEpsilon)
1123 {
1124 intercept=(-z+(double) image->rows)/affine->rx;
1125 x=intercept;
1126 if (x > inverse_edge.x1)
1127 inverse_edge.x1=x;
1128 intercept=(-z/affine->rx);
1129 x=intercept;
1130 if (x < inverse_edge.x2)
1131 inverse_edge.x2=x;
1132 }
1133 else
1134 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1135 {
1136 inverse_edge.x2=edge->x2;
1137 return(inverse_edge);
1138 }
1139 return(inverse_edge);
1140}
1141
1142static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1143{
1144 AffineMatrix
1145 inverse_affine;
1146
1147 double
1148 determinant;
1149
1150 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1151 affine->ry);
1152 inverse_affine.sx=determinant*affine->sy;
1153 inverse_affine.rx=determinant*(-affine->rx);
1154 inverse_affine.ry=determinant*(-affine->ry);
1155 inverse_affine.sy=determinant*affine->sx;
1156 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1157 inverse_affine.ry;
1158 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1159 inverse_affine.sy;
1160 return(inverse_affine);
1161}
1162
1163MagickExport MagickBooleanType DrawAffineImage(Image *image,
1164 const Image *source,const AffineMatrix *affine)
1165{
1166 AffineMatrix
1167 inverse_affine;
1168
1169 CacheView
1170 *image_view,
1171 *source_view;
1172
1173 ExceptionInfo
1174 *exception;
1175
1176 MagickBooleanType
1177 status;
1178
1179 MagickPixelPacket
1180 zero;
1181
1182 PointInfo
1183 extent[4],
1184 min,
1185 max,
1186 point;
1187
1188 ssize_t
1189 i;
1190
1191 SegmentInfo
1192 edge;
1193
1194 ssize_t
1195 start,
1196 stop,
1197 y;
1198
1199 /*
1200 Determine bounding box.
1201 */
1202 assert(image != (Image *) NULL);
1203 assert(image->signature == MagickCoreSignature);
1204 assert(source != (const Image *) NULL);
1205 assert(source->signature == MagickCoreSignature);
1206 if (IsEventLogging() != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1208 assert(affine != (AffineMatrix *) NULL);
1209 extent[0].x=0.0;
1210 extent[0].y=0.0;
1211 extent[1].x=(double) source->columns;
1212 extent[1].y=0.0;
1213 extent[2].x=(double) source->columns;
1214 extent[2].y=(double) source->rows;
1215 extent[3].x=0.0;
1216 extent[3].y=(double) source->rows;
1217 for (i=0; i < 4; i++)
1218 {
1219 point=extent[i];
1220 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1221 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1222 }
1223 min=extent[0];
1224 max=extent[0];
1225 for (i=1; i < 4; i++)
1226 {
1227 if (min.x > extent[i].x)
1228 min.x=extent[i].x;
1229 if (min.y > extent[i].y)
1230 min.y=extent[i].y;
1231 if (max.x < extent[i].x)
1232 max.x=extent[i].x;
1233 if (max.y < extent[i].y)
1234 max.y=extent[i].y;
1235 }
1236 /*
1237 Affine transform image.
1238 */
1239 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1240 return(MagickFalse);
1241 status=MagickTrue;
1242 edge.x1=min.x;
1243 edge.y1=min.y;
1244 edge.x2=max.x;
1245 edge.y2=max.y;
1246 inverse_affine=InverseAffineMatrix(affine);
1247 if (edge.y1 < 0.0)
1248 edge.y1=0.0;
1249 if (edge.y2 > (image->rows-1.0))
1250 edge.y2=image->rows-1.0;
1251 GetMagickPixelPacket(image,&zero);
1252 exception=(&image->exception);
1253 start=CastDoubleToLong(ceil(edge.y1-0.5));
1254 stop=CastDoubleToLong(floor(edge.y2+0.5));
1255 source_view=AcquireVirtualCacheView(source,exception);
1256 image_view=AcquireAuthenticCacheView(image,exception);
1257#if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp parallel for schedule(static) shared(status) \
1259 magick_number_threads(source,image,stop-start,1)
1260#endif
1261 for (y=start; y <= stop; y++)
1262 {
1263 IndexPacket
1264 *magick_restrict indexes;
1265
1266 MagickPixelPacket
1267 composite,
1268 pixel;
1269
1270 PointInfo
1271 point;
1272
1273 PixelPacket
1274 *magick_restrict q;
1275
1276 SegmentInfo
1277 inverse_edge;
1278
1279 ssize_t
1280 x,
1281 x_offset;
1282
1283 if (status == MagickFalse)
1284 continue;
1285 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1286 if (inverse_edge.x2 < inverse_edge.x1)
1287 continue;
1288 if (inverse_edge.x1 < 0.0)
1289 inverse_edge.x1=0.0;
1290 if (inverse_edge.x2 > image->columns-1.0)
1291 inverse_edge.x2=image->columns-1.0;
1292 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToLong(
1293 ceil(inverse_edge.x1-0.5)),y,(size_t) CastDoubleToLong(floor(
1294 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1295 if (q == (PixelPacket *) NULL)
1296 continue;
1297 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1298 pixel=zero;
1299 composite=zero;
1300 x_offset=0;
1301 for (x=CastDoubleToLong(ceil(inverse_edge.x1-0.5));
1302 x <= CastDoubleToLong(floor(inverse_edge.x2+0.5)); x++)
1303 {
1304 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1305 inverse_affine.tx;
1306 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1307 inverse_affine.ty;
1308 status=InterpolateMagickPixelPacket(source,source_view,
1309 UndefinedInterpolatePixel,point.x,point.y,&pixel,exception);
1310 if (status == MagickFalse)
1311 break;
1312 SetMagickPixelPacket(image,q,indexes+x_offset,&composite);
1313 MagickPixelCompositeOver(&pixel,pixel.opacity,&composite,
1314 composite.opacity,&composite);
1315 SetPixelPacket(image,&composite,q,indexes+x_offset);
1316 x_offset++;
1317 q++;
1318 }
1319 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1320 status=MagickFalse;
1321 }
1322 source_view=DestroyCacheView(source_view);
1323 image_view=DestroyCacheView(image_view);
1324 return(status);
1325}
1326
1327/*
1328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1329% %
1330% %
1331% %
1332+ D r a w B o u n d i n g R e c t a n g l e s %
1333% %
1334% %
1335% %
1336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1337%
1338% DrawBoundingRectangles() draws the bounding rectangles on the image. This
1339% is only useful for developers debugging the rendering algorithm.
1340%
1341% The format of the DrawBoundingRectangles method is:
1342%
1343% MagickBooleanType DrawBoundingRectangles(Image *image,
1344% const DrawInfo *draw_info,PolygonInfo *polygon_info)
1345%
1346% A description of each parameter follows:
1347%
1348% o image: the image.
1349%
1350% o draw_info: the draw info.
1351%
1352% o polygon_info: Specifies a pointer to a PolygonInfo structure.
1353%
1354*/
1355
1356static MagickBooleanType DrawBoundingRectangles(Image *image,
1357 const DrawInfo *draw_info,const PolygonInfo *polygon_info)
1358{
1359 double
1360 mid;
1361
1362 DrawInfo
1363 *clone_info;
1364
1365 MagickStatusType
1366 status;
1367
1368 PointInfo
1369 end,
1370 resolution,
1371 start;
1372
1373 PrimitiveInfo
1374 primitive_info[6];
1375
1376 ssize_t
1377 i;
1378
1379 SegmentInfo
1380 bounds;
1381
1382 ssize_t
1383 coordinates;
1384
1385 (void) memset(primitive_info,0,sizeof(primitive_info));
1386 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1387 status=QueryColorDatabase("#0000",&clone_info->fill,&image->exception);
1388 if (status == MagickFalse)
1389 {
1390 clone_info=DestroyDrawInfo(clone_info);
1391 return(MagickFalse);
1392 }
1393 resolution.x=96.0;
1394 resolution.y=96.0;
1395 if (clone_info->density != (char *) NULL)
1396 {
1397 GeometryInfo
1398 geometry_info;
1399
1400 MagickStatusType
1401 flags;
1402
1403 flags=ParseGeometry(clone_info->density,&geometry_info);
1404 if ((flags & RhoValue) != 0)
1405 resolution.x=geometry_info.rho;
1406 resolution.y=resolution.x;
1407 if ((flags & SigmaValue) != 0)
1408 resolution.y=geometry_info.sigma;
1409 }
1410 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1411 clone_info->stroke_width/2.0;
1412 bounds.x1=0.0;
1413 bounds.y1=0.0;
1414 bounds.x2=0.0;
1415 bounds.y2=0.0;
1416 if (polygon_info != (PolygonInfo *) NULL)
1417 {
1418 bounds=polygon_info->edges[0].bounds;
1419 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1420 {
1421 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1422 bounds.x1=polygon_info->edges[i].bounds.x1;
1423 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1424 bounds.y1=polygon_info->edges[i].bounds.y1;
1425 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1426 bounds.x2=polygon_info->edges[i].bounds.x2;
1427 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1428 bounds.y2=polygon_info->edges[i].bounds.y2;
1429 }
1430 bounds.x1-=mid;
1431 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1432 image->columns ? (double) image->columns-1 : bounds.x1;
1433 bounds.y1-=mid;
1434 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1435 image->rows ? (double) image->rows-1 : bounds.y1;
1436 bounds.x2+=mid;
1437 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1438 image->columns ? (double) image->columns-1 : bounds.x2;
1439 bounds.y2+=mid;
1440 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1441 image->rows ? (double) image->rows-1 : bounds.y2;
1442 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1443 {
1444 if (polygon_info->edges[i].direction != 0)
1445 status=QueryColorDatabase("#f00",&clone_info->stroke,
1446 &image->exception);
1447 else
1448 status=QueryColorDatabase("#0f0",&clone_info->stroke,
1449 &image->exception);
1450 if (status == MagickFalse)
1451 break;
1452 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1453 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1454 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1455 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1456 primitive_info[0].primitive=RectanglePrimitive;
1457 status&=TraceRectangle(primitive_info,start,end);
1458 primitive_info[0].method=ReplaceMethod;
1459 coordinates=(ssize_t) primitive_info[0].coordinates;
1460 primitive_info[coordinates].primitive=UndefinedPrimitive;
1461 status=DrawPrimitive(image,clone_info,primitive_info);
1462 if (status == MagickFalse)
1463 break;
1464 }
1465 if (i < (ssize_t) polygon_info->number_edges)
1466 {
1467 clone_info=DestroyDrawInfo(clone_info);
1468 return(status == 0 ? MagickFalse : MagickTrue);
1469 }
1470 }
1471 status=QueryColorDatabase("#00f",&clone_info->stroke,&image->exception);
1472 if (status == MagickFalse)
1473 {
1474 clone_info=DestroyDrawInfo(clone_info);
1475 return(MagickFalse);
1476 }
1477 start.x=(double) (bounds.x1-mid);
1478 start.y=(double) (bounds.y1-mid);
1479 end.x=(double) (bounds.x2+mid);
1480 end.y=(double) (bounds.y2+mid);
1481 primitive_info[0].primitive=RectanglePrimitive;
1482 status&=TraceRectangle(primitive_info,start,end);
1483 primitive_info[0].method=ReplaceMethod;
1484 coordinates=(ssize_t) primitive_info[0].coordinates;
1485 primitive_info[coordinates].primitive=UndefinedPrimitive;
1486 status=DrawPrimitive(image,clone_info,primitive_info);
1487 clone_info=DestroyDrawInfo(clone_info);
1488 return(status == 0 ? MagickFalse : MagickTrue);
1489}
1490
1491/*
1492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493% %
1494% %
1495% %
1496% D r a w C l i p P a t h %
1497% %
1498% %
1499% %
1500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1501%
1502% DrawClipPath() draws the clip path on the image mask.
1503%
1504% The format of the DrawClipPath method is:
1505%
1506% MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1507% const char *id)
1508%
1509% A description of each parameter follows:
1510%
1511% o image: the image.
1512%
1513% o draw_info: the draw info.
1514%
1515% o id: the clip path id.
1516%
1517*/
1518MagickExport MagickBooleanType DrawClipPath(Image *image,
1519 const DrawInfo *draw_info,const char *id)
1520{
1521 const char
1522 *clip_path;
1523
1524 Image
1525 *clipping_mask;
1526
1527 MagickBooleanType
1528 status;
1529
1530 clip_path=GetImageArtifact(image,id);
1531 if (clip_path == (const char *) NULL)
1532 return(MagickFalse);
1533 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1534 &image->exception);
1535 if (clipping_mask == (Image *) NULL)
1536 return(MagickFalse);
1537 status=SetImageClipMask(image,clipping_mask);
1538 clipping_mask=DestroyImage(clipping_mask);
1539 return(status);
1540}
1541
1542/*
1543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1544% %
1545% %
1546% %
1547% D r a w C l i p p i n g M a s k %
1548% %
1549% %
1550% %
1551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1552%
1553% DrawClippingMask() draws the clip path and returns it as an image clipping
1554% mask.
1555%
1556% The format of the DrawClippingMask method is:
1557%
1558% Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1559% const char *id,const char *clip_path,ExceptionInfo *exception)
1560%
1561% A description of each parameter follows:
1562%
1563% o image: the image.
1564%
1565% o draw_info: the draw info.
1566%
1567% o id: the clip path id.
1568%
1569% o clip_path: the clip path.
1570%
1571% o exception: return any errors or warnings in this structure.
1572%
1573*/
1574static Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1575 const char *id,const char *clip_path,ExceptionInfo *exception)
1576{
1577 DrawInfo
1578 *clone_info;
1579
1580 Image
1581 *clip_mask;
1582
1583 MagickStatusType
1584 status;
1585
1586 /*
1587 Draw a clip path.
1588 */
1589 assert(image != (Image *) NULL);
1590 assert(image->signature == MagickCoreSignature);
1591 assert(draw_info != (const DrawInfo *) NULL);
1592 if (IsEventLogging() != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 clip_mask=AcquireImage((const ImageInfo *) NULL);
1595 status=SetImageExtent(clip_mask,image->columns,image->rows);
1596 if (status == MagickFalse)
1597 return(DestroyImage(clip_mask));
1598 status=SetImageClipMask(image,(Image *) NULL);
1599 status=QueryColorCompliance("#0000",AllCompliance,
1600 &clip_mask->background_color,exception);
1601 clip_mask->background_color.opacity=(Quantum) TransparentOpacity;
1602 status=SetImageBackgroundColor(clip_mask);
1603 if (draw_info->debug != MagickFalse)
1604 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1605 id);
1606 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1607 (void) CloneString(&clone_info->primitive,clip_path);
1608 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1609 exception);
1610 if (clone_info->clip_mask != (char *) NULL)
1611 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1612 (void) QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1613 exception);
1614 clone_info->stroke_width=0.0;
1615 clone_info->opacity=OpaqueOpacity;
1616 clone_info->clip_path=MagickTrue;
1617 status=RenderMVGContent(clip_mask,clone_info,0);
1618 clone_info=DestroyDrawInfo(clone_info);
1619 status&=SeparateImageChannel(clip_mask,TrueAlphaChannel);
1620 if (draw_info->compliance != SVGCompliance)
1621 status&=NegateImage(clip_mask,MagickFalse);
1622 if (status == MagickFalse)
1623 clip_mask=DestroyImage(clip_mask);
1624 if (draw_info->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1626 return(clip_mask);
1627}
1628
1629/*
1630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1631% %
1632% %
1633% %
1634% D r a w C o m p o s i t e M a s k %
1635% %
1636% %
1637% %
1638%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1639%
1640% DrawCompositeMask() draws the mask path and returns it as an image mask.
1641%
1642% The format of the DrawCompositeMask method is:
1643%
1644% Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1645% const char *id,const char *mask_path,ExceptionInfo *exception)
1646%
1647% A description of each parameter follows:
1648%
1649% o image: the image.
1650%
1651% o draw_info: the draw info.
1652%
1653% o id: the mask path id.
1654%
1655% o mask_path: the mask path.
1656%
1657% o exception: return any errors or warnings in this structure.
1658%
1659*/
1660static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1661 const char *id,const char *mask_path,ExceptionInfo *exception)
1662{
1663 Image
1664 *composite_mask;
1665
1666 DrawInfo
1667 *clone_info;
1668
1669 MagickStatusType
1670 status;
1671
1672 /*
1673 Draw a mask path.
1674 */
1675 assert(image != (Image *) NULL);
1676 assert(image->signature == MagickCoreSignature);
1677 assert(draw_info != (const DrawInfo *) NULL);
1678 if (IsEventLogging() != MagickFalse)
1679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1680 composite_mask=AcquireImage((const ImageInfo *) NULL);
1681 status=SetImageExtent(composite_mask,image->columns,image->rows);
1682 if (status == MagickFalse)
1683 return(DestroyImage(composite_mask));
1684 status=SetImageMask(image,(Image *) NULL);
1685 status=QueryColorCompliance("#0000",AllCompliance,
1686 &composite_mask->background_color,exception);
1687 composite_mask->background_color.opacity=(Quantum) TransparentOpacity;
1688 (void) SetImageBackgroundColor(composite_mask);
1689 if (draw_info->debug != MagickFalse)
1690 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin mask-path %s",
1691 id);
1692 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1693 (void) CloneString(&clone_info->primitive,mask_path);
1694 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1695 exception);
1696 status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1697 exception);
1698 clone_info->stroke_width=0.0;
1699 clone_info->opacity=OpaqueOpacity;
1700 status=RenderMVGContent(composite_mask,clone_info,0);
1701 clone_info=DestroyDrawInfo(clone_info);
1702 status&=SeparateImageChannel(composite_mask,TrueAlphaChannel);
1703 status&=NegateImage(composite_mask,MagickFalse);
1704 if (status == MagickFalse)
1705 composite_mask=DestroyImage(composite_mask);
1706 if (draw_info->debug != MagickFalse)
1707 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
1708 return(composite_mask);
1709}
1710
1711/*
1712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713% %
1714% %
1715% %
1716+ D r a w D a s h P o l y g o n %
1717% %
1718% %
1719% %
1720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721%
1722% DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1723% image while respecting the dash offset and dash pattern attributes.
1724%
1725% The format of the DrawDashPolygon method is:
1726%
1727% MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1728% const PrimitiveInfo *primitive_info,Image *image)
1729%
1730% A description of each parameter follows:
1731%
1732% o draw_info: the draw info.
1733%
1734% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1735%
1736% o image: the image.
1737%
1738%
1739*/
1740static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1741 const PrimitiveInfo *primitive_info,Image *image)
1742{
1743 double
1744 dx,
1745 dy,
1746 length,
1747 maximum_length,
1748 offset,
1749 scale,
1750 total_length;
1751
1752 DrawInfo
1753 *clone_info;
1754
1755 MagickStatusType
1756 status;
1757
1758 PrimitiveInfo
1759 *dash_polygon;
1760
1761 ssize_t
1762 i;
1763
1764 size_t
1765 number_vertices;
1766
1767 ssize_t
1768 j,
1769 n;
1770
1771 assert(draw_info != (const DrawInfo *) NULL);
1772 if (draw_info->debug != MagickFalse)
1773 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1774 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1775 number_vertices=(size_t) i;
1776 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1777 (2UL*number_vertices+32UL),sizeof(*dash_polygon));
1778 if (dash_polygon == (PrimitiveInfo *) NULL)
1779 {
1780 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1781 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1782 return(MagickFalse);
1783 }
1784 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1785 sizeof(*dash_polygon));
1786 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1787 clone_info->miterlimit=0;
1788 dash_polygon[0]=primitive_info[0];
1789 dash_polygon[0].closed_subpath=MagickFalse;
1790 scale=ExpandAffine(&draw_info->affine);
1791 length=scale*draw_info->dash_pattern[0];
1792 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1793 scale*draw_info->dash_offset : 0.0;
1794 j=1;
1795 for (n=0; offset > 0.0; j=0)
1796 {
1797 if (draw_info->dash_pattern[n] <= 0.0)
1798 break;
1799 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1800 if (offset > length)
1801 {
1802 offset-=length;
1803 n++;
1804 length=scale*draw_info->dash_pattern[n];
1805 continue;
1806 }
1807 if (offset < length)
1808 {
1809 length-=offset;
1810 offset=0.0;
1811 break;
1812 }
1813 offset=0.0;
1814 n++;
1815 }
1816 status=MagickTrue;
1817 maximum_length=0.0;
1818 total_length=0.0;
1819 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1820 {
1821 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1822 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1823 maximum_length=hypot(dx,dy);
1824 if (maximum_length > (double) (MaxBezierCoordinates >> 2))
1825 continue;
1826 if (fabs(length) < MagickEpsilon)
1827 {
1828 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1829 n++;
1830 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1831 n=0;
1832 length=scale*draw_info->dash_pattern[n];
1833 }
1834 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1835 {
1836 total_length+=length;
1837 if ((n & 0x01) != 0)
1838 {
1839 dash_polygon[0]=primitive_info[0];
1840 dash_polygon[0].closed_subpath=MagickFalse;
1841 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1842 total_length*MagickSafeReciprocal(maximum_length));
1843 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1844 total_length*MagickSafeReciprocal(maximum_length));
1845 j=1;
1846 }
1847 else
1848 {
1849 if ((j+1) > (ssize_t) number_vertices)
1850 break;
1851 dash_polygon[j]=primitive_info[i-1];
1852 dash_polygon[j].closed_subpath=MagickFalse;
1853 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1854 total_length*MagickSafeReciprocal(maximum_length));
1855 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1856 total_length*MagickSafeReciprocal(maximum_length));
1857 dash_polygon[j].coordinates=1;
1858 j++;
1859 dash_polygon[0].coordinates=(size_t) j;
1860 dash_polygon[j].primitive=UndefinedPrimitive;
1861 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1862 if (status == MagickFalse)
1863 break;
1864 }
1865 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1866 n++;
1867 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1868 n=0;
1869 length=scale*draw_info->dash_pattern[n];
1870 }
1871 length-=(maximum_length-total_length);
1872 if ((n & 0x01) != 0)
1873 continue;
1874 dash_polygon[j]=primitive_info[i];
1875 dash_polygon[j].coordinates=1;
1876 j++;
1877 }
1878 if ((status != MagickFalse) && (total_length < maximum_length) &&
1879 ((n & 0x01) == 0) && (j > 1))
1880 {
1881 dash_polygon[j]=primitive_info[i-1];
1882 dash_polygon[j].closed_subpath=MagickFalse;
1883 dash_polygon[j].point.x+=MagickEpsilon;
1884 dash_polygon[j].point.y+=MagickEpsilon;
1885 dash_polygon[j].coordinates=1;
1886 j++;
1887 dash_polygon[0].coordinates=(size_t) j;
1888 dash_polygon[j].primitive=UndefinedPrimitive;
1889 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1890 }
1891 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1892 clone_info=DestroyDrawInfo(clone_info);
1893 if (draw_info->debug != MagickFalse)
1894 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1895 return(status != 0 ? MagickTrue : MagickFalse);
1896}
1897
1898/*
1899%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1900% %
1901% %
1902% %
1903% D r a w G r a d i e n t I m a g e %
1904% %
1905% %
1906% %
1907%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1908%
1909% DrawGradientImage() draws a linear gradient on the image.
1910%
1911% The format of the DrawGradientImage method is:
1912%
1913% MagickBooleanType DrawGradientImage(Image *image,
1914% const DrawInfo *draw_info)
1915%
1916% A description of each parameter follows:
1917%
1918% o image: the image.
1919%
1920% o draw_info: the draw info.
1921%
1922*/
1923
1924static inline double GetStopColorOffset(const GradientInfo *gradient,
1925 const ssize_t x,const ssize_t y)
1926{
1927 switch (gradient->type)
1928 {
1929 case UndefinedGradient:
1930 case LinearGradient:
1931 {
1932 double
1933 gamma,
1934 length,
1935 offset,
1936 scale;
1937
1938 PointInfo
1939 p,
1940 q;
1941
1942 const SegmentInfo
1943 *gradient_vector;
1944
1945 gradient_vector=(&gradient->gradient_vector);
1946 p.x=gradient_vector->x2-gradient_vector->x1;
1947 p.y=gradient_vector->y2-gradient_vector->y1;
1948 q.x=(double) x-gradient_vector->x1;
1949 q.y=(double) y-gradient_vector->y1;
1950 length=sqrt(q.x*q.x+q.y*q.y);
1951 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1952 gamma=MagickSafeReciprocal(gamma);
1953 scale=p.x*q.x+p.y*q.y;
1954 offset=gamma*scale*length;
1955 return(offset);
1956 }
1957 case RadialGradient:
1958 {
1959 PointInfo
1960 v;
1961
1962 if (gradient->spread == RepeatSpread)
1963 {
1964 v.x=(double) x-gradient->center.x;
1965 v.y=(double) y-gradient->center.y;
1966 return(sqrt(v.x*v.x+v.y*v.y));
1967 }
1968 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1969 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1970 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1971 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1972 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1973 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1974 return(sqrt(v.x*v.x+v.y*v.y));
1975 }
1976 }
1977 return(0.0);
1978}
1979
1980MagickExport MagickBooleanType DrawGradientImage(Image *image,
1981 const DrawInfo *draw_info)
1982{
1983 CacheView
1984 *image_view;
1985
1986 const GradientInfo
1987 *gradient;
1988
1989 const SegmentInfo
1990 *gradient_vector;
1991
1992 double
1993 length;
1994
1995 ExceptionInfo
1996 *exception;
1997
1998 MagickBooleanType
1999 status;
2000
2001 MagickPixelPacket
2002 zero;
2003
2004 PointInfo
2005 point;
2006
2007 RectangleInfo
2008 bounding_box;
2009
2010 ssize_t
2011 height,
2012 y;
2013
2014 /*
2015 Draw linear or radial gradient on image.
2016 */
2017 assert(image != (Image *) NULL);
2018 assert(image->signature == MagickCoreSignature);
2019 assert(draw_info != (const DrawInfo *) NULL);
2020 if (IsEventLogging() != MagickFalse)
2021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2022 gradient=(&draw_info->gradient);
2023 gradient_vector=(&gradient->gradient_vector);
2024 point.x=gradient_vector->x2-gradient_vector->x1;
2025 point.y=gradient_vector->y2-gradient_vector->y1;
2026 length=sqrt(point.x*point.x+point.y*point.y);
2027 bounding_box=gradient->bounding_box;
2028 status=MagickTrue;
2029 exception=(&image->exception);
2030 GetMagickPixelPacket(image,&zero);
2031 image_view=AcquireAuthenticCacheView(image,exception);
2032 height=(size_t) (bounding_box.y+bounding_box.height);
2033#if defined(MAGICKCORE_OPENMP_SUPPORT)
2034 #pragma omp parallel for schedule(static) shared(status) \
2035 magick_number_threads(image,image,height,1)
2036#endif
2037 for (y=bounding_box.y; y < (ssize_t) height; y++)
2038 {
2039 double
2040 alpha,
2041 offset;
2042
2043 MagickPixelPacket
2044 composite,
2045 pixel;
2046
2047 IndexPacket
2048 *magick_restrict indexes;
2049
2050 PixelPacket
2051 *magick_restrict q;
2052
2053 ssize_t
2054 i,
2055 j,
2056 width,
2057 x;
2058
2059 if (status == MagickFalse)
2060 continue;
2061 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(size_t)
2062 bounding_box.width,1,exception);
2063 if (q == (PixelPacket *) NULL)
2064 {
2065 status=MagickFalse;
2066 continue;
2067 }
2068 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2069 pixel=zero;
2070 composite=zero;
2071 offset=GetStopColorOffset(gradient,0,y);
2072 if (gradient->type != RadialGradient)
2073 offset*=MagickSafeReciprocal(length);
2074 width=(size_t) (bounding_box.x+bounding_box.width);
2075 for (x=bounding_box.x; x < (ssize_t) width; x++)
2076 {
2077 SetMagickPixelPacket(image,q,indexes+x,&pixel);
2078 switch (gradient->spread)
2079 {
2080 case UndefinedSpread:
2081 case PadSpread:
2082 {
2083 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2084 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2085 {
2086 offset=GetStopColorOffset(gradient,x,y);
2087 if (gradient->type != RadialGradient)
2088 offset*=MagickSafeReciprocal(length);
2089 }
2090 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2091 if (offset < gradient->stops[i].offset)
2092 break;
2093 if ((offset < 0.0) || (i == 0))
2094 composite=gradient->stops[0].color;
2095 else
2096 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2097 composite=gradient->stops[gradient->number_stops-1].color;
2098 else
2099 {
2100 j=i;
2101 i--;
2102 alpha=(offset-gradient->stops[i].offset)/
2103 (gradient->stops[j].offset-gradient->stops[i].offset);
2104 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2105 &gradient->stops[j].color,alpha,&composite);
2106 }
2107 break;
2108 }
2109 case ReflectSpread:
2110 {
2111 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2112 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2113 {
2114 offset=GetStopColorOffset(gradient,x,y);
2115 if (gradient->type != RadialGradient)
2116 offset*=MagickSafeReciprocal(length);
2117 }
2118 if (offset < 0.0)
2119 offset=(-offset);
2120 if ((ssize_t) fmod(offset,2.0) == 0)
2121 offset=fmod(offset,1.0);
2122 else
2123 offset=1.0-fmod(offset,1.0);
2124 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2125 if (offset < gradient->stops[i].offset)
2126 break;
2127 if (i == 0)
2128 composite=gradient->stops[0].color;
2129 else
2130 if (i == (ssize_t) gradient->number_stops)
2131 composite=gradient->stops[gradient->number_stops-1].color;
2132 else
2133 {
2134 j=i;
2135 i--;
2136 alpha=(offset-gradient->stops[i].offset)/
2137 (gradient->stops[j].offset-gradient->stops[i].offset);
2138 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2139 &gradient->stops[j].color,alpha,&composite);
2140 }
2141 break;
2142 }
2143 case RepeatSpread:
2144 {
2145 double
2146 repeat;
2147
2148 MagickBooleanType
2149 antialias;
2150
2151 antialias=MagickFalse;
2152 repeat=0.0;
2153 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2154 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2155 {
2156 offset=GetStopColorOffset(gradient,x,y);
2157 if (gradient->type == LinearGradient)
2158 {
2159 repeat=fmod(offset,length);
2160 if (repeat < 0.0)
2161 repeat=length-fmod(-repeat,length);
2162 else
2163 repeat=fmod(offset,length);
2164 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2165 MagickTrue : MagickFalse;
2166 offset=MagickSafeReciprocal(length)*repeat;
2167 }
2168 else
2169 {
2170 repeat=fmod(offset,(double) gradient->radius);
2171 if (repeat < 0.0)
2172 repeat=gradient->radius-fmod(-repeat,
2173 (double) gradient->radius);
2174 else
2175 repeat=fmod(offset,(double) gradient->radius);
2176 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2177 MagickFalse;
2178 offset=repeat*MagickSafeReciprocal(gradient->radius);
2179 }
2180 }
2181 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2182 if (offset < gradient->stops[i].offset)
2183 break;
2184 if (i == 0)
2185 composite=gradient->stops[0].color;
2186 else
2187 if (i == (ssize_t) gradient->number_stops)
2188 composite=gradient->stops[gradient->number_stops-1].color;
2189 else
2190 {
2191 j=i;
2192 i--;
2193 alpha=(offset-gradient->stops[i].offset)/
2194 (gradient->stops[j].offset-gradient->stops[i].offset);
2195 if (antialias != MagickFalse)
2196 {
2197 if (gradient->type == LinearGradient)
2198 alpha=length-repeat;
2199 else
2200 alpha=gradient->radius-repeat;
2201 i=0;
2202 j=(ssize_t) gradient->number_stops-1L;
2203 }
2204 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2205 &gradient->stops[j].color,alpha,&composite);
2206 }
2207 break;
2208 }
2209 }
2210 MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
2211 pixel.opacity,&pixel);
2212 SetPixelPacket(image,&pixel,q,indexes+x);
2213 q++;
2214 }
2215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2216 status=MagickFalse;
2217 }
2218 image_view=DestroyCacheView(image_view);
2219 return(status);
2220}
2221
2222/*
2223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2224% %
2225% %
2226% %
2227% D r a w I m a g e %
2228% %
2229% %
2230% %
2231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2232%
2233% DrawImage() draws a graphic primitive on your image. The primitive
2234% may be represented as a string or filename. Precede the filename with an
2235% "at" sign (@) and the contents of the file are drawn on the image. You
2236% can affect how text is drawn by setting one or more members of the draw
2237% info structure.
2238%
2239% The format of the DrawImage method is:
2240%
2241% MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
2242%
2243% A description of each parameter follows:
2244%
2245% o image: the image.
2246%
2247% o draw_info: the draw info.
2248%
2249*/
2250
2251static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2252 const double pad)
2253{
2254 double
2255 proposed_extent;
2256
2257 PrimitiveInfo
2258 *primitive_info;
2259
2260 size_t
2261 extent;
2262
2263 ssize_t
2264 i;
2265
2266 if ((mvg_info == (MVGInfo *) NULL) ||
2267 (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2268 (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2269 (mvg_info->extent == (size_t *) NULL))
2270 return(MagickFalse);
2271 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2272 if ((proposed_extent <= 0.0) || (proposed_extent > (double) MAGICK_SIZE_MAX))
2273 return(MagickFalse);
2274 extent=CastDoubleToSizeT(ceil(proposed_extent));
2275 if (extent <= *mvg_info->extent)
2276 return(MagickTrue);
2277 if (extent > (GetMaxMemoryRequest()/sizeof(PrimitiveInfo)))
2278 return(MagickFalse);
2279 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2280 *mvg_info->primitive_info,extent,sizeof(PrimitiveInfo));
2281 if (primitive_info == (PrimitiveInfo *) NULL)
2282 {
2283 /*
2284 Create a stack to unwind; report failure.
2285 */
2286 extent=(size_t) PrimitiveExtentPad;
2287 primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(extent*
2288 sizeof(*primitive_info));
2289 (void) memset(primitive_info,0,extent*sizeof(*primitive_info));
2290 *mvg_info->primitive_info=primitive_info;
2291 *mvg_info->extent=extent;
2292 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2293 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2294 return(MagickFalse);
2295 }
2296 /*
2297 Commit updated buffer.
2298 */
2299 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2300 {
2301 primitive_info[i].primitive=UndefinedPrimitive;
2302 primitive_info[i].text=(char *) NULL;
2303 }
2304 *mvg_info->primitive_info=primitive_info;
2305 *mvg_info->extent=extent;
2306 return(MagickTrue);
2307}
2308
2309static inline double GetDrawValue(const char *magick_restrict string,
2310 char **magick_restrict sentinel)
2311{
2312 char
2313 **magick_restrict q;
2314
2315 double
2316 value;
2317
2318 q=sentinel;
2319 value=InterpretLocaleValue(string,q);
2320 sentinel=q;
2321 return(value);
2322}
2323
2324static int MVGMacroCompare(const void *target,const void *source)
2325{
2326 const char
2327 *p,
2328 *q;
2329
2330 p=(const char *) target;
2331 q=(const char *) source;
2332 return(strcmp(p,q));
2333}
2334
2335static SplayTreeInfo *GetMVGMacros(const char *primitive)
2336{
2337 char
2338 *macro,
2339 *token;
2340
2341 const char
2342 *q;
2343
2344 size_t
2345 extent;
2346
2347 SplayTreeInfo
2348 *macros;
2349
2350 /*
2351 Scan graphic primitives for definitions and classes.
2352 */
2353 if (primitive == (const char *) NULL)
2354 return((SplayTreeInfo *) NULL);
2355 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2356 RelinquishMagickMemory);
2357 macro=AcquireString(primitive);
2358 token=AcquireString(primitive);
2359 extent=strlen(token)+MagickPathExtent;
2360 for (q=primitive; *q != '\0'; )
2361 {
2362 if (GetNextToken(q,&q,extent,token) < 1)
2363 break;
2364 if (*token == '\0')
2365 break;
2366 if (LocaleCompare("push",token) == 0)
2367 {
2368 const char
2369 *end,
2370 *start;
2371
2372 (void) GetNextToken(q,&q,extent,token);
2373 if (*q == '"')
2374 {
2375 char
2376 name[MagickPathExtent];
2377
2378 const char
2379 *p;
2380
2381 ssize_t
2382 n;
2383
2384 /*
2385 Named macro (e.g. push graphic-context "wheel").
2386 */
2387 (void) GetNextToken(q,&q,extent,token);
2388 start=q;
2389 end=q;
2390 (void) CopyMagickString(name,token,MagickPathExtent);
2391 n=1;
2392 for (p=q; *p != '\0'; )
2393 {
2394 if (GetNextToken(p,&p,extent,token) < 1)
2395 break;
2396 if (*token == '\0')
2397 break;
2398 if (LocaleCompare(token,"pop") == 0)
2399 {
2400 end=p-strlen(token)-1;
2401 n--;
2402 }
2403 if (LocaleCompare(token,"push") == 0)
2404 n++;
2405 if ((n == 0) && (end >= start))
2406 {
2407 size_t
2408 length=(size_t) (end-start);
2409
2410 /*
2411 Extract macro.
2412 */
2413 (void) GetNextToken(p,&p,extent,token);
2414 if (length > 0)
2415 {
2416 (void) CopyMagickString(macro,start,length);
2417 (void) AddValueToSplayTree(macros,ConstantString(name),
2418 ConstantString(macro));
2419 }
2420 break;
2421 }
2422 }
2423 }
2424 }
2425 }
2426 token=DestroyString(token);
2427 macro=DestroyString(macro);
2428 return(macros);
2429}
2430
2431static inline MagickBooleanType IsPoint(const char *point)
2432{
2433 char
2434 *p;
2435
2436 double
2437 value;
2438
2439 value=GetDrawValue(point,&p);
2440 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2441 MagickTrue);
2442}
2443
2444static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2445 const PointInfo point)
2446{
2447 primitive_info->point=point;
2448 primitive_info->coordinates=1;
2449 primitive_info->closed_subpath=MagickFalse;
2450 primitive_info->text=(char *) NULL;
2451 return(MagickTrue);
2452}
2453
2454static MagickBooleanType RenderMVGContent(Image *image,
2455 const DrawInfo *draw_info,const size_t depth)
2456{
2457#define RenderImageTag "Render/Image"
2458
2459 AffineMatrix
2460 affine,
2461 current;
2462
2463 char
2464 key[2*MaxTextExtent],
2465 keyword[MaxTextExtent],
2466 geometry[MaxTextExtent],
2467 name[MaxTextExtent],
2468 *next_token,
2469 pattern[MaxTextExtent],
2470 *primitive,
2471 *token;
2472
2473 const char
2474 *p,
2475 *q;
2476
2477 double
2478 angle,
2479 coordinates,
2480 cursor,
2481 factor,
2482 primitive_extent;
2483
2484 DrawInfo
2485 *clone_info,
2486 **graphic_context;
2487
2488 MagickBooleanType
2489 proceed;
2490
2491 MagickStatusType
2492 status;
2493
2494 MVGInfo
2495 mvg_info;
2496
2497 PointInfo
2498 point;
2499
2500 PixelPacket
2501 start_color;
2502
2503 PrimitiveInfo
2504 *primitive_info;
2505
2506 PrimitiveType
2507 primitive_type;
2508
2509 SegmentInfo
2510 bounds;
2511
2512 size_t
2513 extent,
2514 number_points;
2515
2516 SplayTreeInfo
2517 *macros;
2518
2519 ssize_t
2520 classDepth = 0,
2521 defsDepth,
2522 i,
2523 j,
2524 k,
2525 n,
2526 symbolDepth,
2527 x;
2528
2529 TypeMetric
2530 metrics;
2531
2532 assert(image != (Image *) NULL);
2533 assert(image->signature == MagickCoreSignature);
2534 assert(draw_info != (DrawInfo *) NULL);
2535 assert(draw_info->signature == MagickCoreSignature);
2536 if (IsEventLogging() != MagickFalse)
2537 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2538 if (depth > MagickMaxRecursionDepth)
2539 ThrowBinaryImageException(DrawError,"VectorGraphicsNestedTooDeeply",
2540 image->filename);
2541 if ((draw_info->primitive == (char *) NULL) ||
2542 (*draw_info->primitive == '\0'))
2543 return(MagickFalse);
2544 if (draw_info->debug != MagickFalse)
2545 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2546 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2547 return(MagickFalse);
2548 if (image->matte == MagickFalse)
2549 {
2550 status=SetImageAlphaChannel(image,OpaqueAlphaChannel);
2551 if (status == MagickFalse)
2552 return(MagickFalse);
2553 }
2554 primitive=(char *) NULL;
2555 if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2556 (*(draw_info->primitive+1) != '-') && (depth == 0))
2557 primitive=FileToString(draw_info->primitive,~0UL,&image->exception);
2558 else
2559 primitive=AcquireString(draw_info->primitive);
2560 if (primitive == (char *) NULL)
2561 return(MagickFalse);
2562 primitive_extent=(double) strlen(primitive);
2563 (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2564 n=0;
2565 /*
2566 Allocate primitive info memory.
2567 */
2568 graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2569 if (graphic_context == (DrawInfo **) NULL)
2570 {
2571 primitive=DestroyString(primitive);
2572 ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2573 image->filename);
2574 }
2575 number_points=(size_t) PrimitiveExtentPad;
2576 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
2577 (number_points+1),sizeof(*primitive_info));
2578 if (primitive_info == (PrimitiveInfo *) NULL)
2579 {
2580 primitive=DestroyString(primitive);
2581 for ( ; n >= 0; n--)
2582 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2583 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2584 ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2585 image->filename);
2586 }
2587 (void) memset(primitive_info,0,(size_t) (number_points+1)*
2588 sizeof(*primitive_info));
2589 (void) memset(&mvg_info,0,sizeof(mvg_info));
2590 mvg_info.primitive_info=(&primitive_info);
2591 mvg_info.extent=(&number_points);
2592 mvg_info.exception=(&image->exception);
2593 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2594 graphic_context[n]->viewbox=image->page;
2595 if ((image->page.width == 0) || (image->page.height == 0))
2596 {
2597 graphic_context[n]->viewbox.width=image->columns;
2598 graphic_context[n]->viewbox.height=image->rows;
2599 }
2600 token=AcquireString(primitive);
2601 extent=strlen(token)+MaxTextExtent;
2602 cursor=0.0;
2603 defsDepth=0;
2604 symbolDepth=0;
2605 macros=GetMVGMacros(primitive);
2606 status=QueryColorDatabase("#000000",&start_color,&image->exception);
2607 for (q=primitive; *q != '\0'; )
2608 {
2609 /*
2610 Interpret graphic primitive.
2611 */
2612 if (GetNextToken(q,&q,MaxTextExtent,keyword) < 1)
2613 break;
2614 if (*keyword == '\0')
2615 break;
2616 if (*keyword == '#')
2617 {
2618 /*
2619 Comment.
2620 */
2621 while ((*q != '\n') && (*q != '\0'))
2622 q++;
2623 continue;
2624 }
2625 p=q-strlen(keyword)-1;
2626 primitive_type=UndefinedPrimitive;
2627 current=graphic_context[n]->affine;
2628 GetAffineMatrix(&affine);
2629 *token='\0';
2630 switch (*keyword)
2631 {
2632 case ';':
2633 break;
2634 case 'a':
2635 case 'A':
2636 {
2637 if (LocaleCompare("affine",keyword) == 0)
2638 {
2639 (void) GetNextToken(q,&q,extent,token);
2640 affine.sx=GetDrawValue(token,&next_token);
2641 if (token == next_token)
2642 ThrowPointExpectedException(image,token);
2643 (void) GetNextToken(q,&q,extent,token);
2644 if (*token == ',')
2645 (void) GetNextToken(q,&q,extent,token);
2646 affine.ry=GetDrawValue(token,&next_token);
2647 if (token == next_token)
2648 ThrowPointExpectedException(image,token);
2649 (void) GetNextToken(q,&q,extent,token);
2650 if (*token == ',')
2651 (void) GetNextToken(q,&q,extent,token);
2652 affine.rx=GetDrawValue(token,&next_token);
2653 if (token == next_token)
2654 ThrowPointExpectedException(image,token);
2655 (void) GetNextToken(q,&q,extent,token);
2656 if (*token == ',')
2657 (void) GetNextToken(q,&q,extent,token);
2658 affine.sy=GetDrawValue(token,&next_token);
2659 if (token == next_token)
2660 ThrowPointExpectedException(image,token);
2661 (void) GetNextToken(q,&q,extent,token);
2662 if (*token == ',')
2663 (void) GetNextToken(q,&q,extent,token);
2664 affine.tx=GetDrawValue(token,&next_token);
2665 if (token == next_token)
2666 ThrowPointExpectedException(image,token);
2667 (void) GetNextToken(q,&q,extent,token);
2668 if (*token == ',')
2669 (void) GetNextToken(q,&q,extent,token);
2670 affine.ty=GetDrawValue(token,&next_token);
2671 if (token == next_token)
2672 ThrowPointExpectedException(image,token);
2673 break;
2674 }
2675 if (LocaleCompare("arc",keyword) == 0)
2676 {
2677 primitive_type=ArcPrimitive;
2678 break;
2679 }
2680 status=MagickFalse;
2681 break;
2682 }
2683 case 'b':
2684 case 'B':
2685 {
2686 if (LocaleCompare("bezier",keyword) == 0)
2687 {
2688 primitive_type=BezierPrimitive;
2689 break;
2690 }
2691 if (LocaleCompare("border-color",keyword) == 0)
2692 {
2693 (void) GetNextToken(q,&q,extent,token);
2694 status&=QueryColorDatabase(token,&graphic_context[n]->border_color,
2695 &image->exception);
2696 break;
2697 }
2698 status=MagickFalse;
2699 break;
2700 }
2701 case 'c':
2702 case 'C':
2703 {
2704 if (LocaleCompare("class",keyword) == 0)
2705 {
2706 const char
2707 *mvg_class;
2708
2709 (void) GetNextToken(q,&q,extent,token);
2710 if ((*token == '\0') || (*token == ';'))
2711 {
2712 status=MagickFalse;
2713 break;
2714 }
2715 /*
2716 Identify recursion.
2717 */
2718 for (i=0; i <= n; i++)
2719 if (LocaleCompare(token,graphic_context[i]->id) == 0)
2720 break;
2721 if (i <= n)
2722 break;
2723 if (classDepth++ > MagickMaxRecursionDepth)
2724 {
2725 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2726 DrawError,"VectorGraphicsNestedTooDeeply","`%s'",token);
2727 status=MagickFalse;
2728 break;
2729 }
2730 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2731 if ((graphic_context[n]->render != MagickFalse) &&
2732 (mvg_class != (const char *) NULL) && (p > primitive))
2733 {
2734 char
2735 *elements;
2736
2737 ssize_t
2738 offset;
2739
2740 /*
2741 Inject class elements in stream.
2742 */
2743 (void) CloneString(&graphic_context[n]->id,token);
2744 offset=(ssize_t) (p-primitive);
2745 elements=AcquireString(primitive);
2746 elements[offset]='\0';
2747 (void) ConcatenateString(&elements,mvg_class);
2748 (void) ConcatenateString(&elements,"\n");
2749 (void) ConcatenateString(&elements,q);
2750 primitive=DestroyString(primitive);
2751 primitive=elements;
2752 q=primitive+offset;
2753 }
2754 break;
2755 }
2756 if (LocaleCompare("clip-path",keyword) == 0)
2757 {
2758 const char
2759 *clip_path;
2760
2761 /*
2762 Take a node from within the MVG document, and duplicate it here.
2763 */
2764 (void) GetNextToken(q,&q,extent,token);
2765 if (*token == '\0')
2766 {
2767 status=MagickFalse;
2768 break;
2769 }
2770 (void) CloneString(&graphic_context[n]->clip_mask,token);
2771 clip_path=(const char *) GetValueFromSplayTree(macros,token);
2772 if (clip_path != (const char *) NULL)
2773 {
2774 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2775 graphic_context[n]->clipping_mask=
2776 DestroyImage(graphic_context[n]->clipping_mask);
2777 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2778 graphic_context[n],token,clip_path,&image->exception);
2779 if (graphic_context[n]->compliance != SVGCompliance)
2780 {
2781 const char
2782 *clip_path;
2783
2784 clip_path=(const char *) GetValueFromSplayTree(macros,
2785 graphic_context[n]->clip_mask);
2786 if (clip_path != (const char *) NULL)
2787 (void) SetImageArtifact(image,
2788 graphic_context[n]->clip_mask,clip_path);
2789 status&=DrawClipPath(image,graphic_context[n],
2790 graphic_context[n]->clip_mask);
2791 }
2792 }
2793 break;
2794 }
2795 if (LocaleCompare("clip-rule",keyword) == 0)
2796 {
2797 ssize_t
2798 fill_rule;
2799
2800 (void) GetNextToken(q,&q,extent,token);
2801 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2802 token);
2803 if (fill_rule == -1)
2804 {
2805 status=MagickFalse;
2806 break;
2807 }
2808 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2809 break;
2810 }
2811 if (LocaleCompare("clip-units",keyword) == 0)
2812 {
2813 ssize_t
2814 clip_units;
2815
2816 (void) GetNextToken(q,&q,extent,token);
2817 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2818 token);
2819 if (clip_units == -1)
2820 {
2821 status=MagickFalse;
2822 break;
2823 }
2824 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2825 if (clip_units == ObjectBoundingBox)
2826 {
2827 GetAffineMatrix(&current);
2828 affine.sx=draw_info->bounds.x2;
2829 affine.sy=draw_info->bounds.y2;
2830 affine.tx=draw_info->bounds.x1;
2831 affine.ty=draw_info->bounds.y1;
2832 break;
2833 }
2834 break;
2835 }
2836 if (LocaleCompare("circle",keyword) == 0)
2837 {
2838 primitive_type=CirclePrimitive;
2839 break;
2840 }
2841 if (LocaleCompare("color",keyword) == 0)
2842 {
2843 primitive_type=ColorPrimitive;
2844 break;
2845 }
2846 if (LocaleCompare("compliance",keyword) == 0)
2847 {
2848 /*
2849 MVG compliance associates a clipping mask with an image; SVG
2850 compliance associates a clipping mask with a graphics context.
2851 */
2852 (void) GetNextToken(q,&q,extent,token);
2853 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2854 MagickComplianceOptions,MagickFalse,token);
2855 break;
2856 }
2857 if (LocaleCompare("currentColor",keyword) == 0)
2858 {
2859 (void) GetNextToken(q,&q,extent,token);
2860 break;
2861 }
2862 status=MagickFalse;
2863 break;
2864 }
2865 case 'd':
2866 case 'D':
2867 {
2868 if (LocaleCompare("decorate",keyword) == 0)
2869 {
2870 ssize_t
2871 decorate;
2872
2873 (void) GetNextToken(q,&q,extent,token);
2874 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2875 token);
2876 if (decorate == -1)
2877 {
2878 status=MagickFalse;
2879 break;
2880 }
2881 graphic_context[n]->decorate=(DecorationType) decorate;
2882 break;
2883 }
2884 if (LocaleCompare("density",keyword) == 0)
2885 {
2886 (void) GetNextToken(q,&q,extent,token);
2887 (void) CloneString(&graphic_context[n]->density,token);
2888 break;
2889 }
2890 if (LocaleCompare("direction",keyword) == 0)
2891 {
2892 ssize_t
2893 direction;
2894
2895 (void) GetNextToken(q,&q,extent,token);
2896 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2897 token);
2898 if (direction == -1)
2899 status=MagickFalse;
2900 else
2901 graphic_context[n]->direction=(DirectionType) direction;
2902 break;
2903 }
2904 status=MagickFalse;
2905 break;
2906 }
2907 case 'e':
2908 case 'E':
2909 {
2910 if (LocaleCompare("ellipse",keyword) == 0)
2911 {
2912 primitive_type=EllipsePrimitive;
2913 break;
2914 }
2915 if (LocaleCompare("encoding",keyword) == 0)
2916 {
2917 (void) GetNextToken(q,&q,extent,token);
2918 (void) CloneString(&graphic_context[n]->encoding,token);
2919 break;
2920 }
2921 status=MagickFalse;
2922 break;
2923 }
2924 case 'f':
2925 case 'F':
2926 {
2927 if (LocaleCompare("fill",keyword) == 0)
2928 {
2929 const char
2930 *mvg_class;
2931
2932 (void) GetNextToken(q,&q,extent,token);
2933 if (graphic_context[n]->clip_path != MagickFalse)
2934 break;
2935 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2936 if (mvg_class != (const char *) NULL)
2937 {
2938 (void) DrawPatternPath(image,draw_info,mvg_class,
2939 &graphic_context[n]->fill_pattern);
2940 break;
2941 }
2942 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2943 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2944 {
2945 (void) DrawPatternPath(image,draw_info,token,
2946 &graphic_context[n]->fill_pattern);
2947 break;
2948 }
2949 status&=QueryColorDatabase(token,&graphic_context[n]->fill,
2950 &image->exception);
2951 if (graphic_context[n]->fill_opacity != (double) OpaqueOpacity)
2952 graphic_context[n]->fill.opacity=ClampToQuantum(
2953 graphic_context[n]->fill_opacity);
2954 break;
2955 }
2956 if (LocaleCompare("fill-opacity",keyword) == 0)
2957 {
2958 double
2959 opacity;
2960
2961 (void) GetNextToken(q,&q,extent,token);
2962 if (graphic_context[n]->clip_path != MagickFalse)
2963 break;
2964 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2965 opacity=MagickMin(MagickMax(factor*
2966 GetDrawValue(token,&next_token),0.0),1.0);
2967 if (token == next_token)
2968 ThrowPointExpectedException(image,token);
2969 if (graphic_context[n]->compliance == SVGCompliance)
2970 graphic_context[n]->fill_opacity*=(1.0-opacity);
2971 else
2972 graphic_context[n]->fill_opacity=((MagickRealType) QuantumRange-
2973 graphic_context[n]->fill_opacity)*(1.0-opacity);
2974 if (graphic_context[n]->fill.opacity != TransparentOpacity)
2975 graphic_context[n]->fill.opacity=(Quantum)
2976 graphic_context[n]->fill_opacity;
2977 else
2978 graphic_context[n]->fill.opacity=ClampToQuantum((MagickRealType)
2979 QuantumRange*(1.0-opacity));
2980 break;
2981 }
2982 if (LocaleCompare("fill-rule",keyword) == 0)
2983 {
2984 ssize_t
2985 fill_rule;
2986
2987 (void) GetNextToken(q,&q,extent,token);
2988 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2989 token);
2990 if (fill_rule == -1)
2991 {
2992 status=MagickFalse;
2993 break;
2994 }
2995 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2996 break;
2997 }
2998 if (LocaleCompare("font",keyword) == 0)
2999 {
3000 (void) GetNextToken(q,&q,extent,token);
3001 (void) CloneString(&graphic_context[n]->font,token);
3002 if (LocaleCompare("none",token) == 0)
3003 graphic_context[n]->font=(char *) RelinquishMagickMemory(
3004 graphic_context[n]->font);
3005 break;
3006 }
3007 if (LocaleCompare("font-family",keyword) == 0)
3008 {
3009 (void) GetNextToken(q,&q,extent,token);
3010 (void) CloneString(&graphic_context[n]->family,token);
3011 break;
3012 }
3013 if (LocaleCompare("font-size",keyword) == 0)
3014 {
3015 (void) GetNextToken(q,&q,extent,token);
3016 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3017 if (token == next_token)
3018 ThrowPointExpectedException(image,token);
3019 break;
3020 }
3021 if (LocaleCompare("font-stretch",keyword) == 0)
3022 {
3023 ssize_t
3024 stretch;
3025
3026 (void) GetNextToken(q,&q,extent,token);
3027 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3028 if (stretch == -1)
3029 {
3030 status=MagickFalse;
3031 break;
3032 }
3033 graphic_context[n]->stretch=(StretchType) stretch;
3034 break;
3035 }
3036 if (LocaleCompare("font-style",keyword) == 0)
3037 {
3038 ssize_t
3039 style;
3040
3041 (void) GetNextToken(q,&q,extent,token);
3042 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3043 if (style == -1)
3044 {
3045 status=MagickFalse;
3046 break;
3047 }
3048 graphic_context[n]->style=(StyleType) style;
3049 break;
3050 }
3051 if (LocaleCompare("font-weight",keyword) == 0)
3052 {
3053 ssize_t
3054 weight;
3055
3056 (void) GetNextToken(q,&q,extent,token);
3057 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3058 if (weight == -1)
3059 weight=(ssize_t) StringToUnsignedLong(token);
3060 graphic_context[n]->weight=(size_t) weight;
3061 break;
3062 }
3063 status=MagickFalse;
3064 break;
3065 }
3066 case 'g':
3067 case 'G':
3068 {
3069 if (LocaleCompare("gradient-units",keyword) == 0)
3070 {
3071 (void) GetNextToken(q,&q,extent,token);
3072 break;
3073 }
3074 if (LocaleCompare("gravity",keyword) == 0)
3075 {
3076 ssize_t
3077 gravity;
3078
3079 (void) GetNextToken(q,&q,extent,token);
3080 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3081 if (gravity == -1)
3082 {
3083 status=MagickFalse;
3084 break;
3085 }
3086 graphic_context[n]->gravity=(GravityType) gravity;
3087 break;
3088 }
3089 status=MagickFalse;
3090 break;
3091 }
3092 case 'i':
3093 case 'I':
3094 {
3095 if (LocaleCompare("image",keyword) == 0)
3096 {
3097 ssize_t
3098 compose;
3099
3100 primitive_type=ImagePrimitive;
3101 (void) GetNextToken(q,&q,extent,token);
3102 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3103 if (compose == -1)
3104 {
3105 status=MagickFalse;
3106 break;
3107 }
3108 graphic_context[n]->compose=(CompositeOperator) compose;
3109 break;
3110 }
3111 if (LocaleCompare("interline-spacing",keyword) == 0)
3112 {
3113 (void) GetNextToken(q,&q,extent,token);
3114 graphic_context[n]->interline_spacing=GetDrawValue(token,
3115 &next_token);
3116 if (token == next_token)
3117 ThrowPointExpectedException(image,token);
3118 break;
3119 }
3120 if (LocaleCompare("interword-spacing",keyword) == 0)
3121 {
3122 (void) GetNextToken(q,&q,extent,token);
3123 graphic_context[n]->interword_spacing=GetDrawValue(token,
3124 &next_token);
3125 if (token == next_token)
3126 ThrowPointExpectedException(image,token);
3127 break;
3128 }
3129 status=MagickFalse;
3130 break;
3131 }
3132 case 'k':
3133 case 'K':
3134 {
3135 if (LocaleCompare("kerning",keyword) == 0)
3136 {
3137 (void) GetNextToken(q,&q,extent,token);
3138 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3139 if (token == next_token)
3140 ThrowPointExpectedException(image,token);
3141 break;
3142 }
3143 status=MagickFalse;
3144 break;
3145 }
3146 case 'l':
3147 case 'L':
3148 {
3149 if (LocaleCompare("letter-spacing",keyword) == 0)
3150 {
3151 (void) GetNextToken(q,&q,extent,token);
3152 if (IsPoint(token) == MagickFalse)
3153 break;
3154 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3155 clone_info->text=AcquireString(" ");
3156 status&=GetTypeMetrics(image,clone_info,&metrics);
3157 graphic_context[n]->kerning=metrics.width*
3158 GetDrawValue(token,&next_token);
3159 clone_info=DestroyDrawInfo(clone_info);
3160 if (token == next_token)
3161 ThrowPointExpectedException(image,token);
3162 break;
3163 }
3164 if (LocaleCompare("line",keyword) == 0)
3165 {
3166 primitive_type=LinePrimitive;
3167 break;
3168 }
3169 status=MagickFalse;
3170 break;
3171 }
3172 case 'm':
3173 case 'M':
3174 {
3175 if (LocaleCompare("mask",keyword) == 0)
3176 {
3177 const char
3178 *mask_path;
3179
3180 /*
3181 Take a node from within the MVG document, and duplicate it here.
3182 */
3183 (void) GetNextToken(q,&q,extent,token);
3184 mask_path=(const char *) GetValueFromSplayTree(macros,token);
3185 if (mask_path != (const char *) NULL)
3186 {
3187 if (graphic_context[n]->composite_mask != (Image *) NULL)
3188 graphic_context[n]->composite_mask=
3189 DestroyImage(graphic_context[n]->composite_mask);
3190 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3191 graphic_context[n],token,mask_path,&image->exception);
3192 if (graphic_context[n]->compliance != SVGCompliance)
3193 status=SetImageMask(image,graphic_context[n]->composite_mask);
3194 }
3195 break;
3196 }
3197 if (LocaleCompare("matte",keyword) == 0)
3198 {
3199 primitive_type=MattePrimitive;
3200 break;
3201 }
3202 status=MagickFalse;
3203 break;
3204 }
3205 case 'o':
3206 case 'O':
3207 {
3208 if (LocaleCompare("offset",keyword) == 0)
3209 {
3210 (void) GetNextToken(q,&q,extent,token);
3211 break;
3212 }
3213 if (LocaleCompare("opacity",keyword) == 0)
3214 {
3215 double
3216 opacity;
3217
3218 (void) GetNextToken(q,&q,extent,token);
3219 if (graphic_context[n]->clip_path != MagickFalse)
3220 break;
3221 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3222 opacity=1.0-MagickMin(MagickMax(factor*
3223 GetDrawValue(token,&next_token),0.0),1.0);
3224 if (token == next_token)
3225 ThrowPointExpectedException(image,token);
3226 if (graphic_context[n]->compliance == SVGCompliance)
3227 {
3228 graphic_context[n]->fill_opacity*=opacity;
3229 graphic_context[n]->stroke_opacity*=opacity;
3230 }
3231 else
3232 {
3233 graphic_context[n]->fill_opacity=(double) QuantumRange*opacity;
3234 graphic_context[n]->stroke_opacity=(double) QuantumRange*
3235 opacity;
3236 }
3237 if (graphic_context[n]->fill.opacity != (double) TransparentOpacity)
3238 {
3239 graphic_context[n]->fill.opacity=
3240 graphic_context[n]->fill_opacity;
3241 graphic_context[n]->stroke.opacity=
3242 graphic_context[n]->stroke_opacity;
3243 }
3244 else
3245 {
3246 graphic_context[n]->fill.opacity=(MagickRealType)
3247 ClampToQuantum((double) QuantumRange*opacity);
3248 graphic_context[n]->stroke.opacity=(MagickRealType)
3249 ClampToQuantum((double) QuantumRange*opacity);
3250 }
3251 break;
3252 }
3253 status=MagickFalse;
3254 break;
3255 }
3256 case 'p':
3257 case 'P':
3258 {
3259 if (LocaleCompare("path",keyword) == 0)
3260 {
3261 primitive_type=PathPrimitive;
3262 break;
3263 }
3264 if (LocaleCompare("point",keyword) == 0)
3265 {
3266 primitive_type=PointPrimitive;
3267 break;
3268 }
3269 if (LocaleCompare("polyline",keyword) == 0)
3270 {
3271 primitive_type=PolylinePrimitive;
3272 break;
3273 }
3274 if (LocaleCompare("polygon",keyword) == 0)
3275 {
3276 primitive_type=PolygonPrimitive;
3277 break;
3278 }
3279 if (LocaleCompare("pop",keyword) == 0)
3280 {
3281 if (GetNextToken(q,&q,extent,token) < 1)
3282 break;
3283 if (LocaleCompare("class",token) == 0)
3284 break;
3285 if (LocaleCompare("clip-path",token) == 0)
3286 break;
3287 if (LocaleCompare("defs",token) == 0)
3288 {
3289 defsDepth--;
3290 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3291 MagickTrue;
3292 break;
3293 }
3294 if (LocaleCompare("gradient",token) == 0)
3295 break;
3296 if (LocaleCompare("graphic-context",token) == 0)
3297 {
3298 if (n <= 0)
3299 {
3300 (void) ThrowMagickException(&image->exception,
3301 GetMagickModule(),DrawError,
3302 "UnbalancedGraphicContextPushPop","`%s'",token);
3303 status=MagickFalse;
3304 n=0;
3305 break;
3306 }
3307 if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3308 (graphic_context[n]->compliance != SVGCompliance))
3309 if (LocaleCompare(graphic_context[n]->clip_mask,
3310 graphic_context[n-1]->clip_mask) != 0)
3311 status=SetImageClipMask(image,(Image *) NULL);
3312 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3313 n--;
3314 break;
3315 }
3316 if (LocaleCompare("mask",token) == 0)
3317 break;
3318 if (LocaleCompare("pattern",token) == 0)
3319 break;
3320 if (LocaleCompare("symbol",token) == 0)
3321 {
3322 symbolDepth--;
3323 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3324 MagickTrue;
3325 break;
3326 }
3327 status=MagickFalse;
3328 break;
3329 }
3330 if (LocaleCompare("push",keyword) == 0)
3331 {
3332 if (GetNextToken(q,&q,extent,token) < 1)
3333 break;
3334 if (LocaleCompare("class",token) == 0)
3335 {
3336 /*
3337 Class context.
3338 */
3339 for (p=q; *q != '\0'; )
3340 {
3341 if (GetNextToken(q,&q,extent,token) < 1)
3342 break;
3343 if (LocaleCompare(token,"pop") != 0)
3344 continue;
3345 (void) GetNextToken(q,(const char **) NULL,extent,token);
3346 if (LocaleCompare(token,"class") != 0)
3347 continue;
3348 break;
3349 }
3350 (void) GetNextToken(q,&q,extent,token);
3351 break;
3352 }
3353 if (LocaleCompare("clip-path",token) == 0)
3354 {
3355 (void) GetNextToken(q,&q,extent,token);
3356 for (p=q; *q != '\0'; )
3357 {
3358 if (GetNextToken(q,&q,extent,token) < 1)
3359 break;
3360 if (LocaleCompare(token,"pop") != 0)
3361 continue;
3362 (void) GetNextToken(q,(const char **) NULL,extent,token);
3363 if (LocaleCompare(token,"clip-path") != 0)
3364 continue;
3365 break;
3366 }
3367 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3368 {
3369 status=MagickFalse;
3370 break;
3371 }
3372 (void) GetNextToken(q,&q,extent,token);
3373 break;
3374 }
3375 if (LocaleCompare("defs",token) == 0)
3376 {
3377 defsDepth++;
3378 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3379 MagickTrue;
3380 break;
3381 }
3382 if (LocaleCompare("gradient",token) == 0)
3383 {
3384 char
3385 key[2*MaxTextExtent],
3386 name[MaxTextExtent],
3387 type[MaxTextExtent];
3388
3389 SegmentInfo
3390 segment;
3391
3392 (void) GetNextToken(q,&q,extent,token);
3393 (void) CopyMagickString(name,token,MaxTextExtent);
3394 (void) GetNextToken(q,&q,extent,token);
3395 (void) CopyMagickString(type,token,MaxTextExtent);
3396 (void) GetNextToken(q,&q,extent,token);
3397 segment.x1=GetDrawValue(token,&next_token);
3398 if (token == next_token)
3399 ThrowPointExpectedException(image,token);
3400 (void) GetNextToken(q,&q,extent,token);
3401 if (*token == ',')
3402 (void) GetNextToken(q,&q,extent,token);
3403 segment.y1=GetDrawValue(token,&next_token);
3404 if (token == next_token)
3405 ThrowPointExpectedException(image,token);
3406 (void) GetNextToken(q,&q,extent,token);
3407 if (*token == ',')
3408 (void) GetNextToken(q,&q,extent,token);
3409 segment.x2=GetDrawValue(token,&next_token);
3410 if (token == next_token)
3411 ThrowPointExpectedException(image,token);
3412 (void) GetNextToken(q,&q,extent,token);
3413 if (*token == ',')
3414 (void) GetNextToken(q,&q,extent,token);
3415 segment.y2=GetDrawValue(token,&next_token);
3416 if (token == next_token)
3417 ThrowPointExpectedException(image,token);
3418 if (LocaleCompare(type,"radial") == 0)
3419 {
3420 (void) GetNextToken(q,&q,extent,token);
3421 if (*token == ',')
3422 (void) GetNextToken(q,&q,extent,token);
3423 }
3424 for (p=q; *q != '\0'; )
3425 {
3426 if (GetNextToken(q,&q,extent,token) < 1)
3427 break;
3428 if (LocaleCompare(token,"pop") != 0)
3429 continue;
3430 (void) GetNextToken(q,(const char **) NULL,extent,token);
3431 if (LocaleCompare(token,"gradient") != 0)
3432 continue;
3433 break;
3434 }
3435 if ((q == (char *) NULL) || (*q == '\0') ||
3436 (p == (char *) NULL) || ((q-4) < p) ||
3437 ((q-p+4+1) > extent))
3438 {
3439 status=MagickFalse;
3440 break;
3441 }
3442 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3443 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3444 graphic_context[n]->affine.ry*segment.y1+
3445 graphic_context[n]->affine.tx;
3446 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3447 graphic_context[n]->affine.sy*segment.y1+
3448 graphic_context[n]->affine.ty;
3449 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3450 graphic_context[n]->affine.ry*segment.y2+
3451 graphic_context[n]->affine.tx;
3452 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3453 graphic_context[n]->affine.sy*segment.y2+
3454 graphic_context[n]->affine.ty;
3455 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3456 (void) SetImageArtifact(image,key,token);
3457 (void) FormatLocaleString(key,MaxTextExtent,"%s-type",name);
3458 (void) SetImageArtifact(image,key,type);
3459 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3460 (void) FormatLocaleString(geometry,MaxTextExtent,
3461 "%gx%g%+.15g%+.15g",
3462 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3463 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3464 bounds.x1,bounds.y1);
3465 (void) SetImageArtifact(image,key,geometry);
3466 (void) GetNextToken(q,&q,extent,token);
3467 break;
3468 }
3469 if (LocaleCompare("graphic-context",token) == 0)
3470 {
3471 n++;
3472 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3473 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3474 if (graphic_context == (DrawInfo **) NULL)
3475 {
3476 (void) ThrowMagickException(&image->exception,
3477 GetMagickModule(),ResourceLimitError,
3478 "MemoryAllocationFailed","`%s'",image->filename);
3479 status=MagickFalse;
3480 break;
3481 }
3482 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3483 graphic_context[n-1]);
3484 if (*q == '"')
3485 {
3486 (void) GetNextToken(q,&q,extent,token);
3487 (void) CloneString(&graphic_context[n]->id,token);
3488 }
3489 if (n > MagickMaxRecursionDepth)
3490 {
3491 (void) ThrowMagickException(&image->exception,
3492 GetMagickModule(),DrawError,
3493 "VectorGraphicsNestedTooDeeply","`%s'",image->filename);
3494 status=MagickFalse;
3495 }
3496 break;
3497 }
3498 if (LocaleCompare("mask",token) == 0)
3499 {
3500 (void) GetNextToken(q,&q,extent,token);
3501 break;
3502 }
3503 if (LocaleCompare("pattern",token) == 0)
3504 {
3505 RectangleInfo
3506 bounds;
3507
3508 (void) GetNextToken(q,&q,extent,token);
3509 (void) CopyMagickString(name,token,MaxTextExtent);
3510 (void) GetNextToken(q,&q,extent,token);
3511 bounds.x=CastDoubleToLong(ceil(GetDrawValue(token,
3512 &next_token)-0.5));
3513 if (token == next_token)
3514 ThrowPointExpectedException(image,token);
3515 (void) GetNextToken(q,&q,extent,token);
3516 if (*token == ',')
3517 (void) GetNextToken(q,&q,extent,token);
3518 bounds.y=CastDoubleToLong(ceil(GetDrawValue(token,
3519 &next_token)-0.5));
3520 if (token == next_token)
3521 ThrowPointExpectedException(image,token);
3522 (void) GetNextToken(q,&q,extent,token);
3523 if (*token == ',')
3524 (void) GetNextToken(q,&q,extent,token);
3525 bounds.width=CastDoubleToUnsigned(GetDrawValue(token,
3526 &next_token)+0.5);
3527 if (token == next_token)
3528 ThrowPointExpectedException(image,token);
3529 (void) GetNextToken(q,&q,extent,token);
3530 if (*token == ',')
3531 (void) GetNextToken(q,&q,extent,token);
3532 bounds.height=CastDoubleToUnsigned(GetDrawValue(token,
3533 &next_token)+0.5);
3534 if (token == next_token)
3535 ThrowPointExpectedException(image,token);
3536 for (p=q; *q != '\0'; )
3537 {
3538 if (GetNextToken(q,&q,extent,token) < 1)
3539 break;
3540 if (LocaleCompare(token,"pop") != 0)
3541 continue;
3542 (void) GetNextToken(q,(const char **) NULL,extent,token);
3543 if (LocaleCompare(token,"pattern") != 0)
3544 continue;
3545 break;
3546 }
3547 if ((q == (char *) NULL) || (p == (char *) NULL) ||
3548 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3549 {
3550 status=MagickFalse;
3551 break;
3552 }
3553 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3554 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3555 (void) SetImageArtifact(image,key,token);
3556 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3557 (void) FormatLocaleString(geometry,MaxTextExtent,
3558 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3559 bounds.height,(double) bounds.x,(double) bounds.y);
3560 (void) SetImageArtifact(image,key,geometry);
3561 (void) GetNextToken(q,&q,extent,token);
3562 break;
3563 }
3564 if (LocaleCompare("symbol",token) == 0)
3565 {
3566 symbolDepth++;
3567 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3568 MagickTrue;
3569 break;
3570 }
3571 status=MagickFalse;
3572 break;
3573 }
3574 status=MagickFalse;
3575 break;
3576 }
3577 case 'r':
3578 case 'R':
3579 {
3580 if (LocaleCompare("rectangle",keyword) == 0)
3581 {
3582 primitive_type=RectanglePrimitive;
3583 break;
3584 }
3585 if (LocaleCompare("rotate",keyword) == 0)
3586 {
3587 (void) GetNextToken(q,&q,extent,token);
3588 angle=GetDrawValue(token,&next_token);
3589 if (token == next_token)
3590 ThrowPointExpectedException(image,token);
3591 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3592 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3593 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3594 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3595 break;
3596 }
3597 if (LocaleCompare("roundRectangle",keyword) == 0)
3598 {
3599 primitive_type=RoundRectanglePrimitive;
3600 break;
3601 }
3602 status=MagickFalse;
3603 break;
3604 }
3605 case 's':
3606 case 'S':
3607 {
3608 if (LocaleCompare("scale",keyword) == 0)
3609 {
3610 (void) GetNextToken(q,&q,extent,token);
3611 affine.sx=GetDrawValue(token,&next_token);
3612 if (token == next_token)
3613 ThrowPointExpectedException(image,token);
3614 (void) GetNextToken(q,&q,extent,token);
3615 if (*token == ',')
3616 (void) GetNextToken(q,&q,extent,token);
3617 affine.sy=GetDrawValue(token,&next_token);
3618 if (token == next_token)
3619 ThrowPointExpectedException(image,token);
3620 break;
3621 }
3622 if (LocaleCompare("skewX",keyword) == 0)
3623 {
3624 (void) GetNextToken(q,&q,extent,token);
3625 angle=GetDrawValue(token,&next_token);
3626 if (token == next_token)
3627 ThrowPointExpectedException(image,token);
3628 affine.ry=sin(DegreesToRadians(angle));
3629 break;
3630 }
3631 if (LocaleCompare("skewY",keyword) == 0)
3632 {
3633 (void) GetNextToken(q,&q,extent,token);
3634 angle=GetDrawValue(token,&next_token);
3635 if (token == next_token)
3636 ThrowPointExpectedException(image,token);
3637 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3638 break;
3639 }
3640 if (LocaleCompare("stop-color",keyword) == 0)
3641 {
3642 GradientType
3643 type;
3644
3645 PixelPacket
3646 stop_color;
3647
3648 (void) GetNextToken(q,&q,extent,token);
3649 status&=QueryColorDatabase(token,&stop_color,&image->exception);
3650 type=LinearGradient;
3651 if (draw_info->gradient.type == RadialGradient)
3652 type=RadialGradient;
3653 (void) GradientImage(image,type,PadSpread,&start_color,&stop_color);
3654 start_color=stop_color;
3655 (void) GetNextToken(q,&q,extent,token);
3656 break;
3657 }
3658 if (LocaleCompare("stroke",keyword) == 0)
3659 {
3660 const char
3661 *mvg_class;
3662
3663 (void) GetNextToken(q,&q,extent,token);
3664 if (graphic_context[n]->clip_path != MagickFalse)
3665 break;
3666 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3667 if (mvg_class != (const char *) NULL)
3668 {
3669 (void) DrawPatternPath(image,draw_info,mvg_class,
3670 &graphic_context[n]->stroke_pattern);
3671 break;
3672 }
3673 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
3674 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3675 {
3676 (void) DrawPatternPath(image,draw_info,token,
3677 &graphic_context[n]->stroke_pattern);
3678 break;
3679 }
3680 status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
3681 &image->exception);
3682 if (graphic_context[n]->stroke_opacity != (MagickRealType) OpaqueOpacity)
3683 graphic_context[n]->stroke.opacity=ClampToQuantum(
3684 graphic_context[n]->stroke_opacity);
3685 break;
3686 }
3687 if (LocaleCompare("stroke-antialias",keyword) == 0)
3688 {
3689 (void) GetNextToken(q,&q,extent,token);
3690 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3691 MagickTrue : MagickFalse;
3692 break;
3693 }
3694 if (LocaleCompare("stroke-dasharray",keyword) == 0)
3695 {
3696 if (graphic_context[n]->dash_pattern != (double *) NULL)
3697 graphic_context[n]->dash_pattern=(double *)
3698 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3699 if (IsPoint(q) != MagickFalse)
3700 {
3701 const char
3702 *p;
3703
3704 p=q;
3705 (void) GetNextToken(p,&p,extent,token);
3706 if (*token == ',')
3707 (void) GetNextToken(p,&p,extent,token);
3708 for (x=0; IsPoint(token) != MagickFalse; x++)
3709 {
3710 (void) GetNextToken(p,&p,extent,token);
3711 if (*token == ',')
3712 (void) GetNextToken(p,&p,extent,token);
3713 }
3714 graphic_context[n]->dash_pattern=(double *)
3715 AcquireQuantumMemory((size_t) (2*x+2),
3716 sizeof(*graphic_context[n]->dash_pattern));
3717 if (graphic_context[n]->dash_pattern == (double *) NULL)
3718 {
3719 (void) ThrowMagickException(&image->exception,
3720 GetMagickModule(),ResourceLimitError,
3721 "MemoryAllocationFailed","`%s'",image->filename);
3722 status=MagickFalse;
3723 break;
3724 }
3725 (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3726 (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3727 for (j=0; j < x; j++)
3728 {
3729 (void) GetNextToken(q,&q,extent,token);
3730 if (*token == ',')
3731 (void) GetNextToken(q,&q,extent,token);
3732 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3733 &next_token);
3734 if (token == next_token)
3735 ThrowPointExpectedException(image,token);
3736 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3737 status=MagickFalse;
3738 }
3739 if ((x & 0x01) != 0)
3740 for ( ; j < (2*x); j++)
3741 graphic_context[n]->dash_pattern[j]=
3742 graphic_context[n]->dash_pattern[j-x];
3743 graphic_context[n]->dash_pattern[j]=0.0;
3744 break;
3745 }
3746 (void) GetNextToken(q,&q,extent,token);
3747 break;
3748 }
3749 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3750 {
3751 (void) GetNextToken(q,&q,extent,token);
3752 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3753 if (token == next_token)
3754 ThrowPointExpectedException(image,token);
3755 break;
3756 }
3757 if (LocaleCompare("stroke-linecap",keyword) == 0)
3758 {
3759 ssize_t
3760 linecap;
3761
3762 (void) GetNextToken(q,&q,extent,token);
3763 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3764 if (linecap == -1)
3765 {
3766 status=MagickFalse;
3767 break;
3768 }
3769 graphic_context[n]->linecap=(LineCap) linecap;
3770 break;
3771 }
3772 if (LocaleCompare("stroke-linejoin",keyword) == 0)
3773 {
3774 ssize_t
3775 linejoin;
3776
3777 (void) GetNextToken(q,&q,extent,token);
3778 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3779 token);
3780 if (linejoin == -1)
3781 {
3782 status=MagickFalse;
3783 break;
3784 }
3785 graphic_context[n]->linejoin=(LineJoin) linejoin;
3786 break;
3787 }
3788 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3789 {
3790 (void) GetNextToken(q,&q,extent,token);
3791 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3792 break;
3793 }
3794 if (LocaleCompare("stroke-opacity",keyword) == 0)
3795 {
3796 double
3797 opacity;
3798
3799 (void) GetNextToken(q,&q,extent,token);
3800 if (graphic_context[n]->clip_path != MagickFalse)
3801 break;
3802 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3803 opacity=MagickMin(MagickMax(factor*
3804 GetDrawValue(token,&next_token),0.0),1.0);
3805 if (token == next_token)
3806 ThrowPointExpectedException(image,token);
3807 if (graphic_context[n]->compliance == SVGCompliance)
3808 graphic_context[n]->stroke_opacity*=(1.0-opacity);
3809 else
3810 graphic_context[n]->stroke_opacity=((MagickRealType) QuantumRange-
3811 graphic_context[n]->stroke_opacity)*(1.0-opacity);
3812 if (graphic_context[n]->stroke.opacity != TransparentOpacity)
3813 graphic_context[n]->stroke.opacity=(Quantum)
3814 graphic_context[n]->stroke_opacity;
3815 else
3816 graphic_context[n]->stroke.opacity=ClampToQuantum(
3817 (MagickRealType) QuantumRange*opacity);
3818 break;
3819 }
3820 if (LocaleCompare("stroke-width",keyword) == 0)
3821 {
3822 (void) GetNextToken(q,&q,extent,token);
3823 if (graphic_context[n]->clip_path != MagickFalse)
3824 break;
3825 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3826 if ((token == next_token) ||
3827 (graphic_context[n]->stroke_width < 0.0))
3828 ThrowPointExpectedException(image,token);
3829 break;
3830 }
3831 status=MagickFalse;
3832 break;
3833 }
3834 case 't':
3835 case 'T':
3836 {
3837 if (LocaleCompare("text",keyword) == 0)
3838 {
3839 primitive_type=TextPrimitive;
3840 cursor=0.0;
3841 break;
3842 }
3843 if (LocaleCompare("text-align",keyword) == 0)
3844 {
3845 ssize_t
3846 align;
3847
3848 (void) GetNextToken(q,&q,extent,token);
3849 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3850 if (align == -1)
3851 {
3852 status=MagickFalse;
3853 break;
3854 }
3855 graphic_context[n]->align=(AlignType) align;
3856 break;
3857 }
3858 if (LocaleCompare("text-anchor",keyword) == 0)
3859 {
3860 ssize_t
3861 align;
3862
3863 (void) GetNextToken(q,&q,extent,token);
3864 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3865 if (align == -1)
3866 {
3867 status=MagickFalse;
3868 break;
3869 }
3870 graphic_context[n]->align=(AlignType) align;
3871 break;
3872 }
3873 if (LocaleCompare("text-antialias",keyword) == 0)
3874 {
3875 (void) GetNextToken(q,&q,extent,token);
3876 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3877 MagickTrue : MagickFalse;
3878 break;
3879 }
3880 if (LocaleCompare("text-undercolor",keyword) == 0)
3881 {
3882 (void) GetNextToken(q,&q,extent,token);
3883 status&=QueryColorDatabase(token,&graphic_context[n]->undercolor,
3884 &image->exception);
3885 break;
3886 }
3887 if (LocaleCompare("translate",keyword) == 0)
3888 {
3889 (void) GetNextToken(q,&q,extent,token);
3890 affine.tx=GetDrawValue(token,&next_token);
3891 if (token == next_token)
3892 ThrowPointExpectedException(image,token);
3893 (void) GetNextToken(q,&q,extent,token);
3894 if (*token == ',')
3895 (void) GetNextToken(q,&q,extent,token);
3896 affine.ty=GetDrawValue(token,&next_token);
3897 if (token == next_token)
3898 ThrowPointExpectedException(image,token);
3899 break;
3900 }
3901 status=MagickFalse;
3902 break;
3903 }
3904 case 'u':
3905 case 'U':
3906 {
3907 if (LocaleCompare("use",keyword) == 0)
3908 {
3909 const char
3910 *use;
3911
3912 /*
3913 Get a macro from the MVG document, and "use" it here.
3914 */
3915 (void) GetNextToken(q,&q,extent,token);
3916 use=(const char *) GetValueFromSplayTree(macros,token);
3917 if (use != (const char *) NULL)
3918 {
3919 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3920 (void) CloneString(&clone_info->primitive,use);
3921 status=RenderMVGContent(image,clone_info,depth+1);
3922 clone_info=DestroyDrawInfo(clone_info);
3923 }
3924 break;
3925 }
3926 status=MagickFalse;
3927 break;
3928 }
3929 case 'v':
3930 case 'V':
3931 {
3932 if (LocaleCompare("viewbox",keyword) == 0)
3933 {
3934 (void) GetNextToken(q,&q,extent,token);
3935 graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3936 GetDrawValue(token,&next_token)-0.5));
3937 if (token == next_token)
3938 ThrowPointExpectedException(image,token);
3939 (void) GetNextToken(q,&q,extent,token);
3940 if (*token == ',')
3941 (void) GetNextToken(q,&q,extent,token);
3942 graphic_context[n]->viewbox.y=CastDoubleToLong(ceil(
3943 GetDrawValue(token,&next_token)-0.5));
3944 if (token == next_token)
3945 ThrowPointExpectedException(image,token);
3946 (void) GetNextToken(q,&q,extent,token);
3947 if (*token == ',')
3948 (void) GetNextToken(q,&q,extent,token);
3949 graphic_context[n]->viewbox.width=CastDoubleToUnsigned(
3950 GetDrawValue(token,&next_token)+0.5);
3951 if (token == next_token)
3952 ThrowPointExpectedException(image,token);
3953 (void) GetNextToken(q,&q,extent,token);
3954 if (*token == ',')
3955 (void) GetNextToken(q,&q,extent,token);
3956 graphic_context[n]->viewbox.height=CastDoubleToUnsigned(
3957 GetDrawValue(token,&next_token)+0.5);
3958 if (token == next_token)
3959 ThrowPointExpectedException(image,token);
3960 break;
3961 }
3962 status=MagickFalse;
3963 break;
3964 }
3965 case 'w':
3966 case 'W':
3967 {
3968 if (LocaleCompare("word-spacing",keyword) == 0)
3969 {
3970 (void) GetNextToken(q,&q,extent,token);
3971 graphic_context[n]->interword_spacing=GetDrawValue(token,
3972 &next_token);
3973 if (token == next_token)
3974 ThrowPointExpectedException(image,token);
3975 break;
3976 }
3977 status=MagickFalse;
3978 break;
3979 }
3980 default:
3981 {
3982 status=MagickFalse;
3983 break;
3984 }
3985 }
3986 if (status == MagickFalse)
3987 break;
3988 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3989 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3990 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3991 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3992 {
3993 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3994 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3995 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3996 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3997 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3998 current.tx;
3999 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4000 current.ty;
4001 }
4002 if (primitive_type == UndefinedPrimitive)
4003 {
4004 if ((draw_info->debug != MagickFalse) && (q > p))
4005 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
4006 (q-p-1),p);
4007 continue;
4008 }
4009 /*
4010 Parse the primitive attributes.
4011 */
4012 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4013 if (primitive_info[i].text != (char *) NULL)
4014 primitive_info[i].text=DestroyString(primitive_info[i].text);
4015 i=0;
4016 mvg_info.offset=i;
4017 j=0;
4018 primitive_info[0].primitive=primitive_type;
4019 primitive_info[0].point.x=0.0;
4020 primitive_info[0].point.y=0.0;
4021 primitive_info[0].coordinates=0;
4022 primitive_info[0].method=FloodfillMethod;
4023 primitive_info[0].closed_subpath=MagickFalse;
4024 for (x=0; *q != '\0'; x++)
4025 {
4026 /*
4027 Define points.
4028 */
4029 if (IsPoint(q) == MagickFalse)
4030 break;
4031 (void) GetNextToken(q,&q,extent,token);
4032 point.x=GetDrawValue(token,&next_token);
4033 if (token == next_token)
4034 ThrowPointExpectedException(image,token);
4035 (void) GetNextToken(q,&q,extent,token);
4036 if (*token == ',')
4037 (void) GetNextToken(q,&q,extent,token);
4038 point.y=GetDrawValue(token,&next_token);
4039 if (token == next_token)
4040 ThrowPointExpectedException(image,token);
4041 (void) GetNextToken(q,(const char **) NULL,extent,token);
4042 if (*token == ',')
4043 (void) GetNextToken(q,&q,extent,token);
4044 primitive_info[i].primitive=primitive_type;
4045 primitive_info[i].point=point;
4046 primitive_info[i].coordinates=0;
4047 primitive_info[i].method=FloodfillMethod;
4048 primitive_info[i].closed_subpath=MagickFalse;
4049 i++;
4050 mvg_info.offset=i;
4051 if (i < (ssize_t) number_points)
4052 continue;
4053 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4054 primitive_info=(*mvg_info.primitive_info);
4055 }
4056 if (status == MagickFalse)
4057 break;
4058 if (primitive_info[j].text != (char *) NULL)
4059 primitive_info[j].text=DestroyString(primitive_info[j].text);
4060 primitive_info[j].primitive=primitive_type;
4061 primitive_info[j].coordinates=(size_t) x;
4062 primitive_info[j].method=FloodfillMethod;
4063 primitive_info[j].closed_subpath=MagickFalse;
4064 /*
4065 Circumscribe primitive within a circle.
4066 */
4067 bounds.x1=primitive_info[j].point.x;
4068 bounds.y1=primitive_info[j].point.y;
4069 bounds.x2=primitive_info[j].point.x;
4070 bounds.y2=primitive_info[j].point.y;
4071 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4072 {
4073 point=primitive_info[j+k].point;
4074 if (point.x < bounds.x1)
4075 bounds.x1=point.x;
4076 if (point.y < bounds.y1)
4077 bounds.y1=point.y;
4078 if (point.x > bounds.x2)
4079 bounds.x2=point.x;
4080 if (point.y > bounds.y2)
4081 bounds.y2=point.y;
4082 }
4083 /*
4084 Speculate how many points our primitive might consume.
4085 */
4086 coordinates=(double) primitive_info[j].coordinates;
4087 switch (primitive_type)
4088 {
4089 case RectanglePrimitive:
4090 {
4091 coordinates*=5.0;
4092 break;
4093 }
4094 case RoundRectanglePrimitive:
4095 {
4096 double
4097 alpha,
4098 beta,
4099 radius;
4100
4101 alpha=bounds.x2-bounds.x1;
4102 beta=bounds.y2-bounds.y1;
4103 radius=hypot(alpha,beta);
4104 coordinates*=5.0;
4105 coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4106 BezierQuantum+360.0;
4107 break;
4108 }
4109 case BezierPrimitive:
4110 {
4111 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4112 break;
4113 }
4114 case PathPrimitive:
4115 {
4116 char
4117 *s,
4118 *t;
4119
4120 (void) GetNextToken(q,&q,extent,token);
4121 coordinates=1.0;
4122 t=token;
4123 for (s=token; *s != '\0'; s=t)
4124 {
4125 double
4126 value;
4127
4128 value=GetDrawValue(s,&t);
4129 (void) value;
4130 if (s == t)
4131 {
4132 t++;
4133 continue;
4134 }
4135 coordinates++;
4136 }
4137 for (s=token; *s != '\0'; s++)
4138 if (strspn(s,"AaCcQqSsTt") != 0)
4139 coordinates+=(20.0*BezierQuantum)+360.0;
4140 break;
4141 }
4142 default:
4143 break;
4144 }
4145 if (status == MagickFalse)
4146 break;
4147 if (((size_t) (i+coordinates)) >= number_points)
4148 {
4149 /*
4150 Resize based on speculative points required by primitive.
4151 */
4152 number_points+=coordinates+1;
4153 if (number_points < (size_t) coordinates)
4154 {
4155 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4156 ResourceLimitError,"MemoryAllocationFailed","`%s'",
4157 image->filename);
4158 status=MagickFalse;
4159 break;
4160 }
4161 mvg_info.offset=i;
4162 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4163 primitive_info=(*mvg_info.primitive_info);
4164 }
4165 status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4166 primitive_info=(*mvg_info.primitive_info);
4167 if (status == MagickFalse)
4168 break;
4169 mvg_info.offset=j;
4170 switch (primitive_type)
4171 {
4172 case PointPrimitive:
4173 default:
4174 {
4175 if (primitive_info[j].coordinates != 1)
4176 {
4177 status=MagickFalse;
4178 break;
4179 }
4180 status&=TracePoint(primitive_info+j,primitive_info[j].point);
4181 primitive_info=(*mvg_info.primitive_info);
4182 i=(ssize_t) (j+primitive_info[j].coordinates);
4183 break;
4184 }
4185 case LinePrimitive:
4186 {
4187 if (primitive_info[j].coordinates != 2)
4188 {
4189 status=MagickFalse;
4190 break;
4191 }
4192 status&=TraceLine(primitive_info+j,primitive_info[j].point,
4193 primitive_info[j+1].point);
4194 primitive_info=(*mvg_info.primitive_info);
4195 i=(ssize_t) (j+primitive_info[j].coordinates);
4196 break;
4197 }
4198 case RectanglePrimitive:
4199 {
4200 if (primitive_info[j].coordinates != 2)
4201 {
4202 status=MagickFalse;
4203 break;
4204 }
4205 status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4206 primitive_info[j+1].point);
4207 primitive_info=(*mvg_info.primitive_info);
4208 i=(ssize_t) (j+primitive_info[j].coordinates);
4209 break;
4210 }
4211 case RoundRectanglePrimitive:
4212 {
4213 if (primitive_info[j].coordinates != 3)
4214 {
4215 status=MagickFalse;
4216 break;
4217 }
4218 if ((primitive_info[j+2].point.x < 0.0) ||
4219 (primitive_info[j+2].point.y < 0.0))
4220 {
4221 status=MagickFalse;
4222 break;
4223 }
4224 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4225 {
4226 status=MagickFalse;
4227 break;
4228 }
4229 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4230 {
4231 status=MagickFalse;
4232 break;
4233 }
4234 status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4235 primitive_info[j+1].point,primitive_info[j+2].point);
4236 primitive_info=(*mvg_info.primitive_info);
4237 i=(ssize_t) (j+primitive_info[j].coordinates);
4238 break;
4239 }
4240 case ArcPrimitive:
4241 {
4242 if (primitive_info[j].coordinates != 3)
4243 {
4244 status=MagickFalse;
4245 break;
4246 }
4247 status&=TraceArc(&mvg_info,primitive_info[j].point,
4248 primitive_info[j+1].point,primitive_info[j+2].point);
4249 primitive_info=(*mvg_info.primitive_info);
4250 i=(ssize_t) (j+primitive_info[j].coordinates);
4251 break;
4252 }
4253 case EllipsePrimitive:
4254 {
4255 if (primitive_info[j].coordinates != 3)
4256 {
4257 status=MagickFalse;
4258 break;
4259 }
4260 if ((primitive_info[j+1].point.x < 0.0) ||
4261 (primitive_info[j+1].point.y < 0.0))
4262 {
4263 status=MagickFalse;
4264 break;
4265 }
4266 status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4267 primitive_info[j+1].point,primitive_info[j+2].point);
4268 primitive_info=(*mvg_info.primitive_info);
4269 i=(ssize_t) (j+primitive_info[j].coordinates);
4270 break;
4271 }
4272 case CirclePrimitive:
4273 {
4274 if (primitive_info[j].coordinates != 2)
4275 {
4276 status=MagickFalse;
4277 break;
4278 }
4279 status&=TraceCircle(&mvg_info,primitive_info[j].point,
4280 primitive_info[j+1].point);
4281 primitive_info=(*mvg_info.primitive_info);
4282 i=(ssize_t) (j+primitive_info[j].coordinates);
4283 break;
4284 }
4285 case PolylinePrimitive:
4286 {
4287 if (primitive_info[j].coordinates < 1)
4288 {
4289 status=MagickFalse;
4290 break;
4291 }
4292 break;
4293 }
4294 case PolygonPrimitive:
4295 {
4296 if (primitive_info[j].coordinates < 3)
4297 {
4298 status=MagickFalse;
4299 break;
4300 }
4301 primitive_info[i]=primitive_info[j];
4302 primitive_info[i].coordinates=0;
4303 primitive_info[j].coordinates++;
4304 primitive_info[j].closed_subpath=MagickTrue;
4305 i++;
4306 break;
4307 }
4308 case BezierPrimitive:
4309 {
4310 if (primitive_info[j].coordinates < 3)
4311 {
4312 status=MagickFalse;
4313 break;
4314 }
4315 status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4316 primitive_info=(*mvg_info.primitive_info);
4317 i=(ssize_t) (j+primitive_info[j].coordinates);
4318 break;
4319 }
4320 case PathPrimitive:
4321 {
4322 coordinates=(double) TracePath(image,&mvg_info,token);
4323 primitive_info=(*mvg_info.primitive_info);
4324 if (coordinates < 0.0)
4325 {
4326 status=MagickFalse;
4327 break;
4328 }
4329 i=(ssize_t) (j+coordinates);
4330 break;
4331 }
4332 case ColorPrimitive:
4333 case MattePrimitive:
4334 {
4335 ssize_t
4336 method;
4337
4338 if (primitive_info[j].coordinates != 1)
4339 {
4340 status=MagickFalse;
4341 break;
4342 }
4343 (void) GetNextToken(q,&q,extent,token);
4344 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4345 if (method == -1)
4346 {
4347 status=MagickFalse;
4348 break;
4349 }
4350 primitive_info[j].method=(PaintMethod) method;
4351 break;
4352 }
4353 case TextPrimitive:
4354 {
4355 char
4356 geometry[MagickPathExtent];
4357
4358 if (primitive_info[j].coordinates != 1)
4359 {
4360 status=MagickFalse;
4361 break;
4362 }
4363 if (*token != ',')
4364 (void) GetNextToken(q,&q,extent,token);
4365 (void) CloneString(&primitive_info[j].text,token);
4366 /*
4367 Compute text cursor offset.
4368 */
4369 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4370 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4371 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4372 {
4373 mvg_info.point=primitive_info->point;
4374 primitive_info->point.x+=cursor;
4375 }
4376 else
4377 {
4378 mvg_info.point=primitive_info->point;
4379 cursor=0.0;
4380 }
4381 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4382 primitive_info->point.x,primitive_info->point.y);
4383 clone_info->render=MagickFalse;
4384 clone_info->text=AcquireString(token);
4385 status&=GetTypeMetrics(image,clone_info,&metrics);
4386 clone_info=DestroyDrawInfo(clone_info);
4387 cursor+=metrics.width;
4388 if (graphic_context[n]->compliance != SVGCompliance)
4389 cursor=0.0;
4390 break;
4391 }
4392 case ImagePrimitive:
4393 {
4394 if (primitive_info[j].coordinates != 2)
4395 {
4396 status=MagickFalse;
4397 break;
4398 }
4399 (void) GetNextToken(q,&q,extent,token);
4400 (void) CloneString(&primitive_info[j].text,token);
4401 break;
4402 }
4403 }
4404 mvg_info.offset=i;
4405 if (status == 0)
4406 break;
4407 primitive_info[i].primitive=UndefinedPrimitive;
4408 if ((draw_info->debug != MagickFalse) && (q > p))
4409 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
4410 /*
4411 Sanity check.
4412 */
4413 status&=CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4414 &graphic_context[n]->affine));
4415 primitive_info=(*mvg_info.primitive_info);
4416 if (status == 0)
4417 break;
4418 status&=CheckPrimitiveExtent(&mvg_info,(double)
4419 graphic_context[n]->stroke_width);
4420 primitive_info=(*mvg_info.primitive_info);
4421 if (status == 0)
4422 break;
4423 if (i == 0)
4424 continue;
4425 /*
4426 Transform points.
4427 */
4428 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4429 {
4430 point=primitive_info[i].point;
4431 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4432 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4433 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4434 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4435 point=primitive_info[i].point;
4436 if (point.x < graphic_context[n]->bounds.x1)
4437 graphic_context[n]->bounds.x1=point.x;
4438 if (point.y < graphic_context[n]->bounds.y1)
4439 graphic_context[n]->bounds.y1=point.y;
4440 if (point.x > graphic_context[n]->bounds.x2)
4441 graphic_context[n]->bounds.x2=point.x;
4442 if (point.y > graphic_context[n]->bounds.y2)
4443 graphic_context[n]->bounds.y2=point.y;
4444 if (primitive_info[i].primitive == ImagePrimitive)
4445 break;
4446 if (i >= (ssize_t) number_points)
4447 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4448 }
4449 if (graphic_context[n]->render != MagickFalse)
4450 {
4451 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4452 (graphic_context[n]->clip_mask != (char *) NULL) &&
4453 (LocaleCompare(graphic_context[n]->clip_mask,
4454 graphic_context[n-1]->clip_mask) != 0))
4455 {
4456 const char
4457 *clip_path;
4458
4459 clip_path=(const char *) GetValueFromSplayTree(macros,
4460 graphic_context[n]->clip_mask);
4461 if (clip_path != (const char *) NULL)
4462 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4463 clip_path);
4464 status&=DrawClipPath(image,graphic_context[n],
4465 graphic_context[n]->clip_mask);
4466 }
4467 status&=DrawPrimitive(image,graphic_context[n],primitive_info);
4468 }
4469 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4470 primitive_extent);
4471 if (proceed == MagickFalse)
4472 break;
4473 if (status == 0)
4474 break;
4475 }
4476 if (draw_info->debug != MagickFalse)
4477 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4478 /*
4479 Relinquish resources.
4480 */
4481 macros=DestroySplayTree(macros);
4482 token=DestroyString(token);
4483 if (primitive_info != (PrimitiveInfo *) NULL)
4484 {
4485 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4486 if (primitive_info[i].text != (char *) NULL)
4487 primitive_info[i].text=DestroyString(primitive_info[i].text);
4488 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4489 }
4490 primitive=DestroyString(primitive);
4491 for ( ; n >= 0; n--)
4492 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4493 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4494 if (status == MagickFalse)
4495 ThrowBinaryImageException(DrawError,
4496 "NonconformingDrawingPrimitiveDefinition",keyword);
4497 return(status != 0 ? MagickTrue : MagickFalse);
4498}
4499
4500MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
4501{
4502 return(RenderMVGContent(image,draw_info,0));
4503}
4504
4505/*
4506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4507% %
4508% %
4509% %
4510% D r a w P a t t e r n P a t h %
4511% %
4512% %
4513% %
4514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4515%
4516% DrawPatternPath() draws a pattern.
4517%
4518% The format of the DrawPatternPath method is:
4519%
4520% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4521% const char *name,Image **pattern)
4522%
4523% A description of each parameter follows:
4524%
4525% o image: the image.
4526%
4527% o draw_info: the draw info.
4528%
4529% o name: the pattern name.
4530%
4531% o image: the image.
4532%
4533*/
4534MagickExport MagickBooleanType DrawPatternPath(Image *image,
4535 const DrawInfo *draw_info,const char *name,Image **pattern)
4536{
4537 char
4538 property[MaxTextExtent];
4539
4540 const char
4541 *geometry,
4542 *path,
4543 *type;
4544
4545 DrawInfo
4546 *clone_info;
4547
4548 ImageInfo
4549 *image_info;
4550
4551 MagickBooleanType
4552 status;
4553
4554 assert(image != (Image *) NULL);
4555 assert(image->signature == MagickCoreSignature);
4556 assert(draw_info != (const DrawInfo *) NULL);
4557 if (IsEventLogging() != MagickFalse)
4558 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4559 assert(name != (const char *) NULL);
4560 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
4561 path=GetImageArtifact(image,property);
4562 if (path == (const char *) NULL)
4563 return(MagickFalse);
4564 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
4565 geometry=GetImageArtifact(image,property);
4566 if (geometry == (const char *) NULL)
4567 return(MagickFalse);
4568 if ((*pattern) != (Image *) NULL)
4569 *pattern=DestroyImage(*pattern);
4570 image_info=AcquireImageInfo();
4571 image_info->size=AcquireString(geometry);
4572 *pattern=AcquireImage(image_info);
4573 image_info=DestroyImageInfo(image_info);
4574 (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
4575 &image->exception);
4576 (void) SetImageBackgroundColor(*pattern);
4577 if (draw_info->debug != MagickFalse)
4578 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4579 "begin pattern-path %s %s",name,geometry);
4580 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4581 if (clone_info->fill_pattern != (Image *) NULL)
4582 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4583 if (clone_info->stroke_pattern != (Image *) NULL)
4584 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4585 (void) FormatLocaleString(property,MaxTextExtent,"%s-type",name);
4586 type=GetImageArtifact(image,property);
4587 if (type != (const char *) NULL)
4588 clone_info->gradient.type=(GradientType) ParseCommandOption(
4589 MagickGradientOptions,MagickFalse,type);
4590 (void) CloneString(&clone_info->primitive,path);
4591 status=RenderMVGContent(*pattern,clone_info,0);
4592 clone_info=DestroyDrawInfo(clone_info);
4593 if (draw_info->debug != MagickFalse)
4594 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4595 return(status);
4596}
4597
4598/*
4599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4600% %
4601% %
4602% %
4603+ D r a w P o l y g o n P r i m i t i v e %
4604% %
4605% %
4606% %
4607%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4608%
4609% DrawPolygonPrimitive() draws a polygon on the image.
4610%
4611% The format of the DrawPolygonPrimitive method is:
4612%
4613% MagickBooleanType DrawPolygonPrimitive(Image *image,
4614% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4615%
4616% A description of each parameter follows:
4617%
4618% o image: the image.
4619%
4620% o draw_info: the draw info.
4621%
4622% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4623%
4624*/
4625
4626static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4627{
4628 ssize_t
4629 i;
4630
4631 assert(polygon_info != (PolygonInfo **) NULL);
4632 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4633 if (polygon_info[i] != (PolygonInfo *) NULL)
4634 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4635 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4636 return(polygon_info);
4637}
4638
4639static PolygonInfo **AcquirePolygonTLS(const DrawInfo *draw_info,
4640 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4641{
4642 PathInfo
4643 *magick_restrict path_info;
4644
4645 PolygonInfo
4646 **polygon_info;
4647
4648 size_t
4649 number_threads;
4650
4651 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4652 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4653 sizeof(*polygon_info));
4654 if (polygon_info == (PolygonInfo **) NULL)
4655 {
4656 (void) ThrowMagickException(exception,GetMagickModule(),
4657 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4658 return((PolygonInfo **) NULL);
4659 }
4660 (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4661 path_info=ConvertPrimitiveToPath(draw_info,primitive_info,exception);
4662 if (path_info == (PathInfo *) NULL)
4663 return(DestroyPolygonTLS(polygon_info));
4664 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4665 if (polygon_info[0] == (PolygonInfo *) NULL)
4666 {
4667 (void) ThrowMagickException(exception,GetMagickModule(),
4668 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4669 return(DestroyPolygonTLS(polygon_info));
4670 }
4671 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4672 return(polygon_info);
4673}
4674
4675static MagickBooleanType AcquirePolygonEdgesTLS(PolygonInfo **polygon_info,
4676 const size_t number_threads,ExceptionInfo *exception)
4677{
4678 ssize_t
4679 i;
4680
4681 for (i=1; i < (ssize_t) number_threads; i++)
4682 {
4683 EdgeInfo
4684 *edge_info;
4685
4686 ssize_t
4687 j;
4688
4689 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4690 sizeof(*polygon_info[i]));
4691 if (polygon_info[i] == (PolygonInfo *) NULL)
4692 {
4693 (void) ThrowMagickException(exception,GetMagickModule(),
4694 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4695 return(MagickFalse);
4696 }
4697 polygon_info[i]->number_edges=0;
4698 edge_info=polygon_info[0]->edges;
4699 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4700 polygon_info[0]->number_edges,sizeof(*edge_info));
4701 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4702 {
4703 (void) ThrowMagickException(exception,GetMagickModule(),
4704 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4705 return(MagickFalse);
4706 }
4707 (void) memcpy(polygon_info[i]->edges,edge_info,
4708 polygon_info[0]->number_edges*sizeof(*edge_info));
4709 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4710 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4711 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4712 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4713 {
4714 edge_info=polygon_info[0]->edges+j;
4715 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4716 edge_info->number_points,sizeof(*edge_info));
4717 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4718 {
4719 (void) ThrowMagickException(exception,GetMagickModule(),
4720 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4721 return(MagickFalse);
4722 }
4723 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4724 edge_info->number_points*sizeof(*edge_info->points));
4725 }
4726 }
4727 return(MagickTrue);
4728}
4729
4730static size_t DestroyEdge(PolygonInfo *polygon_info,const ssize_t edge)
4731{
4732 assert(edge < (ssize_t) polygon_info->number_edges);
4733 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4734 polygon_info->edges[edge].points);
4735 polygon_info->number_edges--;
4736 if (edge < (ssize_t) polygon_info->number_edges)
4737 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4738 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
4739 return(polygon_info->number_edges);
4740}
4741
4742static double GetOpacityPixel(PolygonInfo *polygon_info,const double mid,
4743 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4744 const ssize_t y,double *stroke_opacity)
4745{
4746 double
4747 alpha,
4748 beta,
4749 distance,
4750 subpath_opacity;
4751
4752 PointInfo
4753 delta;
4754
4755 EdgeInfo
4756 *p;
4757
4758 const PointInfo
4759 *q;
4760
4761 ssize_t
4762 i;
4763
4764 ssize_t
4765 j,
4766 winding_number;
4767
4768 /*
4769 Compute fill & stroke opacity for this (x,y) point.
4770 */
4771 *stroke_opacity=0.0;
4772 subpath_opacity=0.0;
4773 p=polygon_info->edges;
4774 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4775 {
4776 if ((double) y <= (p->bounds.y1-mid-0.5))
4777 break;
4778 if ((double) y > (p->bounds.y2+mid+0.5))
4779 {
4780 p--;
4781 (void) DestroyEdge(polygon_info,j--);
4782 continue;
4783 }
4784 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4785 ((double) x > (p->bounds.x2+mid+0.5)))
4786 continue;
4787 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4788 for ( ; i < (ssize_t) p->number_points; i++)
4789 {
4790 if ((double) y <= (p->points[i-1].y-mid-0.5))
4791 break;
4792 if ((double) y > (p->points[i].y+mid+0.5))
4793 continue;
4794 if (p->scanline != (double) y)
4795 {
4796 p->scanline=(double) y;
4797 p->highwater=(size_t) i;
4798 }
4799 /*
4800 Compute distance between a point and an edge.
4801 */
4802 q=p->points+i-1;
4803 delta.x=(q+1)->x-q->x;
4804 delta.y=(q+1)->y-q->y;
4805 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4806 if (beta <= 0.0)
4807 {
4808 delta.x=(double) x-q->x;
4809 delta.y=(double) y-q->y;
4810 distance=delta.x*delta.x+delta.y*delta.y;
4811 }
4812 else
4813 {
4814 alpha=delta.x*delta.x+delta.y*delta.y;
4815 if (beta >= alpha)
4816 {
4817 delta.x=(double) x-(q+1)->x;
4818 delta.y=(double) y-(q+1)->y;
4819 distance=delta.x*delta.x+delta.y*delta.y;
4820 }
4821 else
4822 {
4823 alpha=MagickSafeReciprocal(alpha);
4824 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4825 distance=alpha*beta*beta;
4826 }
4827 }
4828 /*
4829 Compute stroke & subpath opacity.
4830 */
4831 beta=0.0;
4832 if (p->ghostline == MagickFalse)
4833 {
4834 alpha=mid+0.5;
4835 if ((*stroke_opacity < 1.0) &&
4836 (distance <= ((alpha+0.25)*(alpha+0.25))))
4837 {
4838 alpha=mid-0.5;
4839 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4840 *stroke_opacity=1.0;
4841 else
4842 {
4843 beta=1.0;
4844 if (fabs(distance-1.0) >= MagickEpsilon)
4845 beta=sqrt((double) distance);
4846 alpha=beta-mid-0.5;
4847 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
4848 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
4849 }
4850 }
4851 }
4852 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
4853 continue;
4854 if (distance <= 0.0)
4855 {
4856 subpath_opacity=1.0;
4857 continue;
4858 }
4859 if (distance > 1.0)
4860 continue;
4861 if (fabs(beta) < MagickEpsilon)
4862 {
4863 beta=1.0;
4864 if (fabs(distance-1.0) >= MagickEpsilon)
4865 beta=sqrt(distance);
4866 }
4867 alpha=beta-1.0;
4868 if (subpath_opacity < (alpha*alpha))
4869 subpath_opacity=alpha*alpha;
4870 }
4871 }
4872 /*
4873 Compute fill opacity.
4874 */
4875 if (fill == MagickFalse)
4876 return(0.0);
4877 if (subpath_opacity >= 1.0)
4878 return(1.0);
4879 /*
4880 Determine winding number.
4881 */
4882 winding_number=0;
4883 p=polygon_info->edges;
4884 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4885 {
4886 if ((double) y <= p->bounds.y1)
4887 break;
4888 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4889 continue;
4890 if ((double) x > p->bounds.x2)
4891 {
4892 winding_number+=p->direction != 0 ? 1 : -1;
4893 continue;
4894 }
4895 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4896 for ( ; i < (ssize_t) (p->number_points-1); i++)
4897 if ((double) y <= p->points[i].y)
4898 break;
4899 q=p->points+i-1;
4900 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4901 winding_number+=p->direction != 0 ? 1 : -1;
4902 }
4903 if (fill_rule != NonZeroRule)
4904 {
4905 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4906 return(1.0);
4907 }
4908 else
4909 if (MagickAbsoluteValue(winding_number) != 0)
4910 return(1.0);
4911 return(subpath_opacity);
4912}
4913
4914static MagickBooleanType DrawPolygonPrimitive(Image *image,
4915 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4916{
4917 typedef struct _ExtentInfo
4918 {
4919 ssize_t
4920 x1,
4921 y1,
4922 x2,
4923 y2;
4924 } ExtentInfo;
4925
4926 CacheView
4927 *image_view;
4928
4929 const char
4930 *artifact;
4931
4932 double
4933 mid;
4934
4935 ExceptionInfo
4936 *exception;
4937
4938 ExtentInfo
4939 poly_extent;
4940
4941 MagickBooleanType
4942 fill,
4943 status;
4944
4945 PolygonInfo
4946 **magick_restrict polygon_info;
4947
4948 EdgeInfo
4949 *p;
4950
4951 SegmentInfo
4952 bounds;
4953
4954 size_t
4955 number_threads = 1;
4956
4957 ssize_t
4958 i,
4959 y;
4960
4961 assert(image != (Image *) NULL);
4962 assert(image->signature == MagickCoreSignature);
4963 assert(draw_info != (DrawInfo *) NULL);
4964 assert(draw_info->signature == MagickCoreSignature);
4965 assert(primitive_info != (PrimitiveInfo *) NULL);
4966 if (IsEventLogging() != MagickFalse)
4967 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4968 if (primitive_info->coordinates <= 1)
4969 return(MagickTrue);
4970 /*
4971 Compute bounding box.
4972 */
4973 polygon_info=AcquirePolygonTLS(draw_info,primitive_info,&image->exception);
4974 if (polygon_info == (PolygonInfo **) NULL)
4975 return(MagickFalse);
4976 if (draw_info->debug != MagickFalse)
4977 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4978 fill=(primitive_info->method == FillToBorderMethod) ||
4979 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4980 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4981 bounds=polygon_info[0]->edges[0].bounds;
4982 artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
4983 if (IsStringTrue(artifact) != MagickFalse)
4984 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0]);
4985 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4986 {
4987 p=polygon_info[0]->edges+i;
4988 if (p->bounds.x1 < bounds.x1)
4989 bounds.x1=p->bounds.x1;
4990 if (p->bounds.y1 < bounds.y1)
4991 bounds.y1=p->bounds.y1;
4992 if (p->bounds.x2 > bounds.x2)
4993 bounds.x2=p->bounds.x2;
4994 if (p->bounds.y2 > bounds.y2)
4995 bounds.y2=p->bounds.y2;
4996 }
4997 bounds.x1-=(mid+1.0);
4998 bounds.y1-=(mid+1.0);
4999 bounds.x2+=(mid+1.0);
5000 bounds.y2+=(mid+1.0);
5001 if ((bounds.x1 >= (double) image->columns) ||
5002 (bounds.y1 >= (double) image->rows) ||
5003 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5004 {
5005 polygon_info=DestroyPolygonTLS(polygon_info);
5006 return(MagickTrue); /* virtual polygon */
5007 }
5008 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5009 (double) image->columns-1.0 : bounds.x1;
5010 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5011 (double) image->rows-1.0 : bounds.y1;
5012 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5013 (double) image->columns-1.0 : bounds.x2;
5014 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5015 (double) image->rows-1.0 : bounds.y2;
5016 poly_extent.x1=CastDoubleToLong(ceil(bounds.x1-0.5));
5017 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5018 poly_extent.x2=CastDoubleToLong(floor(bounds.x2+0.5));
5019 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5020 number_threads=GetMagickNumberThreads(image,image,poly_extent.y2-
5021 poly_extent.y1+1,1);
5022 status=AcquirePolygonEdgesTLS(polygon_info,number_threads,&image->exception);
5023 if (status == MagickFalse)
5024 {
5025 polygon_info=DestroyPolygonTLS(polygon_info);
5026 return(status);
5027 }
5028 status=MagickTrue;
5029 exception=(&image->exception);
5030 image_view=AcquireAuthenticCacheView(image,exception);
5031 if ((primitive_info->coordinates == 1) ||
5032 (polygon_info[0]->number_edges == 0))
5033 {
5034 /*
5035 Draw point.
5036 */
5037#if defined(MAGICKCORE_OPENMP_SUPPORT)
5038 #pragma omp parallel for schedule(static) shared(status) \
5039 num_threads(number_threads)
5040#endif
5041 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5042 {
5043 MagickBooleanType
5044 sync;
5045
5046 PixelPacket
5047 *magick_restrict q;
5048
5049 ssize_t
5050 x;
5051
5052 if (status == MagickFalse)
5053 continue;
5054 x=poly_extent.x1;
5055 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (poly_extent.x2-
5056 x+1),1,exception);
5057 if (q == (PixelPacket *) NULL)
5058 {
5059 status=MagickFalse;
5060 continue;
5061 }
5062 for ( ; x <= poly_extent.x2; x++)
5063 {
5064 if ((x == CastDoubleToLong(ceil(primitive_info->point.x-0.5))) &&
5065 (y == CastDoubleToLong(ceil(primitive_info->point.y-0.5))))
5066 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,q);
5067 q++;
5068 }
5069 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5070 if (sync == MagickFalse)
5071 status=MagickFalse;
5072 }
5073 image_view=DestroyCacheView(image_view);
5074 polygon_info=DestroyPolygonTLS(polygon_info);
5075 if (draw_info->debug != MagickFalse)
5076 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5077 " end draw-polygon");
5078 return(status);
5079 }
5080 /*
5081 Draw polygon or line.
5082 */
5083 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5084 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5085#if defined(MAGICKCORE_OPENMP_SUPPORT)
5086 #pragma omp parallel for schedule(static) shared(status) \
5087 num_threads(number_threads)
5088#endif
5089 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5090 {
5091 const int
5092 id = GetOpenMPThreadId();
5093
5094 PixelPacket
5095 fill_color,
5096 stroke_color;
5097
5098 PixelPacket
5099 *magick_restrict q;
5100
5101 ssize_t
5102 x;
5103
5104 if (status == MagickFalse)
5105 continue;
5106 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(size_t)
5107 (poly_extent.x2-poly_extent.x1+1),1,exception);
5108 if (q == (PixelPacket *) NULL)
5109 {
5110 status=MagickFalse;
5111 continue;
5112 }
5113 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5114 {
5115 double
5116 fill_opacity,
5117 stroke_opacity;
5118
5119 /*
5120 Fill and/or stroke.
5121 */
5122 fill_opacity=GetOpacityPixel(polygon_info[id],mid,fill,
5123 draw_info->fill_rule,x,y,&stroke_opacity);
5124 if (draw_info->stroke_antialias == MagickFalse)
5125 {
5126 fill_opacity=fill_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5127 stroke_opacity=stroke_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5128 }
5129 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5130 &fill_color);
5131 fill_opacity=(double) ((MagickRealType) QuantumRange-fill_opacity*
5132 ((MagickRealType) QuantumRange-(MagickRealType) fill_color.opacity));
5133 MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
5134 (MagickRealType) q->opacity,q);
5135 (void) GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5136 &stroke_color);
5137 stroke_opacity=(double) ((MagickRealType) QuantumRange-stroke_opacity*
5138 ((MagickRealType) QuantumRange-(MagickRealType) stroke_color.opacity));
5139 MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
5140 (MagickRealType) q->opacity,q);
5141 q++;
5142 }
5143 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5144 status=MagickFalse;
5145 }
5146 image_view=DestroyCacheView(image_view);
5147 polygon_info=DestroyPolygonTLS(polygon_info);
5148 if (draw_info->debug != MagickFalse)
5149 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5150 return(status);
5151}
5152
5153/*
5154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5155% %
5156% %
5157% %
5158% D r a w P r i m i t i v e %
5159% %
5160% %
5161% %
5162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5163%
5164% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5165%
5166% The format of the DrawPrimitive method is:
5167%
5168% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5169% PrimitiveInfo *primitive_info)
5170%
5171% A description of each parameter follows:
5172%
5173% o image: the image.
5174%
5175% o draw_info: the draw info.
5176%
5177% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5178%
5179*/
5180static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5181{
5182 const char
5183 *methods[] =
5184 {
5185 "point",
5186 "replace",
5187 "floodfill",
5188 "filltoborder",
5189 "reset",
5190 "?"
5191 };
5192
5193 PointInfo
5194 p,
5195 q,
5196 point;
5197
5198 ssize_t
5199 i,
5200 x;
5201
5202 ssize_t
5203 coordinates,
5204 y;
5205
5206 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5207 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5208 switch (primitive_info->primitive)
5209 {
5210 case PointPrimitive:
5211 {
5212 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5213 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5214 methods[primitive_info->method]);
5215 return;
5216 }
5217 case ColorPrimitive:
5218 {
5219 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5220 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5221 methods[primitive_info->method]);
5222 return;
5223 }
5224 case MattePrimitive:
5225 {
5226 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5227 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
5228 methods[primitive_info->method]);
5229 return;
5230 }
5231 case TextPrimitive:
5232 {
5233 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5234 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5235 return;
5236 }
5237 case ImagePrimitive:
5238 {
5239 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5240 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5241 return;
5242 }
5243 default:
5244 break;
5245 }
5246 coordinates=0;
5247 p=primitive_info[0].point;
5248 q.x=(-1.0);
5249 q.y=(-1.0);
5250 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5251 {
5252 point=primitive_info[i].point;
5253 if (coordinates <= 0)
5254 {
5255 coordinates=(ssize_t) primitive_info[i].coordinates;
5256 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5257 " begin open (%.20g)",(double) coordinates);
5258 p=point;
5259 }
5260 point=primitive_info[i].point;
5261 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5262 (fabs(q.y-point.y) >= MagickEpsilon))
5263 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5264 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5265 else
5266 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5267 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5268 q=point;
5269 coordinates--;
5270 if (coordinates > 0)
5271 continue;
5272 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5273 (fabs(p.y-point.y) >= MagickEpsilon))
5274 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5275 (double) coordinates);
5276 else
5277 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5278 (double) coordinates);
5279 }
5280}
5281
5282MagickExport MagickBooleanType DrawPrimitive(Image *image,
5283 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5284{
5285 CacheView
5286 *image_view;
5287
5288 ExceptionInfo
5289 *exception;
5290
5291 MagickStatusType
5292 status;
5293
5294 ssize_t
5295 i,
5296 x;
5297
5298 ssize_t
5299 y;
5300
5301 if (draw_info->debug != MagickFalse)
5302 {
5303 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5304 " begin draw-primitive");
5305 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5306 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5307 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5308 draw_info->affine.tx,draw_info->affine.ty);
5309 }
5310 exception=(&image->exception);
5311 status=MagickTrue;
5312 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5313 ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
5314 (IsPixelGray(&draw_info->stroke) == MagickFalse)))
5315 status=SetImageColorspace(image,sRGBColorspace);
5316 if (draw_info->compliance == SVGCompliance)
5317 {
5318 status&=SetImageClipMask(image,draw_info->clipping_mask);
5319 status&=SetImageMask(image,draw_info->composite_mask);
5320 }
5321 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5322 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5323 image_view=AcquireAuthenticCacheView(image,exception);
5324 switch (primitive_info->primitive)
5325 {
5326 case ColorPrimitive:
5327 {
5328 switch (primitive_info->method)
5329 {
5330 case PointMethod:
5331 default:
5332 {
5333 PixelPacket
5334 *q;
5335
5336 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5337 if (q == (PixelPacket *) NULL)
5338 break;
5339 (void) GetFillColor(draw_info,x,y,q);
5340 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5341 break;
5342 }
5343 case ReplaceMethod:
5344 {
5345 PixelPacket
5346 target;
5347
5348 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5349 for (y=0; y < (ssize_t) image->rows; y++)
5350 {
5351 PixelPacket
5352 *magick_restrict q;
5353
5354 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5355 exception);
5356 if (q == (PixelPacket *) NULL)
5357 break;
5358 for (x=0; x < (ssize_t) image->columns; x++)
5359 {
5360 if (IsColorSimilar(image,q,&target) == MagickFalse)
5361 {
5362 q++;
5363 continue;
5364 }
5365 (void) GetFillColor(draw_info,x,y,q);
5366 q++;
5367 }
5368 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5369 if (status == MagickFalse)
5370 break;
5371 }
5372 break;
5373 }
5374 case FloodfillMethod:
5375 case FillToBorderMethod:
5376 {
5377 MagickPixelPacket
5378 target;
5379
5380 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5381 if (primitive_info->method == FillToBorderMethod)
5382 {
5383 target.red=(MagickRealType) draw_info->border_color.red;
5384 target.green=(MagickRealType) draw_info->border_color.green;
5385 target.blue=(MagickRealType) draw_info->border_color.blue;
5386 }
5387 status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
5388 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5389 MagickTrue);
5390 break;
5391 }
5392 case ResetMethod:
5393 {
5394 for (y=0; y < (ssize_t) image->rows; y++)
5395 {
5396 PixelPacket
5397 *magick_restrict q;
5398
5399 ssize_t
5400 x;
5401
5402 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5403 exception);
5404 if (q == (PixelPacket *) NULL)
5405 break;
5406 for (x=0; x < (ssize_t) image->columns; x++)
5407 {
5408 (void) GetFillColor(draw_info,x,y,q);
5409 q++;
5410 }
5411 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5412 if (status == MagickFalse)
5413 break;
5414 }
5415 break;
5416 }
5417 }
5418 break;
5419 }
5420 case MattePrimitive:
5421 {
5422 if (image->matte == MagickFalse)
5423 status&=SetImageAlphaChannel(image,OpaqueAlphaChannel);
5424 switch (primitive_info->method)
5425 {
5426 case PointMethod:
5427 default:
5428 {
5429 PixelPacket
5430 pixel;
5431
5432 PixelPacket
5433 *q;
5434
5435 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5436 if (q == (PixelPacket *) NULL)
5437 break;
5438 (void) GetFillColor(draw_info,x,y,&pixel);
5439 SetPixelOpacity(q,pixel.opacity);
5440 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5441 break;
5442 }
5443 case ReplaceMethod:
5444 {
5445 PixelPacket
5446 pixel,
5447 target;
5448
5449 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5450 for (y=0; y < (ssize_t) image->rows; y++)
5451 {
5452 PixelPacket
5453 *magick_restrict q;
5454
5455 ssize_t
5456 x;
5457
5458 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5459 exception);
5460 if (q == (PixelPacket *) NULL)
5461 break;
5462 for (x=0; x < (ssize_t) image->columns; x++)
5463 {
5464 if (IsColorSimilar(image,q,&target) == MagickFalse)
5465 {
5466 q++;
5467 continue;
5468 }
5469 (void) GetFillColor(draw_info,x,y,&pixel);
5470 SetPixelOpacity(q,pixel.opacity);
5471 q++;
5472 }
5473 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5474 if (status == MagickFalse)
5475 break;
5476 }
5477 break;
5478 }
5479 case FloodfillMethod:
5480 case FillToBorderMethod:
5481 {
5482 MagickPixelPacket
5483 target;
5484
5485 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5486 if (primitive_info->method == FillToBorderMethod)
5487 {
5488 target.red=(MagickRealType) draw_info->border_color.red;
5489 target.green=(MagickRealType) draw_info->border_color.green;
5490 target.blue=(MagickRealType) draw_info->border_color.blue;
5491 }
5492 status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
5493 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5494 MagickTrue);
5495 break;
5496 }
5497 case ResetMethod:
5498 {
5499 PixelPacket
5500 pixel;
5501
5502 for (y=0; y < (ssize_t) image->rows; y++)
5503 {
5504 PixelPacket
5505 *magick_restrict q;
5506
5507 ssize_t
5508 x;
5509
5510 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5511 exception);
5512 if (q == (PixelPacket *) NULL)
5513 break;
5514 for (x=0; x < (ssize_t) image->columns; x++)
5515 {
5516 (void) GetFillColor(draw_info,x,y,&pixel);
5517 SetPixelOpacity(q,pixel.opacity);
5518 q++;
5519 }
5520 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5521 if (status == MagickFalse)
5522 break;
5523 }
5524 break;
5525 }
5526 }
5527 break;
5528 }
5529 case ImagePrimitive:
5530 {
5531 AffineMatrix
5532 affine;
5533
5534 char
5535 composite_geometry[MaxTextExtent];
5536
5537 Image
5538 *composite_image,
5539 *composite_images;
5540
5541 ImageInfo
5542 *clone_info;
5543
5544 RectangleInfo
5545 geometry;
5546
5547 ssize_t
5548 x1,
5549 y1;
5550
5551 if (primitive_info->text == (char *) NULL)
5552 break;
5553 clone_info=AcquireImageInfo();
5554 composite_images=(Image *) NULL;
5555 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5556 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5557 &image->exception);
5558 else
5559 if (*primitive_info->text != '\0')
5560 {
5561 /*
5562 Read composite image.
5563 */
5564 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5565 MagickPathExtent);
5566 (void) SetImageInfo(clone_info,1,exception);
5567 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5568 MagickPathExtent);
5569 if (clone_info->size != (char *) NULL)
5570 clone_info->size=DestroyString(clone_info->size);
5571 if (clone_info->extract != (char *) NULL)
5572 clone_info->extract=DestroyString(clone_info->extract);
5573 if ((LocaleCompare(clone_info->magick,"ftp") != 0) &&
5574 (LocaleCompare(clone_info->magick,"http") != 0) &&
5575 (LocaleCompare(clone_info->magick,"https") != 0) &&
5576 (LocaleCompare(clone_info->magick,"mvg") != 0) &&
5577 (LocaleCompare(clone_info->magick,"vid") != 0))
5578 composite_images=ReadImage(clone_info,exception);
5579 else
5580 (void) ThrowMagickException(exception,GetMagickModule(),
5581 FileOpenError,"UnableToOpenFile","`%s'",clone_info->filename);
5582 }
5583 clone_info=DestroyImageInfo(clone_info);
5584 if (composite_images == (Image *) NULL)
5585 {
5586 status=0;
5587 break;
5588 }
5589 composite_image=RemoveFirstImageFromList(&composite_images);
5590 composite_images=DestroyImageList(composite_images);
5591 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5592 NULL,(void *) NULL);
5593 x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5594 y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5595 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5596 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5597 {
5598 char
5599 geometry[MaxTextExtent];
5600
5601 /*
5602 Resize image.
5603 */
5604 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
5605 primitive_info[1].point.x,primitive_info[1].point.y);
5606 composite_image->filter=image->filter;
5607 status&=TransformImage(&composite_image,(char *) NULL,geometry);
5608 }
5609 if (composite_image->matte == MagickFalse)
5610 status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5611 if (draw_info->opacity != OpaqueOpacity)
5612 status&=SetImageOpacity(composite_image,draw_info->opacity);
5613 SetGeometry(image,&geometry);
5614 image->gravity=draw_info->gravity;
5615 geometry.x=x;
5616 geometry.y=y;
5617 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5618 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5619 composite_image->rows,(double) geometry.x,(double) geometry.y);
5620 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5621 &image->exception);
5622 affine=draw_info->affine;
5623 affine.tx=(double) geometry.x;
5624 affine.ty=(double) geometry.y;
5625 composite_image->interpolate=image->interpolate;
5626 if ((draw_info->compose == OverCompositeOp) ||
5627 (draw_info->compose == SrcOverCompositeOp))
5628 status&=DrawAffineImage(image,composite_image,&affine);
5629 else
5630 status&=CompositeImage(image,draw_info->compose,composite_image,
5631 geometry.x,geometry.y);
5632 composite_image=DestroyImage(composite_image);
5633 break;
5634 }
5635 case PointPrimitive:
5636 {
5637 PixelPacket
5638 fill_color;
5639
5640 PixelPacket
5641 *q;
5642
5643 if ((y < 0) || (y >= (ssize_t) image->rows))
5644 break;
5645 if ((x < 0) || (x >= (ssize_t) image->columns))
5646 break;
5647 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5648 if (q == (PixelPacket *) NULL)
5649 break;
5650 (void) GetFillColor(draw_info,x,y,&fill_color);
5651 MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5652 (MagickRealType) q->opacity,q);
5653 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5654 break;
5655 }
5656 case TextPrimitive:
5657 {
5658 char
5659 geometry[MaxTextExtent];
5660
5661 DrawInfo
5662 *clone_info;
5663
5664 if (primitive_info->text == (char *) NULL)
5665 break;
5666 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5667 (void) CloneString(&clone_info->text,primitive_info->text);
5668 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
5669 primitive_info->point.x,primitive_info->point.y);
5670 (void) CloneString(&clone_info->geometry,geometry);
5671 status&=AnnotateImage(image,clone_info);
5672 clone_info=DestroyDrawInfo(clone_info);
5673 break;
5674 }
5675 default:
5676 {
5677 double
5678 mid,
5679 scale;
5680
5681 DrawInfo
5682 *clone_info;
5683
5684 if (IsEventLogging() != MagickFalse)
5685 LogPrimitiveInfo(primitive_info);
5686 scale=ExpandAffine(&draw_info->affine);
5687 if ((draw_info->dash_pattern != (double *) NULL) &&
5688 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5689 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5690 (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5691 {
5692 /*
5693 Draw dash polygon.
5694 */
5695 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5696 clone_info->stroke_width=0.0;
5697 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5698 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5699 clone_info=DestroyDrawInfo(clone_info);
5700 if (status != MagickFalse)
5701 status&=DrawDashPolygon(draw_info,primitive_info,image);
5702 break;
5703 }
5704 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5705 if ((mid > 1.0) &&
5706 ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5707 (draw_info->stroke_pattern != (Image *) NULL)))
5708 {
5709 double
5710 x,
5711 y;
5712
5713 MagickBooleanType
5714 closed_path;
5715
5716 /*
5717 Draw strokes while respecting line cap/join attributes.
5718 */
5719 closed_path=primitive_info[0].closed_subpath;
5720 i=(ssize_t) primitive_info[0].coordinates;
5721 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5722 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5723 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5724 closed_path=MagickTrue;
5725 if ((((draw_info->linecap == RoundCap) ||
5726 (closed_path != MagickFalse)) &&
5727 (draw_info->linejoin == RoundJoin)) ||
5728 (primitive_info[i].primitive != UndefinedPrimitive))
5729 {
5730 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5731 break;
5732 }
5733 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5734 clone_info->stroke_width=0.0;
5735 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5736 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5737 clone_info=DestroyDrawInfo(clone_info);
5738 if (status != MagickFalse)
5739 status&=DrawStrokePolygon(image,draw_info,primitive_info);
5740 break;
5741 }
5742 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5743 break;
5744 }
5745 }
5746 image_view=DestroyCacheView(image_view);
5747 if (draw_info->compliance == SVGCompliance)
5748 {
5749 status&=SetImageClipMask(image,(Image *) NULL);
5750 status&=SetImageMask(image,(Image *) NULL);
5751 }
5752 if (draw_info->debug != MagickFalse)
5753 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5754 return(status != 0 ? MagickTrue : MagickFalse);
5755}
5756
5757/*
5758%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5759% %
5760% %
5761% %
5762+ D r a w S t r o k e P o l y g o n %
5763% %
5764% %
5765% %
5766%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5767%
5768% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
5769% the image while respecting the line cap and join attributes.
5770%
5771% The format of the DrawStrokePolygon method is:
5772%
5773% MagickBooleanType DrawStrokePolygon(Image *image,
5774% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5775%
5776% A description of each parameter follows:
5777%
5778% o image: the image.
5779%
5780% o draw_info: the draw info.
5781%
5782% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5783%
5784%
5785*/
5786
5787static MagickBooleanType DrawRoundLinecap(Image *image,
5788 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5789{
5790 PrimitiveInfo
5791 linecap[5];
5792
5793 ssize_t
5794 i;
5795
5796 if (primitive_info->coordinates < 1)
5797 return(MagickFalse);
5798 for (i=0; i < 4; i++)
5799 linecap[i]=(*primitive_info);
5800 linecap[0].coordinates=4;
5801 linecap[1].point.x+=2.0*MagickEpsilon;
5802 linecap[2].point.x+=2.0*MagickEpsilon;
5803 linecap[2].point.y+=2.0*MagickEpsilon;
5804 linecap[3].point.y+=2.0*MagickEpsilon;
5805 linecap[4].primitive=UndefinedPrimitive;
5806 return(DrawPolygonPrimitive(image,draw_info,linecap));
5807}
5808
5809static MagickBooleanType DrawStrokePolygon(Image *image,
5810 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5811{
5812 DrawInfo
5813 *clone_info;
5814
5815 MagickBooleanType
5816 closed_path;
5817
5818 MagickStatusType
5819 status;
5820
5821 PrimitiveInfo
5822 *stroke_polygon;
5823
5824 const PrimitiveInfo
5825 *p,
5826 *q;
5827
5828 /*
5829 Draw stroked polygon.
5830 */
5831 if (draw_info->debug != MagickFalse)
5832 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5833 " begin draw-stroke-polygon");
5834 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5835 clone_info->fill=draw_info->stroke;
5836 if (clone_info->fill_pattern != (Image *) NULL)
5837 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5838 if (clone_info->stroke_pattern != (Image *) NULL)
5839 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5840 MagickTrue,&clone_info->stroke_pattern->exception);
5841 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5842 clone_info->stroke_width=0.0;
5843 clone_info->fill_rule=NonZeroRule;
5844 status=MagickTrue;
5845 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5846 {
5847 if (p->coordinates == 1)
5848 continue;
5849 stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5850 if (stroke_polygon == (PrimitiveInfo *) NULL)
5851 {
5852 status=0;
5853 break;
5854 }
5855 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5856 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5857 if (status == 0)
5858 break;
5859 q=p+p->coordinates-1;
5860 closed_path=p->closed_subpath;
5861 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5862 {
5863 status&=DrawRoundLinecap(image,draw_info,p);
5864 status&=DrawRoundLinecap(image,draw_info,q);
5865 }
5866 }
5867 clone_info=DestroyDrawInfo(clone_info);
5868 if (draw_info->debug != MagickFalse)
5869 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5870 " end draw-stroke-polygon");
5871 return(status != 0 ? MagickTrue : MagickFalse);
5872}
5873
5874/*
5875%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5876% %
5877% %
5878% %
5879% G e t A f f i n e M a t r i x %
5880% %
5881% %
5882% %
5883%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5884%
5885% GetAffineMatrix() returns an AffineMatrix initialized to the identity
5886% matrix.
5887%
5888% The format of the GetAffineMatrix method is:
5889%
5890% void GetAffineMatrix(AffineMatrix *affine_matrix)
5891%
5892% A description of each parameter follows:
5893%
5894% o affine_matrix: the affine matrix.
5895%
5896*/
5897MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
5898{
5899 assert(affine_matrix != (AffineMatrix *) NULL);
5900 if (IsEventLogging() != MagickFalse)
5901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5902 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
5903 affine_matrix->sx=1.0;
5904 affine_matrix->sy=1.0;
5905}
5906
5907/*
5908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5909% %
5910% %
5911% %
5912+ G e t D r a w I n f o %
5913% %
5914% %
5915% %
5916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5917%
5918% GetDrawInfo() initializes draw_info to default values from image_info.
5919%
5920% The format of the GetDrawInfo method is:
5921%
5922% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5923%
5924% A description of each parameter follows:
5925%
5926% o image_info: the image info..
5927%
5928% o draw_info: the draw info.
5929%
5930*/
5931MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5932{
5933 char
5934 *next_token;
5935
5936 const char
5937 *option;
5938
5939 ExceptionInfo
5940 *exception;
5941
5942 /*
5943 Initialize draw attributes.
5944 */
5945 assert(draw_info != (DrawInfo *) NULL);
5946 if (IsEventLogging() != MagickFalse)
5947 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5948 (void) memset(draw_info,0,sizeof(*draw_info));
5949 draw_info->image_info=CloneImageInfo(image_info);
5950 GetAffineMatrix(&draw_info->affine);
5951 exception=AcquireExceptionInfo();
5952 (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
5953 (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
5954 draw_info->stroke_antialias=draw_info->image_info->antialias;
5955 draw_info->stroke_width=1.0;
5956 draw_info->fill_rule=EvenOddRule;
5957 draw_info->opacity=OpaqueOpacity;
5958 draw_info->fill_opacity=OpaqueOpacity;
5959 draw_info->stroke_opacity=OpaqueOpacity;
5960 draw_info->linecap=ButtCap;
5961 draw_info->linejoin=MiterJoin;
5962 draw_info->miterlimit=10;
5963 draw_info->decorate=NoDecoration;
5964 if (draw_info->image_info->font != (char *) NULL)
5965 draw_info->font=AcquireString(draw_info->image_info->font);
5966 if (draw_info->image_info->density != (char *) NULL)
5967 draw_info->density=AcquireString(draw_info->image_info->density);
5968 draw_info->text_antialias=draw_info->image_info->antialias;
5969 draw_info->pointsize=12.0;
5970 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
5971 draw_info->pointsize=draw_info->image_info->pointsize;
5972 draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
5973 draw_info->border_color=draw_info->image_info->border_color;
5974 draw_info->compose=OverCompositeOp;
5975 if (draw_info->image_info->server_name != (char *) NULL)
5976 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
5977 draw_info->render=MagickTrue;
5978 draw_info->clip_path=MagickFalse;
5979 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
5980 MagickTrue : MagickFalse;
5981 option=GetImageOption(draw_info->image_info,"direction");
5982 if (option != (const char *) NULL)
5983 draw_info->direction=(DirectionType) ParseCommandOption(
5984 MagickDirectionOptions,MagickFalse,option);
5985 else
5986 draw_info->direction=UndefinedDirection;
5987 option=GetImageOption(draw_info->image_info,"encoding");
5988 if (option != (const char *) NULL)
5989 (void) CloneString(&draw_info->encoding,option);
5990 option=GetImageOption(draw_info->image_info,"family");
5991 if (option != (const char *) NULL)
5992 (void) CloneString(&draw_info->family,option);
5993 option=GetImageOption(draw_info->image_info,"fill");
5994 if (option != (const char *) NULL)
5995 (void) QueryColorDatabase(option,&draw_info->fill,exception);
5996 option=GetImageOption(draw_info->image_info,"gravity");
5997 if (option != (const char *) NULL)
5998 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5999 MagickFalse,option);
6000 option=GetImageOption(draw_info->image_info,"interline-spacing");
6001 if (option != (const char *) NULL)
6002 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6003 option=GetImageOption(draw_info->image_info,"interword-spacing");
6004 if (option != (const char *) NULL)
6005 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6006 option=GetImageOption(draw_info->image_info,"kerning");
6007 if (option != (const char *) NULL)
6008 draw_info->kerning=GetDrawValue(option,&next_token);
6009 option=GetImageOption(draw_info->image_info,"stroke");
6010 if (option != (const char *) NULL)
6011 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
6012 option=GetImageOption(draw_info->image_info,"strokewidth");
6013 if (option != (const char *) NULL)
6014 draw_info->stroke_width=GetDrawValue(option,&next_token);
6015 option=GetImageOption(draw_info->image_info,"style");
6016 if (option != (const char *) NULL)
6017 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6018 MagickFalse,option);
6019 option=GetImageOption(draw_info->image_info,"undercolor");
6020 if (option != (const char *) NULL)
6021 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6022 option=GetImageOption(draw_info->image_info,"weight");
6023 if (option != (const char *) NULL)
6024 {
6025 ssize_t
6026 weight;
6027
6028 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6029 if (weight == -1)
6030 weight=(ssize_t) StringToUnsignedLong(option);
6031 draw_info->weight=(size_t) weight;
6032 }
6033 exception=DestroyExceptionInfo(exception);
6034 draw_info->signature=MagickCoreSignature;
6035}
6036
6037/*
6038%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6039% %
6040% %
6041% %
6042+ P e r m u t a t e %
6043% %
6044% %
6045% %
6046%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6047%
6048% Permutate() returns the permutation of the (n,k).
6049%
6050% The format of the Permutate method is:
6051%
6052% void Permutate(ssize_t n,ssize_t k)
6053%
6054% A description of each parameter follows:
6055%
6056% o n:
6057%
6058% o k:
6059%
6060%
6061*/
6062static inline double Permutate(const ssize_t n,const ssize_t k)
6063{
6064 double
6065 r;
6066
6067 ssize_t
6068 i;
6069
6070 r=1.0;
6071 for (i=k+1; i <= n; i++)
6072 r*=i;
6073 for (i=1; i <= (n-k); i++)
6074 r/=i;
6075 return(r);
6076}
6077
6078/*
6079%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6080% %
6081% %
6082% %
6083+ T r a c e P r i m i t i v e %
6084% %
6085% %
6086% %
6087%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6088%
6089% TracePrimitive is a collection of methods for generating graphic
6090% primitives such as arcs, ellipses, paths, etc.
6091%
6092*/
6093
6094static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6095 const PointInfo end,const PointInfo degrees)
6096{
6097 PointInfo
6098 center,
6099 radius;
6100
6101 center.x=0.5*(end.x+start.x);
6102 center.y=0.5*(end.y+start.y);
6103 radius.x=fabs(center.x-start.x);
6104 radius.y=fabs(center.y-start.y);
6105 return(TraceEllipse(mvg_info,center,radius,degrees));
6106}
6107
6108static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6109 const PointInfo end,const PointInfo arc,const double angle,
6110 const MagickBooleanType large_arc,const MagickBooleanType sweep)
6111{
6112 double
6113 alpha,
6114 beta,
6115 delta,
6116 factor,
6117 gamma,
6118 theta;
6119
6120 MagickStatusType
6121 status;
6122
6123 PointInfo
6124 center,
6125 points[3],
6126 radii;
6127
6128 double
6129 cosine,
6130 sine;
6131
6132 PrimitiveInfo
6133 *primitive_info;
6134
6135 PrimitiveInfo
6136 *p;
6137
6138 ssize_t
6139 i;
6140
6141 size_t
6142 arc_segments;
6143
6144 ssize_t
6145 offset;
6146
6147 offset=mvg_info->offset;
6148 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6149 primitive_info->coordinates=0;
6150 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6151 (fabs(start.y-end.y) < MagickEpsilon))
6152 return(TracePoint(primitive_info,end));
6153 radii.x=fabs(arc.x);
6154 radii.y=fabs(arc.y);
6155 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6156 return(TraceLine(primitive_info,start,end));
6157 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6158 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6159 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6160 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6161 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6162 (radii.y*radii.y);
6163 if (delta < MagickEpsilon)
6164 return(TraceLine(primitive_info,start,end));
6165 if (delta > 1.0)
6166 {
6167 radii.x*=sqrt((double) delta);
6168 radii.y*=sqrt((double) delta);
6169 }
6170 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6171 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6172 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6173 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6174 alpha=points[1].x-points[0].x;
6175 beta=points[1].y-points[0].y;
6176 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6177 return(TraceLine(primitive_info,start,end));
6178 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6179 if (factor <= 0.0)
6180 factor=0.0;
6181 else
6182 {
6183 factor=sqrt((double) factor);
6184 if (sweep == large_arc)
6185 factor=(-factor);
6186 }
6187 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6188 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6189 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6190 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6191 if ((theta < 0.0) && (sweep != MagickFalse))
6192 theta+=2.0*MagickPI;
6193 else
6194 if ((theta > 0.0) && (sweep == MagickFalse))
6195 theta-=2.0*MagickPI;
6196 arc_segments=(size_t) CastDoubleToLong(ceil(fabs((double) (theta/(0.5*
6197 MagickPI+MagickEpsilon)))));
6198 p=primitive_info;
6199 status=MagickTrue;
6200 for (i=0; i < (ssize_t) arc_segments; i++)
6201 {
6202 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6203 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6204 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6205 sin(fmod((double) beta,DegreesToRadians(360.0)));
6206 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6207 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6208 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6209 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6210 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6211 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6212 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6213 theta/arc_segments),DegreesToRadians(360.0))));
6214 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6215 theta/arc_segments),DegreesToRadians(360.0))));
6216 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6217 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6218 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6219 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6220 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6221 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6222 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6223 points[0].y);
6224 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6225 points[0].y);
6226 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6227 points[1].y);
6228 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6229 points[1].y);
6230 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6231 points[2].y);
6232 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6233 points[2].y);
6234 if (i == (ssize_t) (arc_segments-1))
6235 (p+3)->point=end;
6236 status&=TraceBezier(mvg_info,4);
6237 if (status == 0)
6238 break;
6239 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6240 mvg_info->offset+=p->coordinates;
6241 p+=(ptrdiff_t) p->coordinates;
6242 }
6243 if (status == 0)
6244 return(MagickFalse);
6245 mvg_info->offset=offset;
6246 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6247 primitive_info->coordinates=(size_t) (p-primitive_info);
6248 primitive_info->closed_subpath=MagickFalse;
6249 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6250 {
6251 p->primitive=primitive_info->primitive;
6252 p--;
6253 }
6254 return(MagickTrue);
6255}
6256
6257static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6258 const size_t number_coordinates)
6259{
6260 double
6261 alpha,
6262 *coefficients,
6263 weight;
6264
6265 PointInfo
6266 end,
6267 point,
6268 *points;
6269
6270 PrimitiveInfo
6271 *primitive_info;
6272
6273 PrimitiveInfo
6274 *p;
6275
6276 ssize_t
6277 i,
6278 j;
6279
6280 size_t
6281 control_points,
6282 quantum;
6283
6284 /*
6285 Allocate coefficients.
6286 */
6287 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6288 quantum=number_coordinates;
6289 for (i=0; i < (ssize_t) number_coordinates; i++)
6290 {
6291 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6292 {
6293 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6294 if (alpha > (double) GetMaxMemoryRequest())
6295 {
6296 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6297 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6298 return(MagickFalse);
6299 }
6300 if (alpha > (double) quantum)
6301 quantum=(size_t) alpha;
6302 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6303 if (alpha > (double) quantum)
6304 quantum=(size_t) alpha;
6305 }
6306 }
6307 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6308 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6309 if (quantum > (double) GetMaxMemoryRequest())
6310 {
6311 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6312 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6313 return(MagickFalse);
6314 }
6315 coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6316 sizeof(*coefficients));
6317 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6318 sizeof(*points));
6319 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6320 {
6321 if (points != (PointInfo *) NULL)
6322 points=(PointInfo *) RelinquishMagickMemory(points);
6323 if (coefficients != (double *) NULL)
6324 coefficients=(double *) RelinquishMagickMemory(coefficients);
6325 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6326 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6327 return(MagickFalse);
6328 }
6329 control_points=quantum*number_coordinates;
6330 if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6331 {
6332 points=(PointInfo *) RelinquishMagickMemory(points);
6333 coefficients=(double *) RelinquishMagickMemory(coefficients);
6334 return(MagickFalse);
6335 }
6336 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6337 /*
6338 Compute bezier points.
6339 */
6340 end=primitive_info[number_coordinates-1].point;
6341 for (i=0; i < (ssize_t) number_coordinates; i++)
6342 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6343 weight=0.0;
6344 for (i=0; i < (ssize_t) control_points; i++)
6345 {
6346 p=primitive_info;
6347 point.x=0.0;
6348 point.y=0.0;
6349 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6350 for (j=0; j < (ssize_t) number_coordinates; j++)
6351 {
6352 point.x+=alpha*coefficients[j]*p->point.x;
6353 point.y+=alpha*coefficients[j]*p->point.y;
6354 alpha*=weight/(1.0-weight);
6355 p++;
6356 }
6357 points[i]=point;
6358 weight+=1.0/control_points;
6359 }
6360 /*
6361 Bezier curves are just short segmented polys.
6362 */
6363 p=primitive_info;
6364 for (i=0; i < (ssize_t) control_points; i++)
6365 {
6366 if (TracePoint(p,points[i]) == MagickFalse)
6367 {
6368 points=(PointInfo *) RelinquishMagickMemory(points);
6369 coefficients=(double *) RelinquishMagickMemory(coefficients);
6370 return(MagickFalse);
6371 }
6372 p+=(ptrdiff_t) p->coordinates;
6373 }
6374 if (TracePoint(p,end) == MagickFalse)
6375 {
6376 points=(PointInfo *) RelinquishMagickMemory(points);
6377 coefficients=(double *) RelinquishMagickMemory(coefficients);
6378 return(MagickFalse);
6379 }
6380 p+=(ptrdiff_t) p->coordinates;
6381 primitive_info->coordinates=(size_t) (p-primitive_info);
6382 primitive_info->closed_subpath=MagickFalse;
6383 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6384 {
6385 p->primitive=primitive_info->primitive;
6386 p--;
6387 }
6388 points=(PointInfo *) RelinquishMagickMemory(points);
6389 coefficients=(double *) RelinquishMagickMemory(coefficients);
6390 return(MagickTrue);
6391}
6392
6393static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6394 const PointInfo end)
6395{
6396 double
6397 alpha,
6398 beta,
6399 radius;
6400
6401 PointInfo
6402 offset,
6403 degrees;
6404
6405 alpha=end.x-start.x;
6406 beta=end.y-start.y;
6407 radius=hypot((double) alpha,(double) beta);
6408 offset.x=(double) radius;
6409 offset.y=(double) radius;
6410 degrees.x=0.0;
6411 degrees.y=360.0;
6412 return(TraceEllipse(mvg_info,start,offset,degrees));
6413}
6414
6415static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6416 const PointInfo radii,const PointInfo arc)
6417{
6418 double
6419 coordinates,
6420 delta,
6421 step,
6422 x,
6423 y;
6424
6425 PointInfo
6426 angle,
6427 point;
6428
6429 PrimitiveInfo
6430 *primitive_info;
6431
6432 PrimitiveInfo
6433 *p;
6434
6435 ssize_t
6436 i;
6437
6438 /*
6439 Ellipses are just short segmented polys.
6440 */
6441 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6442 primitive_info->coordinates=0;
6443 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6444 return(MagickTrue);
6445 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6446 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6447 angle.x=DegreesToRadians(arc.x);
6448 y=arc.y;
6449 while (y < arc.x)
6450 y+=360.0;
6451 angle.y=DegreesToRadians(y);
6452 coordinates=ceil((angle.y-angle.x)/step+1.0);
6453 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6454 return(MagickFalse);
6455 i=0;
6456 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6457 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6458 {
6459 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6460 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6461 if (i++ >= (ssize_t) coordinates)
6462 break;
6463 if (TracePoint(p,point) == MagickFalse)
6464 return(MagickFalse);
6465 p+=(ptrdiff_t) p->coordinates;
6466 }
6467 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6468 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6469 if (TracePoint(p,point) == MagickFalse)
6470 return(MagickFalse);
6471 p+=(ptrdiff_t) p->coordinates;
6472 primitive_info->coordinates=(size_t) (p-primitive_info);
6473 primitive_info->closed_subpath=MagickFalse;
6474 x=fabs(primitive_info[0].point.x-
6475 primitive_info[primitive_info->coordinates-1].point.x);
6476 y=fabs(primitive_info[0].point.y-
6477 primitive_info[primitive_info->coordinates-1].point.y);
6478 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6479 primitive_info->closed_subpath=MagickTrue;
6480 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6481 {
6482 p->primitive=primitive_info->primitive;
6483 p--;
6484 }
6485 return(MagickTrue);
6486}
6487
6488static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6489 const PointInfo start,const PointInfo end)
6490{
6491 if (TracePoint(primitive_info,start) == MagickFalse)
6492 return(MagickFalse);
6493 if (TracePoint(primitive_info+1,end) == MagickFalse)
6494 return(MagickFalse);
6495 (primitive_info+1)->primitive=primitive_info->primitive;
6496 primitive_info->coordinates=2;
6497 primitive_info->closed_subpath=MagickFalse;
6498 return(MagickTrue);
6499}
6500
6501static ssize_t TracePath(Image *image,MVGInfo *mvg_info,const char *path)
6502{
6503 char
6504 *next_token,
6505 token[MaxTextExtent] = "";
6506
6507 const char
6508 *p;
6509
6510 double
6511 x,
6512 y;
6513
6514 int
6515 attribute,
6516 last_attribute;
6517
6518 MagickStatusType
6519 status;
6520
6521 PointInfo
6522 end = {0.0, 0.0},
6523 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6524 point = {0.0, 0.0},
6525 start = {0.0, 0.0};
6526
6527 PrimitiveInfo
6528 *primitive_info;
6529
6530 PrimitiveType
6531 primitive_type;
6532
6533 PrimitiveInfo
6534 *q;
6535
6536 ssize_t
6537 i;
6538
6539 size_t
6540 number_coordinates,
6541 z_count;
6542
6543 ssize_t
6544 subpath_offset;
6545
6546 subpath_offset=mvg_info->offset;
6547 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6548 status=MagickTrue;
6549 attribute=0;
6550 number_coordinates=0;
6551 z_count=0;
6552 *token='\0';
6553 primitive_type=primitive_info->primitive;
6554 q=primitive_info;
6555 for (p=path; *p != '\0'; )
6556 {
6557 if (status == MagickFalse)
6558 break;
6559 while (isspace((int) ((unsigned char) *p)) != 0)
6560 p++;
6561 if (*p == '\0')
6562 break;
6563 last_attribute=attribute;
6564 attribute=(int) (*p++);
6565 switch (attribute)
6566 {
6567 case 'a':
6568 case 'A':
6569 {
6570 double
6571 angle = 0.0;
6572
6573 MagickBooleanType
6574 large_arc = MagickFalse,
6575 sweep = MagickFalse;
6576
6577 PointInfo
6578 arc = {0.0, 0.0};
6579
6580 /*
6581 Elliptical arc.
6582 */
6583 do
6584 {
6585 (void) GetNextToken(p,&p,MaxTextExtent,token);
6586 if (*token == ',')
6587 (void) GetNextToken(p,&p,MaxTextExtent,token);
6588 arc.x=GetDrawValue(token,&next_token);
6589 if (token == next_token)
6590 ThrowPointExpectedException(image,token);
6591 (void) GetNextToken(p,&p,MaxTextExtent,token);
6592 if (*token == ',')
6593 (void) GetNextToken(p,&p,MaxTextExtent,token);
6594 arc.y=GetDrawValue(token,&next_token);
6595 if (token == next_token)
6596 ThrowPointExpectedException(image,token);
6597 (void) GetNextToken(p,&p,MaxTextExtent,token);
6598 if (*token == ',')
6599 (void) GetNextToken(p,&p,MaxTextExtent,token);
6600 angle=GetDrawValue(token,&next_token);
6601 if (token == next_token)
6602 ThrowPointExpectedException(image,token);
6603 (void) GetNextToken(p,&p,MaxTextExtent,token);
6604 if (*token == ',')
6605 (void) GetNextToken(p,&p,MaxTextExtent,token);
6606 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6607 (void) GetNextToken(p,&p,MaxTextExtent,token);
6608 if (*token == ',')
6609 (void) GetNextToken(p,&p,MaxTextExtent,token);
6610 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6611 if (*token == ',')
6612 (void) GetNextToken(p,&p,MaxTextExtent,token);
6613 (void) GetNextToken(p,&p,MaxTextExtent,token);
6614 if (*token == ',')
6615 (void) GetNextToken(p,&p,MaxTextExtent,token);
6616 x=GetDrawValue(token,&next_token);
6617 if (token == next_token)
6618 ThrowPointExpectedException(image,token);
6619 (void) GetNextToken(p,&p,MaxTextExtent,token);
6620 if (*token == ',')
6621 (void) GetNextToken(p,&p,MaxTextExtent,token);
6622 y=GetDrawValue(token,&next_token);
6623 if (token == next_token)
6624 ThrowPointExpectedException(image,token);
6625 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6626 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6627 status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6628 q=(*mvg_info->primitive_info)+mvg_info->offset;
6629 mvg_info->offset+=q->coordinates;
6630 q+=(ptrdiff_t) q->coordinates;
6631 point=end;
6632 while (isspace((int) ((unsigned char) *p)) != 0)
6633 p++;
6634 if (*p == ',')
6635 p++;
6636 } while (IsPoint(p) != MagickFalse);
6637 break;
6638 }
6639 case 'c':
6640 case 'C':
6641 {
6642 /*
6643 Cubic Bézier curve.
6644 */
6645 do
6646 {
6647 points[0]=point;
6648 for (i=1; i < 4; i++)
6649 {
6650 (void) GetNextToken(p,&p,MaxTextExtent,token);
6651 if (*token == ',')
6652 (void) GetNextToken(p,&p,MaxTextExtent,token);
6653 x=GetDrawValue(token,&next_token);
6654 if (token == next_token)
6655 ThrowPointExpectedException(image,token);
6656 (void) GetNextToken(p,&p,MaxTextExtent,token);
6657 if (*token == ',')
6658 (void) GetNextToken(p,&p,MaxTextExtent,token);
6659 y=GetDrawValue(token,&next_token);
6660 if (token == next_token)
6661 ThrowPointExpectedException(image,token);
6662 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6663 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6664 points[i]=end;
6665 }
6666 for (i=0; i < 4; i++)
6667 (q+i)->point=points[i];
6668 if (TraceBezier(mvg_info,4) == MagickFalse)
6669 return(-1);
6670 q=(*mvg_info->primitive_info)+mvg_info->offset;
6671 mvg_info->offset+=q->coordinates;
6672 q+=(ptrdiff_t) q->coordinates;
6673 point=end;
6674 while (isspace((int) ((unsigned char) *p)) != 0)
6675 p++;
6676 if (*p == ',')
6677 p++;
6678 } while (IsPoint(p) != MagickFalse);
6679 break;
6680 }
6681 case 'H':
6682 case 'h':
6683 {
6684 do
6685 {
6686 (void) GetNextToken(p,&p,MaxTextExtent,token);
6687 if (*token == ',')
6688 (void) GetNextToken(p,&p,MaxTextExtent,token);
6689 x=GetDrawValue(token,&next_token);
6690 if (token == next_token)
6691 ThrowPointExpectedException(image,token);
6692 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6693 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6694 return(-1);
6695 q=(*mvg_info->primitive_info)+mvg_info->offset;
6696 if (TracePoint(q,point) == MagickFalse)
6697 return(-1);
6698 mvg_info->offset+=q->coordinates;
6699 q+=(ptrdiff_t) q->coordinates;
6700 while (isspace((int) ((unsigned char) *p)) != 0)
6701 p++;
6702 if (*p == ',')
6703 p++;
6704 } while (IsPoint(p) != MagickFalse);
6705 break;
6706 }
6707 case 'l':
6708 case 'L':
6709 {
6710 /*
6711 Line to.
6712 */
6713 do
6714 {
6715 (void) GetNextToken(p,&p,MaxTextExtent,token);
6716 if (*token == ',')
6717 (void) GetNextToken(p,&p,MaxTextExtent,token);
6718 x=GetDrawValue(token,&next_token);
6719 if (token == next_token)
6720 ThrowPointExpectedException(image,token);
6721 (void) GetNextToken(p,&p,MaxTextExtent,token);
6722 if (*token == ',')
6723 (void) GetNextToken(p,&p,MaxTextExtent,token);
6724 y=GetDrawValue(token,&next_token);
6725 if (token == next_token)
6726 ThrowPointExpectedException(image,token);
6727 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
6728 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
6729 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6730 return(-1);
6731 q=(*mvg_info->primitive_info)+mvg_info->offset;
6732 if (TracePoint(q,point) == MagickFalse)
6733 return(-1);
6734 mvg_info->offset+=q->coordinates;
6735 q+=(ptrdiff_t) q->coordinates;
6736 while (isspace((int) ((unsigned char) *p)) != 0)
6737 p++;
6738 if (*p == ',')
6739 p++;
6740 } while (IsPoint(p) != MagickFalse);
6741 break;
6742 }
6743 case 'M':
6744 case 'm':
6745 {
6746 /*
6747 Move to.
6748 */
6749 if (mvg_info->offset != subpath_offset)
6750 {
6751 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6752 primitive_info->coordinates=(size_t) (q-primitive_info);
6753 number_coordinates+=primitive_info->coordinates;
6754 primitive_info=q;
6755 subpath_offset=mvg_info->offset;
6756 }
6757 i=0;
6758 do
6759 {
6760 (void) GetNextToken(p,&p,MaxTextExtent,token);
6761 if (*token == ',')
6762 (void) GetNextToken(p,&p,MaxTextExtent,token);
6763 x=GetDrawValue(token,&next_token);
6764 if (token == next_token)
6765 ThrowPointExpectedException(image,token);
6766 (void) GetNextToken(p,&p,MaxTextExtent,token);
6767 if (*token == ',')
6768 (void) GetNextToken(p,&p,MaxTextExtent,token);
6769 y=GetDrawValue(token,&next_token);
6770 if (token == next_token)
6771 ThrowPointExpectedException(image,token);
6772 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
6773 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
6774 if (i == 0)
6775 start=point;
6776 i++;
6777 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6778 return(-1);
6779 q=(*mvg_info->primitive_info)+mvg_info->offset;
6780 if (TracePoint(q,point) == MagickFalse)
6781 return(-1);
6782 mvg_info->offset+=q->coordinates;
6783 q+=(ptrdiff_t) q->coordinates;
6784 while (isspace((int) ((unsigned char) *p)) != 0)
6785 p++;
6786 if (*p == ',')
6787 p++;
6788 } while (IsPoint(p) != MagickFalse);
6789 break;
6790 }
6791 case 'q':
6792 case 'Q':
6793 {
6794 /*
6795 Quadratic Bézier curve.
6796 */
6797 do
6798 {
6799 points[0]=point;
6800 for (i=1; i < 3; i++)
6801 {
6802 (void) GetNextToken(p,&p,MaxTextExtent,token);
6803 if (*token == ',')
6804 (void) GetNextToken(p,&p,MaxTextExtent,token);
6805 x=GetDrawValue(token,&next_token);
6806 if (token == next_token)
6807 ThrowPointExpectedException(image,token);
6808 (void) GetNextToken(p,&p,MaxTextExtent,token);
6809 if (*token == ',')
6810 (void) GetNextToken(p,&p,MaxTextExtent,token);
6811 y=GetDrawValue(token,&next_token);
6812 if (token == next_token)
6813 ThrowPointExpectedException(image,token);
6814 if (*p == ',')
6815 p++;
6816 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
6817 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
6818 points[i]=end;
6819 }
6820 for (i=0; i < 3; i++)
6821 (q+i)->point=points[i];
6822 if (TraceBezier(mvg_info,3) == MagickFalse)
6823 return(-1);
6824 q=(*mvg_info->primitive_info)+mvg_info->offset;
6825 mvg_info->offset+=q->coordinates;
6826 q+=(ptrdiff_t) q->coordinates;
6827 point=end;
6828 while (isspace((int) ((unsigned char) *p)) != 0)
6829 p++;
6830 if (*p == ',')
6831 p++;
6832 } while (IsPoint(p) != MagickFalse);
6833 break;
6834 }
6835 case 's':
6836 case 'S':
6837 {
6838 /*
6839 Cubic Bézier curve.
6840 */
6841 do
6842 {
6843 points[0]=points[3];
6844 points[1].x=2.0*points[3].x-points[2].x;
6845 points[1].y=2.0*points[3].y-points[2].y;
6846 for (i=2; i < 4; i++)
6847 {
6848 (void) GetNextToken(p,&p,MaxTextExtent,token);
6849 if (*token == ',')
6850 (void) GetNextToken(p,&p,MaxTextExtent,token);
6851 x=GetDrawValue(token,&next_token);
6852 if (token == next_token)
6853 ThrowPointExpectedException(image,token);
6854 (void) GetNextToken(p,&p,MaxTextExtent,token);
6855 if (*token == ',')
6856 (void) GetNextToken(p,&p,MaxTextExtent,token);
6857 y=GetDrawValue(token,&next_token);
6858 if (token == next_token)
6859 ThrowPointExpectedException(image,token);
6860 if (*p == ',')
6861 p++;
6862 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
6863 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
6864 points[i]=end;
6865 }
6866 if (strchr("CcSs",last_attribute) == (char *) NULL)
6867 {
6868 points[0]=point;
6869 points[1]=point;
6870 }
6871 for (i=0; i < 4; i++)
6872 (q+i)->point=points[i];
6873 if (TraceBezier(mvg_info,4) == MagickFalse)
6874 return(-1);
6875 q=(*mvg_info->primitive_info)+mvg_info->offset;
6876 mvg_info->offset+=q->coordinates;
6877 q+=(ptrdiff_t) q->coordinates;
6878 point=end;
6879 last_attribute=attribute;
6880 while (isspace((int) ((unsigned char) *p)) != 0)
6881 p++;
6882 if (*p == ',')
6883 p++;
6884 } while (IsPoint(p) != MagickFalse);
6885 break;
6886 }
6887 case 't':
6888 case 'T':
6889 {
6890 /*
6891 Quadratic Bézier curve.
6892 */
6893 do
6894 {
6895 points[0]=points[2];
6896 points[1].x=2.0*points[2].x-points[1].x;
6897 points[1].y=2.0*points[2].y-points[1].y;
6898 for (i=2; i < 3; i++)
6899 {
6900 (void) GetNextToken(p,&p,MaxTextExtent,token);
6901 if (*token == ',')
6902 (void) GetNextToken(p,&p,MaxTextExtent,token);
6903 x=GetDrawValue(token,&next_token);
6904 if (token == next_token)
6905 ThrowPointExpectedException(image,token);
6906 (void) GetNextToken(p,&p,MaxTextExtent,token);
6907 if (*token == ',')
6908 (void) GetNextToken(p,&p,MaxTextExtent,token);
6909 y=GetDrawValue(token,&next_token);
6910 if (token == next_token)
6911 ThrowPointExpectedException(image,token);
6912 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
6913 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
6914 points[i]=end;
6915 }
6916 if (status == MagickFalse)
6917 break;
6918 if (strchr("QqTt",last_attribute) == (char *) NULL)
6919 {
6920 points[0]=point;
6921 points[1]=point;
6922 }
6923 for (i=0; i < 3; i++)
6924 (q+i)->point=points[i];
6925 if (TraceBezier(mvg_info,3) == MagickFalse)
6926 return(-1);
6927 q=(*mvg_info->primitive_info)+mvg_info->offset;
6928 mvg_info->offset+=q->coordinates;
6929 q+=(ptrdiff_t) q->coordinates;
6930 point=end;
6931 last_attribute=attribute;
6932 while (isspace((int) ((unsigned char) *p)) != 0)
6933 p++;
6934 if (*p == ',')
6935 p++;
6936 } while (IsPoint(p) != MagickFalse);
6937 break;
6938 }
6939 case 'v':
6940 case 'V':
6941 {
6942 /*
6943 Line to.
6944 */
6945 do
6946 {
6947 (void) GetNextToken(p,&p,MaxTextExtent,token);
6948 if (*token == ',')
6949 (void) GetNextToken(p,&p,MaxTextExtent,token);
6950 y=GetDrawValue(token,&next_token);
6951 if (token == next_token)
6952 ThrowPointExpectedException(image,token);
6953 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
6954 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6955 return(-1);
6956 q=(*mvg_info->primitive_info)+mvg_info->offset;
6957 if (TracePoint(q,point) == MagickFalse)
6958 return(-1);
6959 mvg_info->offset+=q->coordinates;
6960 q+=(ptrdiff_t) q->coordinates;
6961 while (isspace((int) ((unsigned char) *p)) != 0)
6962 p++;
6963 if (*p == ',')
6964 p++;
6965 } while (IsPoint(p) != MagickFalse);
6966 break;
6967 }
6968 case 'z':
6969 case 'Z':
6970 {
6971 /*
6972 Close path.
6973 */
6974 point=start;
6975 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6976 return(-1);
6977 q=(*mvg_info->primitive_info)+mvg_info->offset;
6978 if (TracePoint(q,point) == MagickFalse)
6979 return(-1);
6980 mvg_info->offset+=q->coordinates;
6981 q+=(ptrdiff_t) q->coordinates;
6982 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6983 primitive_info->coordinates=(size_t) (q-primitive_info);
6984 primitive_info->closed_subpath=MagickTrue;
6985 number_coordinates+=primitive_info->coordinates;
6986 primitive_info=q;
6987 subpath_offset=mvg_info->offset;
6988 z_count++;
6989 break;
6990 }
6991 default:
6992 {
6993 ThrowPointExpectedException(image,token);
6994 break;
6995 }
6996 }
6997 }
6998 if (status == MagickFalse)
6999 return(-1);
7000 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7001 primitive_info->coordinates=(size_t) (q-primitive_info);
7002 number_coordinates+=primitive_info->coordinates;
7003 for (i=0; i < (ssize_t) number_coordinates; i++)
7004 {
7005 q--;
7006 q->primitive=primitive_type;
7007 if (z_count > 1)
7008 q->method=FillToBorderMethod;
7009 }
7010 q=primitive_info;
7011 return((ssize_t) number_coordinates);
7012}
7013
7014static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7015 const PointInfo start,const PointInfo end)
7016{
7017 PointInfo
7018 point;
7019
7020 PrimitiveInfo
7021 *p;
7022
7023 ssize_t
7024 i;
7025
7026 p=primitive_info;
7027 if (TracePoint(p,start) == MagickFalse)
7028 return(MagickFalse);
7029 p+=(ptrdiff_t) p->coordinates;
7030 point.x=start.x;
7031 point.y=end.y;
7032 if (TracePoint(p,point) == MagickFalse)
7033 return(MagickFalse);
7034 p+=(ptrdiff_t) p->coordinates;
7035 if (TracePoint(p,end) == MagickFalse)
7036 return(MagickFalse);
7037 p+=(ptrdiff_t) p->coordinates;
7038 point.x=end.x;
7039 point.y=start.y;
7040 if (TracePoint(p,point) == MagickFalse)
7041 return(MagickFalse);
7042 p+=(ptrdiff_t) p->coordinates;
7043 if (TracePoint(p,start) == MagickFalse)
7044 return(MagickFalse);
7045 p+=(ptrdiff_t) p->coordinates;
7046 primitive_info->coordinates=(size_t) (p-primitive_info);
7047 primitive_info->closed_subpath=MagickTrue;
7048 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7049 {
7050 p->primitive=primitive_info->primitive;
7051 p--;
7052 }
7053 return(MagickTrue);
7054}
7055
7056static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7057 const PointInfo start,const PointInfo end,PointInfo arc)
7058{
7059 PointInfo
7060 degrees,
7061 point,
7062 segment;
7063
7064 PrimitiveInfo
7065 *primitive_info;
7066
7067 PrimitiveInfo
7068 *p;
7069
7070 ssize_t
7071 i;
7072
7073 ssize_t
7074 offset;
7075
7076 offset=mvg_info->offset;
7077 segment.x=fabs(end.x-start.x);
7078 segment.y=fabs(end.y-start.y);
7079 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7080 {
7081 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7082 return(MagickTrue);
7083 }
7084 if (arc.x > (0.5*segment.x))
7085 arc.x=0.5*segment.x;
7086 if (arc.y > (0.5*segment.y))
7087 arc.y=0.5*segment.y;
7088 point.x=start.x+segment.x-arc.x;
7089 point.y=start.y+arc.y;
7090 degrees.x=270.0;
7091 degrees.y=360.0;
7092 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7093 return(MagickFalse);
7094 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7095 mvg_info->offset+=p->coordinates;
7096 point.x=start.x+segment.x-arc.x;
7097 point.y=start.y+segment.y-arc.y;
7098 degrees.x=0.0;
7099 degrees.y=90.0;
7100 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7101 return(MagickFalse);
7102 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7103 mvg_info->offset+=p->coordinates;
7104 point.x=start.x+arc.x;
7105 point.y=start.y+segment.y-arc.y;
7106 degrees.x=90.0;
7107 degrees.y=180.0;
7108 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7109 return(MagickFalse);
7110 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7111 mvg_info->offset+=p->coordinates;
7112 point.x=start.x+arc.x;
7113 point.y=start.y+arc.y;
7114 degrees.x=180.0;
7115 degrees.y=270.0;
7116 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7117 return(MagickFalse);
7118 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7119 mvg_info->offset+=p->coordinates;
7120 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7121 return(MagickFalse);
7122 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7123 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7124 return(MagickFalse);
7125 p+=(ptrdiff_t) p->coordinates;
7126 mvg_info->offset=offset;
7127 primitive_info=(*mvg_info->primitive_info)+offset;
7128 primitive_info->coordinates=(size_t) (p-primitive_info);
7129 primitive_info->closed_subpath=MagickTrue;
7130 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7131 {
7132 p->primitive=primitive_info->primitive;
7133 p--;
7134 }
7135 return(MagickTrue);
7136}
7137
7138static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7139 const size_t number_vertices,const double offset)
7140{
7141 double
7142 distance;
7143
7144 double
7145 dx,
7146 dy;
7147
7148 ssize_t
7149 i;
7150
7151 ssize_t
7152 j;
7153
7154 dx=0.0;
7155 dy=0.0;
7156 for (i=1; i < (ssize_t) number_vertices; i++)
7157 {
7158 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7159 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7160 if ((fabs((double) dx) >= MagickEpsilon) ||
7161 (fabs((double) dy) >= MagickEpsilon))
7162 break;
7163 }
7164 if (i == (ssize_t) number_vertices)
7165 i=(ssize_t) number_vertices-1L;
7166 distance=hypot((double) dx,(double) dy);
7167 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7168 dx*(distance+offset)/distance);
7169 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7170 dy*(distance+offset)/distance);
7171 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7172 {
7173 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7174 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7175 if ((fabs((double) dx) >= MagickEpsilon) ||
7176 (fabs((double) dy) >= MagickEpsilon))
7177 break;
7178 }
7179 distance=hypot((double) dx,(double) dy);
7180 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7181 dx*(distance+offset)/distance);
7182 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7183 dy*(distance+offset)/distance);
7184 return(MagickTrue);
7185}
7186
7187static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7188 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7189{
7190#define MaxStrokePad (6*BezierQuantum+360)
7191#define CheckPathExtent(pad_p,pad_q) \
7192{ \
7193 if ((pad_p) > MaxBezierCoordinates) \
7194 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7195 else \
7196 if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7197 { \
7198 if (~extent_p < (pad_p)) \
7199 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7200 else \
7201 { \
7202 extent_p+=(pad_p); \
7203 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7204 MaxStrokePad,sizeof(*stroke_p)); \
7205 } \
7206 } \
7207 if ((pad_q) > MaxBezierCoordinates) \
7208 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7209 else \
7210 if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7211 { \
7212 if (~extent_q < (pad_q)) \
7213 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7214 else \
7215 { \
7216 extent_q+=(pad_q); \
7217 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7218 MaxStrokePad,sizeof(*stroke_q)); \
7219 } \
7220 } \
7221 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7222 { \
7223 if (stroke_p != (PointInfo *) NULL) \
7224 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7225 if (stroke_q != (PointInfo *) NULL) \
7226 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7227 polygon_primitive=(PrimitiveInfo *) \
7228 RelinquishMagickMemory(polygon_primitive); \
7229 (void) ThrowMagickException(exception,GetMagickModule(), \
7230 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7231 return((PrimitiveInfo *) NULL); \
7232 } \
7233}
7234
7235 typedef struct _StrokeSegment
7236 {
7237 double
7238 p,
7239 q;
7240 } StrokeSegment;
7241
7242 double
7243 delta_theta,
7244 dot_product,
7245 mid,
7246 miterlimit;
7247
7248 MagickBooleanType
7249 closed_path;
7250
7251 PointInfo
7252 box_p[5],
7253 box_q[5],
7254 center,
7255 offset,
7256 *stroke_p,
7257 *stroke_q;
7258
7259 PrimitiveInfo
7260 *polygon_primitive,
7261 *stroke_polygon;
7262
7263 ssize_t
7264 i;
7265
7266 size_t
7267 arc_segments,
7268 extent_p,
7269 extent_q,
7270 number_vertices;
7271
7272 ssize_t
7273 j,
7274 n,
7275 p,
7276 q;
7277
7278 StrokeSegment
7279 dx = {0.0, 0.0},
7280 dy = {0.0, 0.0},
7281 inverse_slope = {0.0, 0.0},
7282 slope = {0.0, 0.0},
7283 theta = {0.0, 0.0};
7284
7285 /*
7286 Allocate paths.
7287 */
7288 number_vertices=primitive_info->coordinates;
7289 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7290 number_vertices+2UL,sizeof(*polygon_primitive));
7291 if (polygon_primitive == (PrimitiveInfo *) NULL)
7292 {
7293 (void) ThrowMagickException(exception,GetMagickModule(),
7294 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7295 return((PrimitiveInfo *) NULL);
7296 }
7297 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7298 sizeof(*polygon_primitive));
7299 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7300 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7301 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7302 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7303 if ((draw_info->linejoin == MiterJoin) ||
7304 ((draw_info->linejoin == RoundJoin) && (closed_path != MagickFalse)))
7305 {
7306 polygon_primitive[number_vertices]=primitive_info[1];
7307 number_vertices++;
7308 }
7309 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7310 /*
7311 Compute the slope for the first line segment, p.
7312 */
7313 closed_path=primitive_info[0].closed_subpath;
7314 dx.p=0.0;
7315 dy.p=0.0;
7316 for (n=1; n < (ssize_t) number_vertices; n++)
7317 {
7318 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7319 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7320 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7321 break;
7322 }
7323 if (n == (ssize_t) number_vertices)
7324 {
7325 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7326 {
7327 /*
7328 Zero length subpath.
7329 */
7330 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7331 sizeof(*stroke_polygon));
7332 stroke_polygon[0]=polygon_primitive[0];
7333 stroke_polygon[0].coordinates=0;
7334 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7335 polygon_primitive);
7336 return(stroke_polygon);
7337 }
7338 n=(ssize_t) number_vertices-1L;
7339 }
7340 extent_p=2*number_vertices;
7341 extent_q=2*number_vertices;
7342 stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7343 sizeof(*stroke_p));
7344 stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7345 sizeof(*stroke_q));
7346 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7347 {
7348 if (stroke_p != (PointInfo *) NULL)
7349 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7350 if (stroke_q != (PointInfo *) NULL)
7351 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7352 polygon_primitive=(PrimitiveInfo *)
7353 RelinquishMagickMemory(polygon_primitive);
7354 (void) ThrowMagickException(exception,GetMagickModule(),
7355 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7356 return((PrimitiveInfo *) NULL);
7357 }
7358 slope.p=0.0;
7359 inverse_slope.p=0.0;
7360 if (fabs(dx.p) < MagickEpsilon)
7361 {
7362 if (dx.p >= 0.0)
7363 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7364 else
7365 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7366 }
7367 else
7368 if (fabs(dy.p) < MagickEpsilon)
7369 {
7370 if (dy.p >= 0.0)
7371 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7372 else
7373 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7374 }
7375 else
7376 {
7377 slope.p=dy.p/dx.p;
7378 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7379 }
7380 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7381 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7382 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7383 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7384 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7385 offset.y=(double) (offset.x*inverse_slope.p);
7386 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7387 {
7388 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7389 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7390 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7391 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7392 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7393 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7394 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7395 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7396 }
7397 else
7398 {
7399 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7400 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7401 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7402 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7403 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7404 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7405 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7406 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7407 }
7408 /*
7409 Create strokes for the line join attribute: bevel, miter, round.
7410 */
7411 p=0;
7412 q=0;
7413 stroke_q[p++]=box_q[0];
7414 stroke_p[q++]=box_p[0];
7415 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7416 {
7417 /*
7418 Compute the slope for this line segment, q.
7419 */
7420 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7421 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7422 dot_product=dx.q*dx.q+dy.q*dy.q;
7423 if (dot_product < 0.25)
7424 continue;
7425 slope.q=0.0;
7426 inverse_slope.q=0.0;
7427 if (fabs(dx.q) < MagickEpsilon)
7428 {
7429 if (dx.q >= 0.0)
7430 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7431 else
7432 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7433 }
7434 else
7435 if (fabs(dy.q) < MagickEpsilon)
7436 {
7437 if (dy.q >= 0.0)
7438 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7439 else
7440 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7441 }
7442 else
7443 {
7444 slope.q=dy.q/dx.q;
7445 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7446 }
7447 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7448 offset.y=(double) (offset.x*inverse_slope.q);
7449 dot_product=dy.q*offset.x-dx.q*offset.y;
7450 if (dot_product > 0.0)
7451 {
7452 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7453 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7454 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7455 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7456 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7457 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7458 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7459 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7460 }
7461 else
7462 {
7463 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7464 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7465 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7466 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7467 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7468 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7469 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7470 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7471 }
7472 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7473 {
7474 box_p[4]=box_p[1];
7475 box_q[4]=box_q[1];
7476 }
7477 else
7478 {
7479 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7480 box_p[3].y)/(slope.p-slope.q));
7481 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7482 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7483 box_q[3].y)/(slope.p-slope.q));
7484 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7485 }
7486 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7487 dot_product=dx.q*dy.p-dx.p*dy.q;
7488 if (dot_product <= 0.0)
7489 switch (draw_info->linejoin)
7490 {
7491 case BevelJoin:
7492 {
7493 stroke_q[q++]=box_q[1];
7494 stroke_q[q++]=box_q[2];
7495 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7496 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7497 if (dot_product <= miterlimit)
7498 stroke_p[p++]=box_p[4];
7499 else
7500 {
7501 stroke_p[p++]=box_p[1];
7502 stroke_p[p++]=box_p[2];
7503 }
7504 break;
7505 }
7506 case MiterJoin:
7507 {
7508 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7509 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7510 if (dot_product <= miterlimit)
7511 {
7512 stroke_q[q++]=box_q[4];
7513 stroke_p[p++]=box_p[4];
7514 }
7515 else
7516 {
7517 stroke_q[q++]=box_q[1];
7518 stroke_q[q++]=box_q[2];
7519 stroke_p[p++]=box_p[1];
7520 stroke_p[p++]=box_p[2];
7521 }
7522 break;
7523 }
7524 case RoundJoin:
7525 {
7526 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7527 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7528 if (dot_product <= miterlimit)
7529 stroke_p[p++]=box_p[4];
7530 else
7531 {
7532 stroke_p[p++]=box_p[1];
7533 stroke_p[p++]=box_p[2];
7534 }
7535 center=polygon_primitive[n].point;
7536 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7537 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7538 if (theta.q < theta.p)
7539 theta.q+=2.0*MagickPI;
7540 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.q-
7541 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7542 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7543 stroke_q[q].x=box_q[1].x;
7544 stroke_q[q].y=box_q[1].y;
7545 q++;
7546 for (j=1; j < (ssize_t) arc_segments; j++)
7547 {
7548 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7549 stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7550 (theta.p+delta_theta),DegreesToRadians(360.0))));
7551 stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7552 (theta.p+delta_theta),DegreesToRadians(360.0))));
7553 q++;
7554 }
7555 stroke_q[q++]=box_q[2];
7556 break;
7557 }
7558 default:
7559 break;
7560 }
7561 else
7562 switch (draw_info->linejoin)
7563 {
7564 case BevelJoin:
7565 {
7566 stroke_p[p++]=box_p[1];
7567 stroke_p[p++]=box_p[2];
7568 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7569 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7570 if (dot_product <= miterlimit)
7571 stroke_q[q++]=box_q[4];
7572 else
7573 {
7574 stroke_q[q++]=box_q[1];
7575 stroke_q[q++]=box_q[2];
7576 }
7577 break;
7578 }
7579 case MiterJoin:
7580 {
7581 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7582 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7583 if (dot_product <= miterlimit)
7584 {
7585 stroke_q[q++]=box_q[4];
7586 stroke_p[p++]=box_p[4];
7587 }
7588 else
7589 {
7590 stroke_q[q++]=box_q[1];
7591 stroke_q[q++]=box_q[2];
7592 stroke_p[p++]=box_p[1];
7593 stroke_p[p++]=box_p[2];
7594 }
7595 break;
7596 }
7597 case RoundJoin:
7598 {
7599 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7600 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7601 if (dot_product <= miterlimit)
7602 stroke_q[q++]=box_q[4];
7603 else
7604 {
7605 stroke_q[q++]=box_q[1];
7606 stroke_q[q++]=box_q[2];
7607 }
7608 center=polygon_primitive[n].point;
7609 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7610 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7611 if (theta.p < theta.q)
7612 theta.p+=2.0*MagickPI;
7613 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.p-
7614 theta.q)/(2.0*sqrt((double) (MagickSafeReciprocal(mid)))))));
7615 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7616 stroke_p[p++]=box_p[1];
7617 for (j=1; j < (ssize_t) arc_segments; j++)
7618 {
7619 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7620 stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7621 (theta.p+delta_theta),DegreesToRadians(360.0))));
7622 stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7623 (theta.p+delta_theta),DegreesToRadians(360.0))));
7624 p++;
7625 }
7626 stroke_p[p++]=box_p[2];
7627 break;
7628 }
7629 default:
7630 break;
7631 }
7632 slope.p=slope.q;
7633 inverse_slope.p=inverse_slope.q;
7634 box_p[0]=box_p[2];
7635 box_p[1]=box_p[3];
7636 box_q[0]=box_q[2];
7637 box_q[1]=box_q[3];
7638 dx.p=dx.q;
7639 dy.p=dy.q;
7640 n=i;
7641 }
7642 stroke_p[p++]=box_p[1];
7643 stroke_q[q++]=box_q[1];
7644 /*
7645 Trace stroked polygon.
7646 */
7647 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7648 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
7649 if (stroke_polygon == (PrimitiveInfo *) NULL)
7650 {
7651 (void) ThrowMagickException(exception,GetMagickModule(),
7652 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7653 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7654 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7655 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7656 polygon_primitive);
7657 return(stroke_polygon);
7658 }
7659 for (i=0; i < (ssize_t) p; i++)
7660 {
7661 stroke_polygon[i]=polygon_primitive[0];
7662 stroke_polygon[i].point=stroke_p[i];
7663 }
7664 if (closed_path != MagickFalse)
7665 {
7666 stroke_polygon[i]=polygon_primitive[0];
7667 stroke_polygon[i].point=stroke_polygon[0].point;
7668 i++;
7669 }
7670 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7671 {
7672 stroke_polygon[i]=polygon_primitive[0];
7673 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7674 }
7675 if (closed_path != MagickFalse)
7676 {
7677 stroke_polygon[i]=polygon_primitive[0];
7678 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7679 i++;
7680 }
7681 stroke_polygon[i]=polygon_primitive[0];
7682 stroke_polygon[i].point=stroke_polygon[0].point;
7683 i++;
7684 stroke_polygon[i].primitive=UndefinedPrimitive;
7685 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7686 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7687 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7688 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7689 return(stroke_polygon);
7690}