blob: b3ed8197e3f620e5884dd24f34ba08116ef62ba7 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% John Cristy %
18% July 1998 %
19% %
20% %
cristy7e41fe82010-12-04 23:12:08 +000021% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000022% 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% http://www.imagemagick.org/script/license.php %
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*/
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/studio.h"
49#include "MagickCore/annotate.h"
50#include "MagickCore/artifact.h"
51#include "MagickCore/blob.h"
52#include "MagickCore/cache.h"
53#include "MagickCore/cache-view.h"
54#include "MagickCore/color.h"
55#include "MagickCore/composite.h"
56#include "MagickCore/composite-private.h"
57#include "MagickCore/constitute.h"
58#include "MagickCore/draw.h"
59#include "MagickCore/draw-private.h"
60#include "MagickCore/enhance.h"
61#include "MagickCore/exception.h"
62#include "MagickCore/exception-private.h"
63#include "MagickCore/gem.h"
64#include "MagickCore/geometry.h"
65#include "MagickCore/image-private.h"
66#include "MagickCore/list.h"
67#include "MagickCore/log.h"
68#include "MagickCore/monitor.h"
69#include "MagickCore/monitor-private.h"
70#include "MagickCore/option.h"
71#include "MagickCore/paint.h"
72#include "MagickCore/pixel-accessor.h"
73#include "MagickCore/property.h"
74#include "MagickCore/resample.h"
75#include "MagickCore/resample-private.h"
76#include "MagickCore/string_.h"
77#include "MagickCore/string-private.h"
78#include "MagickCore/thread-private.h"
79#include "MagickCore/token.h"
80#include "MagickCore/transform.h"
81#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000082
83/*
84 Define declarations.
85*/
86#define BezierQuantum 200
87
88/*
89 Typedef declarations.
90*/
91typedef struct _EdgeInfo
92{
93 SegmentInfo
94 bounds;
95
96 MagickRealType
97 scanline;
98
99 PointInfo
100 *points;
101
cristybb503372010-05-27 20:51:26 +0000102 size_t
cristy3ed852e2009-09-05 21:47:34 +0000103 number_points;
104
cristybb503372010-05-27 20:51:26 +0000105 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000106 direction;
107
108 MagickBooleanType
109 ghostline;
110
cristybb503372010-05-27 20:51:26 +0000111 size_t
cristy3ed852e2009-09-05 21:47:34 +0000112 highwater;
113} EdgeInfo;
114
115typedef struct _ElementInfo
116{
117 MagickRealType
118 cx,
119 cy,
120 major,
121 minor,
122 angle;
123} ElementInfo;
124
125typedef struct _PolygonInfo
126{
127 EdgeInfo
128 *edges;
129
cristybb503372010-05-27 20:51:26 +0000130 size_t
cristy3ed852e2009-09-05 21:47:34 +0000131 number_edges;
132} PolygonInfo;
133
134typedef enum
135{
136 MoveToCode,
137 OpenCode,
138 GhostlineCode,
139 LineToCode,
140 EndCode
141} PathInfoCode;
142
143typedef struct _PathInfo
144{
145 PointInfo
146 point;
147
148 PathInfoCode
149 code;
150} PathInfo;
151
152/*
153 Forward declarations.
154*/
155static MagickBooleanType
cristy947cb4c2011-10-20 18:41:46 +0000156 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
157 ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000158
159static PrimitiveInfo
160 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
161
cristybb503372010-05-27 20:51:26 +0000162static size_t
cristy3ed852e2009-09-05 21:47:34 +0000163 TracePath(PrimitiveInfo *,const char *);
164
165static void
166 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
167 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
168 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
cristybb503372010-05-27 20:51:26 +0000169 TraceBezier(PrimitiveInfo *,const size_t),
cristy3ed852e2009-09-05 21:47:34 +0000170 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
171 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
172 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
173 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
174 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
175 PointInfo),
cristybb503372010-05-27 20:51:26 +0000176 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
cristy3ed852e2009-09-05 21:47:34 +0000177
178/*
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180% %
181% %
182% %
183% A c q u i r e D r a w I n f o %
184% %
185% %
186% %
187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188%
189% AcquireDrawInfo() returns a DrawInfo structure properly initialized.
190%
191% The format of the AcquireDrawInfo method is:
192%
193% DrawInfo *AcquireDrawInfo(void)
194%
195*/
196MagickExport DrawInfo *AcquireDrawInfo(void)
197{
198 DrawInfo
199 *draw_info;
200
cristy73bd4a52010-10-05 11:24:23 +0000201 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
cristy3ed852e2009-09-05 21:47:34 +0000202 if (draw_info == (DrawInfo *) NULL)
203 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
204 GetDrawInfo((ImageInfo *) NULL,draw_info);
205 return(draw_info);
206}
207
208/*
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210% %
211% %
212% %
213% C l o n e D r a w I n f o %
214% %
215% %
216% %
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218%
anthony36fe1752011-09-29 11:28:37 +0000219% CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
220% is specified, a new draw_info structure is created initialized to
221% default values, according to the given image_info.
cristy3ed852e2009-09-05 21:47:34 +0000222%
223% The format of the CloneDrawInfo method is:
224%
225% DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
226% const DrawInfo *draw_info)
227%
228% A description of each parameter follows:
229%
230% o image_info: the image info.
231%
232% o draw_info: the draw info.
233%
234*/
235MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
236 const DrawInfo *draw_info)
237{
238 DrawInfo
239 *clone_info;
240
cristy947cb4c2011-10-20 18:41:46 +0000241 ExceptionInfo
242 *exception;
243
cristy73bd4a52010-10-05 11:24:23 +0000244 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +0000245 if (clone_info == (DrawInfo *) NULL)
246 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
247 GetDrawInfo(image_info,clone_info);
248 if (draw_info == (DrawInfo *) NULL)
249 return(clone_info);
cristy947cb4c2011-10-20 18:41:46 +0000250 exception=AcquireExceptionInfo();
cristy3ed852e2009-09-05 21:47:34 +0000251 if (clone_info->primitive != (char *) NULL)
252 (void) CloneString(&clone_info->primitive,draw_info->primitive);
253 if (draw_info->geometry != (char *) NULL)
254 (void) CloneString(&clone_info->geometry,draw_info->geometry);
255 clone_info->viewbox=draw_info->viewbox;
256 clone_info->affine=draw_info->affine;
257 clone_info->gravity=draw_info->gravity;
258 clone_info->fill=draw_info->fill;
259 clone_info->stroke=draw_info->stroke;
260 clone_info->stroke_width=draw_info->stroke_width;
261 if (draw_info->fill_pattern != (Image *) NULL)
262 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
cristy947cb4c2011-10-20 18:41:46 +0000263 exception);
cristy3ed852e2009-09-05 21:47:34 +0000264 if (draw_info->stroke_pattern != (Image *) NULL)
265 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
cristy947cb4c2011-10-20 18:41:46 +0000266 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000267 clone_info->stroke_antialias=draw_info->stroke_antialias;
268 clone_info->text_antialias=draw_info->text_antialias;
269 clone_info->fill_rule=draw_info->fill_rule;
270 clone_info->linecap=draw_info->linecap;
271 clone_info->linejoin=draw_info->linejoin;
272 clone_info->miterlimit=draw_info->miterlimit;
273 clone_info->dash_offset=draw_info->dash_offset;
274 clone_info->decorate=draw_info->decorate;
275 clone_info->compose=draw_info->compose;
276 if (draw_info->text != (char *) NULL)
277 (void) CloneString(&clone_info->text,draw_info->text);
278 if (draw_info->font != (char *) NULL)
279 (void) CloneString(&clone_info->font,draw_info->font);
280 if (draw_info->metrics != (char *) NULL)
281 (void) CloneString(&clone_info->metrics,draw_info->metrics);
282 if (draw_info->family != (char *) NULL)
283 (void) CloneString(&clone_info->family,draw_info->family);
284 clone_info->style=draw_info->style;
285 clone_info->stretch=draw_info->stretch;
286 clone_info->weight=draw_info->weight;
287 if (draw_info->encoding != (char *) NULL)
288 (void) CloneString(&clone_info->encoding,draw_info->encoding);
289 clone_info->pointsize=draw_info->pointsize;
290 clone_info->kerning=draw_info->kerning;
cristyb32b90a2009-09-07 21:45:48 +0000291 clone_info->interline_spacing=draw_info->interline_spacing;
cristy3ed852e2009-09-05 21:47:34 +0000292 clone_info->interword_spacing=draw_info->interword_spacing;
cristyc9b12952010-03-28 01:12:28 +0000293 clone_info->direction=draw_info->direction;
cristy3ed852e2009-09-05 21:47:34 +0000294 if (draw_info->density != (char *) NULL)
295 (void) CloneString(&clone_info->density,draw_info->density);
296 clone_info->align=draw_info->align;
297 clone_info->undercolor=draw_info->undercolor;
298 clone_info->border_color=draw_info->border_color;
299 if (draw_info->server_name != (char *) NULL)
300 (void) CloneString(&clone_info->server_name,draw_info->server_name);
301 if (draw_info->dash_pattern != (double *) NULL)
302 {
cristybb503372010-05-27 20:51:26 +0000303 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000304 x;
305
306 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
307 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
308 sizeof(*clone_info->dash_pattern));
309 if (clone_info->dash_pattern == (double *) NULL)
310 ThrowFatalException(ResourceLimitFatalError,
311 "UnableToAllocateDashPattern");
312 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
313 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
314 }
315 clone_info->gradient=draw_info->gradient;
316 if (draw_info->gradient.stops != (StopInfo *) NULL)
317 {
cristybb503372010-05-27 20:51:26 +0000318 size_t
cristy3ed852e2009-09-05 21:47:34 +0000319 number_stops;
320
321 number_stops=clone_info->gradient.number_stops;
322 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
323 number_stops,sizeof(*clone_info->gradient.stops));
324 if (clone_info->gradient.stops == (StopInfo *) NULL)
325 ThrowFatalException(ResourceLimitFatalError,
326 "UnableToAllocateDashPattern");
327 (void) CopyMagickMemory(clone_info->gradient.stops,
328 draw_info->gradient.stops,(size_t) number_stops*
329 sizeof(*clone_info->gradient.stops));
330 }
331 if (draw_info->clip_mask != (char *) NULL)
332 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
333 clone_info->bounds=draw_info->bounds;
334 clone_info->clip_units=draw_info->clip_units;
335 clone_info->render=draw_info->render;
cristy4c08aed2011-07-01 19:47:50 +0000336 clone_info->alpha=draw_info->alpha;
cristy3ed852e2009-09-05 21:47:34 +0000337 clone_info->element_reference=draw_info->element_reference;
338 clone_info->debug=IsEventLogging();
cristy947cb4c2011-10-20 18:41:46 +0000339 exception=DestroyExceptionInfo(exception);
cristy3ed852e2009-09-05 21:47:34 +0000340 return(clone_info);
341}
342
343/*
344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345% %
346% %
347% %
348+ C o n v e r t P a t h T o P o l y g o n %
349% %
350% %
351% %
352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353%
354% ConvertPathToPolygon() converts a path to the more efficient sorted
355% rendering form.
356%
357% The format of the ConvertPathToPolygon method is:
358%
359% PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
360% const PathInfo *path_info)
361%
362% A description of each parameter follows:
363%
364% o Method ConvertPathToPolygon returns the path in a more efficient sorted
365% rendering form of type PolygonInfo.
366%
367% o draw_info: Specifies a pointer to an DrawInfo structure.
368%
369% o path_info: Specifies a pointer to an PathInfo structure.
370%
371%
372*/
373
374#if defined(__cplusplus) || defined(c_plusplus)
375extern "C" {
376#endif
377
378static int CompareEdges(const void *x,const void *y)
379{
380 register const EdgeInfo
381 *p,
382 *q;
383
384 /*
385 Compare two edges.
386 */
387 p=(const EdgeInfo *) x;
388 q=(const EdgeInfo *) y;
389 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
390 return(1);
391 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
392 return(-1);
393 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
394 return(1);
395 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
396 return(-1);
397 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
398 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
399 return(1);
400 return(-1);
401}
402
403#if defined(__cplusplus) || defined(c_plusplus)
404}
405#endif
406
407static void LogPolygonInfo(const PolygonInfo *polygon_info)
408{
409 register EdgeInfo
410 *p;
411
cristybb503372010-05-27 20:51:26 +0000412 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000413 i,
414 j;
415
416 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
417 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +0000418 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +0000419 {
cristye8c25f92010-06-03 00:53:06 +0000420 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
421 (double) i);
cristy3ed852e2009-09-05 21:47:34 +0000422 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
423 p->direction != MagickFalse ? "down" : "up");
424 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
425 p->ghostline != MagickFalse ? "transparent" : "opaque");
426 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +0000427 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
cristy8cd5b312010-01-07 01:10:24 +0000428 p->bounds.x2,p->bounds.y2);
cristybb503372010-05-27 20:51:26 +0000429 for (j=0; j < (ssize_t) p->number_points; j++)
cristy14388de2011-05-15 14:57:16 +0000430 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
cristy3ed852e2009-09-05 21:47:34 +0000431 p->points[j].x,p->points[j].y);
432 p++;
433 }
434 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
435}
436
cristybb503372010-05-27 20:51:26 +0000437static void ReversePoints(PointInfo *points,const size_t number_points)
cristy3ed852e2009-09-05 21:47:34 +0000438{
439 PointInfo
440 point;
441
cristybb503372010-05-27 20:51:26 +0000442 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000443 i;
444
cristybb503372010-05-27 20:51:26 +0000445 for (i=0; i < (ssize_t) (number_points >> 1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000446 {
447 point=points[i];
448 points[i]=points[number_points-(i+1)];
449 points[number_points-(i+1)]=point;
450 }
451}
452
453static PolygonInfo *ConvertPathToPolygon(
454 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
455{
cristycee97112010-05-28 00:44:52 +0000456 long
cristy3ed852e2009-09-05 21:47:34 +0000457 direction,
458 next_direction;
459
460 PointInfo
461 point,
462 *points;
463
464 PolygonInfo
465 *polygon_info;
466
467 SegmentInfo
468 bounds;
469
cristybb503372010-05-27 20:51:26 +0000470 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000471 i,
472 n;
473
474 MagickBooleanType
475 ghostline;
476
cristybb503372010-05-27 20:51:26 +0000477 size_t
cristy3ed852e2009-09-05 21:47:34 +0000478 edge,
479 number_edges,
480 number_points;
481
482 /*
483 Convert a path to the more efficient sorted rendering form.
484 */
cristy73bd4a52010-10-05 11:24:23 +0000485 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
cristy3ed852e2009-09-05 21:47:34 +0000486 if (polygon_info == (PolygonInfo *) NULL)
487 return((PolygonInfo *) NULL);
488 number_edges=16;
489 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
490 sizeof(*polygon_info->edges));
491 if (polygon_info->edges == (EdgeInfo *) NULL)
492 return((PolygonInfo *) NULL);
493 direction=0;
494 edge=0;
495 ghostline=MagickFalse;
496 n=0;
497 number_points=0;
498 points=(PointInfo *) NULL;
499 (void) ResetMagickMemory(&point,0,sizeof(point));
500 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
501 for (i=0; path_info[i].code != EndCode; i++)
502 {
503 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
504 (path_info[i].code == GhostlineCode))
505 {
506 /*
507 Move to.
508 */
509 if ((points != (PointInfo *) NULL) && (n >= 2))
510 {
511 if (edge == number_edges)
512 {
513 number_edges<<=1;
514 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
515 polygon_info->edges,(size_t) number_edges,
516 sizeof(*polygon_info->edges));
517 if (polygon_info->edges == (EdgeInfo *) NULL)
518 return((PolygonInfo *) NULL);
519 }
cristybb503372010-05-27 20:51:26 +0000520 polygon_info->edges[edge].number_points=(size_t) n;
cristy3ed852e2009-09-05 21:47:34 +0000521 polygon_info->edges[edge].scanline=(-1.0);
522 polygon_info->edges[edge].highwater=0;
523 polygon_info->edges[edge].ghostline=ghostline;
cristybb503372010-05-27 20:51:26 +0000524 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
cristy3ed852e2009-09-05 21:47:34 +0000525 if (direction < 0)
cristybb503372010-05-27 20:51:26 +0000526 ReversePoints(points,(size_t) n);
cristy3ed852e2009-09-05 21:47:34 +0000527 polygon_info->edges[edge].points=points;
528 polygon_info->edges[edge].bounds=bounds;
529 polygon_info->edges[edge].bounds.y1=points[0].y;
530 polygon_info->edges[edge].bounds.y2=points[n-1].y;
531 points=(PointInfo *) NULL;
532 ghostline=MagickFalse;
533 edge++;
534 }
535 if (points == (PointInfo *) NULL)
536 {
537 number_points=16;
538 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
539 sizeof(*points));
540 if (points == (PointInfo *) NULL)
541 return((PolygonInfo *) NULL);
542 }
543 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
544 point=path_info[i].point;
545 points[0]=point;
546 bounds.x1=point.x;
547 bounds.x2=point.x;
548 direction=0;
549 n=1;
550 continue;
551 }
552 /*
553 Line to.
554 */
555 next_direction=((path_info[i].point.y > point.y) ||
556 ((path_info[i].point.y == point.y) &&
557 (path_info[i].point.x > point.x))) ? 1 : -1;
558 if ((direction != 0) && (direction != next_direction))
559 {
560 /*
561 New edge.
562 */
563 point=points[n-1];
564 if (edge == number_edges)
565 {
566 number_edges<<=1;
567 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
568 polygon_info->edges,(size_t) number_edges,
569 sizeof(*polygon_info->edges));
570 if (polygon_info->edges == (EdgeInfo *) NULL)
571 return((PolygonInfo *) NULL);
572 }
cristybb503372010-05-27 20:51:26 +0000573 polygon_info->edges[edge].number_points=(size_t) n;
cristy3ed852e2009-09-05 21:47:34 +0000574 polygon_info->edges[edge].scanline=(-1.0);
575 polygon_info->edges[edge].highwater=0;
576 polygon_info->edges[edge].ghostline=ghostline;
cristybb503372010-05-27 20:51:26 +0000577 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
cristy3ed852e2009-09-05 21:47:34 +0000578 if (direction < 0)
cristybb503372010-05-27 20:51:26 +0000579 ReversePoints(points,(size_t) n);
cristy3ed852e2009-09-05 21:47:34 +0000580 polygon_info->edges[edge].points=points;
581 polygon_info->edges[edge].bounds=bounds;
582 polygon_info->edges[edge].bounds.y1=points[0].y;
583 polygon_info->edges[edge].bounds.y2=points[n-1].y;
584 number_points=16;
585 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
586 sizeof(*points));
587 if (points == (PointInfo *) NULL)
588 return((PolygonInfo *) NULL);
589 n=1;
590 ghostline=MagickFalse;
591 points[0]=point;
592 bounds.x1=point.x;
593 bounds.x2=point.x;
594 edge++;
595 }
596 direction=next_direction;
597 if (points == (PointInfo *) NULL)
598 continue;
cristybb503372010-05-27 20:51:26 +0000599 if (n == (ssize_t) number_points)
cristy3ed852e2009-09-05 21:47:34 +0000600 {
601 number_points<<=1;
602 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
603 sizeof(*points));
604 if (points == (PointInfo *) NULL)
605 return((PolygonInfo *) NULL);
606 }
607 point=path_info[i].point;
608 points[n]=point;
609 if (point.x < bounds.x1)
610 bounds.x1=point.x;
611 if (point.x > bounds.x2)
612 bounds.x2=point.x;
613 n++;
614 }
615 if (points != (PointInfo *) NULL)
616 {
617 if (n < 2)
618 points=(PointInfo *) RelinquishMagickMemory(points);
619 else
620 {
621 if (edge == number_edges)
622 {
623 number_edges<<=1;
624 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
625 polygon_info->edges,(size_t) number_edges,
626 sizeof(*polygon_info->edges));
627 if (polygon_info->edges == (EdgeInfo *) NULL)
628 return((PolygonInfo *) NULL);
629 }
cristybb503372010-05-27 20:51:26 +0000630 polygon_info->edges[edge].number_points=(size_t) n;
cristy3ed852e2009-09-05 21:47:34 +0000631 polygon_info->edges[edge].scanline=(-1.0);
632 polygon_info->edges[edge].highwater=0;
633 polygon_info->edges[edge].ghostline=ghostline;
cristybb503372010-05-27 20:51:26 +0000634 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
cristy3ed852e2009-09-05 21:47:34 +0000635 if (direction < 0)
cristybb503372010-05-27 20:51:26 +0000636 ReversePoints(points,(size_t) n);
cristy3ed852e2009-09-05 21:47:34 +0000637 polygon_info->edges[edge].points=points;
638 polygon_info->edges[edge].bounds=bounds;
639 polygon_info->edges[edge].bounds.y1=points[0].y;
640 polygon_info->edges[edge].bounds.y2=points[n-1].y;
641 ghostline=MagickFalse;
642 edge++;
643 }
644 }
645 polygon_info->number_edges=edge;
646 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
647 sizeof(*polygon_info->edges),CompareEdges);
648 if (IsEventLogging() != MagickFalse)
649 LogPolygonInfo(polygon_info);
650 return(polygon_info);
651}
652
653/*
654%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655% %
656% %
657% %
658+ C o n v e r t P r i m i t i v e T o P a t h %
659% %
660% %
661% %
662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663%
664% ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
665% path structure.
666%
667% The format of the ConvertPrimitiveToPath method is:
668%
669% PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
670% const PrimitiveInfo *primitive_info)
671%
672% A description of each parameter follows:
673%
674% o Method ConvertPrimitiveToPath returns a vector path structure of type
675% PathInfo.
676%
677% o draw_info: a structure of type DrawInfo.
678%
679% o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
680%
681%
682*/
683
684static void LogPathInfo(const PathInfo *path_info)
685{
686 register const PathInfo
687 *p;
688
689 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
690 for (p=path_info; p->code != EndCode; p++)
691 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +0000692 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
cristy3ed852e2009-09-05 21:47:34 +0000693 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
694 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
695 "?");
696 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
697}
698
699static PathInfo *ConvertPrimitiveToPath(
700 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
701{
cristy3ed852e2009-09-05 21:47:34 +0000702 PathInfo
703 *path_info;
704
705 PathInfoCode
706 code;
707
708 PointInfo
709 p,
710 q;
711
cristybb503372010-05-27 20:51:26 +0000712 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000713 i,
714 n;
715
cristy826a5472010-08-31 23:21:38 +0000716 ssize_t
717 coordinates,
718 start;
719
cristy3ed852e2009-09-05 21:47:34 +0000720 /*
721 Converts a PrimitiveInfo structure into a vector path structure.
722 */
723 switch (primitive_info->primitive)
724 {
725 case PointPrimitive:
726 case ColorPrimitive:
727 case MattePrimitive:
728 case TextPrimitive:
729 case ImagePrimitive:
730 return((PathInfo *) NULL);
731 default:
732 break;
733 }
734 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
735 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
736 sizeof(*path_info));
737 if (path_info == (PathInfo *) NULL)
738 return((PathInfo *) NULL);
739 coordinates=0;
740 n=0;
741 p.x=(-1.0);
742 p.y=(-1.0);
743 q.x=(-1.0);
744 q.y=(-1.0);
745 start=0;
746 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
747 {
748 code=LineToCode;
749 if (coordinates <= 0)
750 {
cristybb503372010-05-27 20:51:26 +0000751 coordinates=(ssize_t) primitive_info[i].coordinates;
cristy3ed852e2009-09-05 21:47:34 +0000752 p=primitive_info[i].point;
753 start=n;
754 code=MoveToCode;
755 }
756 coordinates--;
757 /*
758 Eliminate duplicate points.
759 */
760 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) > MagickEpsilon) ||
761 (fabs(q.y-primitive_info[i].point.y) > MagickEpsilon))
762 {
763 path_info[n].code=code;
764 path_info[n].point=primitive_info[i].point;
765 q=primitive_info[i].point;
766 n++;
767 }
768 if (coordinates > 0)
769 continue;
770 if ((fabs(p.x-primitive_info[i].point.x) <= MagickEpsilon) &&
771 (fabs(p.y-primitive_info[i].point.y) <= MagickEpsilon))
772 continue;
773 /*
774 Mark the p point as open if it does not match the q.
775 */
776 path_info[start].code=OpenCode;
777 path_info[n].code=GhostlineCode;
778 path_info[n].point=primitive_info[i].point;
779 n++;
780 path_info[n].code=LineToCode;
781 path_info[n].point=p;
782 n++;
783 }
784 path_info[n].code=EndCode;
785 path_info[n].point.x=0.0;
786 path_info[n].point.y=0.0;
787 if (IsEventLogging() != MagickFalse)
788 LogPathInfo(path_info);
789 return(path_info);
790}
791
792/*
793%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794% %
795% %
796% %
797% D e s t r o y D r a w I n f o %
798% %
799% %
800% %
801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802%
803% DestroyDrawInfo() deallocates memory associated with an DrawInfo
804% structure.
805%
806% The format of the DestroyDrawInfo method is:
807%
808% DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
809%
810% A description of each parameter follows:
811%
812% o draw_info: the draw info.
813%
814*/
815MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
816{
817 if (draw_info->debug != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
819 assert(draw_info != (DrawInfo *) NULL);
820 assert(draw_info->signature == MagickSignature);
821 if (draw_info->primitive != (char *) NULL)
822 draw_info->primitive=DestroyString(draw_info->primitive);
823 if (draw_info->text != (char *) NULL)
824 draw_info->text=DestroyString(draw_info->text);
825 if (draw_info->geometry != (char *) NULL)
826 draw_info->geometry=DestroyString(draw_info->geometry);
cristy3ed852e2009-09-05 21:47:34 +0000827 if (draw_info->fill_pattern != (Image *) NULL)
828 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
829 if (draw_info->stroke_pattern != (Image *) NULL)
830 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
831 if (draw_info->font != (char *) NULL)
832 draw_info->font=DestroyString(draw_info->font);
833 if (draw_info->metrics != (char *) NULL)
834 draw_info->metrics=DestroyString(draw_info->metrics);
835 if (draw_info->family != (char *) NULL)
836 draw_info->family=DestroyString(draw_info->family);
837 if (draw_info->encoding != (char *) NULL)
838 draw_info->encoding=DestroyString(draw_info->encoding);
839 if (draw_info->density != (char *) NULL)
840 draw_info->density=DestroyString(draw_info->density);
841 if (draw_info->server_name != (char *) NULL)
842 draw_info->server_name=(char *)
843 RelinquishMagickMemory(draw_info->server_name);
844 if (draw_info->dash_pattern != (double *) NULL)
845 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
846 draw_info->dash_pattern);
847 if (draw_info->gradient.stops != (StopInfo *) NULL)
848 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
849 draw_info->gradient.stops);
850 if (draw_info->clip_mask != (char *) NULL)
851 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
852 draw_info->signature=(~MagickSignature);
853 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
854 return(draw_info);
855}
856
857/*
858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859% %
860% %
861% %
862+ D e s t r o y E d g e %
863% %
864% %
865% %
866%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867%
868% DestroyEdge() destroys the specified polygon edge.
869%
870% The format of the DestroyEdge method is:
871%
cristybb503372010-05-27 20:51:26 +0000872% ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
cristy3ed852e2009-09-05 21:47:34 +0000873%
874% A description of each parameter follows:
875%
876% o polygon_info: Specifies a pointer to an PolygonInfo structure.
877%
878% o edge: the polygon edge number to destroy.
879%
880*/
cristybb503372010-05-27 20:51:26 +0000881static size_t DestroyEdge(PolygonInfo *polygon_info,
882 const size_t edge)
cristy3ed852e2009-09-05 21:47:34 +0000883{
884 assert(edge < polygon_info->number_edges);
885 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
886 polygon_info->edges[edge].points);
887 polygon_info->number_edges--;
888 if (edge < polygon_info->number_edges)
889 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
890 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
891 return(polygon_info->number_edges);
892}
893
894/*
895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896% %
897% %
898% %
899+ D e s t r o y P o l y g o n I n f o %
900% %
901% %
902% %
903%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904%
905% DestroyPolygonInfo() destroys the PolygonInfo data structure.
906%
907% The format of the DestroyPolygonInfo method is:
908%
909% PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
910%
911% A description of each parameter follows:
912%
913% o polygon_info: Specifies a pointer to an PolygonInfo structure.
914%
915*/
916static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
917{
cristybb503372010-05-27 20:51:26 +0000918 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000919 i;
920
cristybb503372010-05-27 20:51:26 +0000921 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +0000922 polygon_info->edges[i].points=(PointInfo *)
923 RelinquishMagickMemory(polygon_info->edges[i].points);
924 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
925 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
926}
927
928/*
929%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930% %
931% %
932% %
933% D r a w A f f i n e I m a g e %
934% %
935% %
936% %
937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938%
939% DrawAffineImage() composites the source over the destination image as
940% dictated by the affine transform.
941%
942% The format of the DrawAffineImage method is:
943%
944% MagickBooleanType DrawAffineImage(Image *image,const Image *source,
cristy947cb4c2011-10-20 18:41:46 +0000945% const AffineMatrix *affine,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000946%
947% A description of each parameter follows:
948%
949% o image: the image.
950%
951% o source: the source image.
952%
953% o affine: the affine transform.
954%
cristy947cb4c2011-10-20 18:41:46 +0000955% o exception: return any errors or warnings in this structure.
956%
cristy3ed852e2009-09-05 21:47:34 +0000957*/
958static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
959 const double y,const SegmentInfo *edge)
960{
961 double
962 intercept,
963 z;
964
965 register double
966 x;
967
968 SegmentInfo
969 inverse_edge;
970
971 /*
972 Determine left and right edges.
973 */
974 inverse_edge.x1=edge->x1;
975 inverse_edge.y1=edge->y1;
976 inverse_edge.x2=edge->x2;
977 inverse_edge.y2=edge->y2;
978 z=affine->ry*y+affine->tx;
979 if (affine->sx > MagickEpsilon)
980 {
981 intercept=(-z/affine->sx);
982 x=intercept+MagickEpsilon;
983 if (x > inverse_edge.x1)
984 inverse_edge.x1=x;
985 intercept=(-z+(double) image->columns)/affine->sx;
986 x=intercept-MagickEpsilon;
987 if (x < inverse_edge.x2)
988 inverse_edge.x2=x;
989 }
990 else
991 if (affine->sx < -MagickEpsilon)
992 {
993 intercept=(-z+(double) image->columns)/affine->sx;
994 x=intercept+MagickEpsilon;
995 if (x > inverse_edge.x1)
996 inverse_edge.x1=x;
997 intercept=(-z/affine->sx);
998 x=intercept-MagickEpsilon;
999 if (x < inverse_edge.x2)
1000 inverse_edge.x2=x;
1001 }
1002 else
cristybb503372010-05-27 20:51:26 +00001003 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
cristy3ed852e2009-09-05 21:47:34 +00001004 {
1005 inverse_edge.x2=edge->x1;
1006 return(inverse_edge);
1007 }
1008 /*
1009 Determine top and bottom edges.
1010 */
1011 z=affine->sy*y+affine->ty;
1012 if (affine->rx > MagickEpsilon)
1013 {
1014 intercept=(-z/affine->rx);
1015 x=intercept+MagickEpsilon;
1016 if (x > inverse_edge.x1)
1017 inverse_edge.x1=x;
1018 intercept=(-z+(double) image->rows)/affine->rx;
1019 x=intercept-MagickEpsilon;
1020 if (x < inverse_edge.x2)
1021 inverse_edge.x2=x;
1022 }
1023 else
1024 if (affine->rx < -MagickEpsilon)
1025 {
1026 intercept=(-z+(double) image->rows)/affine->rx;
1027 x=intercept+MagickEpsilon;
1028 if (x > inverse_edge.x1)
1029 inverse_edge.x1=x;
1030 intercept=(-z/affine->rx);
1031 x=intercept-MagickEpsilon;
1032 if (x < inverse_edge.x2)
1033 inverse_edge.x2=x;
1034 }
1035 else
cristybb503372010-05-27 20:51:26 +00001036 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001037 {
1038 inverse_edge.x2=edge->x2;
1039 return(inverse_edge);
1040 }
1041 return(inverse_edge);
1042}
1043
1044static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1045{
1046 AffineMatrix
1047 inverse_affine;
1048
1049 double
1050 determinant;
1051
1052 determinant=1.0/(affine->sx*affine->sy-affine->rx*affine->ry);
1053 inverse_affine.sx=determinant*affine->sy;
1054 inverse_affine.rx=determinant*(-affine->rx);
1055 inverse_affine.ry=determinant*(-affine->ry);
1056 inverse_affine.sy=determinant*affine->sx;
1057 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1058 inverse_affine.ry;
1059 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1060 inverse_affine.sy;
1061 return(inverse_affine);
1062}
1063
cristybb503372010-05-27 20:51:26 +00001064static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +00001065{
1066 if (x < 0)
1067 return(-x);
1068 return(x);
1069}
1070
1071static inline double MagickMax(const double x,const double y)
1072{
1073 if (x > y)
1074 return(x);
1075 return(y);
1076}
1077
1078static inline double MagickMin(const double x,const double y)
1079{
1080 if (x < y)
1081 return(x);
1082 return(y);
1083}
1084
1085MagickExport MagickBooleanType DrawAffineImage(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00001086 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001087{
1088 AffineMatrix
1089 inverse_affine;
1090
cristyfa112112010-01-04 17:48:07 +00001091 CacheView
1092 *image_view,
1093 *source_view;
1094
cristy3ed852e2009-09-05 21:47:34 +00001095 MagickBooleanType
1096 status;
1097
cristy4c08aed2011-07-01 19:47:50 +00001098 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001099 zero;
1100
1101 PointInfo
1102 extent[4],
1103 min,
1104 max,
1105 point;
1106
cristybb503372010-05-27 20:51:26 +00001107 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001108 i;
1109
cristy3ed852e2009-09-05 21:47:34 +00001110 SegmentInfo
1111 edge;
1112
cristy826a5472010-08-31 23:21:38 +00001113 ssize_t
1114 y;
1115
cristy3ed852e2009-09-05 21:47:34 +00001116 /*
1117 Determine bounding box.
1118 */
1119 assert(image != (Image *) NULL);
1120 assert(image->signature == MagickSignature);
1121 if (image->debug != MagickFalse)
1122 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1123 assert(source != (const Image *) NULL);
1124 assert(source->signature == MagickSignature);
1125 assert(affine != (AffineMatrix *) NULL);
1126 extent[0].x=0.0;
1127 extent[0].y=0.0;
1128 extent[1].x=(double) source->columns-1.0;
1129 extent[1].y=0.0;
1130 extent[2].x=(double) source->columns-1.0;
1131 extent[2].y=(double) source->rows-1.0;
1132 extent[3].x=0.0;
1133 extent[3].y=(double) source->rows-1.0;
1134 for (i=0; i < 4; i++)
1135 {
1136 point=extent[i];
1137 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1138 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1139 }
1140 min=extent[0];
1141 max=extent[0];
1142 for (i=1; i < 4; i++)
1143 {
1144 if (min.x > extent[i].x)
1145 min.x=extent[i].x;
1146 if (min.y > extent[i].y)
1147 min.y=extent[i].y;
1148 if (max.x < extent[i].x)
1149 max.x=extent[i].x;
1150 if (max.y < extent[i].y)
1151 max.y=extent[i].y;
1152 }
1153 /*
1154 Affine transform image.
1155 */
cristy574cc262011-08-05 01:23:58 +00001156 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001157 return(MagickFalse);
1158 status=MagickTrue;
1159 edge.x1=MagickMax(min.x,0.0);
1160 edge.y1=MagickMax(min.y,0.0);
1161 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1162 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1163 inverse_affine=InverseAffineMatrix(affine);
cristy4c08aed2011-07-01 19:47:50 +00001164 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +00001165 image_view=AcquireCacheView(image);
1166 source_view=AcquireCacheView(source);
cristyb5d5f722009-11-04 03:03:49 +00001167#if defined(MAGICKCORE_OPENMP_SUPPORT)
1168 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00001169#endif
cristybb503372010-05-27 20:51:26 +00001170 for (y=(ssize_t) ceil(edge.y1-0.5); y <= (ssize_t) floor(edge.y2+0.5); y++)
cristy3ed852e2009-09-05 21:47:34 +00001171 {
cristy4c08aed2011-07-01 19:47:50 +00001172 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001173 composite,
1174 pixel;
1175
1176 PointInfo
1177 point;
1178
cristybb503372010-05-27 20:51:26 +00001179 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001180 x;
1181
cristy4c08aed2011-07-01 19:47:50 +00001182 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001183 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001184
1185 SegmentInfo
1186 inverse_edge;
1187
cristy826a5472010-08-31 23:21:38 +00001188 ssize_t
1189 x_offset;
1190
cristy3ed852e2009-09-05 21:47:34 +00001191 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1192 if (inverse_edge.x2 < inverse_edge.x1)
1193 continue;
cristy6ebe97c2010-07-03 01:17:28 +00001194 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1195 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
cristy06609ee2010-03-17 20:21:27 +00001196 inverse_edge.x1+0.5)+1),1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001197 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001198 continue;
cristy3ed852e2009-09-05 21:47:34 +00001199 pixel=zero;
1200 composite=zero;
1201 x_offset=0;
cristybb503372010-05-27 20:51:26 +00001202 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
cristy3ed852e2009-09-05 21:47:34 +00001203 {
1204 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1205 inverse_affine.tx;
1206 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1207 inverse_affine.ty;
cristy4c08aed2011-07-01 19:47:50 +00001208 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1209 point.x,point.y,&pixel,exception);
1210 SetPixelInfo(image,q,&composite);
1211 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1212 &composite);
1213 SetPixelPixelInfo(image,&composite,q);
cristy3ed852e2009-09-05 21:47:34 +00001214 x_offset++;
cristyed231572011-07-14 02:18:59 +00001215 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001216 }
1217 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1218 status=MagickFalse;
1219 }
cristy3ed852e2009-09-05 21:47:34 +00001220 source_view=DestroyCacheView(source_view);
1221 image_view=DestroyCacheView(image_view);
1222 return(status);
1223}
1224
1225/*
1226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227% %
1228% %
1229% %
1230+ D r a w B o u n d i n g R e c t a n g l e s %
1231% %
1232% %
1233% %
1234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1235%
1236% DrawBoundingRectangles() draws the bounding rectangles on the image. This
1237% is only useful for developers debugging the rendering algorithm.
1238%
1239% The format of the DrawBoundingRectangles method is:
1240%
1241% void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001242% PolygonInfo *polygon_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001243%
1244% A description of each parameter follows:
1245%
1246% o image: the image.
1247%
1248% o draw_info: the draw info.
1249%
1250% o polygon_info: Specifies a pointer to a PolygonInfo structure.
1251%
cristy947cb4c2011-10-20 18:41:46 +00001252% o exception: return any errors or warnings in this structure.
1253%
cristy3ed852e2009-09-05 21:47:34 +00001254*/
1255static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001256 const PolygonInfo *polygon_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001257{
1258 DrawInfo
1259 *clone_info;
1260
cristy3ed852e2009-09-05 21:47:34 +00001261 MagickRealType
1262 mid;
1263
1264 PointInfo
1265 end,
1266 resolution,
1267 start;
1268
1269 PrimitiveInfo
1270 primitive_info[6];
1271
cristybb503372010-05-27 20:51:26 +00001272 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001273 i;
1274
1275 SegmentInfo
1276 bounds;
1277
cristy826a5472010-08-31 23:21:38 +00001278 ssize_t
1279 coordinates;
1280
cristy3ed852e2009-09-05 21:47:34 +00001281 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
cristy9950d572011-10-01 18:22:35 +00001282 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
cristy947cb4c2011-10-20 18:41:46 +00001283 exception);
cristy3ed852e2009-09-05 21:47:34 +00001284 resolution.x=DefaultResolution;
1285 resolution.y=DefaultResolution;
1286 if (clone_info->density != (char *) NULL)
1287 {
1288 GeometryInfo
1289 geometry_info;
1290
1291 MagickStatusType
1292 flags;
1293
1294 flags=ParseGeometry(clone_info->density,&geometry_info);
1295 resolution.x=geometry_info.rho;
1296 resolution.y=geometry_info.sigma;
1297 if ((flags & SigmaValue) == MagickFalse)
1298 resolution.y=resolution.x;
1299 }
1300 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1301 clone_info->stroke_width/2.0;
1302 bounds.x1=0.0;
1303 bounds.y1=0.0;
1304 bounds.x2=0.0;
1305 bounds.y2=0.0;
1306 if (polygon_info != (PolygonInfo *) NULL)
1307 {
1308 bounds=polygon_info->edges[0].bounds;
cristybb503372010-05-27 20:51:26 +00001309 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00001310 {
1311 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1312 bounds.x1=polygon_info->edges[i].bounds.x1;
1313 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1314 bounds.y1=polygon_info->edges[i].bounds.y1;
1315 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1316 bounds.x2=polygon_info->edges[i].bounds.x2;
1317 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1318 bounds.y2=polygon_info->edges[i].bounds.y2;
1319 }
1320 bounds.x1-=mid;
1321 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1322 image->columns ? (double) image->columns-1 : bounds.x1;
1323 bounds.y1-=mid;
1324 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1325 image->rows ? (double) image->rows-1 : bounds.y1;
1326 bounds.x2+=mid;
1327 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1328 image->columns ? (double) image->columns-1 : bounds.x2;
1329 bounds.y2+=mid;
1330 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1331 image->rows ? (double) image->rows-1 : bounds.y2;
cristybb503372010-05-27 20:51:26 +00001332 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00001333 {
1334 if (polygon_info->edges[i].direction != 0)
cristy9950d572011-10-01 18:22:35 +00001335 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
cristy947cb4c2011-10-20 18:41:46 +00001336 exception);
cristy3ed852e2009-09-05 21:47:34 +00001337 else
cristy9950d572011-10-01 18:22:35 +00001338 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
cristy947cb4c2011-10-20 18:41:46 +00001339 exception);
cristy3ed852e2009-09-05 21:47:34 +00001340 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1341 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1342 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1343 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1344 primitive_info[0].primitive=RectanglePrimitive;
1345 TraceRectangle(primitive_info,start,end);
1346 primitive_info[0].method=ReplaceMethod;
cristybb503372010-05-27 20:51:26 +00001347 coordinates=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00001348 primitive_info[coordinates].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00001349 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001350 }
1351 }
cristy9950d572011-10-01 18:22:35 +00001352 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
cristy947cb4c2011-10-20 18:41:46 +00001353 exception);
cristy3ed852e2009-09-05 21:47:34 +00001354 start.x=(double) (bounds.x1-mid);
1355 start.y=(double) (bounds.y1-mid);
1356 end.x=(double) (bounds.x2+mid);
1357 end.y=(double) (bounds.y2+mid);
1358 primitive_info[0].primitive=RectanglePrimitive;
1359 TraceRectangle(primitive_info,start,end);
1360 primitive_info[0].method=ReplaceMethod;
cristybb503372010-05-27 20:51:26 +00001361 coordinates=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00001362 primitive_info[coordinates].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00001363 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001364 clone_info=DestroyDrawInfo(clone_info);
1365}
1366
1367/*
1368%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369% %
1370% %
1371% %
1372% D r a w C l i p P a t h %
1373% %
1374% %
1375% %
1376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377%
1378% DrawClipPath() draws the clip path on the image mask.
1379%
1380% The format of the DrawClipPath method is:
1381%
1382% MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
cristy018f07f2011-09-04 21:15:19 +00001383% const char *name,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001384%
1385% A description of each parameter follows:
1386%
1387% o image: the image.
1388%
1389% o draw_info: the draw info.
1390%
1391% o name: the name of the clip path.
1392%
cristy018f07f2011-09-04 21:15:19 +00001393% o exception: return any errors or warnings in this structure.
1394%
cristy3ed852e2009-09-05 21:47:34 +00001395*/
1396MagickExport MagickBooleanType DrawClipPath(Image *image,
cristy018f07f2011-09-04 21:15:19 +00001397 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001398{
1399 char
1400 clip_mask[MaxTextExtent];
1401
1402 const char
1403 *value;
1404
1405 DrawInfo
1406 *clone_info;
1407
1408 MagickStatusType
1409 status;
1410
1411 assert(image != (Image *) NULL);
1412 assert(image->signature == MagickSignature);
1413 if (image->debug != MagickFalse)
1414 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1415 assert(draw_info != (const DrawInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +00001416 (void) FormatLocaleString(clip_mask,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00001417 value=GetImageArtifact(image,clip_mask);
1418 if (value == (const char *) NULL)
1419 return(MagickFalse);
1420 if (image->clip_mask == (Image *) NULL)
1421 {
1422 Image
1423 *clip_mask;
1424
1425 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,
cristyea1a8aa2011-10-20 13:24:06 +00001426 exception);
cristy3ed852e2009-09-05 21:47:34 +00001427 if (clip_mask == (Image *) NULL)
1428 return(MagickFalse);
cristy018f07f2011-09-04 21:15:19 +00001429 (void) SetImageClipMask(image,clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001430 clip_mask=DestroyImage(clip_mask);
1431 }
cristy9950d572011-10-01 18:22:35 +00001432 (void) QueryColorCompliance("#00000000",AllCompliance,
cristyea1a8aa2011-10-20 13:24:06 +00001433 &image->clip_mask->background_color,exception);
cristy4c08aed2011-07-01 19:47:50 +00001434 image->clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
cristyea1a8aa2011-10-20 13:24:06 +00001435 (void) SetImageBackgroundColor(image->clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001436 if (image->debug != MagickFalse)
1437 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1438 draw_info->clip_mask);
1439 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1440 (void) CloneString(&clone_info->primitive,value);
cristy9950d572011-10-01 18:22:35 +00001441 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
cristyea1a8aa2011-10-20 13:24:06 +00001442 exception);
cristy3ed852e2009-09-05 21:47:34 +00001443 clone_info->clip_mask=(char *) NULL;
cristy018f07f2011-09-04 21:15:19 +00001444 status=DrawImage(image->clip_mask,clone_info,exception);
cristyea1a8aa2011-10-20 13:24:06 +00001445 status|=NegateImage(image->clip_mask,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001446 clone_info=DestroyDrawInfo(clone_info);
1447 if (image->debug != MagickFalse)
1448 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1449 return(status != 0 ? MagickTrue : MagickFalse);
1450}
1451
1452/*
1453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454% %
1455% %
1456% %
1457+ D r a w D a s h P o l y g o n %
1458% %
1459% %
1460% %
1461%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462%
1463% DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1464% image while respecting the dash offset and dash pattern attributes.
1465%
1466% The format of the DrawDashPolygon method is:
1467%
1468% MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001469% const PrimitiveInfo *primitive_info,Image *image,
1470% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001471%
1472% A description of each parameter follows:
1473%
1474% o draw_info: the draw info.
1475%
1476% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1477%
1478% o image: the image.
1479%
cristy947cb4c2011-10-20 18:41:46 +00001480% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00001481%
1482*/
1483static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001484 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001485{
1486 DrawInfo
1487 *clone_info;
1488
cristy3ed852e2009-09-05 21:47:34 +00001489 MagickRealType
1490 length,
1491 maximum_length,
1492 offset,
1493 scale,
1494 total_length;
1495
1496 MagickStatusType
1497 status;
1498
1499 PrimitiveInfo
1500 *dash_polygon;
1501
cristybb503372010-05-27 20:51:26 +00001502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001503 i;
1504
1505 register MagickRealType
1506 dx,
1507 dy;
1508
cristybb503372010-05-27 20:51:26 +00001509 size_t
cristy3ed852e2009-09-05 21:47:34 +00001510 number_vertices;
1511
cristy826a5472010-08-31 23:21:38 +00001512 ssize_t
1513 j,
1514 n;
1515
cristy3ed852e2009-09-05 21:47:34 +00001516 assert(draw_info != (const DrawInfo *) NULL);
1517 if (image->debug != MagickFalse)
1518 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1519 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1520 clone_info->miterlimit=0;
1521 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
cristybb503372010-05-27 20:51:26 +00001522 number_vertices=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00001523 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1524 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1525 if (dash_polygon == (PrimitiveInfo *) NULL)
1526 return(MagickFalse);
1527 dash_polygon[0]=primitive_info[0];
1528 scale=ExpandAffine(&draw_info->affine);
1529 length=scale*(draw_info->dash_pattern[0]-0.5);
1530 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1531 j=1;
1532 for (n=0; offset > 0.0; j=0)
1533 {
1534 if (draw_info->dash_pattern[n] <= 0.0)
1535 break;
1536 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1537 if (offset > length)
1538 {
1539 offset-=length;
1540 n++;
1541 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1542 continue;
1543 }
1544 if (offset < length)
1545 {
1546 length-=offset;
1547 offset=0.0;
1548 break;
1549 }
1550 offset=0.0;
1551 n++;
1552 }
1553 status=MagickTrue;
1554 maximum_length=0.0;
1555 total_length=0.0;
cristybb503372010-05-27 20:51:26 +00001556 for (i=1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1559 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1560 maximum_length=hypot((double) dx,dy);
1561 if (length == 0.0)
1562 {
1563 n++;
1564 if (draw_info->dash_pattern[n] == 0.0)
1565 n=0;
1566 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1567 }
1568 for (total_length=0.0; (total_length+length) < maximum_length; )
1569 {
1570 total_length+=length;
1571 if ((n & 0x01) != 0)
1572 {
1573 dash_polygon[0]=primitive_info[0];
1574 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1575 total_length/maximum_length);
1576 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1577 total_length/maximum_length);
1578 j=1;
1579 }
1580 else
1581 {
cristybb503372010-05-27 20:51:26 +00001582 if ((j+1) > (ssize_t) (2*number_vertices))
cristy3ed852e2009-09-05 21:47:34 +00001583 break;
1584 dash_polygon[j]=primitive_info[i-1];
1585 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1586 total_length/maximum_length);
1587 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1588 total_length/maximum_length);
1589 dash_polygon[j].coordinates=1;
1590 j++;
cristybb503372010-05-27 20:51:26 +00001591 dash_polygon[0].coordinates=(size_t) j;
cristy3ed852e2009-09-05 21:47:34 +00001592 dash_polygon[j].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00001593 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
cristy3ed852e2009-09-05 21:47:34 +00001594 }
1595 n++;
1596 if (draw_info->dash_pattern[n] == 0.0)
1597 n=0;
1598 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1599 }
1600 length-=(maximum_length-total_length);
1601 if ((n & 0x01) != 0)
1602 continue;
1603 dash_polygon[j]=primitive_info[i];
1604 dash_polygon[j].coordinates=1;
1605 j++;
1606 }
1607 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1608 {
1609 dash_polygon[j]=primitive_info[i-1];
1610 dash_polygon[j].point.x+=MagickEpsilon;
1611 dash_polygon[j].point.y+=MagickEpsilon;
1612 dash_polygon[j].coordinates=1;
1613 j++;
cristybb503372010-05-27 20:51:26 +00001614 dash_polygon[0].coordinates=(size_t) j;
cristy3ed852e2009-09-05 21:47:34 +00001615 dash_polygon[j].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00001616 status|=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
cristy3ed852e2009-09-05 21:47:34 +00001617 }
1618 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1619 clone_info=DestroyDrawInfo(clone_info);
1620 if (image->debug != MagickFalse)
1621 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1622 return(status != 0 ? MagickTrue : MagickFalse);
1623}
1624
1625/*
1626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627% %
1628% %
1629% %
1630% D r a w I m a g e %
1631% %
1632% %
1633% %
1634%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1635%
1636% DrawImage() draws a graphic primitive on your image. The primitive
1637% may be represented as a string or filename. Precede the filename with an
1638% "at" sign (@) and the contents of the file are drawn on the image. You
1639% can affect how text is drawn by setting one or more members of the draw
1640% info structure.
1641%
1642% The format of the DrawImage method is:
1643%
cristy018f07f2011-09-04 21:15:19 +00001644% MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1645% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001646%
1647% A description of each parameter follows:
1648%
1649% o image: the image.
1650%
1651% o draw_info: the draw info.
1652%
cristy018f07f2011-09-04 21:15:19 +00001653% o exception: return any errors or warnings in this structure.
1654%
cristy3ed852e2009-09-05 21:47:34 +00001655*/
1656
1657static inline MagickBooleanType IsPoint(const char *point)
1658{
1659 char
1660 *p;
1661
1662 double
1663 value;
1664
cristyc1acd842011-05-19 23:05:47 +00001665 value=InterpretLocaleValue(point,&p);
cristy3ed852e2009-09-05 21:47:34 +00001666 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1667}
1668
1669static inline void TracePoint(PrimitiveInfo *primitive_info,
1670 const PointInfo point)
1671{
1672 primitive_info->coordinates=1;
1673 primitive_info->point=point;
1674}
1675
cristy018f07f2011-09-04 21:15:19 +00001676MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1677 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001678{
1679#define RenderImageTag "Render/Image"
1680
1681 AffineMatrix
1682 affine,
1683 current;
1684
1685 char
1686 key[2*MaxTextExtent],
1687 keyword[MaxTextExtent],
1688 geometry[MaxTextExtent],
1689 name[MaxTextExtent],
1690 pattern[MaxTextExtent],
1691 *primitive,
1692 *token;
1693
1694 const char
1695 *q;
1696
1697 DrawInfo
1698 **graphic_context;
1699
cristy3ed852e2009-09-05 21:47:34 +00001700 MagickBooleanType
1701 proceed,
1702 status;
1703
1704 MagickRealType
1705 angle,
1706 factor,
1707 primitive_extent;
1708
1709 PointInfo
1710 point;
1711
cristy101ab702011-10-13 13:06:32 +00001712 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001713 start_color;
1714
1715 PrimitiveInfo
1716 *primitive_info;
1717
1718 PrimitiveType
1719 primitive_type;
1720
1721 register const char
1722 *p;
1723
cristybb503372010-05-27 20:51:26 +00001724 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001725 i,
1726 x;
1727
1728 SegmentInfo
1729 bounds;
1730
1731 size_t
cristy826a5472010-08-31 23:21:38 +00001732 length,
cristy3ed852e2009-09-05 21:47:34 +00001733 number_points;
1734
cristy826a5472010-08-31 23:21:38 +00001735 ssize_t
1736 j,
1737 k,
1738 n;
1739
cristy3ed852e2009-09-05 21:47:34 +00001740 /*
1741 Ensure the annotation info is valid.
1742 */
1743 assert(image != (Image *) NULL);
1744 assert(image->signature == MagickSignature);
1745 if (image->debug != MagickFalse)
1746 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1747 assert(draw_info != (DrawInfo *) NULL);
1748 assert(draw_info->signature == MagickSignature);
1749 if (image->debug != MagickFalse)
1750 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1751 if ((draw_info->primitive == (char *) NULL) ||
1752 (*draw_info->primitive == '\0'))
1753 return(MagickFalse);
1754 if (image->debug != MagickFalse)
1755 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1756 if (*draw_info->primitive != '@')
1757 primitive=AcquireString(draw_info->primitive);
1758 else
cristy947cb4c2011-10-20 18:41:46 +00001759 primitive=FileToString(draw_info->primitive+1,~0,exception);
cristy3ed852e2009-09-05 21:47:34 +00001760 if (primitive == (char *) NULL)
1761 return(MagickFalse);
1762 primitive_extent=(MagickRealType) strlen(primitive);
1763 (void) SetImageArtifact(image,"MVG",primitive);
1764 n=0;
1765 /*
1766 Allocate primitive info memory.
1767 */
cristy73bd4a52010-10-05 11:24:23 +00001768 graphic_context=(DrawInfo **) AcquireMagickMemory(
cristyed110712010-03-23 01:16:38 +00001769 sizeof(*graphic_context));
cristy3ed852e2009-09-05 21:47:34 +00001770 if (graphic_context == (DrawInfo **) NULL)
1771 {
1772 primitive=DestroyString(primitive);
1773 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1774 image->filename);
1775 }
1776 number_points=2047;
1777 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1778 sizeof(*primitive_info));
1779 if (primitive_info == (PrimitiveInfo *) NULL)
1780 {
1781 primitive=DestroyString(primitive);
1782 for ( ; n >= 0; n--)
1783 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1784 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1785 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1786 image->filename);
1787 }
1788 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1789 graphic_context[n]->viewbox=image->page;
1790 if ((image->page.width == 0) || (image->page.height == 0))
1791 {
1792 graphic_context[n]->viewbox.width=image->columns;
1793 graphic_context[n]->viewbox.height=image->rows;
1794 }
1795 token=AcquireString(primitive);
cristy9950d572011-10-01 18:22:35 +00001796 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
cristy947cb4c2011-10-20 18:41:46 +00001797 exception);
1798 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001799 return(MagickFalse);
1800 status=MagickTrue;
1801 for (q=primitive; *q != '\0'; )
1802 {
1803 /*
1804 Interpret graphic primitive.
1805 */
1806 GetMagickToken(q,&q,keyword);
1807 if (*keyword == '\0')
1808 break;
1809 if (*keyword == '#')
1810 {
1811 /*
1812 Comment.
1813 */
1814 while ((*q != '\n') && (*q != '\0'))
1815 q++;
1816 continue;
1817 }
1818 p=q-strlen(keyword)-1;
1819 primitive_type=UndefinedPrimitive;
1820 current=graphic_context[n]->affine;
1821 GetAffineMatrix(&affine);
1822 switch (*keyword)
1823 {
1824 case ';':
1825 break;
1826 case 'a':
1827 case 'A':
1828 {
1829 if (LocaleCompare("affine",keyword) == 0)
1830 {
1831 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001832 affine.sx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001833 GetMagickToken(q,&q,token);
1834 if (*token == ',')
1835 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001836 affine.rx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001837 GetMagickToken(q,&q,token);
1838 if (*token == ',')
1839 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001840 affine.ry=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001841 GetMagickToken(q,&q,token);
1842 if (*token == ',')
1843 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001844 affine.sy=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001845 GetMagickToken(q,&q,token);
1846 if (*token == ',')
1847 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001848 affine.tx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001849 GetMagickToken(q,&q,token);
1850 if (*token == ',')
1851 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001852 affine.ty=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001853 break;
1854 }
1855 if (LocaleCompare("arc",keyword) == 0)
1856 {
1857 primitive_type=ArcPrimitive;
1858 break;
1859 }
1860 status=MagickFalse;
1861 break;
1862 }
1863 case 'b':
1864 case 'B':
1865 {
1866 if (LocaleCompare("bezier",keyword) == 0)
1867 {
1868 primitive_type=BezierPrimitive;
1869 break;
1870 }
1871 if (LocaleCompare("border-color",keyword) == 0)
1872 {
1873 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00001874 (void) QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00001875 &graphic_context[n]->border_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00001876 break;
1877 }
1878 status=MagickFalse;
1879 break;
1880 }
1881 case 'c':
1882 case 'C':
1883 {
1884 if (LocaleCompare("clip-path",keyword) == 0)
1885 {
1886 /*
1887 Create clip mask.
1888 */
1889 GetMagickToken(q,&q,token);
1890 (void) CloneString(&graphic_context[n]->clip_mask,token);
1891 (void) DrawClipPath(image,graphic_context[n],
cristy018f07f2011-09-04 21:15:19 +00001892 graphic_context[n]->clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001893 break;
1894 }
1895 if (LocaleCompare("clip-rule",keyword) == 0)
1896 {
cristybb503372010-05-27 20:51:26 +00001897 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001898 fill_rule;
1899
1900 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001901 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001902 token);
1903 if (fill_rule == -1)
anthony2a021472011-10-08 11:29:29 +00001904 status=MagickFalse;
1905 else
1906 graphic_context[n]->fill_rule=(FillRule) fill_rule;
cristy3ed852e2009-09-05 21:47:34 +00001907 break;
1908 }
1909 if (LocaleCompare("clip-units",keyword) == 0)
1910 {
cristybb503372010-05-27 20:51:26 +00001911 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001912 clip_units;
1913
1914 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001915 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001916 token);
1917 if (clip_units == -1)
1918 {
1919 status=MagickFalse;
1920 break;
1921 }
1922 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1923 if (clip_units == ObjectBoundingBox)
1924 {
1925 GetAffineMatrix(&current);
1926 affine.sx=draw_info->bounds.x2;
1927 affine.sy=draw_info->bounds.y2;
1928 affine.tx=draw_info->bounds.x1;
1929 affine.ty=draw_info->bounds.y1;
1930 break;
1931 }
1932 break;
1933 }
1934 if (LocaleCompare("circle",keyword) == 0)
1935 {
1936 primitive_type=CirclePrimitive;
1937 break;
1938 }
1939 if (LocaleCompare("color",keyword) == 0)
1940 {
1941 primitive_type=ColorPrimitive;
1942 break;
1943 }
1944 status=MagickFalse;
1945 break;
1946 }
1947 case 'd':
1948 case 'D':
1949 {
1950 if (LocaleCompare("decorate",keyword) == 0)
1951 {
cristybb503372010-05-27 20:51:26 +00001952 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001953 decorate;
1954
1955 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001956 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001957 token);
1958 if (decorate == -1)
anthony2a021472011-10-08 11:29:29 +00001959 status=MagickFalse;
1960 else
1961 graphic_context[n]->decorate=(DecorationType) decorate;
cristy3ed852e2009-09-05 21:47:34 +00001962 break;
1963 }
1964 status=MagickFalse;
1965 break;
1966 }
1967 case 'e':
1968 case 'E':
1969 {
1970 if (LocaleCompare("ellipse",keyword) == 0)
1971 {
1972 primitive_type=EllipsePrimitive;
1973 break;
1974 }
1975 if (LocaleCompare("encoding",keyword) == 0)
1976 {
1977 GetMagickToken(q,&q,token);
1978 (void) CloneString(&graphic_context[n]->encoding,token);
1979 break;
1980 }
1981 status=MagickFalse;
1982 break;
1983 }
1984 case 'f':
1985 case 'F':
1986 {
1987 if (LocaleCompare("fill",keyword) == 0)
1988 {
1989 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00001990 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00001991 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1992 (void) DrawPatternPath(image,draw_info,token,
cristy018f07f2011-09-04 21:15:19 +00001993 &graphic_context[n]->fill_pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00001994 else
1995 {
cristy9950d572011-10-01 18:22:35 +00001996 status=QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00001997 &graphic_context[n]->fill,exception);
cristy3ed852e2009-09-05 21:47:34 +00001998 if (status == MagickFalse)
1999 {
2000 ImageInfo
2001 *pattern_info;
2002
2003 pattern_info=AcquireImageInfo();
2004 (void) CopyMagickString(pattern_info->filename,token,
2005 MaxTextExtent);
cristy947cb4c2011-10-20 18:41:46 +00002006 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2007 exception);
2008 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00002009 pattern_info=DestroyImageInfo(pattern_info);
2010 }
2011 }
2012 break;
2013 }
2014 if (LocaleCompare("fill-opacity",keyword) == 0)
2015 {
2016 GetMagickToken(q,&q,token);
2017 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristyda1f9c12011-10-02 21:39:49 +00002018 graphic_context[n]->fill.alpha=(MagickRealType) QuantumRange*
2019 factor*InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002020 break;
2021 }
2022 if (LocaleCompare("fill-rule",keyword) == 0)
2023 {
cristybb503372010-05-27 20:51:26 +00002024 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002025 fill_rule;
2026
2027 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002028 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00002029 token);
2030 if (fill_rule == -1)
anthony2a021472011-10-08 11:29:29 +00002031 status=MagickFalse;
2032 else
2033 graphic_context[n]->fill_rule=(FillRule) fill_rule;
cristy3ed852e2009-09-05 21:47:34 +00002034 break;
2035 }
2036 if (LocaleCompare("font",keyword) == 0)
2037 {
2038 GetMagickToken(q,&q,token);
2039 (void) CloneString(&graphic_context[n]->font,token);
2040 if (LocaleCompare("none",token) == 0)
2041 graphic_context[n]->font=(char *)
2042 RelinquishMagickMemory(graphic_context[n]->font);
2043 break;
2044 }
2045 if (LocaleCompare("font-family",keyword) == 0)
2046 {
2047 GetMagickToken(q,&q,token);
2048 (void) CloneString(&graphic_context[n]->family,token);
2049 break;
2050 }
2051 if (LocaleCompare("font-size",keyword) == 0)
2052 {
2053 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002054 graphic_context[n]->pointsize=InterpretLocaleValue(token,
2055 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002056 break;
2057 }
2058 if (LocaleCompare("font-stretch",keyword) == 0)
2059 {
cristybb503372010-05-27 20:51:26 +00002060 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002061 stretch;
2062
2063 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002064 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002065 if (stretch == -1)
anthony2a021472011-10-08 11:29:29 +00002066 status=MagickFalse;
2067 else
2068 graphic_context[n]->stretch=(StretchType) stretch;
cristy3ed852e2009-09-05 21:47:34 +00002069 break;
2070 }
2071 if (LocaleCompare("font-style",keyword) == 0)
2072 {
cristybb503372010-05-27 20:51:26 +00002073 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002074 style;
2075
2076 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002077 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002078 if (style == -1)
anthony2a021472011-10-08 11:29:29 +00002079 status=MagickFalse;
2080 else
2081 graphic_context[n]->style=(StyleType) style;
cristy3ed852e2009-09-05 21:47:34 +00002082 break;
2083 }
2084 if (LocaleCompare("font-weight",keyword) == 0)
2085 {
2086 GetMagickToken(q,&q,token);
cristye27293e2009-12-18 02:53:20 +00002087 graphic_context[n]->weight=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00002088 if (LocaleCompare(token,"all") == 0)
2089 graphic_context[n]->weight=0;
2090 if (LocaleCompare(token,"bold") == 0)
2091 graphic_context[n]->weight=700;
2092 if (LocaleCompare(token,"bolder") == 0)
2093 if (graphic_context[n]->weight <= 800)
2094 graphic_context[n]->weight+=100;
2095 if (LocaleCompare(token,"lighter") == 0)
2096 if (graphic_context[n]->weight >= 100)
2097 graphic_context[n]->weight-=100;
2098 if (LocaleCompare(token,"normal") == 0)
2099 graphic_context[n]->weight=400;
2100 break;
2101 }
2102 status=MagickFalse;
2103 break;
2104 }
2105 case 'g':
2106 case 'G':
2107 {
2108 if (LocaleCompare("gradient-units",keyword) == 0)
2109 {
2110 GetMagickToken(q,&q,token);
2111 break;
2112 }
2113 if (LocaleCompare("gravity",keyword) == 0)
2114 {
cristybb503372010-05-27 20:51:26 +00002115 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002116 gravity;
2117
2118 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002119 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002120 if (gravity == -1)
anthony2a021472011-10-08 11:29:29 +00002121 status=MagickFalse;
2122 else
2123 graphic_context[n]->gravity=(GravityType) gravity;
cristy3ed852e2009-09-05 21:47:34 +00002124 break;
2125 }
2126 status=MagickFalse;
2127 break;
2128 }
2129 case 'i':
2130 case 'I':
2131 {
2132 if (LocaleCompare("image",keyword) == 0)
2133 {
cristybb503372010-05-27 20:51:26 +00002134 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002135 compose;
2136
2137 primitive_type=ImagePrimitive;
2138 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002139 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002140 if (compose == -1)
anthony2a021472011-10-08 11:29:29 +00002141 status=MagickFalse;
2142 else
2143 graphic_context[n]->compose=(CompositeOperator) compose;
cristy3ed852e2009-09-05 21:47:34 +00002144 break;
2145 }
cristyb32b90a2009-09-07 21:45:48 +00002146 if (LocaleCompare("interline-spacing",keyword) == 0)
2147 {
2148 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002149 graphic_context[n]->interline_spacing=InterpretLocaleValue(token,
2150 (char **) NULL);
cristyb32b90a2009-09-07 21:45:48 +00002151 break;
2152 }
cristy3ed852e2009-09-05 21:47:34 +00002153 if (LocaleCompare("interword-spacing",keyword) == 0)
2154 {
2155 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002156 graphic_context[n]->interword_spacing=InterpretLocaleValue(token,
2157 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002158 break;
2159 }
2160 status=MagickFalse;
2161 break;
2162 }
2163 case 'k':
2164 case 'K':
2165 {
2166 if (LocaleCompare("kerning",keyword) == 0)
2167 {
2168 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002169 graphic_context[n]->kerning=InterpretLocaleValue(token,
2170 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002171 break;
2172 }
2173 status=MagickFalse;
2174 break;
2175 }
2176 case 'l':
2177 case 'L':
2178 {
2179 if (LocaleCompare("line",keyword) == 0)
anthony2a021472011-10-08 11:29:29 +00002180 primitive_type=LinePrimitive;
2181 else
2182 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002183 break;
2184 }
2185 case 'm':
2186 case 'M':
2187 {
2188 if (LocaleCompare("matte",keyword) == 0)
anthony2a021472011-10-08 11:29:29 +00002189 primitive_type=MattePrimitive;
2190 else
2191 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002192 break;
2193 }
2194 case 'o':
2195 case 'O':
2196 {
2197 if (LocaleCompare("offset",keyword) == 0)
2198 {
2199 GetMagickToken(q,&q,token);
2200 break;
2201 }
2202 if (LocaleCompare("opacity",keyword) == 0)
2203 {
2204 GetMagickToken(q,&q,token);
2205 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristy4c08aed2011-07-01 19:47:50 +00002206 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2207 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
cristyc1acd842011-05-19 23:05:47 +00002208 factor*InterpretLocaleValue(token,(char **) NULL))));
cristyda1f9c12011-10-02 21:39:49 +00002209 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2210 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
cristy3ed852e2009-09-05 21:47:34 +00002211 break;
2212 }
2213 status=MagickFalse;
2214 break;
2215 }
2216 case 'p':
2217 case 'P':
2218 {
2219 if (LocaleCompare("path",keyword) == 0)
2220 {
2221 primitive_type=PathPrimitive;
2222 break;
2223 }
2224 if (LocaleCompare("point",keyword) == 0)
2225 {
2226 primitive_type=PointPrimitive;
2227 break;
2228 }
2229 if (LocaleCompare("polyline",keyword) == 0)
2230 {
2231 primitive_type=PolylinePrimitive;
2232 break;
2233 }
2234 if (LocaleCompare("polygon",keyword) == 0)
2235 {
2236 primitive_type=PolygonPrimitive;
2237 break;
2238 }
2239 if (LocaleCompare("pop",keyword) == 0)
2240 {
2241 GetMagickToken(q,&q,token);
2242 if (LocaleCompare("clip-path",token) == 0)
2243 break;
2244 if (LocaleCompare("defs",token) == 0)
2245 break;
2246 if (LocaleCompare("gradient",token) == 0)
2247 break;
2248 if (LocaleCompare("graphic-context",token) == 0)
2249 {
2250 if (n <= 0)
2251 {
cristy947cb4c2011-10-20 18:41:46 +00002252 (void) ThrowMagickException(exception,GetMagickModule(),
2253 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00002254 n=0;
2255 break;
2256 }
2257 if (graphic_context[n]->clip_mask != (char *) NULL)
2258 if (LocaleCompare(graphic_context[n]->clip_mask,
2259 graphic_context[n-1]->clip_mask) != 0)
cristy018f07f2011-09-04 21:15:19 +00002260 (void) SetImageClipMask(image,(Image *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002261 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2262 n--;
2263 break;
2264 }
2265 if (LocaleCompare("pattern",token) == 0)
2266 break;
2267 status=MagickFalse;
2268 break;
2269 }
2270 if (LocaleCompare("push",keyword) == 0)
2271 {
2272 GetMagickToken(q,&q,token);
2273 if (LocaleCompare("clip-path",token) == 0)
2274 {
2275 char
2276 name[MaxTextExtent];
2277
2278 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00002279 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00002280 for (p=q; *q != '\0'; )
2281 {
2282 GetMagickToken(q,&q,token);
2283 if (LocaleCompare(token,"pop") != 0)
2284 continue;
2285 GetMagickToken(q,(const char **) NULL,token);
2286 if (LocaleCompare(token,"clip-path") != 0)
2287 continue;
2288 break;
2289 }
2290 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2291 (void) SetImageArtifact(image,name,token);
2292 GetMagickToken(q,&q,token);
2293 break;
2294 }
2295 if (LocaleCompare("gradient",token) == 0)
2296 {
2297 char
2298 key[2*MaxTextExtent],
2299 name[MaxTextExtent],
2300 type[MaxTextExtent];
2301
cristy3ed852e2009-09-05 21:47:34 +00002302 SegmentInfo
2303 segment;
2304
2305 GetMagickToken(q,&q,token);
2306 (void) CopyMagickString(name,token,MaxTextExtent);
2307 GetMagickToken(q,&q,token);
2308 (void) CopyMagickString(type,token,MaxTextExtent);
2309 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002310 segment.x1=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002311 GetMagickToken(q,&q,token);
2312 if (*token == ',')
2313 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002314 segment.y1=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002315 GetMagickToken(q,&q,token);
2316 if (*token == ',')
2317 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002318 segment.x2=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002319 GetMagickToken(q,&q,token);
2320 if (*token == ',')
2321 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002322 segment.y2=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002323 if (LocaleCompare(type,"radial") == 0)
2324 {
2325 GetMagickToken(q,&q,token);
2326 if (*token == ',')
2327 GetMagickToken(q,&q,token);
cristy3ed852e2009-09-05 21:47:34 +00002328 }
2329 for (p=q; *q != '\0'; )
2330 {
2331 GetMagickToken(q,&q,token);
2332 if (LocaleCompare(token,"pop") != 0)
2333 continue;
2334 GetMagickToken(q,(const char **) NULL,token);
2335 if (LocaleCompare(token,"gradient") != 0)
2336 continue;
2337 break;
2338 }
2339 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2340 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2341 graphic_context[n]->affine.ry*segment.y1+
2342 graphic_context[n]->affine.tx;
2343 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2344 graphic_context[n]->affine.sy*segment.y1+
2345 graphic_context[n]->affine.ty;
2346 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2347 graphic_context[n]->affine.ry*segment.y2+
2348 graphic_context[n]->affine.tx;
2349 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2350 graphic_context[n]->affine.sy*segment.y2+
2351 graphic_context[n]->affine.ty;
cristyb51dff52011-05-19 16:55:47 +00002352 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00002353 (void) SetImageArtifact(image,key,token);
cristyb51dff52011-05-19 16:55:47 +00002354 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2355 (void) FormatLocaleString(geometry,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002356 "%gx%g%+.15g%+.15g",
cristy3ed852e2009-09-05 21:47:34 +00002357 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2358 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2359 bounds.x1,bounds.y1);
2360 (void) SetImageArtifact(image,key,geometry);
2361 GetMagickToken(q,&q,token);
2362 break;
2363 }
2364 if (LocaleCompare("pattern",token) == 0)
2365 {
2366 RectangleInfo
2367 bounds;
2368
2369 GetMagickToken(q,&q,token);
2370 (void) CopyMagickString(name,token,MaxTextExtent);
2371 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002372 bounds.x=(ssize_t) ceil(InterpretLocaleValue(token,
2373 (char **) NULL)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002374 GetMagickToken(q,&q,token);
2375 if (*token == ',')
2376 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002377 bounds.y=(ssize_t) ceil(InterpretLocaleValue(token,
2378 (char **) NULL)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002379 GetMagickToken(q,&q,token);
2380 if (*token == ',')
2381 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002382 bounds.width=(size_t) floor(InterpretLocaleValue(token,
2383 (char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002384 GetMagickToken(q,&q,token);
2385 if (*token == ',')
2386 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002387 bounds.height=(size_t) floor(InterpretLocaleValue(token,
2388 (char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002389 for (p=q; *q != '\0'; )
2390 {
2391 GetMagickToken(q,&q,token);
2392 if (LocaleCompare(token,"pop") != 0)
2393 continue;
2394 GetMagickToken(q,(const char **) NULL,token);
2395 if (LocaleCompare(token,"pattern") != 0)
2396 continue;
2397 break;
2398 }
2399 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
cristyb51dff52011-05-19 16:55:47 +00002400 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00002401 (void) SetImageArtifact(image,key,token);
cristyb51dff52011-05-19 16:55:47 +00002402 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2403 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002404 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002405 bounds.height,(double) bounds.x,(double) bounds.y);
cristy3ed852e2009-09-05 21:47:34 +00002406 (void) SetImageArtifact(image,key,geometry);
2407 GetMagickToken(q,&q,token);
2408 break;
2409 }
2410 if (LocaleCompare("graphic-context",token) == 0)
2411 {
2412 n++;
2413 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2414 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2415 if (graphic_context == (DrawInfo **) NULL)
2416 {
cristy947cb4c2011-10-20 18:41:46 +00002417 (void) ThrowMagickException(exception,GetMagickModule(),
2418 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2419 image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002420 break;
2421 }
2422 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2423 graphic_context[n-1]);
2424 break;
2425 }
2426 if (LocaleCompare("defs",token) == 0)
2427 break;
2428 status=MagickFalse;
2429 break;
2430 }
2431 status=MagickFalse;
2432 break;
2433 }
2434 case 'r':
2435 case 'R':
2436 {
2437 if (LocaleCompare("rectangle",keyword) == 0)
2438 {
2439 primitive_type=RectanglePrimitive;
2440 break;
2441 }
2442 if (LocaleCompare("rotate",keyword) == 0)
2443 {
2444 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002445 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002446 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2447 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2448 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2449 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2450 break;
2451 }
2452 if (LocaleCompare("roundRectangle",keyword) == 0)
2453 {
2454 primitive_type=RoundRectanglePrimitive;
2455 break;
2456 }
2457 status=MagickFalse;
2458 break;
2459 }
2460 case 's':
2461 case 'S':
2462 {
2463 if (LocaleCompare("scale",keyword) == 0)
2464 {
2465 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002466 affine.sx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002467 GetMagickToken(q,&q,token);
2468 if (*token == ',')
2469 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002470 affine.sy=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002471 break;
2472 }
2473 if (LocaleCompare("skewX",keyword) == 0)
2474 {
2475 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002476 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002477 affine.ry=sin(DegreesToRadians(angle));
2478 break;
2479 }
2480 if (LocaleCompare("skewY",keyword) == 0)
2481 {
2482 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002483 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002484 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2485 break;
2486 }
2487 if (LocaleCompare("stop-color",keyword) == 0)
2488 {
cristy101ab702011-10-13 13:06:32 +00002489 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002490 stop_color;
2491
2492 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00002493 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
cristy947cb4c2011-10-20 18:41:46 +00002494 exception);
cristy3ed852e2009-09-05 21:47:34 +00002495 (void) GradientImage(image,LinearGradient,ReflectSpread,
cristy947cb4c2011-10-20 18:41:46 +00002496 &start_color,&stop_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002497 start_color=stop_color;
2498 GetMagickToken(q,&q,token);
2499 break;
2500 }
2501 if (LocaleCompare("stroke",keyword) == 0)
2502 {
2503 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00002504 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00002505 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2506 (void) DrawPatternPath(image,draw_info,token,
cristy018f07f2011-09-04 21:15:19 +00002507 &graphic_context[n]->stroke_pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00002508 else
2509 {
cristy9950d572011-10-01 18:22:35 +00002510 status=QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00002511 &graphic_context[n]->stroke,exception);
cristy3ed852e2009-09-05 21:47:34 +00002512 if (status == MagickFalse)
2513 {
2514 ImageInfo
2515 *pattern_info;
2516
2517 pattern_info=AcquireImageInfo();
2518 (void) CopyMagickString(pattern_info->filename,token,
2519 MaxTextExtent);
cristy947cb4c2011-10-20 18:41:46 +00002520 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2521 exception);
2522 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00002523 pattern_info=DestroyImageInfo(pattern_info);
2524 }
2525 }
2526 break;
2527 }
2528 if (LocaleCompare("stroke-antialias",keyword) == 0)
2529 {
2530 GetMagickToken(q,&q,token);
2531 graphic_context[n]->stroke_antialias=
cristyf2f27272009-12-17 14:48:46 +00002532 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002533 break;
2534 }
2535 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2536 {
2537 if (graphic_context[n]->dash_pattern != (double *) NULL)
2538 graphic_context[n]->dash_pattern=(double *)
2539 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2540 if (IsPoint(q) != MagickFalse)
2541 {
2542 const char
2543 *p;
2544
2545 p=q;
2546 GetMagickToken(p,&p,token);
2547 if (*token == ',')
2548 GetMagickToken(p,&p,token);
2549 for (x=0; IsPoint(token) != MagickFalse; x++)
2550 {
2551 GetMagickToken(p,&p,token);
2552 if (*token == ',')
2553 GetMagickToken(p,&p,token);
2554 }
2555 graphic_context[n]->dash_pattern=(double *)
2556 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2557 sizeof(*graphic_context[n]->dash_pattern));
2558 if (graphic_context[n]->dash_pattern == (double *) NULL)
2559 {
cristy947cb4c2011-10-20 18:41:46 +00002560 (void) ThrowMagickException(exception,GetMagickModule(),
2561 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2562 image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002563 break;
2564 }
2565 for (j=0; j < x; j++)
2566 {
2567 GetMagickToken(q,&q,token);
2568 if (*token == ',')
2569 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002570 graphic_context[n]->dash_pattern[j]=InterpretLocaleValue(
2571 token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002572 }
2573 if ((x & 0x01) != 0)
2574 for ( ; j < (2*x); j++)
2575 graphic_context[n]->dash_pattern[j]=
2576 graphic_context[n]->dash_pattern[j-x];
2577 graphic_context[n]->dash_pattern[j]=0.0;
2578 break;
2579 }
2580 GetMagickToken(q,&q,token);
2581 break;
2582 }
2583 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2584 {
2585 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002586 graphic_context[n]->dash_offset=InterpretLocaleValue(token,
2587 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002588 break;
2589 }
2590 if (LocaleCompare("stroke-linecap",keyword) == 0)
2591 {
cristybb503372010-05-27 20:51:26 +00002592 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002593 linecap;
2594
2595 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002596 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002597 if (linecap == -1)
anthony2a021472011-10-08 11:29:29 +00002598 status=MagickFalse;
2599 else
2600 graphic_context[n]->linecap=(LineCap) linecap;
cristy3ed852e2009-09-05 21:47:34 +00002601 break;
2602 }
2603 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2604 {
cristybb503372010-05-27 20:51:26 +00002605 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002606 linejoin;
2607
2608 GetMagickToken(q,&q,token);
cristy7118edf2011-10-08 13:33:25 +00002609 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2610 token);
cristy3ed852e2009-09-05 21:47:34 +00002611 if (linejoin == -1)
anthony2a021472011-10-08 11:29:29 +00002612 status=MagickFalse;
2613 else
2614 graphic_context[n]->linejoin=(LineJoin) linejoin;
cristy3ed852e2009-09-05 21:47:34 +00002615 break;
2616 }
2617 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2618 {
2619 GetMagickToken(q,&q,token);
cristye27293e2009-12-18 02:53:20 +00002620 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00002621 break;
2622 }
2623 if (LocaleCompare("stroke-opacity",keyword) == 0)
2624 {
2625 GetMagickToken(q,&q,token);
2626 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristyda1f9c12011-10-02 21:39:49 +00002627 graphic_context[n]->stroke.alpha=(MagickRealType) QuantumRange*
2628 factor*InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002629 break;
2630 }
2631 if (LocaleCompare("stroke-width",keyword) == 0)
2632 {
2633 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002634 graphic_context[n]->stroke_width=InterpretLocaleValue(token,
2635 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002636 break;
2637 }
2638 status=MagickFalse;
2639 break;
2640 }
2641 case 't':
2642 case 'T':
2643 {
2644 if (LocaleCompare("text",keyword) == 0)
2645 {
2646 primitive_type=TextPrimitive;
2647 break;
2648 }
2649 if (LocaleCompare("text-align",keyword) == 0)
2650 {
cristybb503372010-05-27 20:51:26 +00002651 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002652 align;
2653
2654 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002655 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002656 if (align == -1)
anthony2a021472011-10-08 11:29:29 +00002657 status=MagickFalse;
2658 else
2659 graphic_context[n]->align=(AlignType) align;
cristy3ed852e2009-09-05 21:47:34 +00002660 break;
2661 }
2662 if (LocaleCompare("text-anchor",keyword) == 0)
2663 {
cristybb503372010-05-27 20:51:26 +00002664 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002665 align;
2666
2667 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002668 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002669 if (align == -1)
anthony2a021472011-10-08 11:29:29 +00002670 status=MagickFalse;
2671 else
2672 graphic_context[n]->align=(AlignType) align;
cristy3ed852e2009-09-05 21:47:34 +00002673 break;
2674 }
2675 if (LocaleCompare("text-antialias",keyword) == 0)
2676 {
2677 GetMagickToken(q,&q,token);
2678 graphic_context[n]->text_antialias=
cristyf2f27272009-12-17 14:48:46 +00002679 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002680 break;
2681 }
2682 if (LocaleCompare("text-undercolor",keyword) == 0)
2683 {
2684 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00002685 (void) QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00002686 &graphic_context[n]->undercolor,exception);
cristy3ed852e2009-09-05 21:47:34 +00002687 break;
2688 }
2689 if (LocaleCompare("translate",keyword) == 0)
2690 {
2691 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002692 affine.tx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002693 GetMagickToken(q,&q,token);
2694 if (*token == ',')
2695 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002696 affine.ty=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002697 break;
2698 }
2699 status=MagickFalse;
2700 break;
2701 }
2702 case 'v':
2703 case 'V':
2704 {
2705 if (LocaleCompare("viewbox",keyword) == 0)
2706 {
2707 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002708 graphic_context[n]->viewbox.x=(ssize_t) ceil(InterpretLocaleValue(
2709 token,(char **) NULL)-0.5);
cristy06609ee2010-03-17 20:21:27 +00002710 GetMagickToken(q,&q,token);
2711 if (*token == ',')
2712 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002713 graphic_context[n]->viewbox.y=(ssize_t) ceil(InterpretLocaleValue(
2714 token,(char **) NULL)-0.5);
cristy06609ee2010-03-17 20:21:27 +00002715 GetMagickToken(q,&q,token);
2716 if (*token == ',')
2717 GetMagickToken(q,&q,token);
cristybb503372010-05-27 20:51:26 +00002718 graphic_context[n]->viewbox.width=(size_t) floor(
cristyc1acd842011-05-19 23:05:47 +00002719 InterpretLocaleValue(token,(char **) NULL)+0.5);
cristy06609ee2010-03-17 20:21:27 +00002720 GetMagickToken(q,&q,token);
2721 if (*token == ',')
2722 GetMagickToken(q,&q,token);
cristybb503372010-05-27 20:51:26 +00002723 graphic_context[n]->viewbox.height=(size_t) floor(
cristyc1acd842011-05-19 23:05:47 +00002724 InterpretLocaleValue(token,(char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002725 break;
2726 }
2727 status=MagickFalse;
2728 break;
2729 }
2730 default:
2731 {
2732 status=MagickFalse;
2733 break;
2734 }
2735 }
2736 if (status == MagickFalse)
2737 break;
2738 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2739 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2740 {
cristyfbd70092010-12-01 19:31:14 +00002741 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2742 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2743 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2744 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2745 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2746 current.tx;
2747 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2748 current.ty;
cristy3ed852e2009-09-05 21:47:34 +00002749 }
2750 if (primitive_type == UndefinedPrimitive)
2751 {
2752 if (image->debug != MagickFalse)
2753 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2754 (int) (q-p),p);
2755 continue;
2756 }
2757 /*
2758 Parse the primitive attributes.
2759 */
2760 i=0;
2761 j=0;
2762 primitive_info[0].point.x=0.0;
2763 primitive_info[0].point.y=0.0;
2764 for (x=0; *q != '\0'; x++)
2765 {
2766 /*
2767 Define points.
2768 */
2769 if (IsPoint(q) == MagickFalse)
2770 break;
2771 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002772 point.x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002773 GetMagickToken(q,&q,token);
2774 if (*token == ',')
2775 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002776 point.y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002777 GetMagickToken(q,(const char **) NULL,token);
2778 if (*token == ',')
2779 GetMagickToken(q,&q,token);
2780 primitive_info[i].primitive=primitive_type;
2781 primitive_info[i].point=point;
2782 primitive_info[i].coordinates=0;
2783 primitive_info[i].method=FloodfillMethod;
2784 i++;
cristybb503372010-05-27 20:51:26 +00002785 if (i < (ssize_t) number_points)
cristy3ed852e2009-09-05 21:47:34 +00002786 continue;
2787 number_points<<=1;
2788 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2789 (size_t) number_points,sizeof(*primitive_info));
2790 if (primitive_info == (PrimitiveInfo *) NULL)
2791 {
cristy947cb4c2011-10-20 18:41:46 +00002792 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00002793 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2794 break;
2795 }
2796 }
2797 primitive_info[j].primitive=primitive_type;
cristybb503372010-05-27 20:51:26 +00002798 primitive_info[j].coordinates=(size_t) x;
cristy3ed852e2009-09-05 21:47:34 +00002799 primitive_info[j].method=FloodfillMethod;
2800 primitive_info[j].text=(char *) NULL;
2801 /*
2802 Circumscribe primitive within a circle.
2803 */
2804 bounds.x1=primitive_info[j].point.x;
2805 bounds.y1=primitive_info[j].point.y;
2806 bounds.x2=primitive_info[j].point.x;
2807 bounds.y2=primitive_info[j].point.y;
cristybb503372010-05-27 20:51:26 +00002808 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
cristy3ed852e2009-09-05 21:47:34 +00002809 {
2810 point=primitive_info[j+k].point;
2811 if (point.x < bounds.x1)
2812 bounds.x1=point.x;
2813 if (point.y < bounds.y1)
2814 bounds.y1=point.y;
2815 if (point.x > bounds.x2)
2816 bounds.x2=point.x;
2817 if (point.y > bounds.y2)
2818 bounds.y2=point.y;
2819 }
2820 /*
2821 Speculate how many points our primitive might consume.
2822 */
2823 length=primitive_info[j].coordinates;
2824 switch (primitive_type)
2825 {
2826 case RectanglePrimitive:
2827 {
2828 length*=5;
2829 break;
2830 }
2831 case RoundRectanglePrimitive:
2832 {
cristy78817ad2010-05-07 12:25:34 +00002833 length*=5+8*BezierQuantum;
cristy3ed852e2009-09-05 21:47:34 +00002834 break;
2835 }
2836 case BezierPrimitive:
2837 {
2838 if (primitive_info[j].coordinates > 107)
cristy947cb4c2011-10-20 18:41:46 +00002839 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2840 "TooManyBezierCoordinates","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00002841 length=BezierQuantum*primitive_info[j].coordinates;
2842 break;
2843 }
2844 case PathPrimitive:
2845 {
2846 char
2847 *s,
2848 *t;
2849
2850 GetMagickToken(q,&q,token);
cristy40a08ad2010-02-09 02:27:44 +00002851 length=1;
cristy3ed852e2009-09-05 21:47:34 +00002852 t=token;
2853 for (s=token; *s != '\0'; s=t)
2854 {
cristy00976d82011-02-20 20:31:28 +00002855 double
2856 value;
2857
cristyc1acd842011-05-19 23:05:47 +00002858 value=InterpretLocaleValue(s,&t);
cristy00976d82011-02-20 20:31:28 +00002859 (void) value;
cristy3ed852e2009-09-05 21:47:34 +00002860 if (s == t)
2861 {
2862 t++;
2863 continue;
2864 }
cristyef7a6ce2011-05-14 15:01:11 +00002865 length++;
cristy3ed852e2009-09-05 21:47:34 +00002866 }
cristyd2f832c2011-06-28 12:35:24 +00002867 length=length*BezierQuantum/2;
cristy3ed852e2009-09-05 21:47:34 +00002868 break;
2869 }
2870 case CirclePrimitive:
2871 case ArcPrimitive:
2872 case EllipsePrimitive:
2873 {
2874 MagickRealType
2875 alpha,
2876 beta,
2877 radius;
2878
2879 alpha=bounds.x2-bounds.x1;
2880 beta=bounds.y2-bounds.y1;
2881 radius=hypot((double) alpha,(double) beta);
cristyf53f26f2011-05-16 00:56:05 +00002882 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
cristy3ed852e2009-09-05 21:47:34 +00002883 break;
2884 }
2885 default:
2886 break;
2887 }
cristybb503372010-05-27 20:51:26 +00002888 if ((size_t) (i+length) >= number_points)
cristy3ed852e2009-09-05 21:47:34 +00002889 {
2890 /*
2891 Resize based on speculative points required by primitive.
2892 */
cristy9ce61b92010-05-12 16:30:26 +00002893 number_points+=length+1;
cristy3ed852e2009-09-05 21:47:34 +00002894 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2895 (size_t) number_points,sizeof(*primitive_info));
2896 if (primitive_info == (PrimitiveInfo *) NULL)
2897 {
cristy947cb4c2011-10-20 18:41:46 +00002898 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00002899 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2900 image->filename);
2901 break;
2902 }
2903 }
2904 switch (primitive_type)
2905 {
2906 case PointPrimitive:
2907 default:
2908 {
2909 if (primitive_info[j].coordinates != 1)
2910 {
2911 status=MagickFalse;
2912 break;
2913 }
2914 TracePoint(primitive_info+j,primitive_info[j].point);
cristybb503372010-05-27 20:51:26 +00002915 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002916 break;
2917 }
2918 case LinePrimitive:
2919 {
2920 if (primitive_info[j].coordinates != 2)
2921 {
2922 status=MagickFalse;
2923 break;
2924 }
2925 TraceLine(primitive_info+j,primitive_info[j].point,
2926 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002927 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002928 break;
2929 }
2930 case RectanglePrimitive:
2931 {
2932 if (primitive_info[j].coordinates != 2)
2933 {
2934 status=MagickFalse;
2935 break;
2936 }
2937 TraceRectangle(primitive_info+j,primitive_info[j].point,
2938 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002939 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002940 break;
2941 }
2942 case RoundRectanglePrimitive:
2943 {
2944 if (primitive_info[j].coordinates != 3)
2945 {
2946 status=MagickFalse;
2947 break;
2948 }
2949 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2950 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002951 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002952 break;
2953 }
2954 case ArcPrimitive:
2955 {
2956 if (primitive_info[j].coordinates != 3)
2957 {
2958 primitive_type=UndefinedPrimitive;
2959 break;
2960 }
2961 TraceArc(primitive_info+j,primitive_info[j].point,
2962 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002963 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002964 break;
2965 }
2966 case EllipsePrimitive:
2967 {
2968 if (primitive_info[j].coordinates != 3)
2969 {
2970 status=MagickFalse;
2971 break;
2972 }
2973 TraceEllipse(primitive_info+j,primitive_info[j].point,
2974 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002975 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002976 break;
2977 }
2978 case CirclePrimitive:
2979 {
2980 if (primitive_info[j].coordinates != 2)
2981 {
2982 status=MagickFalse;
2983 break;
2984 }
2985 TraceCircle(primitive_info+j,primitive_info[j].point,
2986 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002987 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002988 break;
2989 }
2990 case PolylinePrimitive:
2991 break;
2992 case PolygonPrimitive:
2993 {
2994 primitive_info[i]=primitive_info[j];
2995 primitive_info[i].coordinates=0;
2996 primitive_info[j].coordinates++;
2997 i++;
2998 break;
2999 }
3000 case BezierPrimitive:
3001 {
3002 if (primitive_info[j].coordinates < 3)
3003 {
3004 status=MagickFalse;
3005 break;
3006 }
3007 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
cristybb503372010-05-27 20:51:26 +00003008 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00003009 break;
3010 }
3011 case PathPrimitive:
3012 {
cristybb503372010-05-27 20:51:26 +00003013 i=(ssize_t) (j+TracePath(primitive_info+j,token));
cristy3ed852e2009-09-05 21:47:34 +00003014 break;
3015 }
3016 case ColorPrimitive:
3017 case MattePrimitive:
3018 {
cristybb503372010-05-27 20:51:26 +00003019 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003020 method;
3021
3022 if (primitive_info[j].coordinates != 1)
3023 {
3024 status=MagickFalse;
3025 break;
3026 }
3027 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00003028 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00003029 if (method == -1)
anthony2a021472011-10-08 11:29:29 +00003030 status=MagickFalse;
3031 else
3032 primitive_info[j].method=(PaintMethod) method;
cristy3ed852e2009-09-05 21:47:34 +00003033 break;
3034 }
3035 case TextPrimitive:
3036 {
3037 if (primitive_info[j].coordinates != 1)
3038 {
3039 status=MagickFalse;
3040 break;
3041 }
3042 if (*token != ',')
3043 GetMagickToken(q,&q,token);
3044 primitive_info[j].text=AcquireString(token);
3045 break;
3046 }
3047 case ImagePrimitive:
3048 {
3049 if (primitive_info[j].coordinates != 2)
3050 {
3051 status=MagickFalse;
3052 break;
3053 }
3054 GetMagickToken(q,&q,token);
3055 primitive_info[j].text=AcquireString(token);
3056 break;
3057 }
3058 }
3059 if (primitive_info == (PrimitiveInfo *) NULL)
3060 break;
3061 if (image->debug != MagickFalse)
3062 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3063 if (status == MagickFalse)
3064 break;
3065 primitive_info[i].primitive=UndefinedPrimitive;
3066 if (i == 0)
3067 continue;
3068 /*
3069 Transform points.
3070 */
3071 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3072 {
3073 point=primitive_info[i].point;
3074 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3075 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3076 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3077 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3078 point=primitive_info[i].point;
3079 if (point.x < graphic_context[n]->bounds.x1)
3080 graphic_context[n]->bounds.x1=point.x;
3081 if (point.y < graphic_context[n]->bounds.y1)
3082 graphic_context[n]->bounds.y1=point.y;
3083 if (point.x > graphic_context[n]->bounds.x2)
3084 graphic_context[n]->bounds.x2=point.x;
3085 if (point.y > graphic_context[n]->bounds.y2)
3086 graphic_context[n]->bounds.y2=point.y;
3087 if (primitive_info[i].primitive == ImagePrimitive)
3088 break;
cristybb503372010-05-27 20:51:26 +00003089 if (i >= (ssize_t) number_points)
cristy9ce61b92010-05-12 16:30:26 +00003090 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00003091 }
cristy3ed852e2009-09-05 21:47:34 +00003092 if (graphic_context[n]->render != MagickFalse)
3093 {
3094 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3095 (LocaleCompare(graphic_context[n]->clip_mask,
3096 graphic_context[n-1]->clip_mask) != 0))
3097 (void) DrawClipPath(image,graphic_context[n],
cristy018f07f2011-09-04 21:15:19 +00003098 graphic_context[n]->clip_mask,exception);
cristy947cb4c2011-10-20 18:41:46 +00003099 (void) DrawPrimitive(image,graphic_context[n],primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003100 }
3101 if (primitive_info->text != (char *) NULL)
3102 primitive_info->text=(char *) RelinquishMagickMemory(
3103 primitive_info->text);
3104 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3105 primitive_extent);
3106 if (proceed == MagickFalse)
3107 break;
3108 }
3109 if (image->debug != MagickFalse)
3110 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3111 /*
3112 Relinquish resources.
3113 */
3114 token=DestroyString(token);
3115 if (primitive_info != (PrimitiveInfo *) NULL)
3116 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3117 primitive=DestroyString(primitive);
3118 for ( ; n >= 0; n--)
3119 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3120 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3121 if (status == MagickFalse)
3122 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3123 keyword);
3124 return(status);
3125}
3126
3127/*
3128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129% %
3130% %
3131% %
3132% D r a w G r a d i e n t I m a g e %
3133% %
3134% %
3135% %
3136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3137%
3138% DrawGradientImage() draws a linear gradient on the image.
3139%
3140% The format of the DrawGradientImage method is:
3141%
3142% MagickBooleanType DrawGradientImage(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003143% const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003144%
3145% A description of each parameter follows:
3146%
3147% o image: the image.
3148%
anthony2a021472011-10-08 11:29:29 +00003149% o draw_info: the draw info.
cristy3ed852e2009-09-05 21:47:34 +00003150%
cristy947cb4c2011-10-20 18:41:46 +00003151% o exception: return any errors or warnings in this structure.
3152%
cristy3ed852e2009-09-05 21:47:34 +00003153*/
3154
3155static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
cristybb503372010-05-27 20:51:26 +00003156 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00003157{
3158 switch (gradient->type)
3159 {
3160 case UndefinedGradient:
3161 case LinearGradient:
3162 {
3163 MagickRealType
3164 gamma,
3165 length,
3166 offset,
3167 scale;
3168
3169 PointInfo
3170 p,
3171 q;
3172
3173 const SegmentInfo
3174 *gradient_vector;
3175
3176 gradient_vector=(&gradient->gradient_vector);
3177 p.x=gradient_vector->x2-gradient_vector->x1;
3178 p.y=gradient_vector->y2-gradient_vector->y1;
3179 q.x=(double) x-gradient_vector->x1;
3180 q.y=(double) y-gradient_vector->y1;
3181 length=sqrt(q.x*q.x+q.y*q.y);
3182 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3183 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3184 scale=p.x*q.x+p.y*q.y;
3185 offset=gamma*scale*length;
3186 return(offset);
3187 }
3188 case RadialGradient:
3189 {
3190 MagickRealType
3191 length,
3192 offset;
3193
3194 PointInfo
3195 v;
3196
3197 v.x=(double) x-gradient->center.x;
3198 v.y=(double) y-gradient->center.y;
3199 length=sqrt(v.x*v.x+v.y*v.y);
3200 if (gradient->spread == RepeatSpread)
3201 return(length);
3202 offset=length/gradient->radius;
3203 return(offset);
3204 }
3205 }
3206 return(0.0);
3207}
3208
3209MagickExport MagickBooleanType DrawGradientImage(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003210 const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003211{
cristyc4c8d132010-01-07 01:58:38 +00003212 CacheView
3213 *image_view;
3214
cristy3ed852e2009-09-05 21:47:34 +00003215 const GradientInfo
3216 *gradient;
3217
3218 const SegmentInfo
3219 *gradient_vector;
3220
cristy3ed852e2009-09-05 21:47:34 +00003221 MagickBooleanType
3222 status;
3223
cristy4c08aed2011-07-01 19:47:50 +00003224 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003225 zero;
3226
3227 MagickRealType
3228 length;
3229
3230 PointInfo
3231 point;
3232
3233 RectangleInfo
3234 bounding_box;
3235
cristy826a5472010-08-31 23:21:38 +00003236 ssize_t
3237 y;
3238
cristy3ed852e2009-09-05 21:47:34 +00003239 /*
3240 Draw linear or radial gradient on image.
3241 */
3242 assert(image != (Image *) NULL);
3243 assert(image->signature == MagickSignature);
3244 if (image->debug != MagickFalse)
3245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3246 assert(draw_info != (const DrawInfo *) NULL);
3247 gradient=(&draw_info->gradient);
3248 gradient_vector=(&gradient->gradient_vector);
3249 point.x=gradient_vector->x2-gradient_vector->x1;
3250 point.y=gradient_vector->y2-gradient_vector->y1;
3251 length=sqrt(point.x*point.x+point.y*point.y);
3252 bounding_box=gradient->bounding_box;
3253 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00003254 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +00003255 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003256#if defined(MAGICKCORE_OPENMP_SUPPORT)
3257 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003258#endif
cristybb503372010-05-27 20:51:26 +00003259 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
cristy3ed852e2009-09-05 21:47:34 +00003260 {
cristy4c08aed2011-07-01 19:47:50 +00003261 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003262 composite,
3263 pixel;
3264
3265 MagickRealType
3266 alpha,
3267 offset;
3268
cristy4c08aed2011-07-01 19:47:50 +00003269 register Quantum
3270 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003271
cristybb503372010-05-27 20:51:26 +00003272 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003273 i,
3274 x;
3275
cristy826a5472010-08-31 23:21:38 +00003276 ssize_t
3277 j;
3278
cristy3ed852e2009-09-05 21:47:34 +00003279 if (status == MagickFalse)
3280 continue;
3281 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003282 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003283 {
3284 status=MagickFalse;
3285 continue;
3286 }
cristy3ed852e2009-09-05 21:47:34 +00003287 pixel=zero;
3288 composite=zero;
3289 offset=GetStopColorOffset(gradient,0,y);
3290 if (gradient->type != RadialGradient)
3291 offset/=length;
cristybb503372010-05-27 20:51:26 +00003292 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
cristy3ed852e2009-09-05 21:47:34 +00003293 {
cristy4c08aed2011-07-01 19:47:50 +00003294 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00003295 switch (gradient->spread)
3296 {
3297 case UndefinedSpread:
3298 case PadSpread:
3299 {
cristybb503372010-05-27 20:51:26 +00003300 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3301 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003302 {
3303 offset=GetStopColorOffset(gradient,x,y);
3304 if (gradient->type != RadialGradient)
3305 offset/=length;
3306 }
cristybb503372010-05-27 20:51:26 +00003307 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003308 if (offset < gradient->stops[i].offset)
3309 break;
3310 if ((offset < 0.0) || (i == 0))
3311 composite=gradient->stops[0].color;
3312 else
cristybb503372010-05-27 20:51:26 +00003313 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
cristy3ed852e2009-09-05 21:47:34 +00003314 composite=gradient->stops[gradient->number_stops-1].color;
3315 else
3316 {
3317 j=i;
3318 i--;
3319 alpha=(offset-gradient->stops[i].offset)/
3320 (gradient->stops[j].offset-gradient->stops[i].offset);
cristy4c08aed2011-07-01 19:47:50 +00003321 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003322 &gradient->stops[j].color,alpha,&composite);
3323 }
3324 break;
3325 }
3326 case ReflectSpread:
3327 {
cristybb503372010-05-27 20:51:26 +00003328 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3329 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003330 {
3331 offset=GetStopColorOffset(gradient,x,y);
3332 if (gradient->type != RadialGradient)
3333 offset/=length;
3334 }
3335 if (offset < 0.0)
3336 offset=(-offset);
cristybb503372010-05-27 20:51:26 +00003337 if ((ssize_t) fmod(offset,2.0) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003338 offset=fmod(offset,1.0);
3339 else
3340 offset=1.0-fmod(offset,1.0);
cristybb503372010-05-27 20:51:26 +00003341 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003342 if (offset < gradient->stops[i].offset)
3343 break;
3344 if (i == 0)
3345 composite=gradient->stops[0].color;
3346 else
cristybb503372010-05-27 20:51:26 +00003347 if (i == (ssize_t) gradient->number_stops)
cristy3ed852e2009-09-05 21:47:34 +00003348 composite=gradient->stops[gradient->number_stops-1].color;
3349 else
3350 {
3351 j=i;
3352 i--;
3353 alpha=(offset-gradient->stops[i].offset)/
3354 (gradient->stops[j].offset-gradient->stops[i].offset);
cristy4c08aed2011-07-01 19:47:50 +00003355 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003356 &gradient->stops[j].color,alpha,&composite);
3357 }
3358 break;
3359 }
3360 case RepeatSpread:
3361 {
3362 MagickBooleanType
3363 antialias;
3364
3365 MagickRealType
3366 repeat;
3367
3368 antialias=MagickFalse;
3369 repeat=0.0;
cristybb503372010-05-27 20:51:26 +00003370 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3371 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003372 {
3373 offset=GetStopColorOffset(gradient,x,y);
3374 if (gradient->type == LinearGradient)
3375 {
3376 repeat=fmod(offset,length);
3377 if (repeat < 0.0)
3378 repeat=length-fmod(-repeat,length);
3379 else
3380 repeat=fmod(offset,length);
3381 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3382 MagickTrue : MagickFalse;
3383 offset=repeat/length;
3384 }
3385 else
3386 {
3387 repeat=fmod(offset,gradient->radius);
3388 if (repeat < 0.0)
3389 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3390 else
3391 repeat=fmod(offset,gradient->radius);
cristy4c08aed2011-07-01 19:47:50 +00003392 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3393 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003394 offset=repeat/gradient->radius;
3395 }
3396 }
cristybb503372010-05-27 20:51:26 +00003397 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003398 if (offset < gradient->stops[i].offset)
3399 break;
3400 if (i == 0)
3401 composite=gradient->stops[0].color;
3402 else
cristybb503372010-05-27 20:51:26 +00003403 if (i == (ssize_t) gradient->number_stops)
cristy3ed852e2009-09-05 21:47:34 +00003404 composite=gradient->stops[gradient->number_stops-1].color;
3405 else
3406 {
3407 j=i;
3408 i--;
3409 alpha=(offset-gradient->stops[i].offset)/
3410 (gradient->stops[j].offset-gradient->stops[i].offset);
3411 if (antialias != MagickFalse)
3412 {
3413 if (gradient->type == LinearGradient)
3414 alpha=length-repeat;
3415 else
3416 alpha=gradient->radius-repeat;
3417 i=0;
cristybb503372010-05-27 20:51:26 +00003418 j=(ssize_t) gradient->number_stops-1L;
cristy3ed852e2009-09-05 21:47:34 +00003419 }
cristy4c08aed2011-07-01 19:47:50 +00003420 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003421 &gradient->stops[j].color,alpha,&composite);
3422 }
3423 break;
3424 }
3425 }
cristy4c08aed2011-07-01 19:47:50 +00003426 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3427 &pixel);
3428 SetPixelPixelInfo(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003429 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003430 }
3431 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3432 status=MagickFalse;
3433 }
3434 image_view=DestroyCacheView(image_view);
3435 return(status);
3436}
3437
3438/*
3439%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3440% %
3441% %
3442% %
3443% D r a w P a t t e r n P a t h %
3444% %
3445% %
3446% %
3447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3448%
3449% DrawPatternPath() draws a pattern.
3450%
3451% The format of the DrawPatternPath method is:
3452%
3453% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
cristy018f07f2011-09-04 21:15:19 +00003454% const char *name,Image **pattern,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003455%
3456% A description of each parameter follows:
3457%
3458% o image: the image.
3459%
3460% o draw_info: the draw info.
3461%
3462% o name: the pattern name.
3463%
3464% o image: the image.
3465%
cristy018f07f2011-09-04 21:15:19 +00003466% o exception: return any errors or warnings in this structure.
3467%
cristy3ed852e2009-09-05 21:47:34 +00003468*/
3469MagickExport MagickBooleanType DrawPatternPath(Image *image,
cristy018f07f2011-09-04 21:15:19 +00003470 const DrawInfo *draw_info,const char *name,Image **pattern,
3471 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003472{
3473 char
3474 property[MaxTextExtent];
3475
3476 const char
3477 *geometry,
3478 *path;
3479
3480 DrawInfo
3481 *clone_info;
3482
3483 ImageInfo
3484 *image_info;
3485
3486 MagickBooleanType
3487 status;
3488
3489 assert(image != (Image *) NULL);
3490 assert(image->signature == MagickSignature);
3491 if (image->debug != MagickFalse)
3492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3493 assert(draw_info != (const DrawInfo *) NULL);
3494 assert(name != (const char *) NULL);
cristyb51dff52011-05-19 16:55:47 +00003495 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00003496 path=GetImageArtifact(image,property);
3497 if (path == (const char *) NULL)
3498 return(MagickFalse);
cristyb51dff52011-05-19 16:55:47 +00003499 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
cristy3ed852e2009-09-05 21:47:34 +00003500 geometry=GetImageArtifact(image,property);
3501 if (geometry == (const char *) NULL)
3502 return(MagickFalse);
3503 if ((*pattern) != (Image *) NULL)
3504 *pattern=DestroyImage(*pattern);
3505 image_info=AcquireImageInfo();
3506 image_info->size=AcquireString(geometry);
cristy947cb4c2011-10-20 18:41:46 +00003507 *pattern=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003508 image_info=DestroyImageInfo(image_info);
cristy9950d572011-10-01 18:22:35 +00003509 (void) QueryColorCompliance("#00000000",AllCompliance,
cristyea1a8aa2011-10-20 13:24:06 +00003510 &(*pattern)->background_color,exception);
3511 (void) SetImageBackgroundColor(*pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00003512 if (image->debug != MagickFalse)
3513 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3514 "begin pattern-path %s %s",name,geometry);
3515 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3516 clone_info->fill_pattern=NewImageList();
3517 clone_info->stroke_pattern=NewImageList();
3518 (void) CloneString(&clone_info->primitive,path);
cristy018f07f2011-09-04 21:15:19 +00003519 status=DrawImage(*pattern,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003520 clone_info=DestroyDrawInfo(clone_info);
3521 if (image->debug != MagickFalse)
3522 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3523 return(status);
3524}
3525
3526/*
3527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3528% %
3529% %
3530% %
3531+ D r a w P o l y g o n P r i m i t i v e %
3532% %
3533% %
3534% %
3535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3536%
3537% DrawPolygonPrimitive() draws a polygon on the image.
3538%
3539% The format of the DrawPolygonPrimitive method is:
3540%
3541% MagickBooleanType DrawPolygonPrimitive(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003542% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3543% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003544%
3545% A description of each parameter follows:
3546%
3547% o image: the image.
3548%
3549% o draw_info: the draw info.
3550%
3551% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3552%
cristy947cb4c2011-10-20 18:41:46 +00003553% o exception: return any errors or warnings in this structure.
3554%
cristy3ed852e2009-09-05 21:47:34 +00003555*/
3556
3557static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3558{
cristybb503372010-05-27 20:51:26 +00003559 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003560 i;
3561
3562 assert(polygon_info != (PolygonInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00003563 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00003564 if (polygon_info[i] != (PolygonInfo *) NULL)
3565 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
cristyb41ee102010-10-04 16:46:15 +00003566 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
cristy3ed852e2009-09-05 21:47:34 +00003567 return(polygon_info);
3568}
3569
3570static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3571 const PrimitiveInfo *primitive_info)
3572{
3573 PathInfo
cristyfa112112010-01-04 17:48:07 +00003574 *restrict path_info;
cristy3ed852e2009-09-05 21:47:34 +00003575
cristy3ed852e2009-09-05 21:47:34 +00003576 PolygonInfo
3577 **polygon_info;
3578
cristy826a5472010-08-31 23:21:38 +00003579 register ssize_t
3580 i;
3581
cristybb503372010-05-27 20:51:26 +00003582 size_t
cristy3ed852e2009-09-05 21:47:34 +00003583 number_threads;
3584
3585 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00003586 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +00003587 sizeof(*polygon_info));
3588 if (polygon_info == (PolygonInfo **) NULL)
3589 return((PolygonInfo **) NULL);
3590 (void) ResetMagickMemory(polygon_info,0,GetOpenMPMaximumThreads()*
3591 sizeof(*polygon_info));
3592 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3593 if (path_info == (PathInfo *) NULL)
3594 return(DestroyPolygonThreadSet(polygon_info));
cristybb503372010-05-27 20:51:26 +00003595 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00003596 {
3597 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3598 if (polygon_info[i] == (PolygonInfo *) NULL)
3599 return(DestroyPolygonThreadSet(polygon_info));
3600 }
3601 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3602 return(polygon_info);
3603}
3604
cristy4c08aed2011-07-01 19:47:50 +00003605static MagickRealType GetPixelOpacity(PolygonInfo *polygon_info,
cristy3ed852e2009-09-05 21:47:34 +00003606 const MagickRealType mid,const MagickBooleanType fill,
cristy77f38fb2010-04-22 15:51:47 +00003607 const FillRule fill_rule,const double x,const double y,
cristy3ed852e2009-09-05 21:47:34 +00003608 MagickRealType *stroke_opacity)
3609{
cristy3ed852e2009-09-05 21:47:34 +00003610 MagickRealType
cristyb32b90a2009-09-07 21:45:48 +00003611 alpha,
3612 beta,
cristy3ed852e2009-09-05 21:47:34 +00003613 distance,
cristy3ed852e2009-09-05 21:47:34 +00003614 subpath_opacity;
3615
3616 PointInfo
cristyb32b90a2009-09-07 21:45:48 +00003617 delta;
cristy3ed852e2009-09-05 21:47:34 +00003618
3619 register EdgeInfo
3620 *p;
3621
cristyb32b90a2009-09-07 21:45:48 +00003622 register const PointInfo
3623 *q;
3624
cristybb503372010-05-27 20:51:26 +00003625 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003626 i;
3627
cristycee97112010-05-28 00:44:52 +00003628 ssize_t
3629 j,
3630 winding_number;
3631
cristy3ed852e2009-09-05 21:47:34 +00003632 /*
3633 Compute fill & stroke opacity for this (x,y) point.
3634 */
3635 *stroke_opacity=0.0;
3636 subpath_opacity=0.0;
cristy3ed852e2009-09-05 21:47:34 +00003637 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +00003638 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
cristy3ed852e2009-09-05 21:47:34 +00003639 {
cristyb32b90a2009-09-07 21:45:48 +00003640 if (y <= (p->bounds.y1-mid-0.5))
cristy3ed852e2009-09-05 21:47:34 +00003641 break;
cristyb32b90a2009-09-07 21:45:48 +00003642 if (y > (p->bounds.y2+mid+0.5))
cristy3ed852e2009-09-05 21:47:34 +00003643 {
cristybb503372010-05-27 20:51:26 +00003644 (void) DestroyEdge(polygon_info,(size_t) j);
cristy3ed852e2009-09-05 21:47:34 +00003645 continue;
3646 }
cristyb32b90a2009-09-07 21:45:48 +00003647 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003648 continue;
cristybb503372010-05-27 20:51:26 +00003649 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3650 for ( ; i < (ssize_t) p->number_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00003651 {
cristyb32b90a2009-09-07 21:45:48 +00003652 if (y <= (p->points[i-1].y-mid-0.5))
cristy3ed852e2009-09-05 21:47:34 +00003653 break;
cristyb32b90a2009-09-07 21:45:48 +00003654 if (y > (p->points[i].y+mid+0.5))
cristy3ed852e2009-09-05 21:47:34 +00003655 continue;
cristyb32b90a2009-09-07 21:45:48 +00003656 if (p->scanline != y)
cristy3ed852e2009-09-05 21:47:34 +00003657 {
cristyb32b90a2009-09-07 21:45:48 +00003658 p->scanline=y;
cristybb503372010-05-27 20:51:26 +00003659 p->highwater=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00003660 }
3661 /*
3662 Compute distance between a point and an edge.
3663 */
cristyb32b90a2009-09-07 21:45:48 +00003664 q=p->points+i-1;
3665 delta.x=(q+1)->x-q->x;
3666 delta.y=(q+1)->y-q->y;
3667 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
cristy3ed852e2009-09-05 21:47:34 +00003668 if (beta < 0.0)
3669 {
cristyb32b90a2009-09-07 21:45:48 +00003670 delta.x=x-q->x;
3671 delta.y=y-q->y;
cristy3ed852e2009-09-05 21:47:34 +00003672 distance=delta.x*delta.x+delta.y*delta.y;
3673 }
3674 else
3675 {
3676 alpha=delta.x*delta.x+delta.y*delta.y;
3677 if (beta > alpha)
3678 {
cristyb32b90a2009-09-07 21:45:48 +00003679 delta.x=x-(q+1)->x;
3680 delta.y=y-(q+1)->y;
cristy3ed852e2009-09-05 21:47:34 +00003681 distance=delta.x*delta.x+delta.y*delta.y;
3682 }
3683 else
3684 {
cristyb32b90a2009-09-07 21:45:48 +00003685 alpha=1.0/alpha;
3686 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3687 distance=alpha*beta*beta;
cristy3ed852e2009-09-05 21:47:34 +00003688 }
3689 }
3690 /*
3691 Compute stroke & subpath opacity.
3692 */
3693 beta=0.0;
3694 if (p->ghostline == MagickFalse)
3695 {
cristyb32b90a2009-09-07 21:45:48 +00003696 alpha=mid+0.5;
cristy3ed852e2009-09-05 21:47:34 +00003697 if ((*stroke_opacity < 1.0) &&
3698 (distance <= ((alpha+0.25)*(alpha+0.25))))
3699 {
3700 alpha=mid-0.5;
3701 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3702 *stroke_opacity=1.0;
3703 else
3704 {
3705 beta=1.0;
3706 if (distance != 1.0)
3707 beta=sqrt((double) distance);
cristyb32b90a2009-09-07 21:45:48 +00003708 alpha=beta-mid-0.5;
cristy3ed852e2009-09-05 21:47:34 +00003709 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3710 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3711 }
3712 }
3713 }
3714 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3715 continue;
3716 if (distance <= 0.0)
3717 {
3718 subpath_opacity=1.0;
3719 continue;
3720 }
3721 if (distance > 1.0)
3722 continue;
3723 if (beta == 0.0)
3724 {
3725 beta=1.0;
3726 if (distance != 1.0)
cristyb32b90a2009-09-07 21:45:48 +00003727 beta=sqrt(distance);
cristy3ed852e2009-09-05 21:47:34 +00003728 }
3729 alpha=beta-1.0;
cristyb32b90a2009-09-07 21:45:48 +00003730 if (subpath_opacity < (alpha*alpha))
cristy3ed852e2009-09-05 21:47:34 +00003731 subpath_opacity=alpha*alpha;
3732 }
cristy3ed852e2009-09-05 21:47:34 +00003733 }
3734 /*
3735 Compute fill opacity.
3736 */
3737 if (fill == MagickFalse)
3738 return(0.0);
3739 if (subpath_opacity >= 1.0)
3740 return(1.0);
cristyb32b90a2009-09-07 21:45:48 +00003741 /*
3742 Determine winding number.
3743 */
3744 winding_number=0;
3745 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +00003746 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
cristyb32b90a2009-09-07 21:45:48 +00003747 {
3748 if (y <= p->bounds.y1)
3749 break;
3750 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3751 continue;
3752 if (x > p->bounds.x2)
3753 {
3754 winding_number+=p->direction ? 1 : -1;
3755 continue;
3756 }
cristybb503372010-05-27 20:51:26 +00003757 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3758 for ( ; i < (ssize_t) p->number_points; i++)
cristyb32b90a2009-09-07 21:45:48 +00003759 if (y <= p->points[i].y)
3760 break;
3761 q=p->points+i-1;
3762 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3763 winding_number+=p->direction ? 1 : -1;
3764 }
cristy3ed852e2009-09-05 21:47:34 +00003765 if (fill_rule != NonZeroRule)
3766 {
3767 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3768 return(1.0);
3769 }
3770 else
3771 if (MagickAbsoluteValue(winding_number) != 0)
3772 return(1.0);
3773 return(subpath_opacity);
3774}
3775
3776static MagickBooleanType DrawPolygonPrimitive(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003777 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3778 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003779{
cristyfa112112010-01-04 17:48:07 +00003780 CacheView
3781 *image_view;
3782
cristy3ed852e2009-09-05 21:47:34 +00003783 MagickBooleanType
3784 fill,
3785 status;
3786
3787 MagickRealType
3788 mid;
3789
3790 PolygonInfo
cristyfa112112010-01-04 17:48:07 +00003791 **restrict polygon_info;
cristy3ed852e2009-09-05 21:47:34 +00003792
3793 register EdgeInfo
3794 *p;
3795
cristybb503372010-05-27 20:51:26 +00003796 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003797 i;
3798
3799 SegmentInfo
3800 bounds;
3801
cristy826a5472010-08-31 23:21:38 +00003802 ssize_t
3803 start,
3804 stop,
3805 y;
3806
cristy3ed852e2009-09-05 21:47:34 +00003807 /*
3808 Compute bounding box.
3809 */
3810 assert(image != (Image *) NULL);
3811 assert(image->signature == MagickSignature);
3812 if (image->debug != MagickFalse)
3813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3814 assert(draw_info != (DrawInfo *) NULL);
3815 assert(draw_info->signature == MagickSignature);
3816 assert(primitive_info != (PrimitiveInfo *) NULL);
3817 if (primitive_info->coordinates == 0)
3818 return(MagickTrue);
3819 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3820 if (polygon_info == (PolygonInfo **) NULL)
3821 return(MagickFalse);
3822 if (0)
cristy947cb4c2011-10-20 18:41:46 +00003823 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
cristy3ed852e2009-09-05 21:47:34 +00003824 if (image->debug != MagickFalse)
3825 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3826 fill=(primitive_info->method == FillToBorderMethod) ||
3827 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3828 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3829 bounds=polygon_info[0]->edges[0].bounds;
cristybb503372010-05-27 20:51:26 +00003830 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00003831 {
3832 p=polygon_info[0]->edges+i;
3833 if (p->bounds.x1 < bounds.x1)
3834 bounds.x1=p->bounds.x1;
3835 if (p->bounds.y1 < bounds.y1)
3836 bounds.y1=p->bounds.y1;
3837 if (p->bounds.x2 > bounds.x2)
3838 bounds.x2=p->bounds.x2;
3839 if (p->bounds.y2 > bounds.y2)
3840 bounds.y2=p->bounds.y2;
3841 }
3842 bounds.x1-=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003843 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003844 image->columns ? (double) image->columns-1.0 : bounds.x1;
3845 bounds.y1-=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003846 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003847 image->rows ? (double) image->rows-1.0 : bounds.y1;
3848 bounds.x2+=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003849 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003850 image->columns ? (double) image->columns-1.0 : bounds.x2;
3851 bounds.y2+=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003852 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003853 image->rows ? (double) image->rows-1.0 : bounds.y2;
3854 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00003855 start=(ssize_t) ceil(bounds.x1-0.5);
3856 stop=(ssize_t) floor(bounds.x2+0.5);
cristy3ed852e2009-09-05 21:47:34 +00003857 image_view=AcquireCacheView(image);
3858 if (primitive_info->coordinates == 1)
3859 {
3860 /*
3861 Draw point.
3862 */
cristyb5d5f722009-11-04 03:03:49 +00003863#if defined(MAGICKCORE_OPENMP_SUPPORT)
3864 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003865#endif
cristybb503372010-05-27 20:51:26 +00003866 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
cristy3ed852e2009-09-05 21:47:34 +00003867 {
3868 MagickBooleanType
3869 sync;
3870
cristy101ab702011-10-13 13:06:32 +00003871 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00003872 pixel;
3873
cristybb503372010-05-27 20:51:26 +00003874 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003875 x;
3876
cristy4c08aed2011-07-01 19:47:50 +00003877 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003878 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003879
3880 if (status == MagickFalse)
3881 continue;
3882 x=start;
cristybb503372010-05-27 20:51:26 +00003883 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
cristy3ed852e2009-09-05 21:47:34 +00003884 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003885 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003886 {
3887 status=MagickFalse;
3888 continue;
3889 }
cristy101ab702011-10-13 13:06:32 +00003890 GetPixelInfo(image,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00003891 for ( ; x <= stop; x++)
3892 {
cristybb503372010-05-27 20:51:26 +00003893 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3894 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
cristy4c08aed2011-07-01 19:47:50 +00003895 {
cristy2ed42f62011-10-02 19:49:57 +00003896 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
cristy101ab702011-10-13 13:06:32 +00003897 SetPixelPixelInfo(image,&pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00003898 }
cristyed231572011-07-14 02:18:59 +00003899 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003900 }
3901 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3902 if (sync == MagickFalse)
3903 status=MagickFalse;
3904 }
3905 image_view=DestroyCacheView(image_view);
3906 polygon_info=DestroyPolygonThreadSet(polygon_info);
3907 if (image->debug != MagickFalse)
3908 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3909 " end draw-polygon");
3910 return(status);
3911 }
3912 /*
3913 Draw polygon or line.
3914 */
3915 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00003916 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristyb5d5f722009-11-04 03:03:49 +00003917#if defined(MAGICKCORE_OPENMP_SUPPORT)
3918 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003919#endif
cristybb503372010-05-27 20:51:26 +00003920 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
cristy3ed852e2009-09-05 21:47:34 +00003921 {
cristy5c9e6f22010-09-17 17:31:01 +00003922 const int
3923 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003924
cristy3ed852e2009-09-05 21:47:34 +00003925 MagickRealType
3926 fill_opacity,
3927 stroke_opacity;
3928
cristy101ab702011-10-13 13:06:32 +00003929 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003930 fill_color,
3931 stroke_color;
3932
cristy4c08aed2011-07-01 19:47:50 +00003933 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003934 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003935
cristy826a5472010-08-31 23:21:38 +00003936 register ssize_t
3937 x;
3938
cristy3ed852e2009-09-05 21:47:34 +00003939 if (status == MagickFalse)
3940 continue;
cristybb503372010-05-27 20:51:26 +00003941 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-
cristy3ed852e2009-09-05 21:47:34 +00003942 start+1),1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003943 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003944 {
3945 status=MagickFalse;
3946 continue;
3947 }
cristy3ed852e2009-09-05 21:47:34 +00003948 for (x=start; x <= stop; x++)
3949 {
3950 /*
3951 Fill and/or stroke.
3952 */
cristy4c08aed2011-07-01 19:47:50 +00003953 fill_opacity=GetPixelOpacity(polygon_info[id],mid,fill,
cristy77f38fb2010-04-22 15:51:47 +00003954 draw_info->fill_rule,(double) x,(double) y,&stroke_opacity);
cristy3ed852e2009-09-05 21:47:34 +00003955 if (draw_info->stroke_antialias == MagickFalse)
3956 {
3957 fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
3958 stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
3959 }
cristy2ed42f62011-10-02 19:49:57 +00003960 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy4c08aed2011-07-01 19:47:50 +00003961 fill_opacity=fill_opacity*fill_color.alpha;
3962 CompositePixelOver(image,&fill_color,fill_opacity,q,(MagickRealType)
3963 GetPixelAlpha(image,q),q);
cristy2ed42f62011-10-02 19:49:57 +00003964 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
cristy4c08aed2011-07-01 19:47:50 +00003965 stroke_opacity=stroke_opacity*stroke_color.alpha;
3966 CompositePixelOver(image,&stroke_color,stroke_opacity,q,(MagickRealType)
3967 GetPixelAlpha(image,q),q);
cristyed231572011-07-14 02:18:59 +00003968 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003969 }
3970 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3971 status=MagickFalse;
3972 }
3973 image_view=DestroyCacheView(image_view);
3974 polygon_info=DestroyPolygonThreadSet(polygon_info);
3975 if (image->debug != MagickFalse)
3976 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3977 return(status);
3978}
3979
3980/*
3981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3982% %
3983% %
3984% %
3985% D r a w P r i m i t i v e %
3986% %
3987% %
3988% %
3989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3990%
3991% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
3992%
3993% The format of the DrawPrimitive method is:
3994%
3995% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00003996% PrimitiveInfo *primitive_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003997%
3998% A description of each parameter follows:
3999%
4000% o image: the image.
4001%
4002% o draw_info: the draw info.
4003%
4004% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4005%
cristy947cb4c2011-10-20 18:41:46 +00004006% o exception: return any errors or warnings in this structure.
4007%
cristy3ed852e2009-09-05 21:47:34 +00004008*/
4009
4010static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4011{
4012 const char
4013 *methods[] =
4014 {
4015 "point",
4016 "replace",
4017 "floodfill",
4018 "filltoborder",
4019 "reset",
4020 "?"
4021 };
4022
cristy3ed852e2009-09-05 21:47:34 +00004023 PointInfo
4024 p,
4025 q,
4026 point;
4027
cristybb503372010-05-27 20:51:26 +00004028 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004029 i,
4030 x;
4031
cristy826a5472010-08-31 23:21:38 +00004032 ssize_t
4033 coordinates,
4034 y;
4035
cristybb503372010-05-27 20:51:26 +00004036 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4037 y=(ssize_t) ceil(primitive_info->point.y-0.5);
cristy3ed852e2009-09-05 21:47:34 +00004038 switch (primitive_info->primitive)
4039 {
4040 case PointPrimitive:
4041 {
4042 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004043 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004044 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004045 return;
4046 }
4047 case ColorPrimitive:
4048 {
4049 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004050 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004051 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004052 return;
4053 }
4054 case MattePrimitive:
4055 {
4056 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004057 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004058 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004059 return;
4060 }
4061 case TextPrimitive:
4062 {
4063 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004064 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00004065 return;
4066 }
4067 case ImagePrimitive:
4068 {
4069 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004070 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00004071 return;
4072 }
4073 default:
4074 break;
4075 }
4076 coordinates=0;
4077 p=primitive_info[0].point;
4078 q.x=(-1.0);
4079 q.y=(-1.0);
4080 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4081 {
4082 point=primitive_info[i].point;
4083 if (coordinates <= 0)
4084 {
cristybb503372010-05-27 20:51:26 +00004085 coordinates=(ssize_t) primitive_info[i].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00004086 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004087 " begin open (%.20g)",(double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004088 p=point;
4089 }
4090 point=primitive_info[i].point;
4091 if ((fabs(q.x-point.x) > MagickEpsilon) ||
4092 (fabs(q.y-point.y) > MagickEpsilon))
cristy8cd5b312010-01-07 01:10:24 +00004093 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004094 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
cristy3ed852e2009-09-05 21:47:34 +00004095 else
4096 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +00004097 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
cristy3ed852e2009-09-05 21:47:34 +00004098 q=point;
4099 coordinates--;
4100 if (coordinates > 0)
4101 continue;
4102 if ((fabs(p.x-point.x) > MagickEpsilon) ||
4103 (fabs(p.y-point.y) > MagickEpsilon))
cristye8c25f92010-06-03 00:53:06 +00004104 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4105 (double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004106 else
cristye8c25f92010-06-03 00:53:06 +00004107 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4108 (double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004109 }
4110}
4111
4112MagickExport MagickBooleanType DrawPrimitive(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00004113 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4114 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004115{
cristyc4c8d132010-01-07 01:58:38 +00004116 CacheView
4117 *image_view;
4118
cristy3ed852e2009-09-05 21:47:34 +00004119 MagickStatusType
4120 status;
4121
cristybb503372010-05-27 20:51:26 +00004122 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004123 i,
4124 x;
4125
cristy826a5472010-08-31 23:21:38 +00004126 ssize_t
4127 y;
4128
cristy3ed852e2009-09-05 21:47:34 +00004129 if (image->debug != MagickFalse)
4130 {
4131 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4132 " begin draw-primitive");
4133 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +00004134 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
cristy3ed852e2009-09-05 21:47:34 +00004135 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4136 draw_info->affine.tx,draw_info->affine.ty);
4137 }
4138 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004139 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4140 y=(ssize_t) ceil(primitive_info->point.y-0.5);
cristy3ed852e2009-09-05 21:47:34 +00004141 image_view=AcquireCacheView(image);
4142 switch (primitive_info->primitive)
4143 {
4144 case PointPrimitive:
4145 {
cristy101ab702011-10-13 13:06:32 +00004146 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004147 fill_color;
4148
cristy4c08aed2011-07-01 19:47:50 +00004149 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004150 *q;
4151
cristybb503372010-05-27 20:51:26 +00004152 if ((y < 0) || (y >= (ssize_t) image->rows))
cristyb32b90a2009-09-07 21:45:48 +00004153 break;
cristybb503372010-05-27 20:51:26 +00004154 if ((x < 0) || (x >= (ssize_t) image->columns))
cristyb32b90a2009-09-07 21:45:48 +00004155 break;
cristy3ed852e2009-09-05 21:47:34 +00004156 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004157 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004158 break;
cristy2ed42f62011-10-02 19:49:57 +00004159 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy4c08aed2011-07-01 19:47:50 +00004160 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4161 (MagickRealType) GetPixelAlpha(image,q),q);
cristy3ed852e2009-09-05 21:47:34 +00004162 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4163 break;
4164 }
4165 case ColorPrimitive:
4166 {
4167 switch (primitive_info->method)
4168 {
4169 case PointMethod:
4170 default:
4171 {
cristy101ab702011-10-13 13:06:32 +00004172 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00004173 pixel;
4174
4175 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004176 *q;
4177
4178 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004179 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004180 break;
cristy101ab702011-10-13 13:06:32 +00004181 GetPixelInfo(image,&pixel);
cristy2ed42f62011-10-02 19:49:57 +00004182 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristy101ab702011-10-13 13:06:32 +00004183 SetPixelPixelInfo(image,&pixel,q);
cristy3ed852e2009-09-05 21:47:34 +00004184 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4185 break;
4186 }
4187 case ReplaceMethod:
4188 {
4189 MagickBooleanType
4190 sync;
4191
cristy101ab702011-10-13 13:06:32 +00004192 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00004193 pixel,
cristy3ed852e2009-09-05 21:47:34 +00004194 target;
4195
cristy2ed42f62011-10-02 19:49:57 +00004196 Quantum
4197 virtual_pixel[MaxPixelChannels];
4198
4199 (void) GetOneCacheViewVirtualPixel(image_view,x,y,virtual_pixel,
4200 exception);
cristyda1f9c12011-10-02 21:39:49 +00004201 target.red=(double) virtual_pixel[RedPixelChannel];
4202 target.green=(double) virtual_pixel[GreenPixelChannel];
4203 target.blue=(double) virtual_pixel[BluePixelChannel];
4204 target.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004205 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004206 {
cristy4c08aed2011-07-01 19:47:50 +00004207 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004208 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004209
4210 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4211 exception);
cristy4c08aed2011-07-01 19:47:50 +00004212 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004213 break;
cristybb503372010-05-27 20:51:26 +00004214 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004215 {
cristy101ab702011-10-13 13:06:32 +00004216 GetPixelInfoPixel(image,q,&pixel);
4217 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004218 {
cristyed231572011-07-14 02:18:59 +00004219 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004220 continue;
4221 }
cristy2ed42f62011-10-02 19:49:57 +00004222 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristy101ab702011-10-13 13:06:32 +00004223 SetPixelPixelInfo(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00004224 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004225 }
4226 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4227 if (sync == MagickFalse)
4228 break;
4229 }
4230 break;
4231 }
4232 case FloodfillMethod:
4233 case FillToBorderMethod:
4234 {
cristy4c08aed2011-07-01 19:47:50 +00004235 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004236 target;
4237
cristy52010022011-10-21 18:07:37 +00004238 (void) GetOneVirtualMagickPixel(image,TileVirtualPixelMethod,x,y,
4239 &target,exception);
cristy3ed852e2009-09-05 21:47:34 +00004240 if (primitive_info->method == FillToBorderMethod)
4241 {
4242 target.red=(MagickRealType) draw_info->border_color.red;
4243 target.green=(MagickRealType) draw_info->border_color.green;
4244 target.blue=(MagickRealType) draw_info->border_color.blue;
4245 }
cristyd42d9952011-07-08 14:21:50 +00004246 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4247 primitive_info->method == FloodfillMethod ? MagickFalse :
cristy189e84c2011-08-27 18:08:53 +00004248 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004249 break;
4250 }
4251 case ResetMethod:
4252 {
4253 MagickBooleanType
4254 sync;
4255
cristy101ab702011-10-13 13:06:32 +00004256 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00004257 pixel;
4258
cristy101ab702011-10-13 13:06:32 +00004259 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00004260 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004261 {
cristy4c08aed2011-07-01 19:47:50 +00004262 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004263 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004264
cristy826a5472010-08-31 23:21:38 +00004265 register ssize_t
4266 x;
4267
cristy3ed852e2009-09-05 21:47:34 +00004268 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4269 exception);
cristy4c08aed2011-07-01 19:47:50 +00004270 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004271 break;
cristybb503372010-05-27 20:51:26 +00004272 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004273 {
cristy2ed42f62011-10-02 19:49:57 +00004274 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristy101ab702011-10-13 13:06:32 +00004275 SetPixelPixelInfo(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00004276 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004277 }
4278 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4279 if (sync == MagickFalse)
4280 break;
4281 }
4282 break;
4283 }
4284 }
4285 break;
4286 }
4287 case MattePrimitive:
4288 {
4289 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004290 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004291 switch (primitive_info->method)
4292 {
4293 case PointMethod:
4294 default:
4295 {
cristy101ab702011-10-13 13:06:32 +00004296 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004297 pixel;
4298
cristy4c08aed2011-07-01 19:47:50 +00004299 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004300 *q;
4301
4302 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004303 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004304 break;
cristy2ed42f62011-10-02 19:49:57 +00004305 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristyda1f9c12011-10-02 21:39:49 +00004306 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00004307 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4308 break;
4309 }
4310 case ReplaceMethod:
4311 {
4312 MagickBooleanType
4313 sync;
4314
cristy101ab702011-10-13 13:06:32 +00004315 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004316 pixel,
4317 target;
4318
cristy2ed42f62011-10-02 19:49:57 +00004319 Quantum
4320 virtual_pixel[MaxPixelChannels];
4321
4322 (void) GetOneCacheViewVirtualPixel(image_view,x,y,virtual_pixel,
4323 exception);
cristyda1f9c12011-10-02 21:39:49 +00004324 target.red=(double) virtual_pixel[RedPixelChannel];
4325 target.green=(double) virtual_pixel[GreenPixelChannel];
4326 target.blue=(double) virtual_pixel[BluePixelChannel];
4327 target.alpha=(double) virtual_pixel[AlphaPixelChannel];
cristybb503372010-05-27 20:51:26 +00004328 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004329 {
cristy4c08aed2011-07-01 19:47:50 +00004330 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004331 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004332
cristy826a5472010-08-31 23:21:38 +00004333 register ssize_t
4334 x;
4335
cristy3ed852e2009-09-05 21:47:34 +00004336 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4337 exception);
cristy4c08aed2011-07-01 19:47:50 +00004338 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004339 break;
cristybb503372010-05-27 20:51:26 +00004340 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004341 {
cristy101ab702011-10-13 13:06:32 +00004342 GetPixelInfoPixel(image,q,&pixel);
4343 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004344 {
cristyed231572011-07-14 02:18:59 +00004345 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004346 continue;
4347 }
cristy2ed42f62011-10-02 19:49:57 +00004348 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristyda1f9c12011-10-02 21:39:49 +00004349 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004350 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004351 }
4352 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4353 if (sync == MagickFalse)
4354 break;
4355 }
4356 break;
4357 }
4358 case FloodfillMethod:
4359 case FillToBorderMethod:
4360 {
cristybd5a96c2011-08-21 00:04:26 +00004361 ChannelType
4362 channel_mask;
4363
cristy4c08aed2011-07-01 19:47:50 +00004364 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004365 target;
4366
cristy52010022011-10-21 18:07:37 +00004367 (void) GetOneVirtualMagickPixel(image,TileVirtualPixelMethod,x,y,
4368 &target,exception);
cristy3ed852e2009-09-05 21:47:34 +00004369 if (primitive_info->method == FillToBorderMethod)
4370 {
4371 target.red=(MagickRealType) draw_info->border_color.red;
4372 target.green=(MagickRealType) draw_info->border_color.green;
4373 target.blue=(MagickRealType) draw_info->border_color.blue;
4374 }
cristybd5a96c2011-08-21 00:04:26 +00004375 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristyd42d9952011-07-08 14:21:50 +00004376 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
cristy3ed852e2009-09-05 21:47:34 +00004377 primitive_info->method == FloodfillMethod ? MagickFalse :
cristy189e84c2011-08-27 18:08:53 +00004378 MagickTrue,exception);
cristybd5a96c2011-08-21 00:04:26 +00004379 (void) SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004380 break;
4381 }
4382 case ResetMethod:
4383 {
4384 MagickBooleanType
4385 sync;
4386
cristy101ab702011-10-13 13:06:32 +00004387 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004388 pixel;
4389
cristybb503372010-05-27 20:51:26 +00004390 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004391 {
cristy4c08aed2011-07-01 19:47:50 +00004392 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004393 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004394
cristy826a5472010-08-31 23:21:38 +00004395 register ssize_t
4396 x;
4397
cristy3ed852e2009-09-05 21:47:34 +00004398 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4399 exception);
cristy4c08aed2011-07-01 19:47:50 +00004400 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004401 break;
cristybb503372010-05-27 20:51:26 +00004402 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004403 {
cristy2ed42f62011-10-02 19:49:57 +00004404 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristyda1f9c12011-10-02 21:39:49 +00004405 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004406 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004407 }
4408 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4409 if (sync == MagickFalse)
4410 break;
4411 }
4412 break;
4413 }
4414 }
4415 break;
4416 }
4417 case TextPrimitive:
4418 {
4419 char
4420 geometry[MaxTextExtent];
4421
4422 DrawInfo
4423 *clone_info;
4424
4425 if (primitive_info->text == (char *) NULL)
4426 break;
4427 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4428 (void) CloneString(&clone_info->text,primitive_info->text);
cristyb51dff52011-05-19 16:55:47 +00004429 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
cristy3ed852e2009-09-05 21:47:34 +00004430 primitive_info->point.x,primitive_info->point.y);
4431 (void) CloneString(&clone_info->geometry,geometry);
cristy5cbc0162011-08-29 00:36:28 +00004432 status=AnnotateImage(image,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004433 clone_info=DestroyDrawInfo(clone_info);
4434 break;
4435 }
4436 case ImagePrimitive:
4437 {
4438 AffineMatrix
4439 affine;
4440
4441 char
4442 composite_geometry[MaxTextExtent];
4443
4444 Image
4445 *composite_image;
4446
4447 ImageInfo
4448 *clone_info;
4449
cristy826a5472010-08-31 23:21:38 +00004450 RectangleInfo
4451 geometry;
4452
cristybb503372010-05-27 20:51:26 +00004453 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004454 x1,
4455 y1;
4456
cristy3ed852e2009-09-05 21:47:34 +00004457 if (primitive_info->text == (char *) NULL)
4458 break;
4459 clone_info=AcquireImageInfo();
4460 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4461 composite_image=ReadInlineImage(clone_info,primitive_info->text,
cristy947cb4c2011-10-20 18:41:46 +00004462 exception);
cristy3ed852e2009-09-05 21:47:34 +00004463 else
4464 {
4465 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4466 MaxTextExtent);
cristy947cb4c2011-10-20 18:41:46 +00004467 composite_image=ReadImage(clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004468 }
4469 clone_info=DestroyImageInfo(clone_info);
4470 if (composite_image == (Image *) NULL)
4471 break;
4472 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4473 NULL,(void *) NULL);
cristybb503372010-05-27 20:51:26 +00004474 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4475 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4476 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4477 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
cristy3ed852e2009-09-05 21:47:34 +00004478 {
4479 char
4480 geometry[MaxTextExtent];
4481
4482 /*
4483 Resize image.
4484 */
cristyb51dff52011-05-19 16:55:47 +00004485 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
cristy3ed852e2009-09-05 21:47:34 +00004486 primitive_info[1].point.x,primitive_info[1].point.y);
4487 composite_image->filter=image->filter;
cristye941a752011-10-15 01:52:48 +00004488 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4489 exception);
cristy3ed852e2009-09-05 21:47:34 +00004490 }
4491 if (composite_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004492 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4493 exception);
cristy4c08aed2011-07-01 19:47:50 +00004494 if (draw_info->alpha != OpaqueAlpha)
cristye941a752011-10-15 01:52:48 +00004495 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
cristy3ed852e2009-09-05 21:47:34 +00004496 SetGeometry(image,&geometry);
4497 image->gravity=draw_info->gravity;
4498 geometry.x=x;
4499 geometry.y=y;
cristyb51dff52011-05-19 16:55:47 +00004500 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00004501 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
cristye8c25f92010-06-03 00:53:06 +00004502 composite_image->rows,(double) geometry.x,(double) geometry.y);
cristy947cb4c2011-10-20 18:41:46 +00004503 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
cristy3ed852e2009-09-05 21:47:34 +00004504 affine=draw_info->affine;
4505 affine.tx=(double) geometry.x;
4506 affine.ty=(double) geometry.y;
4507 composite_image->interpolate=image->interpolate;
cristyd5f3fc32011-04-26 14:44:36 +00004508 if (draw_info->compose == OverCompositeOp)
cristy947cb4c2011-10-20 18:41:46 +00004509 (void) DrawAffineImage(image,composite_image,&affine,exception);
cristyd5f3fc32011-04-26 14:44:36 +00004510 else
4511 (void) CompositeImage(image,draw_info->compose,composite_image,
cristye941a752011-10-15 01:52:48 +00004512 geometry.x,geometry.y,exception);
cristy3ed852e2009-09-05 21:47:34 +00004513 composite_image=DestroyImage(composite_image);
4514 break;
4515 }
4516 default:
4517 {
4518 MagickRealType
4519 mid,
4520 scale;
4521
4522 DrawInfo
4523 *clone_info;
4524
4525 if (IsEventLogging() != MagickFalse)
4526 LogPrimitiveInfo(primitive_info);
4527 scale=ExpandAffine(&draw_info->affine);
4528 if ((draw_info->dash_pattern != (double *) NULL) &&
4529 (draw_info->dash_pattern[0] != 0.0) &&
4530 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
cristy4c08aed2011-07-01 19:47:50 +00004531 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00004532 {
4533 /*
4534 Draw dash polygon.
4535 */
4536 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4537 clone_info->stroke_width=0.0;
cristy4c08aed2011-07-01 19:47:50 +00004538 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy947cb4c2011-10-20 18:41:46 +00004539 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4540 exception);
cristy3ed852e2009-09-05 21:47:34 +00004541 clone_info=DestroyDrawInfo(clone_info);
cristy947cb4c2011-10-20 18:41:46 +00004542 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00004543 break;
4544 }
4545 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4546 if ((mid > 1.0) &&
cristy4c08aed2011-07-01 19:47:50 +00004547 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00004548 {
4549 MagickBooleanType
4550 closed_path;
4551
4552 /*
4553 Draw strokes while respecting line cap/join attributes.
4554 */
4555 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4556 closed_path=
4557 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4558 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4559 MagickTrue : MagickFalse;
cristybb503372010-05-27 20:51:26 +00004560 i=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00004561 if ((((draw_info->linecap == RoundCap) ||
4562 (closed_path != MagickFalse)) &&
4563 (draw_info->linejoin == RoundJoin)) ||
4564 (primitive_info[i].primitive != UndefinedPrimitive))
4565 {
cristy947cb4c2011-10-20 18:41:46 +00004566 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4567 exception);
cristy3ed852e2009-09-05 21:47:34 +00004568 break;
4569 }
4570 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4571 clone_info->stroke_width=0.0;
cristy4c08aed2011-07-01 19:47:50 +00004572 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy947cb4c2011-10-20 18:41:46 +00004573 status=DrawPolygonPrimitive(image,clone_info,primitive_info,
4574 exception);
cristy3ed852e2009-09-05 21:47:34 +00004575 clone_info=DestroyDrawInfo(clone_info);
cristy947cb4c2011-10-20 18:41:46 +00004576 status|=DrawStrokePolygon(image,draw_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004577 break;
4578 }
cristy947cb4c2011-10-20 18:41:46 +00004579 status=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004580 break;
4581 }
4582 }
4583 image_view=DestroyCacheView(image_view);
4584 if (image->debug != MagickFalse)
4585 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4586 return(status != 0 ? MagickTrue : MagickFalse);
4587}
4588
4589/*
4590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4591% %
4592% %
4593% %
4594+ D r a w S t r o k e P o l y g o n %
4595% %
4596% %
4597% %
4598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4599%
4600% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4601% the image while respecting the line cap and join attributes.
4602%
4603% The format of the DrawStrokePolygon method is:
4604%
4605% MagickBooleanType DrawStrokePolygon(Image *image,
4606% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4607%
4608% A description of each parameter follows:
4609%
4610% o image: the image.
4611%
4612% o draw_info: the draw info.
4613%
4614% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4615%
4616%
4617*/
4618
4619static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00004620 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004621{
4622 PrimitiveInfo
4623 linecap[5];
4624
cristybb503372010-05-27 20:51:26 +00004625 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004626 i;
4627
4628 for (i=0; i < 4; i++)
4629 linecap[i]=(*primitive_info);
4630 linecap[0].coordinates=4;
4631 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4632 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4633 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4634 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4635 linecap[4].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00004636 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
cristy3ed852e2009-09-05 21:47:34 +00004637}
4638
4639static MagickBooleanType DrawStrokePolygon(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00004640 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4641 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004642{
4643 DrawInfo
4644 *clone_info;
4645
4646 MagickBooleanType
4647 closed_path,
4648 status;
4649
4650 PrimitiveInfo
4651 *stroke_polygon;
4652
4653 register const PrimitiveInfo
4654 *p,
4655 *q;
4656
4657 /*
4658 Draw stroked polygon.
4659 */
4660 if (image->debug != MagickFalse)
4661 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4662 " begin draw-stroke-polygon");
4663 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4664 clone_info->fill=draw_info->stroke;
cristy4c08aed2011-07-01 19:47:50 +00004665 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004666 clone_info->stroke_width=0.0;
4667 clone_info->fill_rule=NonZeroRule;
4668 status=MagickTrue;
4669 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4670 {
4671 stroke_polygon=TraceStrokePolygon(draw_info,p);
cristy947cb4c2011-10-20 18:41:46 +00004672 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
cristy3ed852e2009-09-05 21:47:34 +00004673 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4674 q=p+p->coordinates-1;
4675 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4676 MagickTrue : MagickFalse;
4677 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4678 {
cristy947cb4c2011-10-20 18:41:46 +00004679 DrawRoundLinecap(image,draw_info,p,exception);
4680 DrawRoundLinecap(image,draw_info,q,exception);
cristy3ed852e2009-09-05 21:47:34 +00004681 }
4682 }
4683 clone_info=DestroyDrawInfo(clone_info);
4684 if (image->debug != MagickFalse)
4685 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4686 " end draw-stroke-polygon");
4687 return(status);
4688}
4689
4690/*
4691%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4692% %
4693% %
4694% %
4695% G e t A f f i n e M a t r i x %
4696% %
4697% %
4698% %
4699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4700%
4701% GetAffineMatrix() returns an AffineMatrix initialized to the identity
4702% matrix.
4703%
4704% The format of the GetAffineMatrix method is:
4705%
4706% void GetAffineMatrix(AffineMatrix *affine_matrix)
4707%
4708% A description of each parameter follows:
4709%
4710% o affine_matrix: the affine matrix.
4711%
4712*/
4713MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4714{
4715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4716 assert(affine_matrix != (AffineMatrix *) NULL);
4717 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4718 affine_matrix->sx=1.0;
4719 affine_matrix->sy=1.0;
4720}
4721
4722/*
4723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4724% %
4725% %
4726% %
4727+ G e t D r a w I n f o %
4728% %
4729% %
4730% %
4731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4732%
anthony9c88e8f2011-09-29 11:31:53 +00004733% GetDrawInfo() initializes draw_info to default values from image_info.
cristy3ed852e2009-09-05 21:47:34 +00004734%
4735% The format of the GetDrawInfo method is:
4736%
4737% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4738%
4739% A description of each parameter follows:
4740%
4741% o image_info: the image info..
4742%
4743% o draw_info: the draw info.
4744%
4745*/
4746MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4747{
4748 const char
4749 *option;
4750
4751 ExceptionInfo
4752 *exception;
4753
cristy3ed852e2009-09-05 21:47:34 +00004754 /*
4755 Initialize draw attributes.
4756 */
4757 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4758 assert(draw_info != (DrawInfo *) NULL);
4759 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
cristy3ed852e2009-09-05 21:47:34 +00004760 GetAffineMatrix(&draw_info->affine);
4761 exception=AcquireExceptionInfo();
cristy9950d572011-10-01 18:22:35 +00004762 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4763 exception);
4764 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4765 exception);
cristy3ed852e2009-09-05 21:47:34 +00004766 draw_info->stroke_width=1.0;
cristy4c08aed2011-07-01 19:47:50 +00004767 draw_info->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004768 draw_info->fill_rule=EvenOddRule;
4769 draw_info->linecap=ButtCap;
4770 draw_info->linejoin=MiterJoin;
4771 draw_info->miterlimit=10;
4772 draw_info->decorate=NoDecoration;
cristy3ed852e2009-09-05 21:47:34 +00004773 draw_info->pointsize=12.0;
cristy4c08aed2011-07-01 19:47:50 +00004774 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004775 draw_info->compose=OverCompositeOp;
cristy3ed852e2009-09-05 21:47:34 +00004776 draw_info->render=MagickTrue;
4777 draw_info->debug=IsEventLogging();
cristy0245b5d2011-10-05 12:03:49 +00004778 if (image_info != (ImageInfo *) NULL)
4779 {
4780 draw_info->stroke_antialias=image_info->antialias;
4781 if (image_info->font != (char *) NULL)
4782 draw_info->font=AcquireString(image_info->font);
4783 if (image_info->density != (char *) NULL)
4784 draw_info->density=AcquireString(image_info->density);
4785 draw_info->text_antialias=image_info->antialias;
4786 if (image_info->pointsize != 0.0)
4787 draw_info->pointsize=image_info->pointsize;
4788 draw_info->border_color=image_info->border_color;
4789 if (image_info->server_name != (char *) NULL)
4790 draw_info->server_name=AcquireString(image_info->server_name);
4791 option=GetImageOption(image_info,"encoding");
4792 if (option != (const char *) NULL)
4793 (void) CloneString(&draw_info->encoding,option);
4794 option=GetImageOption(image_info,"kerning");
4795 if (option != (const char *) NULL)
4796 draw_info->kerning=InterpretLocaleValue(option,(char **) NULL);
4797 option=GetImageOption(image_info,"interline-spacing");
4798 if (option != (const char *) NULL)
4799 draw_info->interline_spacing=InterpretLocaleValue(option,
4800 (char **) NULL);
4801 draw_info->direction=UndefinedDirection;
4802 option=GetImageOption(image_info,"interword-spacing");
4803 if (option != (const char *) NULL)
4804 draw_info->interword_spacing=InterpretLocaleValue(option,
4805 (char **) NULL);
4806 option=GetImageOption(image_info,"direction");
4807 if (option != (const char *) NULL)
4808 draw_info->direction=(DirectionType) ParseCommandOption(
4809 MagickDirectionOptions,MagickFalse,option);
4810 option=GetImageOption(image_info,"fill");
4811 if (option != (const char *) NULL)
4812 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4813 exception);
4814 option=GetImageOption(image_info,"stroke");
4815 if (option != (const char *) NULL)
4816 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4817 exception);
4818 option=GetImageOption(image_info,"strokewidth");
4819 if (option != (const char *) NULL)
4820 draw_info->stroke_width=InterpretLocaleValue(option,(char **) NULL);
4821 option=GetImageOption(image_info,"undercolor");
4822 if (option != (const char *) NULL)
4823 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4824 exception);
4825 option=GetImageOption(image_info,"gravity");
4826 if (option != (const char *) NULL)
4827 draw_info->gravity=(GravityType) ParseCommandOption(
4828 MagickGravityOptions,MagickFalse,option);
4829 }
cristy3ed852e2009-09-05 21:47:34 +00004830 exception=DestroyExceptionInfo(exception);
4831 draw_info->signature=MagickSignature;
cristy3ed852e2009-09-05 21:47:34 +00004832}
4833
4834/*
4835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4836% %
4837% %
4838% %
4839+ P e r m u t a t e %
4840% %
4841% %
4842% %
4843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4844%
4845% Permutate() returns the permuation of the (n,k).
4846%
4847% The format of the Permutate method is:
4848%
cristybb503372010-05-27 20:51:26 +00004849% void Permutate(ssize_t n,ssize_t k)
cristy3ed852e2009-09-05 21:47:34 +00004850%
4851% A description of each parameter follows:
4852%
4853% o n:
4854%
4855% o k:
4856%
4857%
4858*/
cristybb503372010-05-27 20:51:26 +00004859static inline MagickRealType Permutate(const ssize_t n,const ssize_t k)
cristy3ed852e2009-09-05 21:47:34 +00004860{
4861 MagickRealType
4862 r;
4863
cristybb503372010-05-27 20:51:26 +00004864 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004865 i;
4866
4867 r=1.0;
4868 for (i=k+1; i <= n; i++)
4869 r*=i;
4870 for (i=1; i <= (n-k); i++)
4871 r/=i;
4872 return(r);
4873}
4874
4875/*
4876%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4877% %
4878% %
4879% %
4880+ T r a c e P r i m i t i v e %
4881% %
4882% %
4883% %
4884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4885%
4886% TracePrimitive is a collection of methods for generating graphic
4887% primitives such as arcs, ellipses, paths, etc.
4888%
4889*/
4890
4891static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4892 const PointInfo end,const PointInfo degrees)
4893{
4894 PointInfo
4895 center,
4896 radii;
4897
4898 center.x=0.5*(end.x+start.x);
4899 center.y=0.5*(end.y+start.y);
4900 radii.x=fabs(center.x-start.x);
4901 radii.y=fabs(center.y-start.y);
4902 TraceEllipse(primitive_info,center,radii,degrees);
4903}
4904
4905static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4906 const PointInfo end,const PointInfo arc,const MagickRealType angle,
4907 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4908{
4909 MagickRealType
4910 alpha,
4911 beta,
4912 delta,
4913 factor,
4914 gamma,
4915 theta;
4916
4917 PointInfo
4918 center,
4919 points[3],
4920 radii;
4921
4922 register MagickRealType
4923 cosine,
4924 sine;
4925
4926 register PrimitiveInfo
4927 *p;
4928
cristybb503372010-05-27 20:51:26 +00004929 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004930 i;
4931
cristybb503372010-05-27 20:51:26 +00004932 size_t
cristy3ed852e2009-09-05 21:47:34 +00004933 arc_segments;
4934
4935 if ((start.x == end.x) && (start.y == end.y))
4936 {
4937 TracePoint(primitive_info,end);
4938 return;
4939 }
4940 radii.x=fabs(arc.x);
4941 radii.y=fabs(arc.y);
4942 if ((radii.x == 0.0) || (radii.y == 0.0))
4943 {
4944 TraceLine(primitive_info,start,end);
4945 return;
4946 }
4947 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4948 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4949 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4950 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4951 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4952 (radii.y*radii.y);
4953 if (delta < MagickEpsilon)
4954 {
4955 TraceLine(primitive_info,start,end);
4956 return;
4957 }
4958 if (delta > 1.0)
4959 {
4960 radii.x*=sqrt((double) delta);
4961 radii.y*=sqrt((double) delta);
4962 }
4963 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4964 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4965 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4966 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4967 alpha=points[1].x-points[0].x;
4968 beta=points[1].y-points[0].y;
4969 factor=1.0/(alpha*alpha+beta*beta)-0.25;
4970 if (factor <= 0.0)
4971 factor=0.0;
4972 else
4973 {
4974 factor=sqrt((double) factor);
4975 if (sweep == large_arc)
4976 factor=(-factor);
4977 }
4978 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4979 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4980 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4981 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4982 if ((theta < 0.0) && (sweep != MagickFalse))
4983 theta+=(MagickRealType) (2.0*MagickPI);
4984 else
4985 if ((theta > 0.0) && (sweep == MagickFalse))
4986 theta-=(MagickRealType) (2.0*MagickPI);
cristybb503372010-05-27 20:51:26 +00004987 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
cristy20be8a02010-08-17 00:23:28 +00004988 MagickEpsilon))));
cristy3ed852e2009-09-05 21:47:34 +00004989 p=primitive_info;
cristybb503372010-05-27 20:51:26 +00004990 for (i=0; i < (ssize_t) arc_segments; i++)
cristy3ed852e2009-09-05 21:47:34 +00004991 {
4992 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
4993 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
4994 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
4995 sin(fmod((double) beta,DegreesToRadians(360.0)));
4996 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
4997 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
4998 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
4999 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5000 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5001 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5002 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5003 theta/arc_segments),DegreesToRadians(360.0))));
5004 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5005 theta/arc_segments),DegreesToRadians(360.0))));
5006 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5007 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5008 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5009 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5010 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5011 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5012 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5013 points[0].y);
5014 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5015 points[0].y);
5016 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5017 points[1].y);
5018 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5019 points[1].y);
5020 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5021 points[2].y);
5022 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5023 points[2].y);
cristybb503372010-05-27 20:51:26 +00005024 if (i == (ssize_t) (arc_segments-1))
cristy3ed852e2009-09-05 21:47:34 +00005025 (p+3)->point=end;
5026 TraceBezier(p,4);
5027 p+=p->coordinates;
5028 }
cristybb503372010-05-27 20:51:26 +00005029 primitive_info->coordinates=(size_t) (p-primitive_info);
5030 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005031 {
5032 p->primitive=primitive_info->primitive;
5033 p--;
5034 }
5035}
5036
5037static void TraceBezier(PrimitiveInfo *primitive_info,
cristybb503372010-05-27 20:51:26 +00005038 const size_t number_coordinates)
cristy3ed852e2009-09-05 21:47:34 +00005039{
5040 MagickRealType
5041 alpha,
5042 *coefficients,
5043 weight;
5044
5045 PointInfo
5046 end,
5047 point,
5048 *points;
5049
cristy826a5472010-08-31 23:21:38 +00005050 register PrimitiveInfo
5051 *p;
5052
cristybb503372010-05-27 20:51:26 +00005053 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005054 i,
5055 j;
5056
cristybb503372010-05-27 20:51:26 +00005057 size_t
cristy3ed852e2009-09-05 21:47:34 +00005058 control_points,
5059 quantum;
5060
5061 /*
5062 Allocate coeficients.
5063 */
5064 quantum=number_coordinates;
cristybb503372010-05-27 20:51:26 +00005065 for (i=0; i < (ssize_t) number_coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005066 {
cristybb503372010-05-27 20:51:26 +00005067 for (j=i+1; j < (ssize_t) number_coordinates; j++)
cristy3ed852e2009-09-05 21:47:34 +00005068 {
5069 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5070 if (alpha > (MagickRealType) quantum)
cristybb503372010-05-27 20:51:26 +00005071 quantum=(size_t) alpha;
cristy3ed852e2009-09-05 21:47:34 +00005072 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5073 if (alpha > (MagickRealType) quantum)
cristybb503372010-05-27 20:51:26 +00005074 quantum=(size_t) alpha;
cristy3ed852e2009-09-05 21:47:34 +00005075 }
5076 }
cristybb503372010-05-27 20:51:26 +00005077 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
cristy3ed852e2009-09-05 21:47:34 +00005078 (double) BezierQuantum);
5079 control_points=quantum*number_coordinates;
5080 coefficients=(MagickRealType *) AcquireQuantumMemory((size_t)
5081 number_coordinates,sizeof(*coefficients));
5082 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5083 sizeof(*points));
5084 if ((coefficients == (MagickRealType *) NULL) ||
5085 (points == (PointInfo *) NULL))
5086 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5087 /*
5088 Compute bezier points.
5089 */
5090 end=primitive_info[number_coordinates-1].point;
cristybb503372010-05-27 20:51:26 +00005091 for (i=0; i < (ssize_t) number_coordinates; i++)
5092 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
cristy3ed852e2009-09-05 21:47:34 +00005093 weight=0.0;
cristybb503372010-05-27 20:51:26 +00005094 for (i=0; i < (ssize_t) control_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00005095 {
5096 p=primitive_info;
5097 point.x=0.0;
5098 point.y=0.0;
5099 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
cristybb503372010-05-27 20:51:26 +00005100 for (j=0; j < (ssize_t) number_coordinates; j++)
cristy3ed852e2009-09-05 21:47:34 +00005101 {
5102 point.x+=alpha*coefficients[j]*p->point.x;
5103 point.y+=alpha*coefficients[j]*p->point.y;
5104 alpha*=weight/(1.0-weight);
5105 p++;
5106 }
5107 points[i]=point;
5108 weight+=1.0/control_points;
5109 }
5110 /*
5111 Bezier curves are just short segmented polys.
5112 */
5113 p=primitive_info;
cristybb503372010-05-27 20:51:26 +00005114 for (i=0; i < (ssize_t) control_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00005115 {
5116 TracePoint(p,points[i]);
5117 p+=p->coordinates;
5118 }
5119 TracePoint(p,end);
5120 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005121 primitive_info->coordinates=(size_t) (p-primitive_info);
5122 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005123 {
5124 p->primitive=primitive_info->primitive;
5125 p--;
5126 }
5127 points=(PointInfo *) RelinquishMagickMemory(points);
5128 coefficients=(MagickRealType *) RelinquishMagickMemory(coefficients);
5129}
5130
5131static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5132 const PointInfo end)
5133{
5134 MagickRealType
5135 alpha,
5136 beta,
5137 radius;
5138
5139 PointInfo
5140 offset,
5141 degrees;
5142
5143 alpha=end.x-start.x;
5144 beta=end.y-start.y;
5145 radius=hypot((double) alpha,(double) beta);
5146 offset.x=(double) radius;
5147 offset.y=(double) radius;
5148 degrees.x=0.0;
5149 degrees.y=360.0;
5150 TraceEllipse(primitive_info,start,offset,degrees);
5151}
5152
5153static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5154 const PointInfo stop,const PointInfo degrees)
5155{
5156 MagickRealType
5157 delta,
5158 step,
5159 y;
5160
5161 PointInfo
5162 angle,
5163 point;
5164
5165 register PrimitiveInfo
5166 *p;
5167
cristybb503372010-05-27 20:51:26 +00005168 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005169 i;
5170
5171 /*
5172 Ellipses are just short segmented polys.
5173 */
5174 if ((stop.x == 0.0) && (stop.y == 0.0))
5175 {
5176 TracePoint(primitive_info,start);
5177 return;
5178 }
5179 delta=2.0/MagickMax(stop.x,stop.y);
5180 step=(MagickRealType) (MagickPI/8.0);
cristyd4e3ffa2011-01-20 13:47:55 +00005181 if ((delta >= 0.0) && (delta < (MagickRealType) (MagickPI/8.0)))
5182 step=(MagickRealType) (MagickPI/(4*(MagickPI/delta/2+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00005183 angle.x=DegreesToRadians(degrees.x);
5184 y=degrees.y;
5185 while (y < degrees.x)
5186 y+=360.0;
5187 angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5188 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5189 {
5190 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5191 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5192 TracePoint(p,point);
5193 p+=p->coordinates;
5194 }
5195 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5196 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5197 TracePoint(p,point);
5198 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005199 primitive_info->coordinates=(size_t) (p-primitive_info);
5200 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005201 {
5202 p->primitive=primitive_info->primitive;
5203 p--;
5204 }
5205}
5206
5207static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5208 const PointInfo end)
5209{
5210 TracePoint(primitive_info,start);
5211 if ((fabs(start.x-end.x) <= MagickEpsilon) &&
5212 (fabs(start.y-end.y) <= MagickEpsilon))
5213 {
5214 primitive_info->primitive=PointPrimitive;
5215 primitive_info->coordinates=1;
5216 return;
5217 }
5218 TracePoint(primitive_info+1,end);
5219 (primitive_info+1)->primitive=primitive_info->primitive;
5220 primitive_info->coordinates=2;
5221}
5222
cristybb503372010-05-27 20:51:26 +00005223static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
cristy3ed852e2009-09-05 21:47:34 +00005224{
5225 char
5226 token[MaxTextExtent];
5227
5228 const char
5229 *p;
5230
5231 int
5232 attribute,
5233 last_attribute;
5234
5235 MagickRealType
5236 x,
5237 y;
5238
5239 PointInfo
5240 end,
5241 points[4],
5242 point,
5243 start;
5244
5245 PrimitiveType
5246 primitive_type;
5247
5248 register PrimitiveInfo
5249 *q;
5250
cristybb503372010-05-27 20:51:26 +00005251 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005252 i;
5253
cristybb503372010-05-27 20:51:26 +00005254 size_t
cristy3ed852e2009-09-05 21:47:34 +00005255 number_coordinates,
5256 z_count;
5257
5258 attribute=0;
5259 point.x=0.0;
5260 point.y=0.0;
5261 start.x=0.0;
5262 start.y=0.0;
5263 number_coordinates=0;
5264 z_count=0;
5265 primitive_type=primitive_info->primitive;
5266 q=primitive_info;
5267 for (p=path; *p != '\0'; )
5268 {
5269 while (isspace((int) ((unsigned char) *p)) != 0)
5270 p++;
5271 if (*p == '\0')
5272 break;
5273 last_attribute=attribute;
5274 attribute=(int) (*p++);
5275 switch (attribute)
5276 {
5277 case 'a':
5278 case 'A':
5279 {
5280 MagickBooleanType
5281 large_arc,
5282 sweep;
5283
5284 MagickRealType
5285 angle;
5286
5287 PointInfo
5288 arc;
5289
5290 /*
5291 Compute arc points.
5292 */
5293 do
5294 {
5295 GetMagickToken(p,&p,token);
5296 if (*token == ',')
5297 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005298 arc.x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005299 GetMagickToken(p,&p,token);
5300 if (*token == ',')
5301 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005302 arc.y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005303 GetMagickToken(p,&p,token);
5304 if (*token == ',')
5305 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005306 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005307 GetMagickToken(p,&p,token);
5308 if (*token == ',')
5309 GetMagickToken(p,&p,token);
cristyf2f27272009-12-17 14:48:46 +00005310 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00005311 GetMagickToken(p,&p,token);
5312 if (*token == ',')
5313 GetMagickToken(p,&p,token);
cristyf2f27272009-12-17 14:48:46 +00005314 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00005315 GetMagickToken(p,&p,token);
5316 if (*token == ',')
5317 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005318 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005319 GetMagickToken(p,&p,token);
5320 if (*token == ',')
5321 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005322 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005323 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5324 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5325 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5326 q+=q->coordinates;
5327 point=end;
5328 } while (IsPoint(p) != MagickFalse);
5329 break;
5330 }
5331 case 'c':
5332 case 'C':
5333 {
5334 /*
5335 Compute bezier points.
5336 */
5337 do
5338 {
5339 points[0]=point;
5340 for (i=1; i < 4; i++)
5341 {
5342 GetMagickToken(p,&p,token);
5343 if (*token == ',')
5344 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005345 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005346 GetMagickToken(p,&p,token);
5347 if (*token == ',')
5348 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005349 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005350 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5351 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5352 points[i]=end;
5353 }
5354 for (i=0; i < 4; i++)
5355 (q+i)->point=points[i];
5356 TraceBezier(q,4);
5357 q+=q->coordinates;
5358 point=end;
5359 } while (IsPoint(p) != MagickFalse);
5360 break;
5361 }
5362 case 'H':
5363 case 'h':
5364 {
5365 do
5366 {
5367 GetMagickToken(p,&p,token);
5368 if (*token == ',')
5369 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005370 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005371 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5372 TracePoint(q,point);
5373 q+=q->coordinates;
5374 } while (IsPoint(p) != MagickFalse);
5375 break;
5376 }
5377 case 'l':
5378 case 'L':
5379 {
5380 do
5381 {
5382 GetMagickToken(p,&p,token);
5383 if (*token == ',')
5384 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005385 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005386 GetMagickToken(p,&p,token);
5387 if (*token == ',')
5388 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005389 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005390 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5391 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5392 TracePoint(q,point);
5393 q+=q->coordinates;
5394 } while (IsPoint(p) != MagickFalse);
5395 break;
5396 }
5397 case 'M':
5398 case 'm':
5399 {
5400 if (q != primitive_info)
5401 {
cristybb503372010-05-27 20:51:26 +00005402 primitive_info->coordinates=(size_t) (q-primitive_info);
cristy3ed852e2009-09-05 21:47:34 +00005403 number_coordinates+=primitive_info->coordinates;
5404 primitive_info=q;
5405 }
5406 i=0;
5407 do
5408 {
5409 GetMagickToken(p,&p,token);
5410 if (*token == ',')
5411 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005412 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005413 GetMagickToken(p,&p,token);
5414 if (*token == ',')
5415 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005416 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005417 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5418 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5419 if (i == 0)
5420 start=point;
5421 i++;
5422 TracePoint(q,point);
5423 q+=q->coordinates;
cristy826a5472010-08-31 23:21:38 +00005424 if ((i != 0) && (attribute == (int) 'M'))
cristy3ed852e2009-09-05 21:47:34 +00005425 {
5426 TracePoint(q,point);
5427 q+=q->coordinates;
5428 }
5429 } while (IsPoint(p) != MagickFalse);
5430 break;
5431 }
5432 case 'q':
5433 case 'Q':
5434 {
5435 /*
5436 Compute bezier points.
5437 */
5438 do
5439 {
5440 points[0]=point;
5441 for (i=1; i < 3; i++)
5442 {
5443 GetMagickToken(p,&p,token);
5444 if (*token == ',')
5445 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005446 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005447 GetMagickToken(p,&p,token);
5448 if (*token == ',')
5449 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005450 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005451 if (*p == ',')
5452 p++;
5453 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5454 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5455 points[i]=end;
5456 }
5457 for (i=0; i < 3; i++)
5458 (q+i)->point=points[i];
5459 TraceBezier(q,3);
5460 q+=q->coordinates;
5461 point=end;
5462 } while (IsPoint(p) != MagickFalse);
5463 break;
5464 }
5465 case 's':
5466 case 'S':
5467 {
5468 /*
5469 Compute bezier points.
5470 */
5471 do
5472 {
5473 points[0]=points[3];
5474 points[1].x=2.0*points[3].x-points[2].x;
5475 points[1].y=2.0*points[3].y-points[2].y;
5476 for (i=2; i < 4; i++)
5477 {
5478 GetMagickToken(p,&p,token);
5479 if (*token == ',')
5480 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005481 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005482 GetMagickToken(p,&p,token);
5483 if (*token == ',')
5484 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005485 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005486 if (*p == ',')
5487 p++;
5488 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5489 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5490 points[i]=end;
5491 }
5492 if (strchr("CcSs",last_attribute) == (char *) NULL)
5493 {
5494 points[0]=points[2];
5495 points[1]=points[3];
5496 }
5497 for (i=0; i < 4; i++)
5498 (q+i)->point=points[i];
5499 TraceBezier(q,4);
5500 q+=q->coordinates;
5501 point=end;
5502 } while (IsPoint(p) != MagickFalse);
5503 break;
5504 }
5505 case 't':
5506 case 'T':
5507 {
5508 /*
5509 Compute bezier points.
5510 */
5511 do
5512 {
5513 points[0]=points[2];
5514 points[1].x=2.0*points[2].x-points[1].x;
5515 points[1].y=2.0*points[2].y-points[1].y;
5516 for (i=2; i < 3; i++)
5517 {
5518 GetMagickToken(p,&p,token);
5519 if (*token == ',')
5520 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005521 x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005522 GetMagickToken(p,&p,token);
5523 if (*token == ',')
5524 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005525 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005526 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5527 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5528 points[i]=end;
5529 }
5530 if (strchr("QqTt",last_attribute) == (char *) NULL)
5531 {
5532 points[0]=points[2];
5533 points[1]=points[3];
5534 }
5535 for (i=0; i < 3; i++)
5536 (q+i)->point=points[i];
5537 TraceBezier(q,3);
5538 q+=q->coordinates;
5539 point=end;
5540 } while (IsPoint(p) != MagickFalse);
5541 break;
5542 }
5543 case 'v':
5544 case 'V':
5545 {
5546 do
5547 {
5548 GetMagickToken(p,&p,token);
5549 if (*token == ',')
5550 GetMagickToken(p,&p,token);
cristyc1acd842011-05-19 23:05:47 +00005551 y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005552 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5553 TracePoint(q,point);
5554 q+=q->coordinates;
5555 } while (IsPoint(p) != MagickFalse);
5556 break;
5557 }
5558 case 'z':
5559 case 'Z':
5560 {
5561 point=start;
5562 TracePoint(q,point);
5563 q+=q->coordinates;
cristybb503372010-05-27 20:51:26 +00005564 primitive_info->coordinates=(size_t) (q-primitive_info);
cristy3ed852e2009-09-05 21:47:34 +00005565 number_coordinates+=primitive_info->coordinates;
5566 primitive_info=q;
5567 z_count++;
5568 break;
5569 }
5570 default:
5571 {
5572 if (isalpha((int) ((unsigned char) attribute)) != 0)
cristy1e604812011-05-19 18:07:50 +00005573 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5574 attribute);
cristy3ed852e2009-09-05 21:47:34 +00005575 break;
5576 }
5577 }
5578 }
cristybb503372010-05-27 20:51:26 +00005579 primitive_info->coordinates=(size_t) (q-primitive_info);
cristy3ed852e2009-09-05 21:47:34 +00005580 number_coordinates+=primitive_info->coordinates;
cristybb503372010-05-27 20:51:26 +00005581 for (i=0; i < (ssize_t) number_coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005582 {
5583 q--;
5584 q->primitive=primitive_type;
5585 if (z_count > 1)
5586 q->method=FillToBorderMethod;
5587 }
5588 q=primitive_info;
5589 return(number_coordinates);
5590}
5591
5592static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5593 const PointInfo end)
5594{
5595 PointInfo
5596 point;
5597
5598 register PrimitiveInfo
5599 *p;
5600
cristybb503372010-05-27 20:51:26 +00005601 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005602 i;
5603
5604 p=primitive_info;
5605 TracePoint(p,start);
5606 p+=p->coordinates;
5607 point.x=start.x;
5608 point.y=end.y;
5609 TracePoint(p,point);
5610 p+=p->coordinates;
5611 TracePoint(p,end);
5612 p+=p->coordinates;
5613 point.x=end.x;
5614 point.y=start.y;
5615 TracePoint(p,point);
5616 p+=p->coordinates;
5617 TracePoint(p,start);
5618 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005619 primitive_info->coordinates=(size_t) (p-primitive_info);
5620 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005621 {
5622 p->primitive=primitive_info->primitive;
5623 p--;
5624 }
5625}
5626
5627static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5628 const PointInfo start,const PointInfo end,PointInfo arc)
5629{
5630 PointInfo
5631 degrees,
5632 offset,
5633 point;
5634
5635 register PrimitiveInfo
5636 *p;
5637
cristybb503372010-05-27 20:51:26 +00005638 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005639 i;
5640
5641 p=primitive_info;
5642 offset.x=fabs(end.x-start.x);
5643 offset.y=fabs(end.y-start.y);
5644 if (arc.x > (0.5*offset.x))
5645 arc.x=0.5*offset.x;
5646 if (arc.y > (0.5*offset.y))
5647 arc.y=0.5*offset.y;
5648 point.x=start.x+offset.x-arc.x;
5649 point.y=start.y+arc.y;
5650 degrees.x=270.0;
5651 degrees.y=360.0;
5652 TraceEllipse(p,point,arc,degrees);
5653 p+=p->coordinates;
5654 point.x=start.x+offset.x-arc.x;
5655 point.y=start.y+offset.y-arc.y;
5656 degrees.x=0.0;
5657 degrees.y=90.0;
5658 TraceEllipse(p,point,arc,degrees);
5659 p+=p->coordinates;
5660 point.x=start.x+arc.x;
5661 point.y=start.y+offset.y-arc.y;
5662 degrees.x=90.0;
5663 degrees.y=180.0;
5664 TraceEllipse(p,point,arc,degrees);
5665 p+=p->coordinates;
5666 point.x=start.x+arc.x;
5667 point.y=start.y+arc.y;
5668 degrees.x=180.0;
5669 degrees.y=270.0;
5670 TraceEllipse(p,point,arc,degrees);
5671 p+=p->coordinates;
5672 TracePoint(p,primitive_info->point);
5673 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005674 primitive_info->coordinates=(size_t) (p-primitive_info);
5675 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005676 {
5677 p->primitive=primitive_info->primitive;
5678 p--;
5679 }
5680}
5681
5682static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
cristybb503372010-05-27 20:51:26 +00005683 const size_t number_vertices,const MagickRealType offset)
cristy3ed852e2009-09-05 21:47:34 +00005684{
5685 MagickRealType
5686 distance;
5687
cristy3ed852e2009-09-05 21:47:34 +00005688 register MagickRealType
5689 dx,
5690 dy;
5691
cristybb503372010-05-27 20:51:26 +00005692 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005693 i;
5694
cristy826a5472010-08-31 23:21:38 +00005695 ssize_t
5696 j;
5697
cristy3ed852e2009-09-05 21:47:34 +00005698 dx=0.0;
5699 dy=0.0;
cristybb503372010-05-27 20:51:26 +00005700 for (i=1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00005701 {
5702 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5703 dy=primitive_info[0].point.y-primitive_info[i].point.y;
5704 if ((fabs((double) dx) >= MagickEpsilon) ||
5705 (fabs((double) dy) >= MagickEpsilon))
5706 break;
5707 }
cristybb503372010-05-27 20:51:26 +00005708 if (i == (ssize_t) number_vertices)
5709 i=(ssize_t) number_vertices-1L;
cristy3ed852e2009-09-05 21:47:34 +00005710 distance=hypot((double) dx,(double) dy);
5711 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5712 dx*(distance+offset)/distance);
5713 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5714 dy*(distance+offset)/distance);
cristybb503372010-05-27 20:51:26 +00005715 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +00005716 {
5717 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5718 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
5719 if ((fabs((double) dx) >= MagickEpsilon) ||
5720 (fabs((double) dy) >= MagickEpsilon))
5721 break;
5722 }
5723 distance=hypot((double) dx,(double) dy);
5724 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5725 dx*(distance+offset)/distance);
5726 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5727 dy*(distance+offset)/distance);
5728}
5729
5730static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5731 const PrimitiveInfo *primitive_info)
5732{
5733 typedef struct _LineSegment
5734 {
5735 double
5736 p,
5737 q;
5738 } LineSegment;
5739
5740 LineSegment
5741 dx,
5742 dy,
5743 inverse_slope,
5744 slope,
5745 theta;
5746
cristy3ed852e2009-09-05 21:47:34 +00005747 MagickBooleanType
5748 closed_path;
5749
5750 MagickRealType
5751 delta_theta,
5752 dot_product,
5753 mid,
5754 miterlimit;
5755
5756 PointInfo
5757 box_p[5],
5758 box_q[5],
5759 center,
5760 offset,
5761 *path_p,
5762 *path_q;
5763
5764 PrimitiveInfo
5765 *polygon_primitive,
5766 *stroke_polygon;
5767
cristybb503372010-05-27 20:51:26 +00005768 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005769 i;
5770
cristybb503372010-05-27 20:51:26 +00005771 size_t
cristy3ed852e2009-09-05 21:47:34 +00005772 arc_segments,
5773 max_strokes,
5774 number_vertices;
5775
cristy826a5472010-08-31 23:21:38 +00005776 ssize_t
5777 j,
5778 n,
5779 p,
5780 q;
5781
cristy3ed852e2009-09-05 21:47:34 +00005782 /*
5783 Allocate paths.
5784 */
5785 number_vertices=primitive_info->coordinates;
5786 max_strokes=2*number_vertices+6*BezierQuantum+360;
5787 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5788 sizeof(*path_p));
5789 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5790 sizeof(*path_q));
5791 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5792 number_vertices+2UL,sizeof(*polygon_primitive));
5793 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5794 (polygon_primitive == (PrimitiveInfo *) NULL))
5795 return((PrimitiveInfo *) NULL);
5796 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5797 number_vertices*sizeof(*polygon_primitive));
5798 closed_path=
5799 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5800 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5801 MagickTrue : MagickFalse;
5802 if ((draw_info->linejoin == RoundJoin) ||
5803 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5804 {
5805 polygon_primitive[number_vertices]=primitive_info[1];
5806 number_vertices++;
5807 }
5808 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5809 /*
5810 Compute the slope for the first line segment, p.
5811 */
5812 dx.p=0.0;
5813 dy.p=0.0;
cristybb503372010-05-27 20:51:26 +00005814 for (n=1; n < (ssize_t) number_vertices; n++)
cristy3ed852e2009-09-05 21:47:34 +00005815 {
5816 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5817 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
5818 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
5819 break;
5820 }
cristybb503372010-05-27 20:51:26 +00005821 if (n == (ssize_t) number_vertices)
5822 n=(ssize_t) number_vertices-1L;
cristy3ed852e2009-09-05 21:47:34 +00005823 slope.p=0.0;
5824 inverse_slope.p=0.0;
5825 if (fabs(dx.p) <= MagickEpsilon)
5826 {
5827 if (dx.p >= 0.0)
5828 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5829 else
5830 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5831 }
5832 else
5833 if (fabs(dy.p) <= MagickEpsilon)
5834 {
5835 if (dy.p >= 0.0)
5836 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5837 else
5838 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5839 }
5840 else
5841 {
5842 slope.p=dy.p/dx.p;
5843 inverse_slope.p=(-1.0/slope.p);
5844 }
5845 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5846 miterlimit=(MagickRealType) (draw_info->miterlimit*draw_info->miterlimit*
5847 mid*mid);
5848 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5849 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5850 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5851 offset.y=(double) (offset.x*inverse_slope.p);
5852 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5853 {
5854 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5855 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5856 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5857 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5858 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5859 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5860 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5861 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5862 }
5863 else
5864 {
5865 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5866 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5867 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5868 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5869 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5870 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5871 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5872 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5873 }
5874 /*
5875 Create strokes for the line join attribute: bevel, miter, round.
5876 */
5877 p=0;
5878 q=0;
5879 path_q[p++]=box_q[0];
5880 path_p[q++]=box_p[0];
cristybb503372010-05-27 20:51:26 +00005881 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00005882 {
5883 /*
5884 Compute the slope for this line segment, q.
5885 */
5886 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5887 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5888 dot_product=dx.q*dx.q+dy.q*dy.q;
5889 if (dot_product < 0.25)
5890 continue;
5891 slope.q=0.0;
5892 inverse_slope.q=0.0;
5893 if (fabs(dx.q) < MagickEpsilon)
5894 {
5895 if (dx.q >= 0.0)
5896 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5897 else
5898 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5899 }
5900 else
5901 if (fabs(dy.q) <= MagickEpsilon)
5902 {
5903 if (dy.q >= 0.0)
5904 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
5905 else
5906 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
5907 }
5908 else
5909 {
5910 slope.q=dy.q/dx.q;
5911 inverse_slope.q=(-1.0/slope.q);
5912 }
5913 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5914 offset.y=(double) (offset.x*inverse_slope.q);
5915 dot_product=dy.q*offset.x-dx.q*offset.y;
5916 if (dot_product > 0.0)
5917 {
5918 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5919 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5920 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5921 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5922 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5923 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5924 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5925 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5926 }
5927 else
5928 {
5929 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5930 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5931 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5932 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5933 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5934 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5935 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5936 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5937 }
5938 if (fabs((double) (slope.p-slope.q)) <= MagickEpsilon)
5939 {
5940 box_p[4]=box_p[1];
5941 box_q[4]=box_q[1];
5942 }
5943 else
5944 {
5945 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5946 box_p[3].y)/(slope.p-slope.q));
5947 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5948 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5949 box_q[3].y)/(slope.p-slope.q));
5950 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5951 }
cristybb503372010-05-27 20:51:26 +00005952 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
cristy3ed852e2009-09-05 21:47:34 +00005953 {
5954 max_strokes+=6*BezierQuantum+360;
5955 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5956 sizeof(*path_p));
5957 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5958 sizeof(*path_q));
5959 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5960 {
5961 polygon_primitive=(PrimitiveInfo *)
5962 RelinquishMagickMemory(polygon_primitive);
5963 return((PrimitiveInfo *) NULL);
5964 }
5965 }
5966 dot_product=dx.q*dy.p-dx.p*dy.q;
5967 if (dot_product <= 0.0)
5968 switch (draw_info->linejoin)
5969 {
5970 case BevelJoin:
5971 {
5972 path_q[q++]=box_q[1];
5973 path_q[q++]=box_q[2];
5974 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5975 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5976 if (dot_product <= miterlimit)
5977 path_p[p++]=box_p[4];
5978 else
5979 {
5980 path_p[p++]=box_p[1];
5981 path_p[p++]=box_p[2];
5982 }
5983 break;
5984 }
5985 case MiterJoin:
5986 {
5987 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5988 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5989 if (dot_product <= miterlimit)
5990 {
5991 path_q[q++]=box_q[4];
5992 path_p[p++]=box_p[4];
5993 }
5994 else
5995 {
5996 path_q[q++]=box_q[1];
5997 path_q[q++]=box_q[2];
5998 path_p[p++]=box_p[1];
5999 path_p[p++]=box_p[2];
6000 }
6001 break;
6002 }
6003 case RoundJoin:
6004 {
6005 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6006 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6007 if (dot_product <= miterlimit)
6008 path_p[p++]=box_p[4];
6009 else
6010 {
6011 path_p[p++]=box_p[1];
6012 path_p[p++]=box_p[2];
6013 }
6014 center=polygon_primitive[n].point;
6015 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6016 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6017 if (theta.q < theta.p)
6018 theta.q+=(MagickRealType) (2.0*MagickPI);
cristybb503372010-05-27 20:51:26 +00006019 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
cristy3ed852e2009-09-05 21:47:34 +00006020 (2.0*sqrt((double) (1.0/mid)))));
6021 path_q[q].x=box_q[1].x;
6022 path_q[q].y=box_q[1].y;
6023 q++;
cristybb503372010-05-27 20:51:26 +00006024 for (j=1; j < (ssize_t) arc_segments; j++)
cristy3ed852e2009-09-05 21:47:34 +00006025 {
6026 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6027 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6028 (theta.p+delta_theta),DegreesToRadians(360.0))));
6029 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6030 (theta.p+delta_theta),DegreesToRadians(360.0))));
6031 q++;
6032 }
6033 path_q[q++]=box_q[2];
6034 break;
6035 }
6036 default:
6037 break;
6038 }
6039 else
6040 switch (draw_info->linejoin)
6041 {
6042 case BevelJoin:
6043 {
6044 path_p[p++]=box_p[1];
6045 path_p[p++]=box_p[2];
6046 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6047 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6048 if (dot_product <= miterlimit)
6049 path_q[q++]=box_q[4];
6050 else
6051 {
6052 path_q[q++]=box_q[1];
6053 path_q[q++]=box_q[2];
6054 }
6055 break;
6056 }
6057 case MiterJoin:
6058 {
6059 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6060 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6061 if (dot_product <= miterlimit)
6062 {
6063 path_q[q++]=box_q[4];
6064 path_p[p++]=box_p[4];
6065 }
6066 else
6067 {
6068 path_q[q++]=box_q[1];
6069 path_q[q++]=box_q[2];
6070 path_p[p++]=box_p[1];
6071 path_p[p++]=box_p[2];
6072 }
6073 break;
6074 }
6075 case RoundJoin:
6076 {
6077 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6078 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6079 if (dot_product <= miterlimit)
6080 path_q[q++]=box_q[4];
6081 else
6082 {
6083 path_q[q++]=box_q[1];
6084 path_q[q++]=box_q[2];
6085 }
6086 center=polygon_primitive[n].point;
6087 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6088 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6089 if (theta.p < theta.q)
6090 theta.p+=(MagickRealType) (2.0*MagickPI);
cristybb503372010-05-27 20:51:26 +00006091 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
cristy3ed852e2009-09-05 21:47:34 +00006092 (2.0*sqrt((double) (1.0/mid)))));
6093 path_p[p++]=box_p[1];
cristybb503372010-05-27 20:51:26 +00006094 for (j=1; j < (ssize_t) arc_segments; j++)
cristy3ed852e2009-09-05 21:47:34 +00006095 {
6096 delta_theta=(MagickRealType) (j*(theta.q-theta.p)/arc_segments);
6097 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6098 (theta.p+delta_theta),DegreesToRadians(360.0))));
6099 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6100 (theta.p+delta_theta),DegreesToRadians(360.0))));
6101 p++;
6102 }
6103 path_p[p++]=box_p[2];
6104 break;
6105 }
6106 default:
6107 break;
6108 }
6109 slope.p=slope.q;
6110 inverse_slope.p=inverse_slope.q;
6111 box_p[0]=box_p[2];
6112 box_p[1]=box_p[3];
6113 box_q[0]=box_q[2];
6114 box_q[1]=box_q[3];
6115 dx.p=dx.q;
6116 dy.p=dy.q;
6117 n=i;
6118 }
6119 path_p[p++]=box_p[1];
6120 path_q[q++]=box_q[1];
6121 /*
6122 Trace stroked polygon.
6123 */
6124 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6125 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6126 if (stroke_polygon != (PrimitiveInfo *) NULL)
6127 {
cristybb503372010-05-27 20:51:26 +00006128 for (i=0; i < (ssize_t) p; i++)
cristy3ed852e2009-09-05 21:47:34 +00006129 {
6130 stroke_polygon[i]=polygon_primitive[0];
6131 stroke_polygon[i].point=path_p[i];
6132 }
6133 if (closed_path != MagickFalse)
6134 {
6135 stroke_polygon[i]=polygon_primitive[0];
6136 stroke_polygon[i].point=stroke_polygon[0].point;
6137 i++;
6138 }
cristybb503372010-05-27 20:51:26 +00006139 for ( ; i < (ssize_t) (p+q+closed_path); i++)
cristy3ed852e2009-09-05 21:47:34 +00006140 {
6141 stroke_polygon[i]=polygon_primitive[0];
6142 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6143 }
6144 if (closed_path != MagickFalse)
6145 {
6146 stroke_polygon[i]=polygon_primitive[0];
6147 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6148 i++;
6149 }
6150 stroke_polygon[i]=polygon_primitive[0];
6151 stroke_polygon[i].point=stroke_polygon[0].point;
6152 i++;
6153 stroke_polygon[i].primitive=UndefinedPrimitive;
cristybb503372010-05-27 20:51:26 +00006154 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
cristy3ed852e2009-09-05 21:47:34 +00006155 }
6156 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6157 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6158 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6159 return(stroke_polygon);
6160}