MagickCore 6.9.13-49
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 Leave old buffer intact; report failure.
2285 */
2286 ThrowMagickException(mvg_info->exception, GetMagickModule(),
2287 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2288 return(MagickFalse);
2289 }
2290 /*
2291 Commit updated buffer.
2292 */
2293 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2294 {
2295 primitive_info[i].primitive=UndefinedPrimitive;
2296 primitive_info[i].text=(char *) NULL;
2297 }
2298 *mvg_info->primitive_info=primitive_info;
2299 *mvg_info->extent=extent;
2300 return(MagickTrue);
2301}
2302
2303static inline double GetDrawValue(const char *magick_restrict string,
2304 char **magick_restrict sentinel)
2305{
2306 char
2307 **magick_restrict q;
2308
2309 double
2310 value;
2311
2312 q=sentinel;
2313 value=InterpretLocaleValue(string,q);
2314 sentinel=q;
2315 return(value);
2316}
2317
2318static int MVGMacroCompare(const void *target,const void *source)
2319{
2320 const char
2321 *p,
2322 *q;
2323
2324 p=(const char *) target;
2325 q=(const char *) source;
2326 return(strcmp(p,q));
2327}
2328
2329static SplayTreeInfo *GetMVGMacros(const char *primitive)
2330{
2331 char
2332 *macro,
2333 *token;
2334
2335 const char
2336 *q;
2337
2338 size_t
2339 extent;
2340
2341 SplayTreeInfo
2342 *macros;
2343
2344 /*
2345 Scan graphic primitives for definitions and classes.
2346 */
2347 if (primitive == (const char *) NULL)
2348 return((SplayTreeInfo *) NULL);
2349 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2350 RelinquishMagickMemory);
2351 macro=AcquireString(primitive);
2352 token=AcquireString(primitive);
2353 extent=strlen(token)+MagickPathExtent;
2354 for (q=primitive; *q != '\0'; )
2355 {
2356 if (GetNextToken(q,&q,extent,token) < 1)
2357 break;
2358 if (*token == '\0')
2359 break;
2360 if (LocaleCompare("push",token) == 0)
2361 {
2362 const char
2363 *end,
2364 *start;
2365
2366 (void) GetNextToken(q,&q,extent,token);
2367 if (*q == '"')
2368 {
2369 char
2370 name[MagickPathExtent];
2371
2372 const char
2373 *p;
2374
2375 ssize_t
2376 n;
2377
2378 /*
2379 Named macro (e.g. push graphic-context "wheel").
2380 */
2381 (void) GetNextToken(q,&q,extent,token);
2382 start=q;
2383 end=q;
2384 (void) CopyMagickString(name,token,MagickPathExtent);
2385 n=1;
2386 for (p=q; *p != '\0'; )
2387 {
2388 if (GetNextToken(p,&p,extent,token) < 1)
2389 break;
2390 if (*token == '\0')
2391 break;
2392 if (LocaleCompare(token,"pop") == 0)
2393 {
2394 end=p-strlen(token)-1;
2395 n--;
2396 }
2397 if (LocaleCompare(token,"push") == 0)
2398 n++;
2399 if ((n == 0) && (end >= start))
2400 {
2401 size_t
2402 length=(size_t) (end-start);
2403
2404 /*
2405 Extract macro.
2406 */
2407 (void) GetNextToken(p,&p,extent,token);
2408 if (length > 0)
2409 {
2410 (void) CopyMagickString(macro,start,length);
2411 (void) AddValueToSplayTree(macros,ConstantString(name),
2412 ConstantString(macro));
2413 }
2414 break;
2415 }
2416 }
2417 }
2418 }
2419 }
2420 token=DestroyString(token);
2421 macro=DestroyString(macro);
2422 return(macros);
2423}
2424
2425static inline MagickBooleanType IsPoint(const char *point)
2426{
2427 char
2428 *p;
2429
2430 double
2431 value;
2432
2433 value=GetDrawValue(point,&p);
2434 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2435 MagickTrue);
2436}
2437
2438static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2439 const PointInfo point)
2440{
2441 primitive_info->coordinates=1;
2442 primitive_info->closed_subpath=MagickFalse;
2443 primitive_info->point=point;
2444 return(MagickTrue);
2445}
2446
2447static MagickBooleanType RenderMVGContent(Image *image,
2448 const DrawInfo *draw_info,const size_t depth)
2449{
2450#define RenderImageTag "Render/Image"
2451
2452 AffineMatrix
2453 affine,
2454 current;
2455
2456 char
2457 key[2*MaxTextExtent],
2458 keyword[MaxTextExtent],
2459 geometry[MaxTextExtent],
2460 name[MaxTextExtent],
2461 *next_token,
2462 pattern[MaxTextExtent],
2463 *primitive,
2464 *token;
2465
2466 const char
2467 *p,
2468 *q;
2469
2470 double
2471 angle,
2472 coordinates,
2473 cursor,
2474 factor,
2475 primitive_extent;
2476
2477 DrawInfo
2478 *clone_info,
2479 **graphic_context;
2480
2481 MagickBooleanType
2482 proceed;
2483
2484 MagickStatusType
2485 status;
2486
2487 MVGInfo
2488 mvg_info;
2489
2490 PointInfo
2491 point;
2492
2493 PixelPacket
2494 start_color;
2495
2496 PrimitiveInfo
2497 *primitive_info;
2498
2499 PrimitiveType
2500 primitive_type;
2501
2502 SegmentInfo
2503 bounds;
2504
2505 size_t
2506 extent,
2507 number_points;
2508
2509 SplayTreeInfo
2510 *macros;
2511
2512 ssize_t
2513 classDepth = 0,
2514 defsDepth,
2515 i,
2516 j,
2517 k,
2518 n,
2519 symbolDepth,
2520 x;
2521
2522 TypeMetric
2523 metrics;
2524
2525 assert(image != (Image *) NULL);
2526 assert(image->signature == MagickCoreSignature);
2527 assert(draw_info != (DrawInfo *) NULL);
2528 assert(draw_info->signature == MagickCoreSignature);
2529 if (IsEventLogging() != MagickFalse)
2530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2531 if (depth > MagickMaxRecursionDepth)
2532 ThrowBinaryImageException(DrawError,"VectorGraphicsNestedTooDeeply",
2533 image->filename);
2534 if ((draw_info->primitive == (char *) NULL) ||
2535 (*draw_info->primitive == '\0'))
2536 return(MagickFalse);
2537 if (draw_info->debug != MagickFalse)
2538 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2539 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2540 return(MagickFalse);
2541 if (image->matte == MagickFalse)
2542 {
2543 status=SetImageAlphaChannel(image,OpaqueAlphaChannel);
2544 if (status == MagickFalse)
2545 return(MagickFalse);
2546 }
2547 primitive=(char *) NULL;
2548 if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2549 (*(draw_info->primitive+1) != '-') && (depth == 0))
2550 primitive=FileToString(draw_info->primitive,~0UL,&image->exception);
2551 else
2552 primitive=AcquireString(draw_info->primitive);
2553 if (primitive == (char *) NULL)
2554 return(MagickFalse);
2555 primitive_extent=(double) strlen(primitive);
2556 (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2557 n=0;
2558 /*
2559 Allocate primitive info memory.
2560 */
2561 graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2562 if (graphic_context == (DrawInfo **) NULL)
2563 {
2564 primitive=DestroyString(primitive);
2565 ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2566 image->filename);
2567 }
2568 number_points=(size_t) PrimitiveExtentPad;
2569 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
2570 (number_points+1),sizeof(*primitive_info));
2571 if (primitive_info == (PrimitiveInfo *) NULL)
2572 {
2573 primitive=DestroyString(primitive);
2574 for ( ; n >= 0; n--)
2575 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2576 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2577 ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2578 image->filename);
2579 }
2580 (void) memset(primitive_info,0,(size_t) (number_points+1)*
2581 sizeof(*primitive_info));
2582 (void) memset(&mvg_info,0,sizeof(mvg_info));
2583 mvg_info.primitive_info=(&primitive_info);
2584 mvg_info.extent=(&number_points);
2585 mvg_info.exception=(&image->exception);
2586 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2587 graphic_context[n]->viewbox=image->page;
2588 if ((image->page.width == 0) || (image->page.height == 0))
2589 {
2590 graphic_context[n]->viewbox.width=image->columns;
2591 graphic_context[n]->viewbox.height=image->rows;
2592 }
2593 token=AcquireString(primitive);
2594 extent=strlen(token)+MaxTextExtent;
2595 cursor=0.0;
2596 defsDepth=0;
2597 symbolDepth=0;
2598 macros=GetMVGMacros(primitive);
2599 status=QueryColorDatabase("#000000",&start_color,&image->exception);
2600 for (q=primitive; *q != '\0'; )
2601 {
2602 /*
2603 Interpret graphic primitive.
2604 */
2605 if (GetNextToken(q,&q,MaxTextExtent,keyword) < 1)
2606 break;
2607 if (*keyword == '\0')
2608 break;
2609 if (*keyword == '#')
2610 {
2611 /*
2612 Comment.
2613 */
2614 while ((*q != '\n') && (*q != '\0'))
2615 q++;
2616 continue;
2617 }
2618 p=q-strlen(keyword)-1;
2619 primitive_type=UndefinedPrimitive;
2620 current=graphic_context[n]->affine;
2621 GetAffineMatrix(&affine);
2622 *token='\0';
2623 switch (*keyword)
2624 {
2625 case ';':
2626 break;
2627 case 'a':
2628 case 'A':
2629 {
2630 if (LocaleCompare("affine",keyword) == 0)
2631 {
2632 (void) GetNextToken(q,&q,extent,token);
2633 affine.sx=GetDrawValue(token,&next_token);
2634 if (token == next_token)
2635 ThrowPointExpectedException(image,token);
2636 (void) GetNextToken(q,&q,extent,token);
2637 if (*token == ',')
2638 (void) GetNextToken(q,&q,extent,token);
2639 affine.ry=GetDrawValue(token,&next_token);
2640 if (token == next_token)
2641 ThrowPointExpectedException(image,token);
2642 (void) GetNextToken(q,&q,extent,token);
2643 if (*token == ',')
2644 (void) GetNextToken(q,&q,extent,token);
2645 affine.rx=GetDrawValue(token,&next_token);
2646 if (token == next_token)
2647 ThrowPointExpectedException(image,token);
2648 (void) GetNextToken(q,&q,extent,token);
2649 if (*token == ',')
2650 (void) GetNextToken(q,&q,extent,token);
2651 affine.sy=GetDrawValue(token,&next_token);
2652 if (token == next_token)
2653 ThrowPointExpectedException(image,token);
2654 (void) GetNextToken(q,&q,extent,token);
2655 if (*token == ',')
2656 (void) GetNextToken(q,&q,extent,token);
2657 affine.tx=GetDrawValue(token,&next_token);
2658 if (token == next_token)
2659 ThrowPointExpectedException(image,token);
2660 (void) GetNextToken(q,&q,extent,token);
2661 if (*token == ',')
2662 (void) GetNextToken(q,&q,extent,token);
2663 affine.ty=GetDrawValue(token,&next_token);
2664 if (token == next_token)
2665 ThrowPointExpectedException(image,token);
2666 break;
2667 }
2668 if (LocaleCompare("arc",keyword) == 0)
2669 {
2670 primitive_type=ArcPrimitive;
2671 break;
2672 }
2673 status=MagickFalse;
2674 break;
2675 }
2676 case 'b':
2677 case 'B':
2678 {
2679 if (LocaleCompare("bezier",keyword) == 0)
2680 {
2681 primitive_type=BezierPrimitive;
2682 break;
2683 }
2684 if (LocaleCompare("border-color",keyword) == 0)
2685 {
2686 (void) GetNextToken(q,&q,extent,token);
2687 status&=QueryColorDatabase(token,&graphic_context[n]->border_color,
2688 &image->exception);
2689 break;
2690 }
2691 status=MagickFalse;
2692 break;
2693 }
2694 case 'c':
2695 case 'C':
2696 {
2697 if (LocaleCompare("class",keyword) == 0)
2698 {
2699 const char
2700 *mvg_class;
2701
2702 (void) GetNextToken(q,&q,extent,token);
2703 if ((*token == '\0') || (*token == ';'))
2704 {
2705 status=MagickFalse;
2706 break;
2707 }
2708 /*
2709 Identify recursion.
2710 */
2711 for (i=0; i <= n; i++)
2712 if (LocaleCompare(token,graphic_context[i]->id) == 0)
2713 break;
2714 if (i <= n)
2715 break;
2716 if (classDepth++ > MagickMaxRecursionDepth)
2717 {
2718 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2719 DrawError,"VectorGraphicsNestedTooDeeply","`%s'",token);
2720 status=MagickFalse;
2721 break;
2722 }
2723 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2724 if ((graphic_context[n]->render != MagickFalse) &&
2725 (mvg_class != (const char *) NULL) && (p > primitive))
2726 {
2727 char
2728 *elements;
2729
2730 ssize_t
2731 offset;
2732
2733 /*
2734 Inject class elements in stream.
2735 */
2736 (void) CloneString(&graphic_context[n]->id,token);
2737 offset=(ssize_t) (p-primitive);
2738 elements=AcquireString(primitive);
2739 elements[offset]='\0';
2740 (void) ConcatenateString(&elements,mvg_class);
2741 (void) ConcatenateString(&elements,"\n");
2742 (void) ConcatenateString(&elements,q);
2743 primitive=DestroyString(primitive);
2744 primitive=elements;
2745 q=primitive+offset;
2746 }
2747 break;
2748 }
2749 if (LocaleCompare("clip-path",keyword) == 0)
2750 {
2751 const char
2752 *clip_path;
2753
2754 /*
2755 Take a node from within the MVG document, and duplicate it here.
2756 */
2757 (void) GetNextToken(q,&q,extent,token);
2758 if (*token == '\0')
2759 {
2760 status=MagickFalse;
2761 break;
2762 }
2763 (void) CloneString(&graphic_context[n]->clip_mask,token);
2764 clip_path=(const char *) GetValueFromSplayTree(macros,token);
2765 if (clip_path != (const char *) NULL)
2766 {
2767 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2768 graphic_context[n]->clipping_mask=
2769 DestroyImage(graphic_context[n]->clipping_mask);
2770 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2771 graphic_context[n],token,clip_path,&image->exception);
2772 if (graphic_context[n]->compliance != SVGCompliance)
2773 {
2774 const char
2775 *clip_path;
2776
2777 clip_path=(const char *) GetValueFromSplayTree(macros,
2778 graphic_context[n]->clip_mask);
2779 if (clip_path != (const char *) NULL)
2780 (void) SetImageArtifact(image,
2781 graphic_context[n]->clip_mask,clip_path);
2782 status&=DrawClipPath(image,graphic_context[n],
2783 graphic_context[n]->clip_mask);
2784 }
2785 }
2786 break;
2787 }
2788 if (LocaleCompare("clip-rule",keyword) == 0)
2789 {
2790 ssize_t
2791 fill_rule;
2792
2793 (void) GetNextToken(q,&q,extent,token);
2794 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2795 token);
2796 if (fill_rule == -1)
2797 {
2798 status=MagickFalse;
2799 break;
2800 }
2801 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2802 break;
2803 }
2804 if (LocaleCompare("clip-units",keyword) == 0)
2805 {
2806 ssize_t
2807 clip_units;
2808
2809 (void) GetNextToken(q,&q,extent,token);
2810 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2811 token);
2812 if (clip_units == -1)
2813 {
2814 status=MagickFalse;
2815 break;
2816 }
2817 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2818 if (clip_units == ObjectBoundingBox)
2819 {
2820 GetAffineMatrix(&current);
2821 affine.sx=draw_info->bounds.x2;
2822 affine.sy=draw_info->bounds.y2;
2823 affine.tx=draw_info->bounds.x1;
2824 affine.ty=draw_info->bounds.y1;
2825 break;
2826 }
2827 break;
2828 }
2829 if (LocaleCompare("circle",keyword) == 0)
2830 {
2831 primitive_type=CirclePrimitive;
2832 break;
2833 }
2834 if (LocaleCompare("color",keyword) == 0)
2835 {
2836 primitive_type=ColorPrimitive;
2837 break;
2838 }
2839 if (LocaleCompare("compliance",keyword) == 0)
2840 {
2841 /*
2842 MVG compliance associates a clipping mask with an image; SVG
2843 compliance associates a clipping mask with a graphics context.
2844 */
2845 (void) GetNextToken(q,&q,extent,token);
2846 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2847 MagickComplianceOptions,MagickFalse,token);
2848 break;
2849 }
2850 if (LocaleCompare("currentColor",keyword) == 0)
2851 {
2852 (void) GetNextToken(q,&q,extent,token);
2853 break;
2854 }
2855 status=MagickFalse;
2856 break;
2857 }
2858 case 'd':
2859 case 'D':
2860 {
2861 if (LocaleCompare("decorate",keyword) == 0)
2862 {
2863 ssize_t
2864 decorate;
2865
2866 (void) GetNextToken(q,&q,extent,token);
2867 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2868 token);
2869 if (decorate == -1)
2870 {
2871 status=MagickFalse;
2872 break;
2873 }
2874 graphic_context[n]->decorate=(DecorationType) decorate;
2875 break;
2876 }
2877 if (LocaleCompare("density",keyword) == 0)
2878 {
2879 (void) GetNextToken(q,&q,extent,token);
2880 (void) CloneString(&graphic_context[n]->density,token);
2881 break;
2882 }
2883 if (LocaleCompare("direction",keyword) == 0)
2884 {
2885 ssize_t
2886 direction;
2887
2888 (void) GetNextToken(q,&q,extent,token);
2889 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2890 token);
2891 if (direction == -1)
2892 status=MagickFalse;
2893 else
2894 graphic_context[n]->direction=(DirectionType) direction;
2895 break;
2896 }
2897 status=MagickFalse;
2898 break;
2899 }
2900 case 'e':
2901 case 'E':
2902 {
2903 if (LocaleCompare("ellipse",keyword) == 0)
2904 {
2905 primitive_type=EllipsePrimitive;
2906 break;
2907 }
2908 if (LocaleCompare("encoding",keyword) == 0)
2909 {
2910 (void) GetNextToken(q,&q,extent,token);
2911 (void) CloneString(&graphic_context[n]->encoding,token);
2912 break;
2913 }
2914 status=MagickFalse;
2915 break;
2916 }
2917 case 'f':
2918 case 'F':
2919 {
2920 if (LocaleCompare("fill",keyword) == 0)
2921 {
2922 const char
2923 *mvg_class;
2924
2925 (void) GetNextToken(q,&q,extent,token);
2926 if (graphic_context[n]->clip_path != MagickFalse)
2927 break;
2928 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2929 if (mvg_class != (const char *) NULL)
2930 {
2931 (void) DrawPatternPath(image,draw_info,mvg_class,
2932 &graphic_context[n]->fill_pattern);
2933 break;
2934 }
2935 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2936 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2937 {
2938 (void) DrawPatternPath(image,draw_info,token,
2939 &graphic_context[n]->fill_pattern);
2940 break;
2941 }
2942 status&=QueryColorDatabase(token,&graphic_context[n]->fill,
2943 &image->exception);
2944 if (graphic_context[n]->fill_opacity != (double) OpaqueOpacity)
2945 graphic_context[n]->fill.opacity=ClampToQuantum(
2946 graphic_context[n]->fill_opacity);
2947 break;
2948 }
2949 if (LocaleCompare("fill-opacity",keyword) == 0)
2950 {
2951 double
2952 opacity;
2953
2954 (void) GetNextToken(q,&q,extent,token);
2955 if (graphic_context[n]->clip_path != MagickFalse)
2956 break;
2957 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2958 opacity=MagickMin(MagickMax(factor*
2959 GetDrawValue(token,&next_token),0.0),1.0);
2960 if (token == next_token)
2961 ThrowPointExpectedException(image,token);
2962 if (graphic_context[n]->compliance == SVGCompliance)
2963 graphic_context[n]->fill_opacity*=(1.0-opacity);
2964 else
2965 graphic_context[n]->fill_opacity=((MagickRealType) QuantumRange-
2966 graphic_context[n]->fill_opacity)*(1.0-opacity);
2967 if (graphic_context[n]->fill.opacity != TransparentOpacity)
2968 graphic_context[n]->fill.opacity=(Quantum)
2969 graphic_context[n]->fill_opacity;
2970 else
2971 graphic_context[n]->fill.opacity=ClampToQuantum((MagickRealType)
2972 QuantumRange*(1.0-opacity));
2973 break;
2974 }
2975 if (LocaleCompare("fill-rule",keyword) == 0)
2976 {
2977 ssize_t
2978 fill_rule;
2979
2980 (void) GetNextToken(q,&q,extent,token);
2981 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2982 token);
2983 if (fill_rule == -1)
2984 {
2985 status=MagickFalse;
2986 break;
2987 }
2988 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2989 break;
2990 }
2991 if (LocaleCompare("font",keyword) == 0)
2992 {
2993 (void) GetNextToken(q,&q,extent,token);
2994 (void) CloneString(&graphic_context[n]->font,token);
2995 if (LocaleCompare("none",token) == 0)
2996 graphic_context[n]->font=(char *) RelinquishMagickMemory(
2997 graphic_context[n]->font);
2998 break;
2999 }
3000 if (LocaleCompare("font-family",keyword) == 0)
3001 {
3002 (void) GetNextToken(q,&q,extent,token);
3003 (void) CloneString(&graphic_context[n]->family,token);
3004 break;
3005 }
3006 if (LocaleCompare("font-size",keyword) == 0)
3007 {
3008 (void) GetNextToken(q,&q,extent,token);
3009 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3010 if (token == next_token)
3011 ThrowPointExpectedException(image,token);
3012 break;
3013 }
3014 if (LocaleCompare("font-stretch",keyword) == 0)
3015 {
3016 ssize_t
3017 stretch;
3018
3019 (void) GetNextToken(q,&q,extent,token);
3020 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3021 if (stretch == -1)
3022 {
3023 status=MagickFalse;
3024 break;
3025 }
3026 graphic_context[n]->stretch=(StretchType) stretch;
3027 break;
3028 }
3029 if (LocaleCompare("font-style",keyword) == 0)
3030 {
3031 ssize_t
3032 style;
3033
3034 (void) GetNextToken(q,&q,extent,token);
3035 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3036 if (style == -1)
3037 {
3038 status=MagickFalse;
3039 break;
3040 }
3041 graphic_context[n]->style=(StyleType) style;
3042 break;
3043 }
3044 if (LocaleCompare("font-weight",keyword) == 0)
3045 {
3046 ssize_t
3047 weight;
3048
3049 (void) GetNextToken(q,&q,extent,token);
3050 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3051 if (weight == -1)
3052 weight=(ssize_t) StringToUnsignedLong(token);
3053 graphic_context[n]->weight=(size_t) weight;
3054 break;
3055 }
3056 status=MagickFalse;
3057 break;
3058 }
3059 case 'g':
3060 case 'G':
3061 {
3062 if (LocaleCompare("gradient-units",keyword) == 0)
3063 {
3064 (void) GetNextToken(q,&q,extent,token);
3065 break;
3066 }
3067 if (LocaleCompare("gravity",keyword) == 0)
3068 {
3069 ssize_t
3070 gravity;
3071
3072 (void) GetNextToken(q,&q,extent,token);
3073 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3074 if (gravity == -1)
3075 {
3076 status=MagickFalse;
3077 break;
3078 }
3079 graphic_context[n]->gravity=(GravityType) gravity;
3080 break;
3081 }
3082 status=MagickFalse;
3083 break;
3084 }
3085 case 'i':
3086 case 'I':
3087 {
3088 if (LocaleCompare("image",keyword) == 0)
3089 {
3090 ssize_t
3091 compose;
3092
3093 primitive_type=ImagePrimitive;
3094 (void) GetNextToken(q,&q,extent,token);
3095 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3096 if (compose == -1)
3097 {
3098 status=MagickFalse;
3099 break;
3100 }
3101 graphic_context[n]->compose=(CompositeOperator) compose;
3102 break;
3103 }
3104 if (LocaleCompare("interline-spacing",keyword) == 0)
3105 {
3106 (void) GetNextToken(q,&q,extent,token);
3107 graphic_context[n]->interline_spacing=GetDrawValue(token,
3108 &next_token);
3109 if (token == next_token)
3110 ThrowPointExpectedException(image,token);
3111 break;
3112 }
3113 if (LocaleCompare("interword-spacing",keyword) == 0)
3114 {
3115 (void) GetNextToken(q,&q,extent,token);
3116 graphic_context[n]->interword_spacing=GetDrawValue(token,
3117 &next_token);
3118 if (token == next_token)
3119 ThrowPointExpectedException(image,token);
3120 break;
3121 }
3122 status=MagickFalse;
3123 break;
3124 }
3125 case 'k':
3126 case 'K':
3127 {
3128 if (LocaleCompare("kerning",keyword) == 0)
3129 {
3130 (void) GetNextToken(q,&q,extent,token);
3131 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3132 if (token == next_token)
3133 ThrowPointExpectedException(image,token);
3134 break;
3135 }
3136 status=MagickFalse;
3137 break;
3138 }
3139 case 'l':
3140 case 'L':
3141 {
3142 if (LocaleCompare("letter-spacing",keyword) == 0)
3143 {
3144 (void) GetNextToken(q,&q,extent,token);
3145 if (IsPoint(token) == MagickFalse)
3146 break;
3147 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3148 clone_info->text=AcquireString(" ");
3149 status&=GetTypeMetrics(image,clone_info,&metrics);
3150 graphic_context[n]->kerning=metrics.width*
3151 GetDrawValue(token,&next_token);
3152 clone_info=DestroyDrawInfo(clone_info);
3153 if (token == next_token)
3154 ThrowPointExpectedException(image,token);
3155 break;
3156 }
3157 if (LocaleCompare("line",keyword) == 0)
3158 {
3159 primitive_type=LinePrimitive;
3160 break;
3161 }
3162 status=MagickFalse;
3163 break;
3164 }
3165 case 'm':
3166 case 'M':
3167 {
3168 if (LocaleCompare("mask",keyword) == 0)
3169 {
3170 const char
3171 *mask_path;
3172
3173 /*
3174 Take a node from within the MVG document, and duplicate it here.
3175 */
3176 (void) GetNextToken(q,&q,extent,token);
3177 mask_path=(const char *) GetValueFromSplayTree(macros,token);
3178 if (mask_path != (const char *) NULL)
3179 {
3180 if (graphic_context[n]->composite_mask != (Image *) NULL)
3181 graphic_context[n]->composite_mask=
3182 DestroyImage(graphic_context[n]->composite_mask);
3183 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3184 graphic_context[n],token,mask_path,&image->exception);
3185 if (graphic_context[n]->compliance != SVGCompliance)
3186 status=SetImageMask(image,graphic_context[n]->composite_mask);
3187 }
3188 break;
3189 }
3190 if (LocaleCompare("matte",keyword) == 0)
3191 {
3192 primitive_type=MattePrimitive;
3193 break;
3194 }
3195 status=MagickFalse;
3196 break;
3197 }
3198 case 'o':
3199 case 'O':
3200 {
3201 if (LocaleCompare("offset",keyword) == 0)
3202 {
3203 (void) GetNextToken(q,&q,extent,token);
3204 break;
3205 }
3206 if (LocaleCompare("opacity",keyword) == 0)
3207 {
3208 double
3209 opacity;
3210
3211 (void) GetNextToken(q,&q,extent,token);
3212 if (graphic_context[n]->clip_path != MagickFalse)
3213 break;
3214 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3215 opacity=1.0-MagickMin(MagickMax(factor*
3216 GetDrawValue(token,&next_token),0.0),1.0);
3217 if (token == next_token)
3218 ThrowPointExpectedException(image,token);
3219 if (graphic_context[n]->compliance == SVGCompliance)
3220 {
3221 graphic_context[n]->fill_opacity*=opacity;
3222 graphic_context[n]->stroke_opacity*=opacity;
3223 }
3224 else
3225 {
3226 graphic_context[n]->fill_opacity=(double) QuantumRange*opacity;
3227 graphic_context[n]->stroke_opacity=(double) QuantumRange*
3228 opacity;
3229 }
3230 if (graphic_context[n]->fill.opacity != (double) TransparentOpacity)
3231 {
3232 graphic_context[n]->fill.opacity=
3233 graphic_context[n]->fill_opacity;
3234 graphic_context[n]->stroke.opacity=
3235 graphic_context[n]->stroke_opacity;
3236 }
3237 else
3238 {
3239 graphic_context[n]->fill.opacity=(MagickRealType)
3240 ClampToQuantum((double) QuantumRange*opacity);
3241 graphic_context[n]->stroke.opacity=(MagickRealType)
3242 ClampToQuantum((double) QuantumRange*opacity);
3243 }
3244 break;
3245 }
3246 status=MagickFalse;
3247 break;
3248 }
3249 case 'p':
3250 case 'P':
3251 {
3252 if (LocaleCompare("path",keyword) == 0)
3253 {
3254 primitive_type=PathPrimitive;
3255 break;
3256 }
3257 if (LocaleCompare("point",keyword) == 0)
3258 {
3259 primitive_type=PointPrimitive;
3260 break;
3261 }
3262 if (LocaleCompare("polyline",keyword) == 0)
3263 {
3264 primitive_type=PolylinePrimitive;
3265 break;
3266 }
3267 if (LocaleCompare("polygon",keyword) == 0)
3268 {
3269 primitive_type=PolygonPrimitive;
3270 break;
3271 }
3272 if (LocaleCompare("pop",keyword) == 0)
3273 {
3274 if (GetNextToken(q,&q,extent,token) < 1)
3275 break;
3276 if (LocaleCompare("class",token) == 0)
3277 break;
3278 if (LocaleCompare("clip-path",token) == 0)
3279 break;
3280 if (LocaleCompare("defs",token) == 0)
3281 {
3282 defsDepth--;
3283 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3284 MagickTrue;
3285 break;
3286 }
3287 if (LocaleCompare("gradient",token) == 0)
3288 break;
3289 if (LocaleCompare("graphic-context",token) == 0)
3290 {
3291 if (n <= 0)
3292 {
3293 (void) ThrowMagickException(&image->exception,
3294 GetMagickModule(),DrawError,
3295 "UnbalancedGraphicContextPushPop","`%s'",token);
3296 status=MagickFalse;
3297 n=0;
3298 break;
3299 }
3300 if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3301 (graphic_context[n]->compliance != SVGCompliance))
3302 if (LocaleCompare(graphic_context[n]->clip_mask,
3303 graphic_context[n-1]->clip_mask) != 0)
3304 status=SetImageClipMask(image,(Image *) NULL);
3305 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3306 n--;
3307 break;
3308 }
3309 if (LocaleCompare("mask",token) == 0)
3310 break;
3311 if (LocaleCompare("pattern",token) == 0)
3312 break;
3313 if (LocaleCompare("symbol",token) == 0)
3314 {
3315 symbolDepth--;
3316 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3317 MagickTrue;
3318 break;
3319 }
3320 status=MagickFalse;
3321 break;
3322 }
3323 if (LocaleCompare("push",keyword) == 0)
3324 {
3325 if (GetNextToken(q,&q,extent,token) < 1)
3326 break;
3327 if (LocaleCompare("class",token) == 0)
3328 {
3329 /*
3330 Class context.
3331 */
3332 for (p=q; *q != '\0'; )
3333 {
3334 if (GetNextToken(q,&q,extent,token) < 1)
3335 break;
3336 if (LocaleCompare(token,"pop") != 0)
3337 continue;
3338 (void) GetNextToken(q,(const char **) NULL,extent,token);
3339 if (LocaleCompare(token,"class") != 0)
3340 continue;
3341 break;
3342 }
3343 (void) GetNextToken(q,&q,extent,token);
3344 break;
3345 }
3346 if (LocaleCompare("clip-path",token) == 0)
3347 {
3348 (void) GetNextToken(q,&q,extent,token);
3349 for (p=q; *q != '\0'; )
3350 {
3351 if (GetNextToken(q,&q,extent,token) < 1)
3352 break;
3353 if (LocaleCompare(token,"pop") != 0)
3354 continue;
3355 (void) GetNextToken(q,(const char **) NULL,extent,token);
3356 if (LocaleCompare(token,"clip-path") != 0)
3357 continue;
3358 break;
3359 }
3360 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3361 {
3362 status=MagickFalse;
3363 break;
3364 }
3365 (void) GetNextToken(q,&q,extent,token);
3366 break;
3367 }
3368 if (LocaleCompare("defs",token) == 0)
3369 {
3370 defsDepth++;
3371 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3372 MagickTrue;
3373 break;
3374 }
3375 if (LocaleCompare("gradient",token) == 0)
3376 {
3377 char
3378 key[2*MaxTextExtent],
3379 name[MaxTextExtent],
3380 type[MaxTextExtent];
3381
3382 SegmentInfo
3383 segment;
3384
3385 (void) GetNextToken(q,&q,extent,token);
3386 (void) CopyMagickString(name,token,MaxTextExtent);
3387 (void) GetNextToken(q,&q,extent,token);
3388 (void) CopyMagickString(type,token,MaxTextExtent);
3389 (void) GetNextToken(q,&q,extent,token);
3390 segment.x1=GetDrawValue(token,&next_token);
3391 if (token == next_token)
3392 ThrowPointExpectedException(image,token);
3393 (void) GetNextToken(q,&q,extent,token);
3394 if (*token == ',')
3395 (void) GetNextToken(q,&q,extent,token);
3396 segment.y1=GetDrawValue(token,&next_token);
3397 if (token == next_token)
3398 ThrowPointExpectedException(image,token);
3399 (void) GetNextToken(q,&q,extent,token);
3400 if (*token == ',')
3401 (void) GetNextToken(q,&q,extent,token);
3402 segment.x2=GetDrawValue(token,&next_token);
3403 if (token == next_token)
3404 ThrowPointExpectedException(image,token);
3405 (void) GetNextToken(q,&q,extent,token);
3406 if (*token == ',')
3407 (void) GetNextToken(q,&q,extent,token);
3408 segment.y2=GetDrawValue(token,&next_token);
3409 if (token == next_token)
3410 ThrowPointExpectedException(image,token);
3411 if (LocaleCompare(type,"radial") == 0)
3412 {
3413 (void) GetNextToken(q,&q,extent,token);
3414 if (*token == ',')
3415 (void) GetNextToken(q,&q,extent,token);
3416 }
3417 for (p=q; *q != '\0'; )
3418 {
3419 if (GetNextToken(q,&q,extent,token) < 1)
3420 break;
3421 if (LocaleCompare(token,"pop") != 0)
3422 continue;
3423 (void) GetNextToken(q,(const char **) NULL,extent,token);
3424 if (LocaleCompare(token,"gradient") != 0)
3425 continue;
3426 break;
3427 }
3428 if ((q == (char *) NULL) || (*q == '\0') ||
3429 (p == (char *) NULL) || ((q-4) < p) ||
3430 ((q-p+4+1) > extent))
3431 {
3432 status=MagickFalse;
3433 break;
3434 }
3435 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3436 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3437 graphic_context[n]->affine.ry*segment.y1+
3438 graphic_context[n]->affine.tx;
3439 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3440 graphic_context[n]->affine.sy*segment.y1+
3441 graphic_context[n]->affine.ty;
3442 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3443 graphic_context[n]->affine.ry*segment.y2+
3444 graphic_context[n]->affine.tx;
3445 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3446 graphic_context[n]->affine.sy*segment.y2+
3447 graphic_context[n]->affine.ty;
3448 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3449 (void) SetImageArtifact(image,key,token);
3450 (void) FormatLocaleString(key,MaxTextExtent,"%s-type",name);
3451 (void) SetImageArtifact(image,key,type);
3452 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3453 (void) FormatLocaleString(geometry,MaxTextExtent,
3454 "%gx%g%+.15g%+.15g",
3455 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3456 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3457 bounds.x1,bounds.y1);
3458 (void) SetImageArtifact(image,key,geometry);
3459 (void) GetNextToken(q,&q,extent,token);
3460 break;
3461 }
3462 if (LocaleCompare("graphic-context",token) == 0)
3463 {
3464 n++;
3465 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3466 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3467 if (graphic_context == (DrawInfo **) NULL)
3468 {
3469 (void) ThrowMagickException(&image->exception,
3470 GetMagickModule(),ResourceLimitError,
3471 "MemoryAllocationFailed","`%s'",image->filename);
3472 status=MagickFalse;
3473 break;
3474 }
3475 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3476 graphic_context[n-1]);
3477 if (*q == '"')
3478 {
3479 (void) GetNextToken(q,&q,extent,token);
3480 (void) CloneString(&graphic_context[n]->id,token);
3481 }
3482 if (n > MagickMaxRecursionDepth)
3483 {
3484 (void) ThrowMagickException(&image->exception,
3485 GetMagickModule(),DrawError,
3486 "VectorGraphicsNestedTooDeeply","`%s'",image->filename);
3487 status=MagickFalse;
3488 }
3489 break;
3490 }
3491 if (LocaleCompare("mask",token) == 0)
3492 {
3493 (void) GetNextToken(q,&q,extent,token);
3494 break;
3495 }
3496 if (LocaleCompare("pattern",token) == 0)
3497 {
3498 RectangleInfo
3499 bounds;
3500
3501 (void) GetNextToken(q,&q,extent,token);
3502 (void) CopyMagickString(name,token,MaxTextExtent);
3503 (void) GetNextToken(q,&q,extent,token);
3504 bounds.x=CastDoubleToLong(ceil(GetDrawValue(token,
3505 &next_token)-0.5));
3506 if (token == next_token)
3507 ThrowPointExpectedException(image,token);
3508 (void) GetNextToken(q,&q,extent,token);
3509 if (*token == ',')
3510 (void) GetNextToken(q,&q,extent,token);
3511 bounds.y=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.width=CastDoubleToUnsigned(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.height=CastDoubleToUnsigned(GetDrawValue(token,
3526 &next_token)+0.5);
3527 if (token == next_token)
3528 ThrowPointExpectedException(image,token);
3529 for (p=q; *q != '\0'; )
3530 {
3531 if (GetNextToken(q,&q,extent,token) < 1)
3532 break;
3533 if (LocaleCompare(token,"pop") != 0)
3534 continue;
3535 (void) GetNextToken(q,(const char **) NULL,extent,token);
3536 if (LocaleCompare(token,"pattern") != 0)
3537 continue;
3538 break;
3539 }
3540 if ((q == (char *) NULL) || (p == (char *) NULL) ||
3541 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3542 {
3543 status=MagickFalse;
3544 break;
3545 }
3546 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3547 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3548 (void) SetImageArtifact(image,key,token);
3549 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3550 (void) FormatLocaleString(geometry,MaxTextExtent,
3551 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3552 bounds.height,(double) bounds.x,(double) bounds.y);
3553 (void) SetImageArtifact(image,key,geometry);
3554 (void) GetNextToken(q,&q,extent,token);
3555 break;
3556 }
3557 if (LocaleCompare("symbol",token) == 0)
3558 {
3559 symbolDepth++;
3560 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3561 MagickTrue;
3562 break;
3563 }
3564 status=MagickFalse;
3565 break;
3566 }
3567 status=MagickFalse;
3568 break;
3569 }
3570 case 'r':
3571 case 'R':
3572 {
3573 if (LocaleCompare("rectangle",keyword) == 0)
3574 {
3575 primitive_type=RectanglePrimitive;
3576 break;
3577 }
3578 if (LocaleCompare("rotate",keyword) == 0)
3579 {
3580 (void) GetNextToken(q,&q,extent,token);
3581 angle=GetDrawValue(token,&next_token);
3582 if (token == next_token)
3583 ThrowPointExpectedException(image,token);
3584 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3585 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3586 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3587 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3588 break;
3589 }
3590 if (LocaleCompare("roundRectangle",keyword) == 0)
3591 {
3592 primitive_type=RoundRectanglePrimitive;
3593 break;
3594 }
3595 status=MagickFalse;
3596 break;
3597 }
3598 case 's':
3599 case 'S':
3600 {
3601 if (LocaleCompare("scale",keyword) == 0)
3602 {
3603 (void) GetNextToken(q,&q,extent,token);
3604 affine.sx=GetDrawValue(token,&next_token);
3605 if (token == next_token)
3606 ThrowPointExpectedException(image,token);
3607 (void) GetNextToken(q,&q,extent,token);
3608 if (*token == ',')
3609 (void) GetNextToken(q,&q,extent,token);
3610 affine.sy=GetDrawValue(token,&next_token);
3611 if (token == next_token)
3612 ThrowPointExpectedException(image,token);
3613 break;
3614 }
3615 if (LocaleCompare("skewX",keyword) == 0)
3616 {
3617 (void) GetNextToken(q,&q,extent,token);
3618 angle=GetDrawValue(token,&next_token);
3619 if (token == next_token)
3620 ThrowPointExpectedException(image,token);
3621 affine.ry=sin(DegreesToRadians(angle));
3622 break;
3623 }
3624 if (LocaleCompare("skewY",keyword) == 0)
3625 {
3626 (void) GetNextToken(q,&q,extent,token);
3627 angle=GetDrawValue(token,&next_token);
3628 if (token == next_token)
3629 ThrowPointExpectedException(image,token);
3630 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3631 break;
3632 }
3633 if (LocaleCompare("stop-color",keyword) == 0)
3634 {
3635 GradientType
3636 type;
3637
3638 PixelPacket
3639 stop_color;
3640
3641 (void) GetNextToken(q,&q,extent,token);
3642 status&=QueryColorDatabase(token,&stop_color,&image->exception);
3643 type=LinearGradient;
3644 if (draw_info->gradient.type == RadialGradient)
3645 type=RadialGradient;
3646 (void) GradientImage(image,type,PadSpread,&start_color,&stop_color);
3647 start_color=stop_color;
3648 (void) GetNextToken(q,&q,extent,token);
3649 break;
3650 }
3651 if (LocaleCompare("stroke",keyword) == 0)
3652 {
3653 const char
3654 *mvg_class;
3655
3656 (void) GetNextToken(q,&q,extent,token);
3657 if (graphic_context[n]->clip_path != MagickFalse)
3658 break;
3659 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3660 if (mvg_class != (const char *) NULL)
3661 {
3662 (void) DrawPatternPath(image,draw_info,mvg_class,
3663 &graphic_context[n]->stroke_pattern);
3664 break;
3665 }
3666 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
3667 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3668 {
3669 (void) DrawPatternPath(image,draw_info,token,
3670 &graphic_context[n]->stroke_pattern);
3671 break;
3672 }
3673 status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
3674 &image->exception);
3675 if (graphic_context[n]->stroke_opacity != (MagickRealType) OpaqueOpacity)
3676 graphic_context[n]->stroke.opacity=ClampToQuantum(
3677 graphic_context[n]->stroke_opacity);
3678 break;
3679 }
3680 if (LocaleCompare("stroke-antialias",keyword) == 0)
3681 {
3682 (void) GetNextToken(q,&q,extent,token);
3683 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3684 MagickTrue : MagickFalse;
3685 break;
3686 }
3687 if (LocaleCompare("stroke-dasharray",keyword) == 0)
3688 {
3689 if (graphic_context[n]->dash_pattern != (double *) NULL)
3690 graphic_context[n]->dash_pattern=(double *)
3691 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3692 if (IsPoint(q) != MagickFalse)
3693 {
3694 const char
3695 *p;
3696
3697 p=q;
3698 (void) GetNextToken(p,&p,extent,token);
3699 if (*token == ',')
3700 (void) GetNextToken(p,&p,extent,token);
3701 for (x=0; IsPoint(token) != MagickFalse; x++)
3702 {
3703 (void) GetNextToken(p,&p,extent,token);
3704 if (*token == ',')
3705 (void) GetNextToken(p,&p,extent,token);
3706 }
3707 graphic_context[n]->dash_pattern=(double *)
3708 AcquireQuantumMemory((size_t) (2*x+2),
3709 sizeof(*graphic_context[n]->dash_pattern));
3710 if (graphic_context[n]->dash_pattern == (double *) NULL)
3711 {
3712 (void) ThrowMagickException(&image->exception,
3713 GetMagickModule(),ResourceLimitError,
3714 "MemoryAllocationFailed","`%s'",image->filename);
3715 status=MagickFalse;
3716 break;
3717 }
3718 (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3719 (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3720 for (j=0; j < x; j++)
3721 {
3722 (void) GetNextToken(q,&q,extent,token);
3723 if (*token == ',')
3724 (void) GetNextToken(q,&q,extent,token);
3725 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3726 &next_token);
3727 if (token == next_token)
3728 ThrowPointExpectedException(image,token);
3729 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3730 status=MagickFalse;
3731 }
3732 if ((x & 0x01) != 0)
3733 for ( ; j < (2*x); j++)
3734 graphic_context[n]->dash_pattern[j]=
3735 graphic_context[n]->dash_pattern[j-x];
3736 graphic_context[n]->dash_pattern[j]=0.0;
3737 break;
3738 }
3739 (void) GetNextToken(q,&q,extent,token);
3740 break;
3741 }
3742 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3743 {
3744 (void) GetNextToken(q,&q,extent,token);
3745 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3746 if (token == next_token)
3747 ThrowPointExpectedException(image,token);
3748 break;
3749 }
3750 if (LocaleCompare("stroke-linecap",keyword) == 0)
3751 {
3752 ssize_t
3753 linecap;
3754
3755 (void) GetNextToken(q,&q,extent,token);
3756 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3757 if (linecap == -1)
3758 {
3759 status=MagickFalse;
3760 break;
3761 }
3762 graphic_context[n]->linecap=(LineCap) linecap;
3763 break;
3764 }
3765 if (LocaleCompare("stroke-linejoin",keyword) == 0)
3766 {
3767 ssize_t
3768 linejoin;
3769
3770 (void) GetNextToken(q,&q,extent,token);
3771 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3772 token);
3773 if (linejoin == -1)
3774 {
3775 status=MagickFalse;
3776 break;
3777 }
3778 graphic_context[n]->linejoin=(LineJoin) linejoin;
3779 break;
3780 }
3781 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3782 {
3783 (void) GetNextToken(q,&q,extent,token);
3784 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3785 break;
3786 }
3787 if (LocaleCompare("stroke-opacity",keyword) == 0)
3788 {
3789 double
3790 opacity;
3791
3792 (void) GetNextToken(q,&q,extent,token);
3793 if (graphic_context[n]->clip_path != MagickFalse)
3794 break;
3795 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3796 opacity=MagickMin(MagickMax(factor*
3797 GetDrawValue(token,&next_token),0.0),1.0);
3798 if (token == next_token)
3799 ThrowPointExpectedException(image,token);
3800 if (graphic_context[n]->compliance == SVGCompliance)
3801 graphic_context[n]->stroke_opacity*=(1.0-opacity);
3802 else
3803 graphic_context[n]->stroke_opacity=((MagickRealType) QuantumRange-
3804 graphic_context[n]->stroke_opacity)*(1.0-opacity);
3805 if (graphic_context[n]->stroke.opacity != TransparentOpacity)
3806 graphic_context[n]->stroke.opacity=(Quantum)
3807 graphic_context[n]->stroke_opacity;
3808 else
3809 graphic_context[n]->stroke.opacity=ClampToQuantum(
3810 (MagickRealType) QuantumRange*opacity);
3811 break;
3812 }
3813 if (LocaleCompare("stroke-width",keyword) == 0)
3814 {
3815 (void) GetNextToken(q,&q,extent,token);
3816 if (graphic_context[n]->clip_path != MagickFalse)
3817 break;
3818 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3819 if ((token == next_token) ||
3820 (graphic_context[n]->stroke_width < 0.0))
3821 ThrowPointExpectedException(image,token);
3822 break;
3823 }
3824 status=MagickFalse;
3825 break;
3826 }
3827 case 't':
3828 case 'T':
3829 {
3830 if (LocaleCompare("text",keyword) == 0)
3831 {
3832 primitive_type=TextPrimitive;
3833 cursor=0.0;
3834 break;
3835 }
3836 if (LocaleCompare("text-align",keyword) == 0)
3837 {
3838 ssize_t
3839 align;
3840
3841 (void) GetNextToken(q,&q,extent,token);
3842 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3843 if (align == -1)
3844 {
3845 status=MagickFalse;
3846 break;
3847 }
3848 graphic_context[n]->align=(AlignType) align;
3849 break;
3850 }
3851 if (LocaleCompare("text-anchor",keyword) == 0)
3852 {
3853 ssize_t
3854 align;
3855
3856 (void) GetNextToken(q,&q,extent,token);
3857 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3858 if (align == -1)
3859 {
3860 status=MagickFalse;
3861 break;
3862 }
3863 graphic_context[n]->align=(AlignType) align;
3864 break;
3865 }
3866 if (LocaleCompare("text-antialias",keyword) == 0)
3867 {
3868 (void) GetNextToken(q,&q,extent,token);
3869 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3870 MagickTrue : MagickFalse;
3871 break;
3872 }
3873 if (LocaleCompare("text-undercolor",keyword) == 0)
3874 {
3875 (void) GetNextToken(q,&q,extent,token);
3876 status&=QueryColorDatabase(token,&graphic_context[n]->undercolor,
3877 &image->exception);
3878 break;
3879 }
3880 if (LocaleCompare("translate",keyword) == 0)
3881 {
3882 (void) GetNextToken(q,&q,extent,token);
3883 affine.tx=GetDrawValue(token,&next_token);
3884 if (token == next_token)
3885 ThrowPointExpectedException(image,token);
3886 (void) GetNextToken(q,&q,extent,token);
3887 if (*token == ',')
3888 (void) GetNextToken(q,&q,extent,token);
3889 affine.ty=GetDrawValue(token,&next_token);
3890 if (token == next_token)
3891 ThrowPointExpectedException(image,token);
3892 break;
3893 }
3894 status=MagickFalse;
3895 break;
3896 }
3897 case 'u':
3898 case 'U':
3899 {
3900 if (LocaleCompare("use",keyword) == 0)
3901 {
3902 const char
3903 *use;
3904
3905 /*
3906 Get a macro from the MVG document, and "use" it here.
3907 */
3908 (void) GetNextToken(q,&q,extent,token);
3909 use=(const char *) GetValueFromSplayTree(macros,token);
3910 if (use != (const char *) NULL)
3911 {
3912 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3913 (void) CloneString(&clone_info->primitive,use);
3914 status=RenderMVGContent(image,clone_info,depth+1);
3915 clone_info=DestroyDrawInfo(clone_info);
3916 }
3917 break;
3918 }
3919 status=MagickFalse;
3920 break;
3921 }
3922 case 'v':
3923 case 'V':
3924 {
3925 if (LocaleCompare("viewbox",keyword) == 0)
3926 {
3927 (void) GetNextToken(q,&q,extent,token);
3928 graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3929 GetDrawValue(token,&next_token)-0.5));
3930 if (token == next_token)
3931 ThrowPointExpectedException(image,token);
3932 (void) GetNextToken(q,&q,extent,token);
3933 if (*token == ',')
3934 (void) GetNextToken(q,&q,extent,token);
3935 graphic_context[n]->viewbox.y=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.width=CastDoubleToUnsigned(
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.height=CastDoubleToUnsigned(
3950 GetDrawValue(token,&next_token)+0.5);
3951 if (token == next_token)
3952 ThrowPointExpectedException(image,token);
3953 break;
3954 }
3955 status=MagickFalse;
3956 break;
3957 }
3958 case 'w':
3959 case 'W':
3960 {
3961 if (LocaleCompare("word-spacing",keyword) == 0)
3962 {
3963 (void) GetNextToken(q,&q,extent,token);
3964 graphic_context[n]->interword_spacing=GetDrawValue(token,
3965 &next_token);
3966 if (token == next_token)
3967 ThrowPointExpectedException(image,token);
3968 break;
3969 }
3970 status=MagickFalse;
3971 break;
3972 }
3973 default:
3974 {
3975 status=MagickFalse;
3976 break;
3977 }
3978 }
3979 if (status == MagickFalse)
3980 break;
3981 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3982 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3983 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3984 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3985 {
3986 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3987 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3988 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3989 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3990 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3991 current.tx;
3992 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3993 current.ty;
3994 }
3995 if (primitive_type == UndefinedPrimitive)
3996 {
3997 if ((draw_info->debug != MagickFalse) && (q > p))
3998 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3999 (q-p-1),p);
4000 continue;
4001 }
4002 /*
4003 Parse the primitive attributes.
4004 */
4005 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4006 if (primitive_info[i].text != (char *) NULL)
4007 primitive_info[i].text=DestroyString(primitive_info[i].text);
4008 i=0;
4009 mvg_info.offset=i;
4010 j=0;
4011 primitive_info[0].primitive=primitive_type;
4012 primitive_info[0].point.x=0.0;
4013 primitive_info[0].point.y=0.0;
4014 primitive_info[0].coordinates=0;
4015 primitive_info[0].method=FloodfillMethod;
4016 primitive_info[0].closed_subpath=MagickFalse;
4017 for (x=0; *q != '\0'; x++)
4018 {
4019 /*
4020 Define points.
4021 */
4022 if (IsPoint(q) == MagickFalse)
4023 break;
4024 (void) GetNextToken(q,&q,extent,token);
4025 point.x=GetDrawValue(token,&next_token);
4026 if (token == next_token)
4027 ThrowPointExpectedException(image,token);
4028 (void) GetNextToken(q,&q,extent,token);
4029 if (*token == ',')
4030 (void) GetNextToken(q,&q,extent,token);
4031 point.y=GetDrawValue(token,&next_token);
4032 if (token == next_token)
4033 ThrowPointExpectedException(image,token);
4034 (void) GetNextToken(q,(const char **) NULL,extent,token);
4035 if (*token == ',')
4036 (void) GetNextToken(q,&q,extent,token);
4037 primitive_info[i].primitive=primitive_type;
4038 primitive_info[i].point=point;
4039 primitive_info[i].coordinates=0;
4040 primitive_info[i].method=FloodfillMethod;
4041 primitive_info[i].closed_subpath=MagickFalse;
4042 i++;
4043 mvg_info.offset=i;
4044 if (i < (ssize_t) number_points)
4045 continue;
4046 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4047 primitive_info=(*mvg_info.primitive_info);
4048 }
4049 if (status == MagickFalse)
4050 break;
4051 if (primitive_info[j].text != (char *) NULL)
4052 primitive_info[j].text=DestroyString(primitive_info[j].text);
4053 primitive_info[j].primitive=primitive_type;
4054 primitive_info[j].coordinates=(size_t) x;
4055 primitive_info[j].method=FloodfillMethod;
4056 primitive_info[j].closed_subpath=MagickFalse;
4057 /*
4058 Circumscribe primitive within a circle.
4059 */
4060 bounds.x1=primitive_info[j].point.x;
4061 bounds.y1=primitive_info[j].point.y;
4062 bounds.x2=primitive_info[j].point.x;
4063 bounds.y2=primitive_info[j].point.y;
4064 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4065 {
4066 point=primitive_info[j+k].point;
4067 if (point.x < bounds.x1)
4068 bounds.x1=point.x;
4069 if (point.y < bounds.y1)
4070 bounds.y1=point.y;
4071 if (point.x > bounds.x2)
4072 bounds.x2=point.x;
4073 if (point.y > bounds.y2)
4074 bounds.y2=point.y;
4075 }
4076 /*
4077 Speculate how many points our primitive might consume.
4078 */
4079 coordinates=(double) primitive_info[j].coordinates;
4080 switch (primitive_type)
4081 {
4082 case RectanglePrimitive:
4083 {
4084 coordinates*=5.0;
4085 break;
4086 }
4087 case RoundRectanglePrimitive:
4088 {
4089 double
4090 alpha,
4091 beta,
4092 radius;
4093
4094 alpha=bounds.x2-bounds.x1;
4095 beta=bounds.y2-bounds.y1;
4096 radius=hypot(alpha,beta);
4097 coordinates*=5.0;
4098 coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4099 BezierQuantum+360.0;
4100 break;
4101 }
4102 case BezierPrimitive:
4103 {
4104 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4105 break;
4106 }
4107 case PathPrimitive:
4108 {
4109 char
4110 *s,
4111 *t;
4112
4113 (void) GetNextToken(q,&q,extent,token);
4114 coordinates=1.0;
4115 t=token;
4116 for (s=token; *s != '\0'; s=t)
4117 {
4118 double
4119 value;
4120
4121 value=GetDrawValue(s,&t);
4122 (void) value;
4123 if (s == t)
4124 {
4125 t++;
4126 continue;
4127 }
4128 coordinates++;
4129 }
4130 for (s=token; *s != '\0'; s++)
4131 if (strspn(s,"AaCcQqSsTt") != 0)
4132 coordinates+=(20.0*BezierQuantum)+360.0;
4133 break;
4134 }
4135 default:
4136 break;
4137 }
4138 if (status == MagickFalse)
4139 break;
4140 if (((size_t) (i+coordinates)) >= number_points)
4141 {
4142 /*
4143 Resize based on speculative points required by primitive.
4144 */
4145 number_points+=coordinates+1;
4146 if (number_points < (size_t) coordinates)
4147 {
4148 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4149 ResourceLimitError,"MemoryAllocationFailed","`%s'",
4150 image->filename);
4151 status=MagickFalse;
4152 break;
4153 }
4154 mvg_info.offset=i;
4155 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4156 primitive_info=(*mvg_info.primitive_info);
4157 }
4158 status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4159 primitive_info=(*mvg_info.primitive_info);
4160 if (status == MagickFalse)
4161 break;
4162 mvg_info.offset=j;
4163 switch (primitive_type)
4164 {
4165 case PointPrimitive:
4166 default:
4167 {
4168 if (primitive_info[j].coordinates != 1)
4169 {
4170 status=MagickFalse;
4171 break;
4172 }
4173 status&=TracePoint(primitive_info+j,primitive_info[j].point);
4174 primitive_info=(*mvg_info.primitive_info);
4175 i=(ssize_t) (j+primitive_info[j].coordinates);
4176 break;
4177 }
4178 case LinePrimitive:
4179 {
4180 if (primitive_info[j].coordinates != 2)
4181 {
4182 status=MagickFalse;
4183 break;
4184 }
4185 status&=TraceLine(primitive_info+j,primitive_info[j].point,
4186 primitive_info[j+1].point);
4187 primitive_info=(*mvg_info.primitive_info);
4188 i=(ssize_t) (j+primitive_info[j].coordinates);
4189 break;
4190 }
4191 case RectanglePrimitive:
4192 {
4193 if (primitive_info[j].coordinates != 2)
4194 {
4195 status=MagickFalse;
4196 break;
4197 }
4198 status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4199 primitive_info[j+1].point);
4200 primitive_info=(*mvg_info.primitive_info);
4201 i=(ssize_t) (j+primitive_info[j].coordinates);
4202 break;
4203 }
4204 case RoundRectanglePrimitive:
4205 {
4206 if (primitive_info[j].coordinates != 3)
4207 {
4208 status=MagickFalse;
4209 break;
4210 }
4211 if ((primitive_info[j+2].point.x < 0.0) ||
4212 (primitive_info[j+2].point.y < 0.0))
4213 {
4214 status=MagickFalse;
4215 break;
4216 }
4217 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4218 {
4219 status=MagickFalse;
4220 break;
4221 }
4222 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4223 {
4224 status=MagickFalse;
4225 break;
4226 }
4227 status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4228 primitive_info[j+1].point,primitive_info[j+2].point);
4229 primitive_info=(*mvg_info.primitive_info);
4230 i=(ssize_t) (j+primitive_info[j].coordinates);
4231 break;
4232 }
4233 case ArcPrimitive:
4234 {
4235 if (primitive_info[j].coordinates != 3)
4236 {
4237 status=MagickFalse;
4238 break;
4239 }
4240 status&=TraceArc(&mvg_info,primitive_info[j].point,
4241 primitive_info[j+1].point,primitive_info[j+2].point);
4242 primitive_info=(*mvg_info.primitive_info);
4243 i=(ssize_t) (j+primitive_info[j].coordinates);
4244 break;
4245 }
4246 case EllipsePrimitive:
4247 {
4248 if (primitive_info[j].coordinates != 3)
4249 {
4250 status=MagickFalse;
4251 break;
4252 }
4253 if ((primitive_info[j+1].point.x < 0.0) ||
4254 (primitive_info[j+1].point.y < 0.0))
4255 {
4256 status=MagickFalse;
4257 break;
4258 }
4259 status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4260 primitive_info[j+1].point,primitive_info[j+2].point);
4261 primitive_info=(*mvg_info.primitive_info);
4262 i=(ssize_t) (j+primitive_info[j].coordinates);
4263 break;
4264 }
4265 case CirclePrimitive:
4266 {
4267 if (primitive_info[j].coordinates != 2)
4268 {
4269 status=MagickFalse;
4270 break;
4271 }
4272 status&=TraceCircle(&mvg_info,primitive_info[j].point,
4273 primitive_info[j+1].point);
4274 primitive_info=(*mvg_info.primitive_info);
4275 i=(ssize_t) (j+primitive_info[j].coordinates);
4276 break;
4277 }
4278 case PolylinePrimitive:
4279 {
4280 if (primitive_info[j].coordinates < 1)
4281 {
4282 status=MagickFalse;
4283 break;
4284 }
4285 break;
4286 }
4287 case PolygonPrimitive:
4288 {
4289 if (primitive_info[j].coordinates < 3)
4290 {
4291 status=MagickFalse;
4292 break;
4293 }
4294 primitive_info[i]=primitive_info[j];
4295 primitive_info[i].coordinates=0;
4296 primitive_info[j].coordinates++;
4297 primitive_info[j].closed_subpath=MagickTrue;
4298 i++;
4299 break;
4300 }
4301 case BezierPrimitive:
4302 {
4303 if (primitive_info[j].coordinates < 3)
4304 {
4305 status=MagickFalse;
4306 break;
4307 }
4308 status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4309 primitive_info=(*mvg_info.primitive_info);
4310 i=(ssize_t) (j+primitive_info[j].coordinates);
4311 break;
4312 }
4313 case PathPrimitive:
4314 {
4315 coordinates=(double) TracePath(image,&mvg_info,token);
4316 primitive_info=(*mvg_info.primitive_info);
4317 if (coordinates < 0.0)
4318 {
4319 status=MagickFalse;
4320 break;
4321 }
4322 i=(ssize_t) (j+coordinates);
4323 break;
4324 }
4325 case ColorPrimitive:
4326 case MattePrimitive:
4327 {
4328 ssize_t
4329 method;
4330
4331 if (primitive_info[j].coordinates != 1)
4332 {
4333 status=MagickFalse;
4334 break;
4335 }
4336 (void) GetNextToken(q,&q,extent,token);
4337 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4338 if (method == -1)
4339 {
4340 status=MagickFalse;
4341 break;
4342 }
4343 primitive_info[j].method=(PaintMethod) method;
4344 break;
4345 }
4346 case TextPrimitive:
4347 {
4348 char
4349 geometry[MagickPathExtent];
4350
4351 if (primitive_info[j].coordinates != 1)
4352 {
4353 status=MagickFalse;
4354 break;
4355 }
4356 if (*token != ',')
4357 (void) GetNextToken(q,&q,extent,token);
4358 (void) CloneString(&primitive_info[j].text,token);
4359 /*
4360 Compute text cursor offset.
4361 */
4362 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4363 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4364 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4365 {
4366 mvg_info.point=primitive_info->point;
4367 primitive_info->point.x+=cursor;
4368 }
4369 else
4370 {
4371 mvg_info.point=primitive_info->point;
4372 cursor=0.0;
4373 }
4374 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4375 primitive_info->point.x,primitive_info->point.y);
4376 clone_info->render=MagickFalse;
4377 clone_info->text=AcquireString(token);
4378 status&=GetTypeMetrics(image,clone_info,&metrics);
4379 clone_info=DestroyDrawInfo(clone_info);
4380 cursor+=metrics.width;
4381 if (graphic_context[n]->compliance != SVGCompliance)
4382 cursor=0.0;
4383 break;
4384 }
4385 case ImagePrimitive:
4386 {
4387 if (primitive_info[j].coordinates != 2)
4388 {
4389 status=MagickFalse;
4390 break;
4391 }
4392 (void) GetNextToken(q,&q,extent,token);
4393 (void) CloneString(&primitive_info[j].text,token);
4394 break;
4395 }
4396 }
4397 mvg_info.offset=i;
4398 if (status == 0)
4399 break;
4400 primitive_info[i].primitive=UndefinedPrimitive;
4401 if ((draw_info->debug != MagickFalse) && (q > p))
4402 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
4403 /*
4404 Sanity check.
4405 */
4406 status&=CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4407 &graphic_context[n]->affine));
4408 primitive_info=(*mvg_info.primitive_info);
4409 if (status == 0)
4410 break;
4411 status&=CheckPrimitiveExtent(&mvg_info,(double)
4412 graphic_context[n]->stroke_width);
4413 primitive_info=(*mvg_info.primitive_info);
4414 if (status == 0)
4415 break;
4416 if (i == 0)
4417 continue;
4418 /*
4419 Transform points.
4420 */
4421 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4422 {
4423 point=primitive_info[i].point;
4424 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4425 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4426 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4427 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4428 point=primitive_info[i].point;
4429 if (point.x < graphic_context[n]->bounds.x1)
4430 graphic_context[n]->bounds.x1=point.x;
4431 if (point.y < graphic_context[n]->bounds.y1)
4432 graphic_context[n]->bounds.y1=point.y;
4433 if (point.x > graphic_context[n]->bounds.x2)
4434 graphic_context[n]->bounds.x2=point.x;
4435 if (point.y > graphic_context[n]->bounds.y2)
4436 graphic_context[n]->bounds.y2=point.y;
4437 if (primitive_info[i].primitive == ImagePrimitive)
4438 break;
4439 if (i >= (ssize_t) number_points)
4440 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4441 }
4442 if (graphic_context[n]->render != MagickFalse)
4443 {
4444 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4445 (graphic_context[n]->clip_mask != (char *) NULL) &&
4446 (LocaleCompare(graphic_context[n]->clip_mask,
4447 graphic_context[n-1]->clip_mask) != 0))
4448 {
4449 const char
4450 *clip_path;
4451
4452 clip_path=(const char *) GetValueFromSplayTree(macros,
4453 graphic_context[n]->clip_mask);
4454 if (clip_path != (const char *) NULL)
4455 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4456 clip_path);
4457 status&=DrawClipPath(image,graphic_context[n],
4458 graphic_context[n]->clip_mask);
4459 }
4460 status&=DrawPrimitive(image,graphic_context[n],primitive_info);
4461 }
4462 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4463 primitive_extent);
4464 if (proceed == MagickFalse)
4465 break;
4466 if (status == 0)
4467 break;
4468 }
4469 if (draw_info->debug != MagickFalse)
4470 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4471 /*
4472 Relinquish resources.
4473 */
4474 macros=DestroySplayTree(macros);
4475 token=DestroyString(token);
4476 if (primitive_info != (PrimitiveInfo *) NULL)
4477 {
4478 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4479 if (primitive_info[i].text != (char *) NULL)
4480 primitive_info[i].text=DestroyString(primitive_info[i].text);
4481 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4482 }
4483 primitive=DestroyString(primitive);
4484 for ( ; n >= 0; n--)
4485 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4486 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4487 if (status == MagickFalse)
4488 ThrowBinaryImageException(DrawError,
4489 "NonconformingDrawingPrimitiveDefinition",keyword);
4490 return(status != 0 ? MagickTrue : MagickFalse);
4491}
4492
4493MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
4494{
4495 return(RenderMVGContent(image,draw_info,0));
4496}
4497
4498/*
4499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4500% %
4501% %
4502% %
4503% D r a w P a t t e r n P a t h %
4504% %
4505% %
4506% %
4507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4508%
4509% DrawPatternPath() draws a pattern.
4510%
4511% The format of the DrawPatternPath method is:
4512%
4513% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4514% const char *name,Image **pattern)
4515%
4516% A description of each parameter follows:
4517%
4518% o image: the image.
4519%
4520% o draw_info: the draw info.
4521%
4522% o name: the pattern name.
4523%
4524% o image: the image.
4525%
4526*/
4527MagickExport MagickBooleanType DrawPatternPath(Image *image,
4528 const DrawInfo *draw_info,const char *name,Image **pattern)
4529{
4530 char
4531 property[MaxTextExtent];
4532
4533 const char
4534 *geometry,
4535 *path,
4536 *type;
4537
4538 DrawInfo
4539 *clone_info;
4540
4541 ImageInfo
4542 *image_info;
4543
4544 MagickBooleanType
4545 status;
4546
4547 assert(image != (Image *) NULL);
4548 assert(image->signature == MagickCoreSignature);
4549 assert(draw_info != (const DrawInfo *) NULL);
4550 if (IsEventLogging() != MagickFalse)
4551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4552 assert(name != (const char *) NULL);
4553 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
4554 path=GetImageArtifact(image,property);
4555 if (path == (const char *) NULL)
4556 return(MagickFalse);
4557 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
4558 geometry=GetImageArtifact(image,property);
4559 if (geometry == (const char *) NULL)
4560 return(MagickFalse);
4561 if ((*pattern) != (Image *) NULL)
4562 *pattern=DestroyImage(*pattern);
4563 image_info=AcquireImageInfo();
4564 image_info->size=AcquireString(geometry);
4565 *pattern=AcquireImage(image_info);
4566 image_info=DestroyImageInfo(image_info);
4567 (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
4568 &image->exception);
4569 (void) SetImageBackgroundColor(*pattern);
4570 if (draw_info->debug != MagickFalse)
4571 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4572 "begin pattern-path %s %s",name,geometry);
4573 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4574 if (clone_info->fill_pattern != (Image *) NULL)
4575 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4576 if (clone_info->stroke_pattern != (Image *) NULL)
4577 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4578 (void) FormatLocaleString(property,MaxTextExtent,"%s-type",name);
4579 type=GetImageArtifact(image,property);
4580 if (type != (const char *) NULL)
4581 clone_info->gradient.type=(GradientType) ParseCommandOption(
4582 MagickGradientOptions,MagickFalse,type);
4583 (void) CloneString(&clone_info->primitive,path);
4584 status=RenderMVGContent(*pattern,clone_info,0);
4585 clone_info=DestroyDrawInfo(clone_info);
4586 if (draw_info->debug != MagickFalse)
4587 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4588 return(status);
4589}
4590
4591/*
4592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4593% %
4594% %
4595% %
4596+ D r a w P o l y g o n P r i m i t i v e %
4597% %
4598% %
4599% %
4600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4601%
4602% DrawPolygonPrimitive() draws a polygon on the image.
4603%
4604% The format of the DrawPolygonPrimitive method is:
4605%
4606% MagickBooleanType DrawPolygonPrimitive(Image *image,
4607% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4608%
4609% A description of each parameter follows:
4610%
4611% o image: the image.
4612%
4613% o draw_info: the draw info.
4614%
4615% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4616%
4617*/
4618
4619static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4620{
4621 ssize_t
4622 i;
4623
4624 assert(polygon_info != (PolygonInfo **) NULL);
4625 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4626 if (polygon_info[i] != (PolygonInfo *) NULL)
4627 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4628 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4629 return(polygon_info);
4630}
4631
4632static PolygonInfo **AcquirePolygonTLS(const DrawInfo *draw_info,
4633 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4634{
4635 PathInfo
4636 *magick_restrict path_info;
4637
4638 PolygonInfo
4639 **polygon_info;
4640
4641 size_t
4642 number_threads;
4643
4644 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4645 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4646 sizeof(*polygon_info));
4647 if (polygon_info == (PolygonInfo **) NULL)
4648 {
4649 (void) ThrowMagickException(exception,GetMagickModule(),
4650 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4651 return((PolygonInfo **) NULL);
4652 }
4653 (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4654 path_info=ConvertPrimitiveToPath(draw_info,primitive_info,exception);
4655 if (path_info == (PathInfo *) NULL)
4656 return(DestroyPolygonTLS(polygon_info));
4657 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4658 if (polygon_info[0] == (PolygonInfo *) NULL)
4659 {
4660 (void) ThrowMagickException(exception,GetMagickModule(),
4661 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4662 return(DestroyPolygonTLS(polygon_info));
4663 }
4664 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4665 return(polygon_info);
4666}
4667
4668static MagickBooleanType AcquirePolygonEdgesTLS(PolygonInfo **polygon_info,
4669 const size_t number_threads,ExceptionInfo *exception)
4670{
4671 ssize_t
4672 i;
4673
4674 for (i=1; i < (ssize_t) number_threads; i++)
4675 {
4676 EdgeInfo
4677 *edge_info;
4678
4679 ssize_t
4680 j;
4681
4682 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4683 sizeof(*polygon_info[i]));
4684 if (polygon_info[i] == (PolygonInfo *) NULL)
4685 {
4686 (void) ThrowMagickException(exception,GetMagickModule(),
4687 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4688 return(MagickFalse);
4689 }
4690 polygon_info[i]->number_edges=0;
4691 edge_info=polygon_info[0]->edges;
4692 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4693 polygon_info[0]->number_edges,sizeof(*edge_info));
4694 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4695 {
4696 (void) ThrowMagickException(exception,GetMagickModule(),
4697 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4698 return(MagickFalse);
4699 }
4700 (void) memcpy(polygon_info[i]->edges,edge_info,
4701 polygon_info[0]->number_edges*sizeof(*edge_info));
4702 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4703 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4704 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4705 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4706 {
4707 edge_info=polygon_info[0]->edges+j;
4708 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4709 edge_info->number_points,sizeof(*edge_info));
4710 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4711 {
4712 (void) ThrowMagickException(exception,GetMagickModule(),
4713 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4714 return(MagickFalse);
4715 }
4716 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4717 edge_info->number_points*sizeof(*edge_info->points));
4718 }
4719 }
4720 return(MagickTrue);
4721}
4722
4723static size_t DestroyEdge(PolygonInfo *polygon_info,const ssize_t edge)
4724{
4725 assert(edge < (ssize_t) polygon_info->number_edges);
4726 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4727 polygon_info->edges[edge].points);
4728 polygon_info->number_edges--;
4729 if (edge < (ssize_t) polygon_info->number_edges)
4730 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4731 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
4732 return(polygon_info->number_edges);
4733}
4734
4735static double GetOpacityPixel(PolygonInfo *polygon_info,const double mid,
4736 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4737 const ssize_t y,double *stroke_opacity)
4738{
4739 double
4740 alpha,
4741 beta,
4742 distance,
4743 subpath_opacity;
4744
4745 PointInfo
4746 delta;
4747
4748 EdgeInfo
4749 *p;
4750
4751 const PointInfo
4752 *q;
4753
4754 ssize_t
4755 i;
4756
4757 ssize_t
4758 j,
4759 winding_number;
4760
4761 /*
4762 Compute fill & stroke opacity for this (x,y) point.
4763 */
4764 *stroke_opacity=0.0;
4765 subpath_opacity=0.0;
4766 p=polygon_info->edges;
4767 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4768 {
4769 if ((double) y <= (p->bounds.y1-mid-0.5))
4770 break;
4771 if ((double) y > (p->bounds.y2+mid+0.5))
4772 {
4773 p--;
4774 (void) DestroyEdge(polygon_info,j--);
4775 continue;
4776 }
4777 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4778 ((double) x > (p->bounds.x2+mid+0.5)))
4779 continue;
4780 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4781 for ( ; i < (ssize_t) p->number_points; i++)
4782 {
4783 if ((double) y <= (p->points[i-1].y-mid-0.5))
4784 break;
4785 if ((double) y > (p->points[i].y+mid+0.5))
4786 continue;
4787 if (p->scanline != (double) y)
4788 {
4789 p->scanline=(double) y;
4790 p->highwater=(size_t) i;
4791 }
4792 /*
4793 Compute distance between a point and an edge.
4794 */
4795 q=p->points+i-1;
4796 delta.x=(q+1)->x-q->x;
4797 delta.y=(q+1)->y-q->y;
4798 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4799 if (beta <= 0.0)
4800 {
4801 delta.x=(double) x-q->x;
4802 delta.y=(double) y-q->y;
4803 distance=delta.x*delta.x+delta.y*delta.y;
4804 }
4805 else
4806 {
4807 alpha=delta.x*delta.x+delta.y*delta.y;
4808 if (beta >= alpha)
4809 {
4810 delta.x=(double) x-(q+1)->x;
4811 delta.y=(double) y-(q+1)->y;
4812 distance=delta.x*delta.x+delta.y*delta.y;
4813 }
4814 else
4815 {
4816 alpha=MagickSafeReciprocal(alpha);
4817 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4818 distance=alpha*beta*beta;
4819 }
4820 }
4821 /*
4822 Compute stroke & subpath opacity.
4823 */
4824 beta=0.0;
4825 if (p->ghostline == MagickFalse)
4826 {
4827 alpha=mid+0.5;
4828 if ((*stroke_opacity < 1.0) &&
4829 (distance <= ((alpha+0.25)*(alpha+0.25))))
4830 {
4831 alpha=mid-0.5;
4832 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4833 *stroke_opacity=1.0;
4834 else
4835 {
4836 beta=1.0;
4837 if (fabs(distance-1.0) >= MagickEpsilon)
4838 beta=sqrt((double) distance);
4839 alpha=beta-mid-0.5;
4840 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
4841 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
4842 }
4843 }
4844 }
4845 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
4846 continue;
4847 if (distance <= 0.0)
4848 {
4849 subpath_opacity=1.0;
4850 continue;
4851 }
4852 if (distance > 1.0)
4853 continue;
4854 if (fabs(beta) < MagickEpsilon)
4855 {
4856 beta=1.0;
4857 if (fabs(distance-1.0) >= MagickEpsilon)
4858 beta=sqrt(distance);
4859 }
4860 alpha=beta-1.0;
4861 if (subpath_opacity < (alpha*alpha))
4862 subpath_opacity=alpha*alpha;
4863 }
4864 }
4865 /*
4866 Compute fill opacity.
4867 */
4868 if (fill == MagickFalse)
4869 return(0.0);
4870 if (subpath_opacity >= 1.0)
4871 return(1.0);
4872 /*
4873 Determine winding number.
4874 */
4875 winding_number=0;
4876 p=polygon_info->edges;
4877 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4878 {
4879 if ((double) y <= p->bounds.y1)
4880 break;
4881 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4882 continue;
4883 if ((double) x > p->bounds.x2)
4884 {
4885 winding_number+=p->direction != 0 ? 1 : -1;
4886 continue;
4887 }
4888 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4889 for ( ; i < (ssize_t) (p->number_points-1); i++)
4890 if ((double) y <= p->points[i].y)
4891 break;
4892 q=p->points+i-1;
4893 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4894 winding_number+=p->direction != 0 ? 1 : -1;
4895 }
4896 if (fill_rule != NonZeroRule)
4897 {
4898 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4899 return(1.0);
4900 }
4901 else
4902 if (MagickAbsoluteValue(winding_number) != 0)
4903 return(1.0);
4904 return(subpath_opacity);
4905}
4906
4907static MagickBooleanType DrawPolygonPrimitive(Image *image,
4908 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4909{
4910 typedef struct _ExtentInfo
4911 {
4912 ssize_t
4913 x1,
4914 y1,
4915 x2,
4916 y2;
4917 } ExtentInfo;
4918
4919 CacheView
4920 *image_view;
4921
4922 const char
4923 *artifact;
4924
4925 double
4926 mid;
4927
4928 ExceptionInfo
4929 *exception;
4930
4931 ExtentInfo
4932 poly_extent;
4933
4934 MagickBooleanType
4935 fill,
4936 status;
4937
4938 PolygonInfo
4939 **magick_restrict polygon_info;
4940
4941 EdgeInfo
4942 *p;
4943
4944 SegmentInfo
4945 bounds;
4946
4947 size_t
4948 number_threads = 1;
4949
4950 ssize_t
4951 i,
4952 y;
4953
4954 assert(image != (Image *) NULL);
4955 assert(image->signature == MagickCoreSignature);
4956 assert(draw_info != (DrawInfo *) NULL);
4957 assert(draw_info->signature == MagickCoreSignature);
4958 assert(primitive_info != (PrimitiveInfo *) NULL);
4959 if (IsEventLogging() != MagickFalse)
4960 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4961 if (primitive_info->coordinates <= 1)
4962 return(MagickTrue);
4963 /*
4964 Compute bounding box.
4965 */
4966 polygon_info=AcquirePolygonTLS(draw_info,primitive_info,&image->exception);
4967 if (polygon_info == (PolygonInfo **) NULL)
4968 return(MagickFalse);
4969 if (draw_info->debug != MagickFalse)
4970 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4971 fill=(primitive_info->method == FillToBorderMethod) ||
4972 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4973 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4974 bounds=polygon_info[0]->edges[0].bounds;
4975 artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
4976 if (IsStringTrue(artifact) != MagickFalse)
4977 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0]);
4978 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4979 {
4980 p=polygon_info[0]->edges+i;
4981 if (p->bounds.x1 < bounds.x1)
4982 bounds.x1=p->bounds.x1;
4983 if (p->bounds.y1 < bounds.y1)
4984 bounds.y1=p->bounds.y1;
4985 if (p->bounds.x2 > bounds.x2)
4986 bounds.x2=p->bounds.x2;
4987 if (p->bounds.y2 > bounds.y2)
4988 bounds.y2=p->bounds.y2;
4989 }
4990 bounds.x1-=(mid+1.0);
4991 bounds.y1-=(mid+1.0);
4992 bounds.x2+=(mid+1.0);
4993 bounds.y2+=(mid+1.0);
4994 if ((bounds.x1 >= (double) image->columns) ||
4995 (bounds.y1 >= (double) image->rows) ||
4996 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
4997 {
4998 polygon_info=DestroyPolygonTLS(polygon_info);
4999 return(MagickTrue); /* virtual polygon */
5000 }
5001 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5002 (double) image->columns-1.0 : bounds.x1;
5003 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5004 (double) image->rows-1.0 : bounds.y1;
5005 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5006 (double) image->columns-1.0 : bounds.x2;
5007 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5008 (double) image->rows-1.0 : bounds.y2;
5009 poly_extent.x1=CastDoubleToLong(ceil(bounds.x1-0.5));
5010 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5011 poly_extent.x2=CastDoubleToLong(floor(bounds.x2+0.5));
5012 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5013 number_threads=GetMagickNumberThreads(image,image,poly_extent.y2-
5014 poly_extent.y1+1,1);
5015 status=AcquirePolygonEdgesTLS(polygon_info,number_threads,&image->exception);
5016 if (status == MagickFalse)
5017 {
5018 polygon_info=DestroyPolygonTLS(polygon_info);
5019 return(status);
5020 }
5021 status=MagickTrue;
5022 exception=(&image->exception);
5023 image_view=AcquireAuthenticCacheView(image,exception);
5024 if ((primitive_info->coordinates == 1) ||
5025 (polygon_info[0]->number_edges == 0))
5026 {
5027 /*
5028 Draw point.
5029 */
5030#if defined(MAGICKCORE_OPENMP_SUPPORT)
5031 #pragma omp parallel for schedule(static) shared(status) \
5032 num_threads(number_threads)
5033#endif
5034 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5035 {
5036 MagickBooleanType
5037 sync;
5038
5039 PixelPacket
5040 *magick_restrict q;
5041
5042 ssize_t
5043 x;
5044
5045 if (status == MagickFalse)
5046 continue;
5047 x=poly_extent.x1;
5048 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (poly_extent.x2-
5049 x+1),1,exception);
5050 if (q == (PixelPacket *) NULL)
5051 {
5052 status=MagickFalse;
5053 continue;
5054 }
5055 for ( ; x <= poly_extent.x2; x++)
5056 {
5057 if ((x == CastDoubleToLong(ceil(primitive_info->point.x-0.5))) &&
5058 (y == CastDoubleToLong(ceil(primitive_info->point.y-0.5))))
5059 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,q);
5060 q++;
5061 }
5062 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5063 if (sync == MagickFalse)
5064 status=MagickFalse;
5065 }
5066 image_view=DestroyCacheView(image_view);
5067 polygon_info=DestroyPolygonTLS(polygon_info);
5068 if (draw_info->debug != MagickFalse)
5069 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5070 " end draw-polygon");
5071 return(status);
5072 }
5073 /*
5074 Draw polygon or line.
5075 */
5076 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5077 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5078#if defined(MAGICKCORE_OPENMP_SUPPORT)
5079 #pragma omp parallel for schedule(static) shared(status) \
5080 num_threads(number_threads)
5081#endif
5082 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5083 {
5084 const int
5085 id = GetOpenMPThreadId();
5086
5087 PixelPacket
5088 fill_color,
5089 stroke_color;
5090
5091 PixelPacket
5092 *magick_restrict q;
5093
5094 ssize_t
5095 x;
5096
5097 if (status == MagickFalse)
5098 continue;
5099 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(size_t)
5100 (poly_extent.x2-poly_extent.x1+1),1,exception);
5101 if (q == (PixelPacket *) NULL)
5102 {
5103 status=MagickFalse;
5104 continue;
5105 }
5106 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5107 {
5108 double
5109 fill_opacity,
5110 stroke_opacity;
5111
5112 /*
5113 Fill and/or stroke.
5114 */
5115 fill_opacity=GetOpacityPixel(polygon_info[id],mid,fill,
5116 draw_info->fill_rule,x,y,&stroke_opacity);
5117 if (draw_info->stroke_antialias == MagickFalse)
5118 {
5119 fill_opacity=fill_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5120 stroke_opacity=stroke_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5121 }
5122 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5123 &fill_color);
5124 fill_opacity=(double) ((MagickRealType) QuantumRange-fill_opacity*
5125 ((MagickRealType) QuantumRange-(MagickRealType) fill_color.opacity));
5126 MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
5127 (MagickRealType) q->opacity,q);
5128 (void) GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5129 &stroke_color);
5130 stroke_opacity=(double) ((MagickRealType) QuantumRange-stroke_opacity*
5131 ((MagickRealType) QuantumRange-(MagickRealType) stroke_color.opacity));
5132 MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
5133 (MagickRealType) q->opacity,q);
5134 q++;
5135 }
5136 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5137 status=MagickFalse;
5138 }
5139 image_view=DestroyCacheView(image_view);
5140 polygon_info=DestroyPolygonTLS(polygon_info);
5141 if (draw_info->debug != MagickFalse)
5142 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5143 return(status);
5144}
5145
5146/*
5147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5148% %
5149% %
5150% %
5151% D r a w P r i m i t i v e %
5152% %
5153% %
5154% %
5155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5156%
5157% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5158%
5159% The format of the DrawPrimitive method is:
5160%
5161% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5162% PrimitiveInfo *primitive_info)
5163%
5164% A description of each parameter follows:
5165%
5166% o image: the image.
5167%
5168% o draw_info: the draw info.
5169%
5170% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5171%
5172*/
5173static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5174{
5175 const char
5176 *methods[] =
5177 {
5178 "point",
5179 "replace",
5180 "floodfill",
5181 "filltoborder",
5182 "reset",
5183 "?"
5184 };
5185
5186 PointInfo
5187 p,
5188 q,
5189 point;
5190
5191 ssize_t
5192 i,
5193 x;
5194
5195 ssize_t
5196 coordinates,
5197 y;
5198
5199 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5200 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5201 switch (primitive_info->primitive)
5202 {
5203 case PointPrimitive:
5204 {
5205 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5206 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5207 methods[primitive_info->method]);
5208 return;
5209 }
5210 case ColorPrimitive:
5211 {
5212 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5213 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5214 methods[primitive_info->method]);
5215 return;
5216 }
5217 case MattePrimitive:
5218 {
5219 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5220 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
5221 methods[primitive_info->method]);
5222 return;
5223 }
5224 case TextPrimitive:
5225 {
5226 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5227 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5228 return;
5229 }
5230 case ImagePrimitive:
5231 {
5232 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5233 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5234 return;
5235 }
5236 default:
5237 break;
5238 }
5239 coordinates=0;
5240 p=primitive_info[0].point;
5241 q.x=(-1.0);
5242 q.y=(-1.0);
5243 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5244 {
5245 point=primitive_info[i].point;
5246 if (coordinates <= 0)
5247 {
5248 coordinates=(ssize_t) primitive_info[i].coordinates;
5249 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5250 " begin open (%.20g)",(double) coordinates);
5251 p=point;
5252 }
5253 point=primitive_info[i].point;
5254 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5255 (fabs(q.y-point.y) >= MagickEpsilon))
5256 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5257 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5258 else
5259 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5260 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5261 q=point;
5262 coordinates--;
5263 if (coordinates > 0)
5264 continue;
5265 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5266 (fabs(p.y-point.y) >= MagickEpsilon))
5267 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5268 (double) coordinates);
5269 else
5270 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5271 (double) coordinates);
5272 }
5273}
5274
5275MagickExport MagickBooleanType DrawPrimitive(Image *image,
5276 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5277{
5278 CacheView
5279 *image_view;
5280
5281 ExceptionInfo
5282 *exception;
5283
5284 MagickStatusType
5285 status;
5286
5287 ssize_t
5288 i,
5289 x;
5290
5291 ssize_t
5292 y;
5293
5294 if (draw_info->debug != MagickFalse)
5295 {
5296 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5297 " begin draw-primitive");
5298 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5299 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5300 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5301 draw_info->affine.tx,draw_info->affine.ty);
5302 }
5303 exception=(&image->exception);
5304 status=MagickTrue;
5305 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5306 ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
5307 (IsPixelGray(&draw_info->stroke) == MagickFalse)))
5308 status=SetImageColorspace(image,sRGBColorspace);
5309 if (draw_info->compliance == SVGCompliance)
5310 {
5311 status&=SetImageClipMask(image,draw_info->clipping_mask);
5312 status&=SetImageMask(image,draw_info->composite_mask);
5313 }
5314 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5315 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5316 image_view=AcquireAuthenticCacheView(image,exception);
5317 switch (primitive_info->primitive)
5318 {
5319 case ColorPrimitive:
5320 {
5321 switch (primitive_info->method)
5322 {
5323 case PointMethod:
5324 default:
5325 {
5326 PixelPacket
5327 *q;
5328
5329 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5330 if (q == (PixelPacket *) NULL)
5331 break;
5332 (void) GetFillColor(draw_info,x,y,q);
5333 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5334 break;
5335 }
5336 case ReplaceMethod:
5337 {
5338 PixelPacket
5339 target;
5340
5341 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5342 for (y=0; y < (ssize_t) image->rows; y++)
5343 {
5344 PixelPacket
5345 *magick_restrict q;
5346
5347 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5348 exception);
5349 if (q == (PixelPacket *) NULL)
5350 break;
5351 for (x=0; x < (ssize_t) image->columns; x++)
5352 {
5353 if (IsColorSimilar(image,q,&target) == MagickFalse)
5354 {
5355 q++;
5356 continue;
5357 }
5358 (void) GetFillColor(draw_info,x,y,q);
5359 q++;
5360 }
5361 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5362 if (status == MagickFalse)
5363 break;
5364 }
5365 break;
5366 }
5367 case FloodfillMethod:
5368 case FillToBorderMethod:
5369 {
5370 MagickPixelPacket
5371 target;
5372
5373 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5374 if (primitive_info->method == FillToBorderMethod)
5375 {
5376 target.red=(MagickRealType) draw_info->border_color.red;
5377 target.green=(MagickRealType) draw_info->border_color.green;
5378 target.blue=(MagickRealType) draw_info->border_color.blue;
5379 }
5380 status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
5381 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5382 MagickTrue);
5383 break;
5384 }
5385 case ResetMethod:
5386 {
5387 for (y=0; y < (ssize_t) image->rows; y++)
5388 {
5389 PixelPacket
5390 *magick_restrict q;
5391
5392 ssize_t
5393 x;
5394
5395 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5396 exception);
5397 if (q == (PixelPacket *) NULL)
5398 break;
5399 for (x=0; x < (ssize_t) image->columns; x++)
5400 {
5401 (void) GetFillColor(draw_info,x,y,q);
5402 q++;
5403 }
5404 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5405 if (status == MagickFalse)
5406 break;
5407 }
5408 break;
5409 }
5410 }
5411 break;
5412 }
5413 case MattePrimitive:
5414 {
5415 if (image->matte == MagickFalse)
5416 status&=SetImageAlphaChannel(image,OpaqueAlphaChannel);
5417 switch (primitive_info->method)
5418 {
5419 case PointMethod:
5420 default:
5421 {
5422 PixelPacket
5423 pixel;
5424
5425 PixelPacket
5426 *q;
5427
5428 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5429 if (q == (PixelPacket *) NULL)
5430 break;
5431 (void) GetFillColor(draw_info,x,y,&pixel);
5432 SetPixelOpacity(q,pixel.opacity);
5433 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5434 break;
5435 }
5436 case ReplaceMethod:
5437 {
5438 PixelPacket
5439 pixel,
5440 target;
5441
5442 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5443 for (y=0; y < (ssize_t) image->rows; y++)
5444 {
5445 PixelPacket
5446 *magick_restrict q;
5447
5448 ssize_t
5449 x;
5450
5451 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5452 exception);
5453 if (q == (PixelPacket *) NULL)
5454 break;
5455 for (x=0; x < (ssize_t) image->columns; x++)
5456 {
5457 if (IsColorSimilar(image,q,&target) == MagickFalse)
5458 {
5459 q++;
5460 continue;
5461 }
5462 (void) GetFillColor(draw_info,x,y,&pixel);
5463 SetPixelOpacity(q,pixel.opacity);
5464 q++;
5465 }
5466 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5467 if (status == MagickFalse)
5468 break;
5469 }
5470 break;
5471 }
5472 case FloodfillMethod:
5473 case FillToBorderMethod:
5474 {
5475 MagickPixelPacket
5476 target;
5477
5478 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5479 if (primitive_info->method == FillToBorderMethod)
5480 {
5481 target.red=(MagickRealType) draw_info->border_color.red;
5482 target.green=(MagickRealType) draw_info->border_color.green;
5483 target.blue=(MagickRealType) draw_info->border_color.blue;
5484 }
5485 status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
5486 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5487 MagickTrue);
5488 break;
5489 }
5490 case ResetMethod:
5491 {
5492 PixelPacket
5493 pixel;
5494
5495 for (y=0; y < (ssize_t) image->rows; y++)
5496 {
5497 PixelPacket
5498 *magick_restrict q;
5499
5500 ssize_t
5501 x;
5502
5503 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5504 exception);
5505 if (q == (PixelPacket *) NULL)
5506 break;
5507 for (x=0; x < (ssize_t) image->columns; x++)
5508 {
5509 (void) GetFillColor(draw_info,x,y,&pixel);
5510 SetPixelOpacity(q,pixel.opacity);
5511 q++;
5512 }
5513 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5514 if (status == MagickFalse)
5515 break;
5516 }
5517 break;
5518 }
5519 }
5520 break;
5521 }
5522 case ImagePrimitive:
5523 {
5524 AffineMatrix
5525 affine;
5526
5527 char
5528 composite_geometry[MaxTextExtent];
5529
5530 Image
5531 *composite_image,
5532 *composite_images;
5533
5534 ImageInfo
5535 *clone_info;
5536
5537 RectangleInfo
5538 geometry;
5539
5540 ssize_t
5541 x1,
5542 y1;
5543
5544 if (primitive_info->text == (char *) NULL)
5545 break;
5546 clone_info=AcquireImageInfo();
5547 composite_images=(Image *) NULL;
5548 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5549 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5550 &image->exception);
5551 else
5552 if (*primitive_info->text != '\0')
5553 {
5554 /*
5555 Read composite image.
5556 */
5557 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5558 MagickPathExtent);
5559 (void) SetImageInfo(clone_info,1,exception);
5560 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5561 MagickPathExtent);
5562 if (clone_info->size != (char *) NULL)
5563 clone_info->size=DestroyString(clone_info->size);
5564 if (clone_info->extract != (char *) NULL)
5565 clone_info->extract=DestroyString(clone_info->extract);
5566 if ((LocaleCompare(clone_info->magick,"ftp") != 0) &&
5567 (LocaleCompare(clone_info->magick,"http") != 0) &&
5568 (LocaleCompare(clone_info->magick,"https") != 0) &&
5569 (LocaleCompare(clone_info->magick,"mvg") != 0) &&
5570 (LocaleCompare(clone_info->magick,"vid") != 0))
5571 composite_images=ReadImage(clone_info,exception);
5572 else
5573 (void) ThrowMagickException(exception,GetMagickModule(),
5574 FileOpenError,"UnableToOpenFile","`%s'",clone_info->filename);
5575 }
5576 clone_info=DestroyImageInfo(clone_info);
5577 if (composite_images == (Image *) NULL)
5578 {
5579 status=0;
5580 break;
5581 }
5582 composite_image=RemoveFirstImageFromList(&composite_images);
5583 composite_images=DestroyImageList(composite_images);
5584 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5585 NULL,(void *) NULL);
5586 x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5587 y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5588 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5589 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5590 {
5591 char
5592 geometry[MaxTextExtent];
5593
5594 /*
5595 Resize image.
5596 */
5597 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
5598 primitive_info[1].point.x,primitive_info[1].point.y);
5599 composite_image->filter=image->filter;
5600 status&=TransformImage(&composite_image,(char *) NULL,geometry);
5601 }
5602 if (composite_image->matte == MagickFalse)
5603 status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5604 if (draw_info->opacity != OpaqueOpacity)
5605 status&=SetImageOpacity(composite_image,draw_info->opacity);
5606 SetGeometry(image,&geometry);
5607 image->gravity=draw_info->gravity;
5608 geometry.x=x;
5609 geometry.y=y;
5610 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5611 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5612 composite_image->rows,(double) geometry.x,(double) geometry.y);
5613 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5614 &image->exception);
5615 affine=draw_info->affine;
5616 affine.tx=(double) geometry.x;
5617 affine.ty=(double) geometry.y;
5618 composite_image->interpolate=image->interpolate;
5619 if ((draw_info->compose == OverCompositeOp) ||
5620 (draw_info->compose == SrcOverCompositeOp))
5621 status&=DrawAffineImage(image,composite_image,&affine);
5622 else
5623 status&=CompositeImage(image,draw_info->compose,composite_image,
5624 geometry.x,geometry.y);
5625 composite_image=DestroyImage(composite_image);
5626 break;
5627 }
5628 case PointPrimitive:
5629 {
5630 PixelPacket
5631 fill_color;
5632
5633 PixelPacket
5634 *q;
5635
5636 if ((y < 0) || (y >= (ssize_t) image->rows))
5637 break;
5638 if ((x < 0) || (x >= (ssize_t) image->columns))
5639 break;
5640 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5641 if (q == (PixelPacket *) NULL)
5642 break;
5643 (void) GetFillColor(draw_info,x,y,&fill_color);
5644 MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5645 (MagickRealType) q->opacity,q);
5646 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5647 break;
5648 }
5649 case TextPrimitive:
5650 {
5651 char
5652 geometry[MaxTextExtent];
5653
5654 DrawInfo
5655 *clone_info;
5656
5657 if (primitive_info->text == (char *) NULL)
5658 break;
5659 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5660 (void) CloneString(&clone_info->text,primitive_info->text);
5661 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
5662 primitive_info->point.x,primitive_info->point.y);
5663 (void) CloneString(&clone_info->geometry,geometry);
5664 status&=AnnotateImage(image,clone_info);
5665 clone_info=DestroyDrawInfo(clone_info);
5666 break;
5667 }
5668 default:
5669 {
5670 double
5671 mid,
5672 scale;
5673
5674 DrawInfo
5675 *clone_info;
5676
5677 if (IsEventLogging() != MagickFalse)
5678 LogPrimitiveInfo(primitive_info);
5679 scale=ExpandAffine(&draw_info->affine);
5680 if ((draw_info->dash_pattern != (double *) NULL) &&
5681 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5682 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5683 (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5684 {
5685 /*
5686 Draw dash polygon.
5687 */
5688 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5689 clone_info->stroke_width=0.0;
5690 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5691 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5692 clone_info=DestroyDrawInfo(clone_info);
5693 if (status != MagickFalse)
5694 status&=DrawDashPolygon(draw_info,primitive_info,image);
5695 break;
5696 }
5697 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5698 if ((mid > 1.0) &&
5699 ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5700 (draw_info->stroke_pattern != (Image *) NULL)))
5701 {
5702 double
5703 x,
5704 y;
5705
5706 MagickBooleanType
5707 closed_path;
5708
5709 /*
5710 Draw strokes while respecting line cap/join attributes.
5711 */
5712 closed_path=primitive_info[0].closed_subpath;
5713 i=(ssize_t) primitive_info[0].coordinates;
5714 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5715 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5716 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5717 closed_path=MagickTrue;
5718 if ((((draw_info->linecap == RoundCap) ||
5719 (closed_path != MagickFalse)) &&
5720 (draw_info->linejoin == RoundJoin)) ||
5721 (primitive_info[i].primitive != UndefinedPrimitive))
5722 {
5723 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5724 break;
5725 }
5726 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5727 clone_info->stroke_width=0.0;
5728 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5729 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5730 clone_info=DestroyDrawInfo(clone_info);
5731 if (status != MagickFalse)
5732 status&=DrawStrokePolygon(image,draw_info,primitive_info);
5733 break;
5734 }
5735 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5736 break;
5737 }
5738 }
5739 image_view=DestroyCacheView(image_view);
5740 if (draw_info->compliance == SVGCompliance)
5741 {
5742 status&=SetImageClipMask(image,(Image *) NULL);
5743 status&=SetImageMask(image,(Image *) NULL);
5744 }
5745 if (draw_info->debug != MagickFalse)
5746 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5747 return(status != 0 ? MagickTrue : MagickFalse);
5748}
5749
5750/*
5751%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5752% %
5753% %
5754% %
5755+ D r a w S t r o k e P o l y g o n %
5756% %
5757% %
5758% %
5759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5760%
5761% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
5762% the image while respecting the line cap and join attributes.
5763%
5764% The format of the DrawStrokePolygon method is:
5765%
5766% MagickBooleanType DrawStrokePolygon(Image *image,
5767% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5768%
5769% A description of each parameter follows:
5770%
5771% o image: the image.
5772%
5773% o draw_info: the draw info.
5774%
5775% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5776%
5777%
5778*/
5779
5780static MagickBooleanType DrawRoundLinecap(Image *image,
5781 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5782{
5783 PrimitiveInfo
5784 linecap[5];
5785
5786 ssize_t
5787 i;
5788
5789 if (primitive_info->coordinates < 1)
5790 return(MagickFalse);
5791 for (i=0; i < 4; i++)
5792 linecap[i]=(*primitive_info);
5793 linecap[0].coordinates=4;
5794 linecap[1].point.x+=2.0*MagickEpsilon;
5795 linecap[2].point.x+=2.0*MagickEpsilon;
5796 linecap[2].point.y+=2.0*MagickEpsilon;
5797 linecap[3].point.y+=2.0*MagickEpsilon;
5798 linecap[4].primitive=UndefinedPrimitive;
5799 return(DrawPolygonPrimitive(image,draw_info,linecap));
5800}
5801
5802static MagickBooleanType DrawStrokePolygon(Image *image,
5803 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5804{
5805 DrawInfo
5806 *clone_info;
5807
5808 MagickBooleanType
5809 closed_path;
5810
5811 MagickStatusType
5812 status;
5813
5814 PrimitiveInfo
5815 *stroke_polygon;
5816
5817 const PrimitiveInfo
5818 *p,
5819 *q;
5820
5821 /*
5822 Draw stroked polygon.
5823 */
5824 if (draw_info->debug != MagickFalse)
5825 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5826 " begin draw-stroke-polygon");
5827 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5828 clone_info->fill=draw_info->stroke;
5829 if (clone_info->fill_pattern != (Image *) NULL)
5830 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5831 if (clone_info->stroke_pattern != (Image *) NULL)
5832 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5833 MagickTrue,&clone_info->stroke_pattern->exception);
5834 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5835 clone_info->stroke_width=0.0;
5836 clone_info->fill_rule=NonZeroRule;
5837 status=MagickTrue;
5838 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5839 {
5840 if (p->coordinates == 1)
5841 continue;
5842 stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5843 if (stroke_polygon == (PrimitiveInfo *) NULL)
5844 {
5845 status=0;
5846 break;
5847 }
5848 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5849 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5850 if (status == 0)
5851 break;
5852 q=p+p->coordinates-1;
5853 closed_path=p->closed_subpath;
5854 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5855 {
5856 status&=DrawRoundLinecap(image,draw_info,p);
5857 status&=DrawRoundLinecap(image,draw_info,q);
5858 }
5859 }
5860 clone_info=DestroyDrawInfo(clone_info);
5861 if (draw_info->debug != MagickFalse)
5862 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5863 " end draw-stroke-polygon");
5864 return(status != 0 ? MagickTrue : MagickFalse);
5865}
5866
5867/*
5868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5869% %
5870% %
5871% %
5872% G e t A f f i n e M a t r i x %
5873% %
5874% %
5875% %
5876%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5877%
5878% GetAffineMatrix() returns an AffineMatrix initialized to the identity
5879% matrix.
5880%
5881% The format of the GetAffineMatrix method is:
5882%
5883% void GetAffineMatrix(AffineMatrix *affine_matrix)
5884%
5885% A description of each parameter follows:
5886%
5887% o affine_matrix: the affine matrix.
5888%
5889*/
5890MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
5891{
5892 assert(affine_matrix != (AffineMatrix *) NULL);
5893 if (IsEventLogging() != MagickFalse)
5894 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5895 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
5896 affine_matrix->sx=1.0;
5897 affine_matrix->sy=1.0;
5898}
5899
5900/*
5901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5902% %
5903% %
5904% %
5905+ G e t D r a w I n f o %
5906% %
5907% %
5908% %
5909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5910%
5911% GetDrawInfo() initializes draw_info to default values from image_info.
5912%
5913% The format of the GetDrawInfo method is:
5914%
5915% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5916%
5917% A description of each parameter follows:
5918%
5919% o image_info: the image info..
5920%
5921% o draw_info: the draw info.
5922%
5923*/
5924MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5925{
5926 char
5927 *next_token;
5928
5929 const char
5930 *option;
5931
5932 ExceptionInfo
5933 *exception;
5934
5935 /*
5936 Initialize draw attributes.
5937 */
5938 assert(draw_info != (DrawInfo *) NULL);
5939 if (IsEventLogging() != MagickFalse)
5940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5941 (void) memset(draw_info,0,sizeof(*draw_info));
5942 draw_info->image_info=CloneImageInfo(image_info);
5943 GetAffineMatrix(&draw_info->affine);
5944 exception=AcquireExceptionInfo();
5945 (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
5946 (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
5947 draw_info->stroke_antialias=draw_info->image_info->antialias;
5948 draw_info->stroke_width=1.0;
5949 draw_info->fill_rule=EvenOddRule;
5950 draw_info->opacity=OpaqueOpacity;
5951 draw_info->fill_opacity=OpaqueOpacity;
5952 draw_info->stroke_opacity=OpaqueOpacity;
5953 draw_info->linecap=ButtCap;
5954 draw_info->linejoin=MiterJoin;
5955 draw_info->miterlimit=10;
5956 draw_info->decorate=NoDecoration;
5957 if (draw_info->image_info->font != (char *) NULL)
5958 draw_info->font=AcquireString(draw_info->image_info->font);
5959 if (draw_info->image_info->density != (char *) NULL)
5960 draw_info->density=AcquireString(draw_info->image_info->density);
5961 draw_info->text_antialias=draw_info->image_info->antialias;
5962 draw_info->pointsize=12.0;
5963 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
5964 draw_info->pointsize=draw_info->image_info->pointsize;
5965 draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
5966 draw_info->border_color=draw_info->image_info->border_color;
5967 draw_info->compose=OverCompositeOp;
5968 if (draw_info->image_info->server_name != (char *) NULL)
5969 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
5970 draw_info->render=MagickTrue;
5971 draw_info->clip_path=MagickFalse;
5972 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
5973 MagickTrue : MagickFalse;
5974 option=GetImageOption(draw_info->image_info,"direction");
5975 if (option != (const char *) NULL)
5976 draw_info->direction=(DirectionType) ParseCommandOption(
5977 MagickDirectionOptions,MagickFalse,option);
5978 else
5979 draw_info->direction=UndefinedDirection;
5980 option=GetImageOption(draw_info->image_info,"encoding");
5981 if (option != (const char *) NULL)
5982 (void) CloneString(&draw_info->encoding,option);
5983 option=GetImageOption(draw_info->image_info,"family");
5984 if (option != (const char *) NULL)
5985 (void) CloneString(&draw_info->family,option);
5986 option=GetImageOption(draw_info->image_info,"fill");
5987 if (option != (const char *) NULL)
5988 (void) QueryColorDatabase(option,&draw_info->fill,exception);
5989 option=GetImageOption(draw_info->image_info,"gravity");
5990 if (option != (const char *) NULL)
5991 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5992 MagickFalse,option);
5993 option=GetImageOption(draw_info->image_info,"interline-spacing");
5994 if (option != (const char *) NULL)
5995 draw_info->interline_spacing=GetDrawValue(option,&next_token);
5996 option=GetImageOption(draw_info->image_info,"interword-spacing");
5997 if (option != (const char *) NULL)
5998 draw_info->interword_spacing=GetDrawValue(option,&next_token);
5999 option=GetImageOption(draw_info->image_info,"kerning");
6000 if (option != (const char *) NULL)
6001 draw_info->kerning=GetDrawValue(option,&next_token);
6002 option=GetImageOption(draw_info->image_info,"stroke");
6003 if (option != (const char *) NULL)
6004 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
6005 option=GetImageOption(draw_info->image_info,"strokewidth");
6006 if (option != (const char *) NULL)
6007 draw_info->stroke_width=GetDrawValue(option,&next_token);
6008 option=GetImageOption(draw_info->image_info,"style");
6009 if (option != (const char *) NULL)
6010 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6011 MagickFalse,option);
6012 option=GetImageOption(draw_info->image_info,"undercolor");
6013 if (option != (const char *) NULL)
6014 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6015 option=GetImageOption(draw_info->image_info,"weight");
6016 if (option != (const char *) NULL)
6017 {
6018 ssize_t
6019 weight;
6020
6021 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6022 if (weight == -1)
6023 weight=(ssize_t) StringToUnsignedLong(option);
6024 draw_info->weight=(size_t) weight;
6025 }
6026 exception=DestroyExceptionInfo(exception);
6027 draw_info->signature=MagickCoreSignature;
6028}
6029
6030/*
6031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6032% %
6033% %
6034% %
6035+ P e r m u t a t e %
6036% %
6037% %
6038% %
6039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6040%
6041% Permutate() returns the permutation of the (n,k).
6042%
6043% The format of the Permutate method is:
6044%
6045% void Permutate(ssize_t n,ssize_t k)
6046%
6047% A description of each parameter follows:
6048%
6049% o n:
6050%
6051% o k:
6052%
6053%
6054*/
6055static inline double Permutate(const ssize_t n,const ssize_t k)
6056{
6057 double
6058 r;
6059
6060 ssize_t
6061 i;
6062
6063 r=1.0;
6064 for (i=k+1; i <= n; i++)
6065 r*=i;
6066 for (i=1; i <= (n-k); i++)
6067 r/=i;
6068 return(r);
6069}
6070
6071/*
6072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6073% %
6074% %
6075% %
6076+ T r a c e P r i m i t i v e %
6077% %
6078% %
6079% %
6080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6081%
6082% TracePrimitive is a collection of methods for generating graphic
6083% primitives such as arcs, ellipses, paths, etc.
6084%
6085*/
6086
6087static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6088 const PointInfo end,const PointInfo degrees)
6089{
6090 PointInfo
6091 center,
6092 radius;
6093
6094 center.x=0.5*(end.x+start.x);
6095 center.y=0.5*(end.y+start.y);
6096 radius.x=fabs(center.x-start.x);
6097 radius.y=fabs(center.y-start.y);
6098 return(TraceEllipse(mvg_info,center,radius,degrees));
6099}
6100
6101static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6102 const PointInfo end,const PointInfo arc,const double angle,
6103 const MagickBooleanType large_arc,const MagickBooleanType sweep)
6104{
6105 double
6106 alpha,
6107 beta,
6108 delta,
6109 factor,
6110 gamma,
6111 theta;
6112
6113 MagickStatusType
6114 status;
6115
6116 PointInfo
6117 center,
6118 points[3],
6119 radii;
6120
6121 double
6122 cosine,
6123 sine;
6124
6125 PrimitiveInfo
6126 *primitive_info;
6127
6128 PrimitiveInfo
6129 *p;
6130
6131 ssize_t
6132 i;
6133
6134 size_t
6135 arc_segments;
6136
6137 ssize_t
6138 offset;
6139
6140 offset=mvg_info->offset;
6141 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6142 primitive_info->coordinates=0;
6143 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6144 (fabs(start.y-end.y) < MagickEpsilon))
6145 return(TracePoint(primitive_info,end));
6146 radii.x=fabs(arc.x);
6147 radii.y=fabs(arc.y);
6148 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6149 return(TraceLine(primitive_info,start,end));
6150 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6151 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6152 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6153 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6154 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6155 (radii.y*radii.y);
6156 if (delta < MagickEpsilon)
6157 return(TraceLine(primitive_info,start,end));
6158 if (delta > 1.0)
6159 {
6160 radii.x*=sqrt((double) delta);
6161 radii.y*=sqrt((double) delta);
6162 }
6163 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6164 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6165 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6166 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6167 alpha=points[1].x-points[0].x;
6168 beta=points[1].y-points[0].y;
6169 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6170 return(TraceLine(primitive_info,start,end));
6171 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6172 if (factor <= 0.0)
6173 factor=0.0;
6174 else
6175 {
6176 factor=sqrt((double) factor);
6177 if (sweep == large_arc)
6178 factor=(-factor);
6179 }
6180 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6181 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6182 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6183 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6184 if ((theta < 0.0) && (sweep != MagickFalse))
6185 theta+=2.0*MagickPI;
6186 else
6187 if ((theta > 0.0) && (sweep == MagickFalse))
6188 theta-=2.0*MagickPI;
6189 arc_segments=(size_t) CastDoubleToLong(ceil(fabs((double) (theta/(0.5*
6190 MagickPI+MagickEpsilon)))));
6191 p=primitive_info;
6192 status=MagickTrue;
6193 for (i=0; i < (ssize_t) arc_segments; i++)
6194 {
6195 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6196 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6197 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6198 sin(fmod((double) beta,DegreesToRadians(360.0)));
6199 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6200 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6201 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6202 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6203 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6204 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6205 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6206 theta/arc_segments),DegreesToRadians(360.0))));
6207 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6208 theta/arc_segments),DegreesToRadians(360.0))));
6209 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6210 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6211 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6212 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6213 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6214 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6215 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6216 points[0].y);
6217 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6218 points[0].y);
6219 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6220 points[1].y);
6221 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6222 points[1].y);
6223 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6224 points[2].y);
6225 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6226 points[2].y);
6227 if (i == (ssize_t) (arc_segments-1))
6228 (p+3)->point=end;
6229 status&=TraceBezier(mvg_info,4);
6230 if (status == 0)
6231 break;
6232 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6233 mvg_info->offset+=p->coordinates;
6234 p+=(ptrdiff_t) p->coordinates;
6235 }
6236 if (status == 0)
6237 return(MagickFalse);
6238 mvg_info->offset=offset;
6239 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6240 primitive_info->coordinates=(size_t) (p-primitive_info);
6241 primitive_info->closed_subpath=MagickFalse;
6242 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6243 {
6244 p->primitive=primitive_info->primitive;
6245 p--;
6246 }
6247 return(MagickTrue);
6248}
6249
6250static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6251 const size_t number_coordinates)
6252{
6253 double
6254 alpha,
6255 *coefficients,
6256 weight;
6257
6258 PointInfo
6259 end,
6260 point,
6261 *points;
6262
6263 PrimitiveInfo
6264 *primitive_info;
6265
6266 PrimitiveInfo
6267 *p;
6268
6269 ssize_t
6270 i,
6271 j;
6272
6273 size_t
6274 control_points,
6275 quantum;
6276
6277 /*
6278 Allocate coefficients.
6279 */
6280 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6281 quantum=number_coordinates;
6282 for (i=0; i < (ssize_t) number_coordinates; i++)
6283 {
6284 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6285 {
6286 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6287 if (alpha > (double) GetMaxMemoryRequest())
6288 {
6289 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6290 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6291 return(MagickFalse);
6292 }
6293 if (alpha > (double) quantum)
6294 quantum=(size_t) alpha;
6295 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6296 if (alpha > (double) quantum)
6297 quantum=(size_t) alpha;
6298 }
6299 }
6300 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6301 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6302 if (quantum > (double) GetMaxMemoryRequest())
6303 {
6304 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6305 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6306 return(MagickFalse);
6307 }
6308 coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6309 sizeof(*coefficients));
6310 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6311 sizeof(*points));
6312 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6313 {
6314 if (points != (PointInfo *) NULL)
6315 points=(PointInfo *) RelinquishMagickMemory(points);
6316 if (coefficients != (double *) NULL)
6317 coefficients=(double *) RelinquishMagickMemory(coefficients);
6318 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6319 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6320 return(MagickFalse);
6321 }
6322 control_points=quantum*number_coordinates;
6323 if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6324 {
6325 points=(PointInfo *) RelinquishMagickMemory(points);
6326 coefficients=(double *) RelinquishMagickMemory(coefficients);
6327 return(MagickFalse);
6328 }
6329 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6330 /*
6331 Compute bezier points.
6332 */
6333 end=primitive_info[number_coordinates-1].point;
6334 for (i=0; i < (ssize_t) number_coordinates; i++)
6335 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6336 weight=0.0;
6337 for (i=0; i < (ssize_t) control_points; i++)
6338 {
6339 p=primitive_info;
6340 point.x=0.0;
6341 point.y=0.0;
6342 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6343 for (j=0; j < (ssize_t) number_coordinates; j++)
6344 {
6345 point.x+=alpha*coefficients[j]*p->point.x;
6346 point.y+=alpha*coefficients[j]*p->point.y;
6347 alpha*=weight/(1.0-weight);
6348 p++;
6349 }
6350 points[i]=point;
6351 weight+=1.0/control_points;
6352 }
6353 /*
6354 Bezier curves are just short segmented polys.
6355 */
6356 p=primitive_info;
6357 for (i=0; i < (ssize_t) control_points; i++)
6358 {
6359 if (TracePoint(p,points[i]) == MagickFalse)
6360 {
6361 points=(PointInfo *) RelinquishMagickMemory(points);
6362 coefficients=(double *) RelinquishMagickMemory(coefficients);
6363 return(MagickFalse);
6364 }
6365 p+=(ptrdiff_t) p->coordinates;
6366 }
6367 if (TracePoint(p,end) == MagickFalse)
6368 {
6369 points=(PointInfo *) RelinquishMagickMemory(points);
6370 coefficients=(double *) RelinquishMagickMemory(coefficients);
6371 return(MagickFalse);
6372 }
6373 p+=(ptrdiff_t) p->coordinates;
6374 primitive_info->coordinates=(size_t) (p-primitive_info);
6375 primitive_info->closed_subpath=MagickFalse;
6376 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6377 {
6378 p->primitive=primitive_info->primitive;
6379 p--;
6380 }
6381 points=(PointInfo *) RelinquishMagickMemory(points);
6382 coefficients=(double *) RelinquishMagickMemory(coefficients);
6383 return(MagickTrue);
6384}
6385
6386static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6387 const PointInfo end)
6388{
6389 double
6390 alpha,
6391 beta,
6392 radius;
6393
6394 PointInfo
6395 offset,
6396 degrees;
6397
6398 alpha=end.x-start.x;
6399 beta=end.y-start.y;
6400 radius=hypot((double) alpha,(double) beta);
6401 offset.x=(double) radius;
6402 offset.y=(double) radius;
6403 degrees.x=0.0;
6404 degrees.y=360.0;
6405 return(TraceEllipse(mvg_info,start,offset,degrees));
6406}
6407
6408static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6409 const PointInfo radii,const PointInfo arc)
6410{
6411 double
6412 coordinates,
6413 delta,
6414 step,
6415 x,
6416 y;
6417
6418 PointInfo
6419 angle,
6420 point;
6421
6422 PrimitiveInfo
6423 *primitive_info;
6424
6425 PrimitiveInfo
6426 *p;
6427
6428 ssize_t
6429 i;
6430
6431 /*
6432 Ellipses are just short segmented polys.
6433 */
6434 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6435 primitive_info->coordinates=0;
6436 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6437 return(MagickTrue);
6438 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6439 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6440 angle.x=DegreesToRadians(arc.x);
6441 y=arc.y;
6442 while (y < arc.x)
6443 y+=360.0;
6444 angle.y=DegreesToRadians(y);
6445 coordinates=ceil((angle.y-angle.x)/step+1.0);
6446 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6447 return(MagickFalse);
6448 i=0;
6449 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6450 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6451 {
6452 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6453 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6454 if (i++ >= (ssize_t) coordinates)
6455 break;
6456 if (TracePoint(p,point) == MagickFalse)
6457 return(MagickFalse);
6458 p+=(ptrdiff_t) p->coordinates;
6459 }
6460 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6461 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6462 if (TracePoint(p,point) == MagickFalse)
6463 return(MagickFalse);
6464 p+=(ptrdiff_t) p->coordinates;
6465 primitive_info->coordinates=(size_t) (p-primitive_info);
6466 primitive_info->closed_subpath=MagickFalse;
6467 x=fabs(primitive_info[0].point.x-
6468 primitive_info[primitive_info->coordinates-1].point.x);
6469 y=fabs(primitive_info[0].point.y-
6470 primitive_info[primitive_info->coordinates-1].point.y);
6471 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6472 primitive_info->closed_subpath=MagickTrue;
6473 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6474 {
6475 p->primitive=primitive_info->primitive;
6476 p--;
6477 }
6478 return(MagickTrue);
6479}
6480
6481static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6482 const PointInfo start,const PointInfo end)
6483{
6484 if (TracePoint(primitive_info,start) == MagickFalse)
6485 return(MagickFalse);
6486 if (TracePoint(primitive_info+1,end) == MagickFalse)
6487 return(MagickFalse);
6488 (primitive_info+1)->primitive=primitive_info->primitive;
6489 primitive_info->coordinates=2;
6490 primitive_info->closed_subpath=MagickFalse;
6491 return(MagickTrue);
6492}
6493
6494static ssize_t TracePath(Image *image,MVGInfo *mvg_info,const char *path)
6495{
6496 char
6497 *next_token,
6498 token[MaxTextExtent] = "";
6499
6500 const char
6501 *p;
6502
6503 double
6504 x,
6505 y;
6506
6507 int
6508 attribute,
6509 last_attribute;
6510
6511 MagickStatusType
6512 status;
6513
6514 PointInfo
6515 end = {0.0, 0.0},
6516 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6517 point = {0.0, 0.0},
6518 start = {0.0, 0.0};
6519
6520 PrimitiveInfo
6521 *primitive_info;
6522
6523 PrimitiveType
6524 primitive_type;
6525
6526 PrimitiveInfo
6527 *q;
6528
6529 ssize_t
6530 i;
6531
6532 size_t
6533 number_coordinates,
6534 z_count;
6535
6536 ssize_t
6537 subpath_offset;
6538
6539 subpath_offset=mvg_info->offset;
6540 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6541 status=MagickTrue;
6542 attribute=0;
6543 number_coordinates=0;
6544 z_count=0;
6545 *token='\0';
6546 primitive_type=primitive_info->primitive;
6547 q=primitive_info;
6548 for (p=path; *p != '\0'; )
6549 {
6550 if (status == MagickFalse)
6551 break;
6552 while (isspace((int) ((unsigned char) *p)) != 0)
6553 p++;
6554 if (*p == '\0')
6555 break;
6556 last_attribute=attribute;
6557 attribute=(int) (*p++);
6558 switch (attribute)
6559 {
6560 case 'a':
6561 case 'A':
6562 {
6563 double
6564 angle = 0.0;
6565
6566 MagickBooleanType
6567 large_arc = MagickFalse,
6568 sweep = MagickFalse;
6569
6570 PointInfo
6571 arc = {0.0, 0.0};
6572
6573 /*
6574 Elliptical arc.
6575 */
6576 do
6577 {
6578 (void) GetNextToken(p,&p,MaxTextExtent,token);
6579 if (*token == ',')
6580 (void) GetNextToken(p,&p,MaxTextExtent,token);
6581 arc.x=GetDrawValue(token,&next_token);
6582 if (token == next_token)
6583 ThrowPointExpectedException(image,token);
6584 (void) GetNextToken(p,&p,MaxTextExtent,token);
6585 if (*token == ',')
6586 (void) GetNextToken(p,&p,MaxTextExtent,token);
6587 arc.y=GetDrawValue(token,&next_token);
6588 if (token == next_token)
6589 ThrowPointExpectedException(image,token);
6590 (void) GetNextToken(p,&p,MaxTextExtent,token);
6591 if (*token == ',')
6592 (void) GetNextToken(p,&p,MaxTextExtent,token);
6593 angle=GetDrawValue(token,&next_token);
6594 if (token == next_token)
6595 ThrowPointExpectedException(image,token);
6596 (void) GetNextToken(p,&p,MaxTextExtent,token);
6597 if (*token == ',')
6598 (void) GetNextToken(p,&p,MaxTextExtent,token);
6599 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6600 (void) GetNextToken(p,&p,MaxTextExtent,token);
6601 if (*token == ',')
6602 (void) GetNextToken(p,&p,MaxTextExtent,token);
6603 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6604 if (*token == ',')
6605 (void) GetNextToken(p,&p,MaxTextExtent,token);
6606 (void) GetNextToken(p,&p,MaxTextExtent,token);
6607 if (*token == ',')
6608 (void) GetNextToken(p,&p,MaxTextExtent,token);
6609 x=GetDrawValue(token,&next_token);
6610 if (token == next_token)
6611 ThrowPointExpectedException(image,token);
6612 (void) GetNextToken(p,&p,MaxTextExtent,token);
6613 if (*token == ',')
6614 (void) GetNextToken(p,&p,MaxTextExtent,token);
6615 y=GetDrawValue(token,&next_token);
6616 if (token == next_token)
6617 ThrowPointExpectedException(image,token);
6618 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6619 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6620 status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6621 q=(*mvg_info->primitive_info)+mvg_info->offset;
6622 mvg_info->offset+=q->coordinates;
6623 q+=(ptrdiff_t) q->coordinates;
6624 point=end;
6625 while (isspace((int) ((unsigned char) *p)) != 0)
6626 p++;
6627 if (*p == ',')
6628 p++;
6629 } while (IsPoint(p) != MagickFalse);
6630 break;
6631 }
6632 case 'c':
6633 case 'C':
6634 {
6635 /*
6636 Cubic Bézier curve.
6637 */
6638 do
6639 {
6640 points[0]=point;
6641 for (i=1; i < 4; i++)
6642 {
6643 (void) GetNextToken(p,&p,MaxTextExtent,token);
6644 if (*token == ',')
6645 (void) GetNextToken(p,&p,MaxTextExtent,token);
6646 x=GetDrawValue(token,&next_token);
6647 if (token == next_token)
6648 ThrowPointExpectedException(image,token);
6649 (void) GetNextToken(p,&p,MaxTextExtent,token);
6650 if (*token == ',')
6651 (void) GetNextToken(p,&p,MaxTextExtent,token);
6652 y=GetDrawValue(token,&next_token);
6653 if (token == next_token)
6654 ThrowPointExpectedException(image,token);
6655 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6656 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6657 points[i]=end;
6658 }
6659 for (i=0; i < 4; i++)
6660 (q+i)->point=points[i];
6661 if (TraceBezier(mvg_info,4) == MagickFalse)
6662 return(-1);
6663 q=(*mvg_info->primitive_info)+mvg_info->offset;
6664 mvg_info->offset+=q->coordinates;
6665 q+=(ptrdiff_t) q->coordinates;
6666 point=end;
6667 while (isspace((int) ((unsigned char) *p)) != 0)
6668 p++;
6669 if (*p == ',')
6670 p++;
6671 } while (IsPoint(p) != MagickFalse);
6672 break;
6673 }
6674 case 'H':
6675 case 'h':
6676 {
6677 do
6678 {
6679 (void) GetNextToken(p,&p,MaxTextExtent,token);
6680 if (*token == ',')
6681 (void) GetNextToken(p,&p,MaxTextExtent,token);
6682 x=GetDrawValue(token,&next_token);
6683 if (token == next_token)
6684 ThrowPointExpectedException(image,token);
6685 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6686 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6687 return(-1);
6688 q=(*mvg_info->primitive_info)+mvg_info->offset;
6689 if (TracePoint(q,point) == MagickFalse)
6690 return(-1);
6691 mvg_info->offset+=q->coordinates;
6692 q+=(ptrdiff_t) q->coordinates;
6693 while (isspace((int) ((unsigned char) *p)) != 0)
6694 p++;
6695 if (*p == ',')
6696 p++;
6697 } while (IsPoint(p) != MagickFalse);
6698 break;
6699 }
6700 case 'l':
6701 case 'L':
6702 {
6703 /*
6704 Line to.
6705 */
6706 do
6707 {
6708 (void) GetNextToken(p,&p,MaxTextExtent,token);
6709 if (*token == ',')
6710 (void) GetNextToken(p,&p,MaxTextExtent,token);
6711 x=GetDrawValue(token,&next_token);
6712 if (token == next_token)
6713 ThrowPointExpectedException(image,token);
6714 (void) GetNextToken(p,&p,MaxTextExtent,token);
6715 if (*token == ',')
6716 (void) GetNextToken(p,&p,MaxTextExtent,token);
6717 y=GetDrawValue(token,&next_token);
6718 if (token == next_token)
6719 ThrowPointExpectedException(image,token);
6720 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
6721 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
6722 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6723 return(-1);
6724 q=(*mvg_info->primitive_info)+mvg_info->offset;
6725 if (TracePoint(q,point) == MagickFalse)
6726 return(-1);
6727 mvg_info->offset+=q->coordinates;
6728 q+=(ptrdiff_t) q->coordinates;
6729 while (isspace((int) ((unsigned char) *p)) != 0)
6730 p++;
6731 if (*p == ',')
6732 p++;
6733 } while (IsPoint(p) != MagickFalse);
6734 break;
6735 }
6736 case 'M':
6737 case 'm':
6738 {
6739 /*
6740 Move to.
6741 */
6742 if (mvg_info->offset != subpath_offset)
6743 {
6744 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6745 primitive_info->coordinates=(size_t) (q-primitive_info);
6746 number_coordinates+=primitive_info->coordinates;
6747 primitive_info=q;
6748 subpath_offset=mvg_info->offset;
6749 }
6750 i=0;
6751 do
6752 {
6753 (void) GetNextToken(p,&p,MaxTextExtent,token);
6754 if (*token == ',')
6755 (void) GetNextToken(p,&p,MaxTextExtent,token);
6756 x=GetDrawValue(token,&next_token);
6757 if (token == next_token)
6758 ThrowPointExpectedException(image,token);
6759 (void) GetNextToken(p,&p,MaxTextExtent,token);
6760 if (*token == ',')
6761 (void) GetNextToken(p,&p,MaxTextExtent,token);
6762 y=GetDrawValue(token,&next_token);
6763 if (token == next_token)
6764 ThrowPointExpectedException(image,token);
6765 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
6766 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
6767 if (i == 0)
6768 start=point;
6769 i++;
6770 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6771 return(-1);
6772 q=(*mvg_info->primitive_info)+mvg_info->offset;
6773 if (TracePoint(q,point) == MagickFalse)
6774 return(-1);
6775 mvg_info->offset+=q->coordinates;
6776 q+=(ptrdiff_t) q->coordinates;
6777 while (isspace((int) ((unsigned char) *p)) != 0)
6778 p++;
6779 if (*p == ',')
6780 p++;
6781 } while (IsPoint(p) != MagickFalse);
6782 break;
6783 }
6784 case 'q':
6785 case 'Q':
6786 {
6787 /*
6788 Quadratic Bézier curve.
6789 */
6790 do
6791 {
6792 points[0]=point;
6793 for (i=1; i < 3; i++)
6794 {
6795 (void) GetNextToken(p,&p,MaxTextExtent,token);
6796 if (*token == ',')
6797 (void) GetNextToken(p,&p,MaxTextExtent,token);
6798 x=GetDrawValue(token,&next_token);
6799 if (token == next_token)
6800 ThrowPointExpectedException(image,token);
6801 (void) GetNextToken(p,&p,MaxTextExtent,token);
6802 if (*token == ',')
6803 (void) GetNextToken(p,&p,MaxTextExtent,token);
6804 y=GetDrawValue(token,&next_token);
6805 if (token == next_token)
6806 ThrowPointExpectedException(image,token);
6807 if (*p == ',')
6808 p++;
6809 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
6810 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
6811 points[i]=end;
6812 }
6813 for (i=0; i < 3; i++)
6814 (q+i)->point=points[i];
6815 if (TraceBezier(mvg_info,3) == MagickFalse)
6816 return(-1);
6817 q=(*mvg_info->primitive_info)+mvg_info->offset;
6818 mvg_info->offset+=q->coordinates;
6819 q+=(ptrdiff_t) q->coordinates;
6820 point=end;
6821 while (isspace((int) ((unsigned char) *p)) != 0)
6822 p++;
6823 if (*p == ',')
6824 p++;
6825 } while (IsPoint(p) != MagickFalse);
6826 break;
6827 }
6828 case 's':
6829 case 'S':
6830 {
6831 /*
6832 Cubic Bézier curve.
6833 */
6834 do
6835 {
6836 points[0]=points[3];
6837 points[1].x=2.0*points[3].x-points[2].x;
6838 points[1].y=2.0*points[3].y-points[2].y;
6839 for (i=2; i < 4; i++)
6840 {
6841 (void) GetNextToken(p,&p,MaxTextExtent,token);
6842 if (*token == ',')
6843 (void) GetNextToken(p,&p,MaxTextExtent,token);
6844 x=GetDrawValue(token,&next_token);
6845 if (token == next_token)
6846 ThrowPointExpectedException(image,token);
6847 (void) GetNextToken(p,&p,MaxTextExtent,token);
6848 if (*token == ',')
6849 (void) GetNextToken(p,&p,MaxTextExtent,token);
6850 y=GetDrawValue(token,&next_token);
6851 if (token == next_token)
6852 ThrowPointExpectedException(image,token);
6853 if (*p == ',')
6854 p++;
6855 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
6856 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
6857 points[i]=end;
6858 }
6859 if (strchr("CcSs",last_attribute) == (char *) NULL)
6860 {
6861 points[0]=point;
6862 points[1]=point;
6863 }
6864 for (i=0; i < 4; i++)
6865 (q+i)->point=points[i];
6866 if (TraceBezier(mvg_info,4) == MagickFalse)
6867 return(-1);
6868 q=(*mvg_info->primitive_info)+mvg_info->offset;
6869 mvg_info->offset+=q->coordinates;
6870 q+=(ptrdiff_t) q->coordinates;
6871 point=end;
6872 last_attribute=attribute;
6873 while (isspace((int) ((unsigned char) *p)) != 0)
6874 p++;
6875 if (*p == ',')
6876 p++;
6877 } while (IsPoint(p) != MagickFalse);
6878 break;
6879 }
6880 case 't':
6881 case 'T':
6882 {
6883 /*
6884 Quadratic Bézier curve.
6885 */
6886 do
6887 {
6888 points[0]=points[2];
6889 points[1].x=2.0*points[2].x-points[1].x;
6890 points[1].y=2.0*points[2].y-points[1].y;
6891 for (i=2; i < 3; i++)
6892 {
6893 (void) GetNextToken(p,&p,MaxTextExtent,token);
6894 if (*token == ',')
6895 (void) GetNextToken(p,&p,MaxTextExtent,token);
6896 x=GetDrawValue(token,&next_token);
6897 if (token == next_token)
6898 ThrowPointExpectedException(image,token);
6899 (void) GetNextToken(p,&p,MaxTextExtent,token);
6900 if (*token == ',')
6901 (void) GetNextToken(p,&p,MaxTextExtent,token);
6902 y=GetDrawValue(token,&next_token);
6903 if (token == next_token)
6904 ThrowPointExpectedException(image,token);
6905 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
6906 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
6907 points[i]=end;
6908 }
6909 if (status == MagickFalse)
6910 break;
6911 if (strchr("QqTt",last_attribute) == (char *) NULL)
6912 {
6913 points[0]=point;
6914 points[1]=point;
6915 }
6916 for (i=0; i < 3; i++)
6917 (q+i)->point=points[i];
6918 if (TraceBezier(mvg_info,3) == MagickFalse)
6919 return(-1);
6920 q=(*mvg_info->primitive_info)+mvg_info->offset;
6921 mvg_info->offset+=q->coordinates;
6922 q+=(ptrdiff_t) q->coordinates;
6923 point=end;
6924 last_attribute=attribute;
6925 while (isspace((int) ((unsigned char) *p)) != 0)
6926 p++;
6927 if (*p == ',')
6928 p++;
6929 } while (IsPoint(p) != MagickFalse);
6930 break;
6931 }
6932 case 'v':
6933 case 'V':
6934 {
6935 /*
6936 Line to.
6937 */
6938 do
6939 {
6940 (void) GetNextToken(p,&p,MaxTextExtent,token);
6941 if (*token == ',')
6942 (void) GetNextToken(p,&p,MaxTextExtent,token);
6943 y=GetDrawValue(token,&next_token);
6944 if (token == next_token)
6945 ThrowPointExpectedException(image,token);
6946 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
6947 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6948 return(-1);
6949 q=(*mvg_info->primitive_info)+mvg_info->offset;
6950 if (TracePoint(q,point) == MagickFalse)
6951 return(-1);
6952 mvg_info->offset+=q->coordinates;
6953 q+=(ptrdiff_t) q->coordinates;
6954 while (isspace((int) ((unsigned char) *p)) != 0)
6955 p++;
6956 if (*p == ',')
6957 p++;
6958 } while (IsPoint(p) != MagickFalse);
6959 break;
6960 }
6961 case 'z':
6962 case 'Z':
6963 {
6964 /*
6965 Close path.
6966 */
6967 point=start;
6968 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6969 return(-1);
6970 q=(*mvg_info->primitive_info)+mvg_info->offset;
6971 if (TracePoint(q,point) == MagickFalse)
6972 return(-1);
6973 mvg_info->offset+=q->coordinates;
6974 q+=(ptrdiff_t) q->coordinates;
6975 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6976 primitive_info->coordinates=(size_t) (q-primitive_info);
6977 primitive_info->closed_subpath=MagickTrue;
6978 number_coordinates+=primitive_info->coordinates;
6979 primitive_info=q;
6980 subpath_offset=mvg_info->offset;
6981 z_count++;
6982 break;
6983 }
6984 default:
6985 {
6986 ThrowPointExpectedException(image,token);
6987 break;
6988 }
6989 }
6990 }
6991 if (status == MagickFalse)
6992 return(-1);
6993 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6994 primitive_info->coordinates=(size_t) (q-primitive_info);
6995 number_coordinates+=primitive_info->coordinates;
6996 for (i=0; i < (ssize_t) number_coordinates; i++)
6997 {
6998 q--;
6999 q->primitive=primitive_type;
7000 if (z_count > 1)
7001 q->method=FillToBorderMethod;
7002 }
7003 q=primitive_info;
7004 return((ssize_t) number_coordinates);
7005}
7006
7007static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7008 const PointInfo start,const PointInfo end)
7009{
7010 PointInfo
7011 point;
7012
7013 PrimitiveInfo
7014 *p;
7015
7016 ssize_t
7017 i;
7018
7019 p=primitive_info;
7020 if (TracePoint(p,start) == MagickFalse)
7021 return(MagickFalse);
7022 p+=(ptrdiff_t) p->coordinates;
7023 point.x=start.x;
7024 point.y=end.y;
7025 if (TracePoint(p,point) == MagickFalse)
7026 return(MagickFalse);
7027 p+=(ptrdiff_t) p->coordinates;
7028 if (TracePoint(p,end) == MagickFalse)
7029 return(MagickFalse);
7030 p+=(ptrdiff_t) p->coordinates;
7031 point.x=end.x;
7032 point.y=start.y;
7033 if (TracePoint(p,point) == MagickFalse)
7034 return(MagickFalse);
7035 p+=(ptrdiff_t) p->coordinates;
7036 if (TracePoint(p,start) == MagickFalse)
7037 return(MagickFalse);
7038 p+=(ptrdiff_t) p->coordinates;
7039 primitive_info->coordinates=(size_t) (p-primitive_info);
7040 primitive_info->closed_subpath=MagickTrue;
7041 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7042 {
7043 p->primitive=primitive_info->primitive;
7044 p--;
7045 }
7046 return(MagickTrue);
7047}
7048
7049static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7050 const PointInfo start,const PointInfo end,PointInfo arc)
7051{
7052 PointInfo
7053 degrees,
7054 point,
7055 segment;
7056
7057 PrimitiveInfo
7058 *primitive_info;
7059
7060 PrimitiveInfo
7061 *p;
7062
7063 ssize_t
7064 i;
7065
7066 ssize_t
7067 offset;
7068
7069 offset=mvg_info->offset;
7070 segment.x=fabs(end.x-start.x);
7071 segment.y=fabs(end.y-start.y);
7072 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7073 {
7074 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7075 return(MagickTrue);
7076 }
7077 if (arc.x > (0.5*segment.x))
7078 arc.x=0.5*segment.x;
7079 if (arc.y > (0.5*segment.y))
7080 arc.y=0.5*segment.y;
7081 point.x=start.x+segment.x-arc.x;
7082 point.y=start.y+arc.y;
7083 degrees.x=270.0;
7084 degrees.y=360.0;
7085 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7086 return(MagickFalse);
7087 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7088 mvg_info->offset+=p->coordinates;
7089 point.x=start.x+segment.x-arc.x;
7090 point.y=start.y+segment.y-arc.y;
7091 degrees.x=0.0;
7092 degrees.y=90.0;
7093 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7094 return(MagickFalse);
7095 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7096 mvg_info->offset+=p->coordinates;
7097 point.x=start.x+arc.x;
7098 point.y=start.y+segment.y-arc.y;
7099 degrees.x=90.0;
7100 degrees.y=180.0;
7101 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7102 return(MagickFalse);
7103 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7104 mvg_info->offset+=p->coordinates;
7105 point.x=start.x+arc.x;
7106 point.y=start.y+arc.y;
7107 degrees.x=180.0;
7108 degrees.y=270.0;
7109 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7110 return(MagickFalse);
7111 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7112 mvg_info->offset+=p->coordinates;
7113 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7114 return(MagickFalse);
7115 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7116 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7117 return(MagickFalse);
7118 p+=(ptrdiff_t) p->coordinates;
7119 mvg_info->offset=offset;
7120 primitive_info=(*mvg_info->primitive_info)+offset;
7121 primitive_info->coordinates=(size_t) (p-primitive_info);
7122 primitive_info->closed_subpath=MagickTrue;
7123 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7124 {
7125 p->primitive=primitive_info->primitive;
7126 p--;
7127 }
7128 return(MagickTrue);
7129}
7130
7131static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7132 const size_t number_vertices,const double offset)
7133{
7134 double
7135 distance;
7136
7137 double
7138 dx,
7139 dy;
7140
7141 ssize_t
7142 i;
7143
7144 ssize_t
7145 j;
7146
7147 dx=0.0;
7148 dy=0.0;
7149 for (i=1; i < (ssize_t) number_vertices; i++)
7150 {
7151 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7152 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7153 if ((fabs((double) dx) >= MagickEpsilon) ||
7154 (fabs((double) dy) >= MagickEpsilon))
7155 break;
7156 }
7157 if (i == (ssize_t) number_vertices)
7158 i=(ssize_t) number_vertices-1L;
7159 distance=hypot((double) dx,(double) dy);
7160 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7161 dx*(distance+offset)/distance);
7162 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7163 dy*(distance+offset)/distance);
7164 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7165 {
7166 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7167 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7168 if ((fabs((double) dx) >= MagickEpsilon) ||
7169 (fabs((double) dy) >= MagickEpsilon))
7170 break;
7171 }
7172 distance=hypot((double) dx,(double) dy);
7173 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7174 dx*(distance+offset)/distance);
7175 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7176 dy*(distance+offset)/distance);
7177 return(MagickTrue);
7178}
7179
7180static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7181 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7182{
7183#define MaxStrokePad (6*BezierQuantum+360)
7184#define CheckPathExtent(pad_p,pad_q) \
7185{ \
7186 if ((pad_p) > MaxBezierCoordinates) \
7187 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7188 else \
7189 if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7190 { \
7191 if (~extent_p < (pad_p)) \
7192 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7193 else \
7194 { \
7195 extent_p+=(pad_p); \
7196 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7197 MaxStrokePad,sizeof(*stroke_p)); \
7198 } \
7199 } \
7200 if ((pad_q) > MaxBezierCoordinates) \
7201 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7202 else \
7203 if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7204 { \
7205 if (~extent_q < (pad_q)) \
7206 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7207 else \
7208 { \
7209 extent_q+=(pad_q); \
7210 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7211 MaxStrokePad,sizeof(*stroke_q)); \
7212 } \
7213 } \
7214 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7215 { \
7216 if (stroke_p != (PointInfo *) NULL) \
7217 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7218 if (stroke_q != (PointInfo *) NULL) \
7219 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7220 polygon_primitive=(PrimitiveInfo *) \
7221 RelinquishMagickMemory(polygon_primitive); \
7222 (void) ThrowMagickException(exception,GetMagickModule(), \
7223 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7224 return((PrimitiveInfo *) NULL); \
7225 } \
7226}
7227
7228 typedef struct _StrokeSegment
7229 {
7230 double
7231 p,
7232 q;
7233 } StrokeSegment;
7234
7235 double
7236 delta_theta,
7237 dot_product,
7238 mid,
7239 miterlimit;
7240
7241 MagickBooleanType
7242 closed_path;
7243
7244 PointInfo
7245 box_p[5],
7246 box_q[5],
7247 center,
7248 offset,
7249 *stroke_p,
7250 *stroke_q;
7251
7252 PrimitiveInfo
7253 *polygon_primitive,
7254 *stroke_polygon;
7255
7256 ssize_t
7257 i;
7258
7259 size_t
7260 arc_segments,
7261 extent_p,
7262 extent_q,
7263 number_vertices;
7264
7265 ssize_t
7266 j,
7267 n,
7268 p,
7269 q;
7270
7271 StrokeSegment
7272 dx = {0.0, 0.0},
7273 dy = {0.0, 0.0},
7274 inverse_slope = {0.0, 0.0},
7275 slope = {0.0, 0.0},
7276 theta = {0.0, 0.0};
7277
7278 /*
7279 Allocate paths.
7280 */
7281 number_vertices=primitive_info->coordinates;
7282 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7283 number_vertices+2UL,sizeof(*polygon_primitive));
7284 if (polygon_primitive == (PrimitiveInfo *) NULL)
7285 {
7286 (void) ThrowMagickException(exception,GetMagickModule(),
7287 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7288 return((PrimitiveInfo *) NULL);
7289 }
7290 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7291 sizeof(*polygon_primitive));
7292 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7293 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7294 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7295 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7296 if ((draw_info->linejoin == MiterJoin) ||
7297 ((draw_info->linejoin == RoundJoin) && (closed_path != MagickFalse)))
7298 {
7299 polygon_primitive[number_vertices]=primitive_info[1];
7300 number_vertices++;
7301 }
7302 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7303 /*
7304 Compute the slope for the first line segment, p.
7305 */
7306 closed_path=primitive_info[0].closed_subpath;
7307 dx.p=0.0;
7308 dy.p=0.0;
7309 for (n=1; n < (ssize_t) number_vertices; n++)
7310 {
7311 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7312 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7313 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7314 break;
7315 }
7316 if (n == (ssize_t) number_vertices)
7317 {
7318 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7319 {
7320 /*
7321 Zero length subpath.
7322 */
7323 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7324 sizeof(*stroke_polygon));
7325 stroke_polygon[0]=polygon_primitive[0];
7326 stroke_polygon[0].coordinates=0;
7327 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7328 polygon_primitive);
7329 return(stroke_polygon);
7330 }
7331 n=(ssize_t) number_vertices-1L;
7332 }
7333 extent_p=2*number_vertices;
7334 extent_q=2*number_vertices;
7335 stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7336 sizeof(*stroke_p));
7337 stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7338 sizeof(*stroke_q));
7339 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7340 {
7341 if (stroke_p != (PointInfo *) NULL)
7342 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7343 if (stroke_q != (PointInfo *) NULL)
7344 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7345 polygon_primitive=(PrimitiveInfo *)
7346 RelinquishMagickMemory(polygon_primitive);
7347 (void) ThrowMagickException(exception,GetMagickModule(),
7348 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7349 return((PrimitiveInfo *) NULL);
7350 }
7351 slope.p=0.0;
7352 inverse_slope.p=0.0;
7353 if (fabs(dx.p) < MagickEpsilon)
7354 {
7355 if (dx.p >= 0.0)
7356 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7357 else
7358 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7359 }
7360 else
7361 if (fabs(dy.p) < MagickEpsilon)
7362 {
7363 if (dy.p >= 0.0)
7364 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7365 else
7366 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7367 }
7368 else
7369 {
7370 slope.p=dy.p/dx.p;
7371 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7372 }
7373 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7374 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7375 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7376 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7377 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7378 offset.y=(double) (offset.x*inverse_slope.p);
7379 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7380 {
7381 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7382 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7383 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7384 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7385 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7386 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7387 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7388 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7389 }
7390 else
7391 {
7392 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7393 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7394 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7395 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7396 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7397 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7398 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7399 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7400 }
7401 /*
7402 Create strokes for the line join attribute: bevel, miter, round.
7403 */
7404 p=0;
7405 q=0;
7406 stroke_q[p++]=box_q[0];
7407 stroke_p[q++]=box_p[0];
7408 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7409 {
7410 /*
7411 Compute the slope for this line segment, q.
7412 */
7413 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7414 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7415 dot_product=dx.q*dx.q+dy.q*dy.q;
7416 if (dot_product < 0.25)
7417 continue;
7418 slope.q=0.0;
7419 inverse_slope.q=0.0;
7420 if (fabs(dx.q) < MagickEpsilon)
7421 {
7422 if (dx.q >= 0.0)
7423 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7424 else
7425 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7426 }
7427 else
7428 if (fabs(dy.q) < MagickEpsilon)
7429 {
7430 if (dy.q >= 0.0)
7431 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7432 else
7433 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7434 }
7435 else
7436 {
7437 slope.q=dy.q/dx.q;
7438 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7439 }
7440 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7441 offset.y=(double) (offset.x*inverse_slope.q);
7442 dot_product=dy.q*offset.x-dx.q*offset.y;
7443 if (dot_product > 0.0)
7444 {
7445 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7446 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7447 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7448 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7449 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7450 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7451 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7452 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7453 }
7454 else
7455 {
7456 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7457 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7458 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7459 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7460 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7461 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7462 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7463 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7464 }
7465 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7466 {
7467 box_p[4]=box_p[1];
7468 box_q[4]=box_q[1];
7469 }
7470 else
7471 {
7472 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7473 box_p[3].y)/(slope.p-slope.q));
7474 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7475 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7476 box_q[3].y)/(slope.p-slope.q));
7477 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7478 }
7479 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7480 dot_product=dx.q*dy.p-dx.p*dy.q;
7481 if (dot_product <= 0.0)
7482 switch (draw_info->linejoin)
7483 {
7484 case BevelJoin:
7485 {
7486 stroke_q[q++]=box_q[1];
7487 stroke_q[q++]=box_q[2];
7488 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7489 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7490 if (dot_product <= miterlimit)
7491 stroke_p[p++]=box_p[4];
7492 else
7493 {
7494 stroke_p[p++]=box_p[1];
7495 stroke_p[p++]=box_p[2];
7496 }
7497 break;
7498 }
7499 case MiterJoin:
7500 {
7501 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7502 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7503 if (dot_product <= miterlimit)
7504 {
7505 stroke_q[q++]=box_q[4];
7506 stroke_p[p++]=box_p[4];
7507 }
7508 else
7509 {
7510 stroke_q[q++]=box_q[1];
7511 stroke_q[q++]=box_q[2];
7512 stroke_p[p++]=box_p[1];
7513 stroke_p[p++]=box_p[2];
7514 }
7515 break;
7516 }
7517 case RoundJoin:
7518 {
7519 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7520 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7521 if (dot_product <= miterlimit)
7522 stroke_p[p++]=box_p[4];
7523 else
7524 {
7525 stroke_p[p++]=box_p[1];
7526 stroke_p[p++]=box_p[2];
7527 }
7528 center=polygon_primitive[n].point;
7529 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7530 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7531 if (theta.q < theta.p)
7532 theta.q+=2.0*MagickPI;
7533 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.q-
7534 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7535 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7536 stroke_q[q].x=box_q[1].x;
7537 stroke_q[q].y=box_q[1].y;
7538 q++;
7539 for (j=1; j < (ssize_t) arc_segments; j++)
7540 {
7541 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7542 stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7543 (theta.p+delta_theta),DegreesToRadians(360.0))));
7544 stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7545 (theta.p+delta_theta),DegreesToRadians(360.0))));
7546 q++;
7547 }
7548 stroke_q[q++]=box_q[2];
7549 break;
7550 }
7551 default:
7552 break;
7553 }
7554 else
7555 switch (draw_info->linejoin)
7556 {
7557 case BevelJoin:
7558 {
7559 stroke_p[p++]=box_p[1];
7560 stroke_p[p++]=box_p[2];
7561 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7562 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7563 if (dot_product <= miterlimit)
7564 stroke_q[q++]=box_q[4];
7565 else
7566 {
7567 stroke_q[q++]=box_q[1];
7568 stroke_q[q++]=box_q[2];
7569 }
7570 break;
7571 }
7572 case MiterJoin:
7573 {
7574 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7575 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7576 if (dot_product <= miterlimit)
7577 {
7578 stroke_q[q++]=box_q[4];
7579 stroke_p[p++]=box_p[4];
7580 }
7581 else
7582 {
7583 stroke_q[q++]=box_q[1];
7584 stroke_q[q++]=box_q[2];
7585 stroke_p[p++]=box_p[1];
7586 stroke_p[p++]=box_p[2];
7587 }
7588 break;
7589 }
7590 case RoundJoin:
7591 {
7592 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7593 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7594 if (dot_product <= miterlimit)
7595 stroke_q[q++]=box_q[4];
7596 else
7597 {
7598 stroke_q[q++]=box_q[1];
7599 stroke_q[q++]=box_q[2];
7600 }
7601 center=polygon_primitive[n].point;
7602 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7603 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7604 if (theta.p < theta.q)
7605 theta.p+=2.0*MagickPI;
7606 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.p-
7607 theta.q)/(2.0*sqrt((double) (MagickSafeReciprocal(mid)))))));
7608 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7609 stroke_p[p++]=box_p[1];
7610 for (j=1; j < (ssize_t) arc_segments; j++)
7611 {
7612 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7613 stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7614 (theta.p+delta_theta),DegreesToRadians(360.0))));
7615 stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7616 (theta.p+delta_theta),DegreesToRadians(360.0))));
7617 p++;
7618 }
7619 stroke_p[p++]=box_p[2];
7620 break;
7621 }
7622 default:
7623 break;
7624 }
7625 slope.p=slope.q;
7626 inverse_slope.p=inverse_slope.q;
7627 box_p[0]=box_p[2];
7628 box_p[1]=box_p[3];
7629 box_q[0]=box_q[2];
7630 box_q[1]=box_q[3];
7631 dx.p=dx.q;
7632 dy.p=dy.q;
7633 n=i;
7634 }
7635 stroke_p[p++]=box_p[1];
7636 stroke_q[q++]=box_q[1];
7637 /*
7638 Trace stroked polygon.
7639 */
7640 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7641 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
7642 if (stroke_polygon == (PrimitiveInfo *) NULL)
7643 {
7644 (void) ThrowMagickException(exception,GetMagickModule(),
7645 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7646 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7647 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7648 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7649 polygon_primitive);
7650 return(stroke_polygon);
7651 }
7652 for (i=0; i < (ssize_t) p; i++)
7653 {
7654 stroke_polygon[i]=polygon_primitive[0];
7655 stroke_polygon[i].point=stroke_p[i];
7656 }
7657 if (closed_path != MagickFalse)
7658 {
7659 stroke_polygon[i]=polygon_primitive[0];
7660 stroke_polygon[i].point=stroke_polygon[0].point;
7661 i++;
7662 }
7663 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7664 {
7665 stroke_polygon[i]=polygon_primitive[0];
7666 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7667 }
7668 if (closed_path != MagickFalse)
7669 {
7670 stroke_polygon[i]=polygon_primitive[0];
7671 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7672 i++;
7673 }
7674 stroke_polygon[i]=polygon_primitive[0];
7675 stroke_polygon[i].point=stroke_polygon[0].point;
7676 i++;
7677 stroke_polygon[i].primitive=UndefinedPrimitive;
7678 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7679 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7680 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7681 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7682 return(stroke_polygon);
7683}