blob: 1ec4a23db9a2309a6314d798bd21aec8fca8ade0 [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
156 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *);
157
158static PrimitiveInfo
159 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
160
cristybb503372010-05-27 20:51:26 +0000161static size_t
cristy3ed852e2009-09-05 21:47:34 +0000162 TracePath(PrimitiveInfo *,const char *);
163
164static void
165 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
166 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
167 const MagickRealType,const MagickBooleanType,const MagickBooleanType),
cristybb503372010-05-27 20:51:26 +0000168 TraceBezier(PrimitiveInfo *,const size_t),
cristy3ed852e2009-09-05 21:47:34 +0000169 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
170 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
172 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
173 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
174 PointInfo),
cristybb503372010-05-27 20:51:26 +0000175 TraceSquareLinecap(PrimitiveInfo *,const size_t,const MagickRealType);
cristy3ed852e2009-09-05 21:47:34 +0000176
177/*
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179% %
180% %
181% %
182% A c q u i r e D r a w I n f o %
183% %
184% %
185% %
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187%
188% AcquireDrawInfo() returns a DrawInfo structure properly initialized.
189%
190% The format of the AcquireDrawInfo method is:
191%
192% DrawInfo *AcquireDrawInfo(void)
193%
194*/
195MagickExport DrawInfo *AcquireDrawInfo(void)
196{
197 DrawInfo
198 *draw_info;
199
cristy73bd4a52010-10-05 11:24:23 +0000200 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
cristy3ed852e2009-09-05 21:47:34 +0000201 if (draw_info == (DrawInfo *) NULL)
202 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
203 GetDrawInfo((ImageInfo *) NULL,draw_info);
204 return(draw_info);
205}
206
207/*
208%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209% %
210% %
211% %
212% C l o n e D r a w I n f o %
213% %
214% %
215% %
216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217%
anthony36fe1752011-09-29 11:28:37 +0000218% CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
219% is specified, a new draw_info structure is created initialized to
220% default values, according to the given image_info.
cristy3ed852e2009-09-05 21:47:34 +0000221%
222% The format of the CloneDrawInfo method is:
223%
224% DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
225% const DrawInfo *draw_info)
226%
227% A description of each parameter follows:
228%
229% o image_info: the image info.
230%
231% o draw_info: the draw info.
232%
233*/
234MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
235 const DrawInfo *draw_info)
236{
237 DrawInfo
238 *clone_info;
239
cristy73bd4a52010-10-05 11:24:23 +0000240 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +0000241 if (clone_info == (DrawInfo *) NULL)
242 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
243 GetDrawInfo(image_info,clone_info);
244 if (draw_info == (DrawInfo *) NULL)
245 return(clone_info);
246 if (clone_info->primitive != (char *) NULL)
247 (void) CloneString(&clone_info->primitive,draw_info->primitive);
248 if (draw_info->geometry != (char *) NULL)
249 (void) CloneString(&clone_info->geometry,draw_info->geometry);
250 clone_info->viewbox=draw_info->viewbox;
251 clone_info->affine=draw_info->affine;
252 clone_info->gravity=draw_info->gravity;
253 clone_info->fill=draw_info->fill;
254 clone_info->stroke=draw_info->stroke;
255 clone_info->stroke_width=draw_info->stroke_width;
256 if (draw_info->fill_pattern != (Image *) NULL)
257 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
258 &draw_info->fill_pattern->exception);
cristy3ed852e2009-09-05 21:47:34 +0000259 if (draw_info->stroke_pattern != (Image *) NULL)
260 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
261 MagickTrue,&draw_info->stroke_pattern->exception);
262 clone_info->stroke_antialias=draw_info->stroke_antialias;
263 clone_info->text_antialias=draw_info->text_antialias;
264 clone_info->fill_rule=draw_info->fill_rule;
265 clone_info->linecap=draw_info->linecap;
266 clone_info->linejoin=draw_info->linejoin;
267 clone_info->miterlimit=draw_info->miterlimit;
268 clone_info->dash_offset=draw_info->dash_offset;
269 clone_info->decorate=draw_info->decorate;
270 clone_info->compose=draw_info->compose;
271 if (draw_info->text != (char *) NULL)
272 (void) CloneString(&clone_info->text,draw_info->text);
273 if (draw_info->font != (char *) NULL)
274 (void) CloneString(&clone_info->font,draw_info->font);
275 if (draw_info->metrics != (char *) NULL)
276 (void) CloneString(&clone_info->metrics,draw_info->metrics);
277 if (draw_info->family != (char *) NULL)
278 (void) CloneString(&clone_info->family,draw_info->family);
279 clone_info->style=draw_info->style;
280 clone_info->stretch=draw_info->stretch;
281 clone_info->weight=draw_info->weight;
282 if (draw_info->encoding != (char *) NULL)
283 (void) CloneString(&clone_info->encoding,draw_info->encoding);
284 clone_info->pointsize=draw_info->pointsize;
285 clone_info->kerning=draw_info->kerning;
cristyb32b90a2009-09-07 21:45:48 +0000286 clone_info->interline_spacing=draw_info->interline_spacing;
cristy3ed852e2009-09-05 21:47:34 +0000287 clone_info->interword_spacing=draw_info->interword_spacing;
cristyc9b12952010-03-28 01:12:28 +0000288 clone_info->direction=draw_info->direction;
cristy3ed852e2009-09-05 21:47:34 +0000289 if (draw_info->density != (char *) NULL)
290 (void) CloneString(&clone_info->density,draw_info->density);
291 clone_info->align=draw_info->align;
292 clone_info->undercolor=draw_info->undercolor;
293 clone_info->border_color=draw_info->border_color;
294 if (draw_info->server_name != (char *) NULL)
295 (void) CloneString(&clone_info->server_name,draw_info->server_name);
296 if (draw_info->dash_pattern != (double *) NULL)
297 {
cristybb503372010-05-27 20:51:26 +0000298 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000299 x;
300
301 for (x=0; draw_info->dash_pattern[x] != 0.0; x++) ;
302 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
303 sizeof(*clone_info->dash_pattern));
304 if (clone_info->dash_pattern == (double *) NULL)
305 ThrowFatalException(ResourceLimitFatalError,
306 "UnableToAllocateDashPattern");
307 (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
308 (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
309 }
310 clone_info->gradient=draw_info->gradient;
311 if (draw_info->gradient.stops != (StopInfo *) NULL)
312 {
cristybb503372010-05-27 20:51:26 +0000313 size_t
cristy3ed852e2009-09-05 21:47:34 +0000314 number_stops;
315
316 number_stops=clone_info->gradient.number_stops;
317 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
318 number_stops,sizeof(*clone_info->gradient.stops));
319 if (clone_info->gradient.stops == (StopInfo *) NULL)
320 ThrowFatalException(ResourceLimitFatalError,
321 "UnableToAllocateDashPattern");
322 (void) CopyMagickMemory(clone_info->gradient.stops,
323 draw_info->gradient.stops,(size_t) number_stops*
324 sizeof(*clone_info->gradient.stops));
325 }
326 if (draw_info->clip_mask != (char *) NULL)
327 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
328 clone_info->bounds=draw_info->bounds;
329 clone_info->clip_units=draw_info->clip_units;
330 clone_info->render=draw_info->render;
cristy4c08aed2011-07-01 19:47:50 +0000331 clone_info->alpha=draw_info->alpha;
cristy3ed852e2009-09-05 21:47:34 +0000332 clone_info->element_reference=draw_info->element_reference;
333 clone_info->debug=IsEventLogging();
334 return(clone_info);
335}
336
337/*
338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339% %
340% %
341% %
342+ C o n v e r t P a t h T o P o l y g o n %
343% %
344% %
345% %
346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347%
348% ConvertPathToPolygon() converts a path to the more efficient sorted
349% rendering form.
350%
351% The format of the ConvertPathToPolygon method is:
352%
353% PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
354% const PathInfo *path_info)
355%
356% A description of each parameter follows:
357%
358% o Method ConvertPathToPolygon returns the path in a more efficient sorted
359% rendering form of type PolygonInfo.
360%
361% o draw_info: Specifies a pointer to an DrawInfo structure.
362%
363% o path_info: Specifies a pointer to an PathInfo structure.
364%
365%
366*/
367
368#if defined(__cplusplus) || defined(c_plusplus)
369extern "C" {
370#endif
371
372static int CompareEdges(const void *x,const void *y)
373{
374 register const EdgeInfo
375 *p,
376 *q;
377
378 /*
379 Compare two edges.
380 */
381 p=(const EdgeInfo *) x;
382 q=(const EdgeInfo *) y;
383 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
384 return(1);
385 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
386 return(-1);
387 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
388 return(1);
389 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
390 return(-1);
391 if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
392 (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
393 return(1);
394 return(-1);
395}
396
397#if defined(__cplusplus) || defined(c_plusplus)
398}
399#endif
400
401static void LogPolygonInfo(const PolygonInfo *polygon_info)
402{
403 register EdgeInfo
404 *p;
405
cristybb503372010-05-27 20:51:26 +0000406 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000407 i,
408 j;
409
410 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
411 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +0000412 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +0000413 {
cristye8c25f92010-06-03 00:53:06 +0000414 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
415 (double) i);
cristy3ed852e2009-09-05 21:47:34 +0000416 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
417 p->direction != MagickFalse ? "down" : "up");
418 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
419 p->ghostline != MagickFalse ? "transparent" : "opaque");
420 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +0000421 " bounds: %g %g - %g %g",p->bounds.x1,p->bounds.y1,
cristy8cd5b312010-01-07 01:10:24 +0000422 p->bounds.x2,p->bounds.y2);
cristybb503372010-05-27 20:51:26 +0000423 for (j=0; j < (ssize_t) p->number_points; j++)
cristy14388de2011-05-15 14:57:16 +0000424 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g %g",
cristy3ed852e2009-09-05 21:47:34 +0000425 p->points[j].x,p->points[j].y);
426 p++;
427 }
428 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
429}
430
cristybb503372010-05-27 20:51:26 +0000431static void ReversePoints(PointInfo *points,const size_t number_points)
cristy3ed852e2009-09-05 21:47:34 +0000432{
433 PointInfo
434 point;
435
cristybb503372010-05-27 20:51:26 +0000436 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000437 i;
438
cristybb503372010-05-27 20:51:26 +0000439 for (i=0; i < (ssize_t) (number_points >> 1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000440 {
441 point=points[i];
442 points[i]=points[number_points-(i+1)];
443 points[number_points-(i+1)]=point;
444 }
445}
446
447static PolygonInfo *ConvertPathToPolygon(
448 const DrawInfo *magick_unused(draw_info),const PathInfo *path_info)
449{
cristycee97112010-05-28 00:44:52 +0000450 long
cristy3ed852e2009-09-05 21:47:34 +0000451 direction,
452 next_direction;
453
454 PointInfo
455 point,
456 *points;
457
458 PolygonInfo
459 *polygon_info;
460
461 SegmentInfo
462 bounds;
463
cristybb503372010-05-27 20:51:26 +0000464 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000465 i,
466 n;
467
468 MagickBooleanType
469 ghostline;
470
cristybb503372010-05-27 20:51:26 +0000471 size_t
cristy3ed852e2009-09-05 21:47:34 +0000472 edge,
473 number_edges,
474 number_points;
475
476 /*
477 Convert a path to the more efficient sorted rendering form.
478 */
cristy73bd4a52010-10-05 11:24:23 +0000479 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
cristy3ed852e2009-09-05 21:47:34 +0000480 if (polygon_info == (PolygonInfo *) NULL)
481 return((PolygonInfo *) NULL);
482 number_edges=16;
483 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory((size_t) number_edges,
484 sizeof(*polygon_info->edges));
485 if (polygon_info->edges == (EdgeInfo *) NULL)
486 return((PolygonInfo *) NULL);
487 direction=0;
488 edge=0;
489 ghostline=MagickFalse;
490 n=0;
491 number_points=0;
492 points=(PointInfo *) NULL;
493 (void) ResetMagickMemory(&point,0,sizeof(point));
494 (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
495 for (i=0; path_info[i].code != EndCode; i++)
496 {
497 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
498 (path_info[i].code == GhostlineCode))
499 {
500 /*
501 Move to.
502 */
503 if ((points != (PointInfo *) NULL) && (n >= 2))
504 {
505 if (edge == number_edges)
506 {
507 number_edges<<=1;
508 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
509 polygon_info->edges,(size_t) number_edges,
510 sizeof(*polygon_info->edges));
511 if (polygon_info->edges == (EdgeInfo *) NULL)
512 return((PolygonInfo *) NULL);
513 }
cristybb503372010-05-27 20:51:26 +0000514 polygon_info->edges[edge].number_points=(size_t) n;
cristy3ed852e2009-09-05 21:47:34 +0000515 polygon_info->edges[edge].scanline=(-1.0);
516 polygon_info->edges[edge].highwater=0;
517 polygon_info->edges[edge].ghostline=ghostline;
cristybb503372010-05-27 20:51:26 +0000518 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
cristy3ed852e2009-09-05 21:47:34 +0000519 if (direction < 0)
cristybb503372010-05-27 20:51:26 +0000520 ReversePoints(points,(size_t) n);
cristy3ed852e2009-09-05 21:47:34 +0000521 polygon_info->edges[edge].points=points;
522 polygon_info->edges[edge].bounds=bounds;
523 polygon_info->edges[edge].bounds.y1=points[0].y;
524 polygon_info->edges[edge].bounds.y2=points[n-1].y;
525 points=(PointInfo *) NULL;
526 ghostline=MagickFalse;
527 edge++;
528 }
529 if (points == (PointInfo *) NULL)
530 {
531 number_points=16;
532 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
533 sizeof(*points));
534 if (points == (PointInfo *) NULL)
535 return((PolygonInfo *) NULL);
536 }
537 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
538 point=path_info[i].point;
539 points[0]=point;
540 bounds.x1=point.x;
541 bounds.x2=point.x;
542 direction=0;
543 n=1;
544 continue;
545 }
546 /*
547 Line to.
548 */
549 next_direction=((path_info[i].point.y > point.y) ||
550 ((path_info[i].point.y == point.y) &&
551 (path_info[i].point.x > point.x))) ? 1 : -1;
552 if ((direction != 0) && (direction != next_direction))
553 {
554 /*
555 New edge.
556 */
557 point=points[n-1];
558 if (edge == number_edges)
559 {
560 number_edges<<=1;
561 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
562 polygon_info->edges,(size_t) number_edges,
563 sizeof(*polygon_info->edges));
564 if (polygon_info->edges == (EdgeInfo *) NULL)
565 return((PolygonInfo *) NULL);
566 }
cristybb503372010-05-27 20:51:26 +0000567 polygon_info->edges[edge].number_points=(size_t) n;
cristy3ed852e2009-09-05 21:47:34 +0000568 polygon_info->edges[edge].scanline=(-1.0);
569 polygon_info->edges[edge].highwater=0;
570 polygon_info->edges[edge].ghostline=ghostline;
cristybb503372010-05-27 20:51:26 +0000571 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
cristy3ed852e2009-09-05 21:47:34 +0000572 if (direction < 0)
cristybb503372010-05-27 20:51:26 +0000573 ReversePoints(points,(size_t) n);
cristy3ed852e2009-09-05 21:47:34 +0000574 polygon_info->edges[edge].points=points;
575 polygon_info->edges[edge].bounds=bounds;
576 polygon_info->edges[edge].bounds.y1=points[0].y;
577 polygon_info->edges[edge].bounds.y2=points[n-1].y;
578 number_points=16;
579 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
580 sizeof(*points));
581 if (points == (PointInfo *) NULL)
582 return((PolygonInfo *) NULL);
583 n=1;
584 ghostline=MagickFalse;
585 points[0]=point;
586 bounds.x1=point.x;
587 bounds.x2=point.x;
588 edge++;
589 }
590 direction=next_direction;
591 if (points == (PointInfo *) NULL)
592 continue;
cristybb503372010-05-27 20:51:26 +0000593 if (n == (ssize_t) number_points)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
595 number_points<<=1;
596 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
597 sizeof(*points));
598 if (points == (PointInfo *) NULL)
599 return((PolygonInfo *) NULL);
600 }
601 point=path_info[i].point;
602 points[n]=point;
603 if (point.x < bounds.x1)
604 bounds.x1=point.x;
605 if (point.x > bounds.x2)
606 bounds.x2=point.x;
607 n++;
608 }
609 if (points != (PointInfo *) NULL)
610 {
611 if (n < 2)
612 points=(PointInfo *) RelinquishMagickMemory(points);
613 else
614 {
615 if (edge == number_edges)
616 {
617 number_edges<<=1;
618 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
619 polygon_info->edges,(size_t) number_edges,
620 sizeof(*polygon_info->edges));
621 if (polygon_info->edges == (EdgeInfo *) NULL)
622 return((PolygonInfo *) NULL);
623 }
cristybb503372010-05-27 20:51:26 +0000624 polygon_info->edges[edge].number_points=(size_t) n;
cristy3ed852e2009-09-05 21:47:34 +0000625 polygon_info->edges[edge].scanline=(-1.0);
626 polygon_info->edges[edge].highwater=0;
627 polygon_info->edges[edge].ghostline=ghostline;
cristybb503372010-05-27 20:51:26 +0000628 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
cristy3ed852e2009-09-05 21:47:34 +0000629 if (direction < 0)
cristybb503372010-05-27 20:51:26 +0000630 ReversePoints(points,(size_t) n);
cristy3ed852e2009-09-05 21:47:34 +0000631 polygon_info->edges[edge].points=points;
632 polygon_info->edges[edge].bounds=bounds;
633 polygon_info->edges[edge].bounds.y1=points[0].y;
634 polygon_info->edges[edge].bounds.y2=points[n-1].y;
635 ghostline=MagickFalse;
636 edge++;
637 }
638 }
639 polygon_info->number_edges=edge;
640 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
641 sizeof(*polygon_info->edges),CompareEdges);
642 if (IsEventLogging() != MagickFalse)
643 LogPolygonInfo(polygon_info);
644 return(polygon_info);
645}
646
647/*
648%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649% %
650% %
651% %
652+ C o n v e r t P r i m i t i v e T o P a t h %
653% %
654% %
655% %
656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657%
658% ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
659% path structure.
660%
661% The format of the ConvertPrimitiveToPath method is:
662%
663% PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
664% const PrimitiveInfo *primitive_info)
665%
666% A description of each parameter follows:
667%
668% o Method ConvertPrimitiveToPath returns a vector path structure of type
669% PathInfo.
670%
671% o draw_info: a structure of type DrawInfo.
672%
673% o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
674%
675%
676*/
677
678static void LogPathInfo(const PathInfo *path_info)
679{
680 register const PathInfo
681 *p;
682
683 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
684 for (p=path_info; p->code != EndCode; p++)
685 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +0000686 " %g %g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
cristy3ed852e2009-09-05 21:47:34 +0000687 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
688 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
689 "?");
690 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
691}
692
693static PathInfo *ConvertPrimitiveToPath(
694 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info)
695{
cristy3ed852e2009-09-05 21:47:34 +0000696 PathInfo
697 *path_info;
698
699 PathInfoCode
700 code;
701
702 PointInfo
703 p,
704 q;
705
cristybb503372010-05-27 20:51:26 +0000706 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000707 i,
708 n;
709
cristy826a5472010-08-31 23:21:38 +0000710 ssize_t
711 coordinates,
712 start;
713
cristy3ed852e2009-09-05 21:47:34 +0000714 /*
715 Converts a PrimitiveInfo structure into a vector path structure.
716 */
717 switch (primitive_info->primitive)
718 {
719 case PointPrimitive:
720 case ColorPrimitive:
721 case MattePrimitive:
722 case TextPrimitive:
723 case ImagePrimitive:
724 return((PathInfo *) NULL);
725 default:
726 break;
727 }
728 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
729 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
730 sizeof(*path_info));
731 if (path_info == (PathInfo *) NULL)
732 return((PathInfo *) NULL);
733 coordinates=0;
734 n=0;
735 p.x=(-1.0);
736 p.y=(-1.0);
737 q.x=(-1.0);
738 q.y=(-1.0);
739 start=0;
740 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
741 {
742 code=LineToCode;
743 if (coordinates <= 0)
744 {
cristybb503372010-05-27 20:51:26 +0000745 coordinates=(ssize_t) primitive_info[i].coordinates;
cristy3ed852e2009-09-05 21:47:34 +0000746 p=primitive_info[i].point;
747 start=n;
748 code=MoveToCode;
749 }
750 coordinates--;
751 /*
752 Eliminate duplicate points.
753 */
754 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) > MagickEpsilon) ||
755 (fabs(q.y-primitive_info[i].point.y) > MagickEpsilon))
756 {
757 path_info[n].code=code;
758 path_info[n].point=primitive_info[i].point;
759 q=primitive_info[i].point;
760 n++;
761 }
762 if (coordinates > 0)
763 continue;
764 if ((fabs(p.x-primitive_info[i].point.x) <= MagickEpsilon) &&
765 (fabs(p.y-primitive_info[i].point.y) <= MagickEpsilon))
766 continue;
767 /*
768 Mark the p point as open if it does not match the q.
769 */
770 path_info[start].code=OpenCode;
771 path_info[n].code=GhostlineCode;
772 path_info[n].point=primitive_info[i].point;
773 n++;
774 path_info[n].code=LineToCode;
775 path_info[n].point=p;
776 n++;
777 }
778 path_info[n].code=EndCode;
779 path_info[n].point.x=0.0;
780 path_info[n].point.y=0.0;
781 if (IsEventLogging() != MagickFalse)
782 LogPathInfo(path_info);
783 return(path_info);
784}
785
786/*
787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788% %
789% %
790% %
791% D e s t r o y D r a w I n f o %
792% %
793% %
794% %
795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796%
797% DestroyDrawInfo() deallocates memory associated with an DrawInfo
798% structure.
799%
800% The format of the DestroyDrawInfo method is:
801%
802% DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
803%
804% A description of each parameter follows:
805%
806% o draw_info: the draw info.
807%
808*/
809MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
810{
811 if (draw_info->debug != MagickFalse)
812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
813 assert(draw_info != (DrawInfo *) NULL);
814 assert(draw_info->signature == MagickSignature);
815 if (draw_info->primitive != (char *) NULL)
816 draw_info->primitive=DestroyString(draw_info->primitive);
817 if (draw_info->text != (char *) NULL)
818 draw_info->text=DestroyString(draw_info->text);
819 if (draw_info->geometry != (char *) NULL)
820 draw_info->geometry=DestroyString(draw_info->geometry);
cristy3ed852e2009-09-05 21:47:34 +0000821 if (draw_info->fill_pattern != (Image *) NULL)
822 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
823 if (draw_info->stroke_pattern != (Image *) NULL)
824 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
825 if (draw_info->font != (char *) NULL)
826 draw_info->font=DestroyString(draw_info->font);
827 if (draw_info->metrics != (char *) NULL)
828 draw_info->metrics=DestroyString(draw_info->metrics);
829 if (draw_info->family != (char *) NULL)
830 draw_info->family=DestroyString(draw_info->family);
831 if (draw_info->encoding != (char *) NULL)
832 draw_info->encoding=DestroyString(draw_info->encoding);
833 if (draw_info->density != (char *) NULL)
834 draw_info->density=DestroyString(draw_info->density);
835 if (draw_info->server_name != (char *) NULL)
836 draw_info->server_name=(char *)
837 RelinquishMagickMemory(draw_info->server_name);
838 if (draw_info->dash_pattern != (double *) NULL)
839 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
840 draw_info->dash_pattern);
841 if (draw_info->gradient.stops != (StopInfo *) NULL)
842 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
843 draw_info->gradient.stops);
844 if (draw_info->clip_mask != (char *) NULL)
845 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
846 draw_info->signature=(~MagickSignature);
847 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
848 return(draw_info);
849}
850
851/*
852%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853% %
854% %
855% %
856+ D e s t r o y E d g e %
857% %
858% %
859% %
860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861%
862% DestroyEdge() destroys the specified polygon edge.
863%
864% The format of the DestroyEdge method is:
865%
cristybb503372010-05-27 20:51:26 +0000866% ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
cristy3ed852e2009-09-05 21:47:34 +0000867%
868% A description of each parameter follows:
869%
870% o polygon_info: Specifies a pointer to an PolygonInfo structure.
871%
872% o edge: the polygon edge number to destroy.
873%
874*/
cristybb503372010-05-27 20:51:26 +0000875static size_t DestroyEdge(PolygonInfo *polygon_info,
876 const size_t edge)
cristy3ed852e2009-09-05 21:47:34 +0000877{
878 assert(edge < polygon_info->number_edges);
879 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
880 polygon_info->edges[edge].points);
881 polygon_info->number_edges--;
882 if (edge < polygon_info->number_edges)
883 (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
884 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
885 return(polygon_info->number_edges);
886}
887
888/*
889%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890% %
891% %
892% %
893+ D e s t r o y P o l y g o n I n f o %
894% %
895% %
896% %
897%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898%
899% DestroyPolygonInfo() destroys the PolygonInfo data structure.
900%
901% The format of the DestroyPolygonInfo method is:
902%
903% PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
904%
905% A description of each parameter follows:
906%
907% o polygon_info: Specifies a pointer to an PolygonInfo structure.
908%
909*/
910static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
911{
cristybb503372010-05-27 20:51:26 +0000912 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000913 i;
914
cristybb503372010-05-27 20:51:26 +0000915 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +0000916 polygon_info->edges[i].points=(PointInfo *)
917 RelinquishMagickMemory(polygon_info->edges[i].points);
918 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
919 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
920}
921
922/*
923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924% %
925% %
926% %
927% D r a w A f f i n e I m a g e %
928% %
929% %
930% %
931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932%
933% DrawAffineImage() composites the source over the destination image as
934% dictated by the affine transform.
935%
936% The format of the DrawAffineImage method is:
937%
938% MagickBooleanType DrawAffineImage(Image *image,const Image *source,
939% const AffineMatrix *affine)
940%
941% A description of each parameter follows:
942%
943% o image: the image.
944%
945% o source: the source image.
946%
947% o affine: the affine transform.
948%
949*/
950static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
951 const double y,const SegmentInfo *edge)
952{
953 double
954 intercept,
955 z;
956
957 register double
958 x;
959
960 SegmentInfo
961 inverse_edge;
962
963 /*
964 Determine left and right edges.
965 */
966 inverse_edge.x1=edge->x1;
967 inverse_edge.y1=edge->y1;
968 inverse_edge.x2=edge->x2;
969 inverse_edge.y2=edge->y2;
970 z=affine->ry*y+affine->tx;
971 if (affine->sx > MagickEpsilon)
972 {
973 intercept=(-z/affine->sx);
974 x=intercept+MagickEpsilon;
975 if (x > inverse_edge.x1)
976 inverse_edge.x1=x;
977 intercept=(-z+(double) image->columns)/affine->sx;
978 x=intercept-MagickEpsilon;
979 if (x < inverse_edge.x2)
980 inverse_edge.x2=x;
981 }
982 else
983 if (affine->sx < -MagickEpsilon)
984 {
985 intercept=(-z+(double) image->columns)/affine->sx;
986 x=intercept+MagickEpsilon;
987 if (x > inverse_edge.x1)
988 inverse_edge.x1=x;
989 intercept=(-z/affine->sx);
990 x=intercept-MagickEpsilon;
991 if (x < inverse_edge.x2)
992 inverse_edge.x2=x;
993 }
994 else
cristybb503372010-05-27 20:51:26 +0000995 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000996 {
997 inverse_edge.x2=edge->x1;
998 return(inverse_edge);
999 }
1000 /*
1001 Determine top and bottom edges.
1002 */
1003 z=affine->sy*y+affine->ty;
1004 if (affine->rx > MagickEpsilon)
1005 {
1006 intercept=(-z/affine->rx);
1007 x=intercept+MagickEpsilon;
1008 if (x > inverse_edge.x1)
1009 inverse_edge.x1=x;
1010 intercept=(-z+(double) image->rows)/affine->rx;
1011 x=intercept-MagickEpsilon;
1012 if (x < inverse_edge.x2)
1013 inverse_edge.x2=x;
1014 }
1015 else
1016 if (affine->rx < -MagickEpsilon)
1017 {
1018 intercept=(-z+(double) image->rows)/affine->rx;
1019 x=intercept+MagickEpsilon;
1020 if (x > inverse_edge.x1)
1021 inverse_edge.x1=x;
1022 intercept=(-z/affine->rx);
1023 x=intercept-MagickEpsilon;
1024 if (x < inverse_edge.x2)
1025 inverse_edge.x2=x;
1026 }
1027 else
cristybb503372010-05-27 20:51:26 +00001028 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001029 {
1030 inverse_edge.x2=edge->x2;
1031 return(inverse_edge);
1032 }
1033 return(inverse_edge);
1034}
1035
1036static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1037{
1038 AffineMatrix
1039 inverse_affine;
1040
1041 double
1042 determinant;
1043
1044 determinant=1.0/(affine->sx*affine->sy-affine->rx*affine->ry);
1045 inverse_affine.sx=determinant*affine->sy;
1046 inverse_affine.rx=determinant*(-affine->rx);
1047 inverse_affine.ry=determinant*(-affine->ry);
1048 inverse_affine.sy=determinant*affine->sx;
1049 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1050 inverse_affine.ry;
1051 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1052 inverse_affine.sy;
1053 return(inverse_affine);
1054}
1055
cristybb503372010-05-27 20:51:26 +00001056static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +00001057{
1058 if (x < 0)
1059 return(-x);
1060 return(x);
1061}
1062
1063static inline double MagickMax(const double x,const double y)
1064{
1065 if (x > y)
1066 return(x);
1067 return(y);
1068}
1069
1070static inline double MagickMin(const double x,const double y)
1071{
1072 if (x < y)
1073 return(x);
1074 return(y);
1075}
1076
1077MagickExport MagickBooleanType DrawAffineImage(Image *image,
1078 const Image *source,const AffineMatrix *affine)
1079{
1080 AffineMatrix
1081 inverse_affine;
1082
cristyfa112112010-01-04 17:48:07 +00001083 CacheView
1084 *image_view,
1085 *source_view;
1086
cristy3ed852e2009-09-05 21:47:34 +00001087 ExceptionInfo
1088 *exception;
1089
cristy3ed852e2009-09-05 21:47:34 +00001090 MagickBooleanType
1091 status;
1092
cristy4c08aed2011-07-01 19:47:50 +00001093 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001094 zero;
1095
1096 PointInfo
1097 extent[4],
1098 min,
1099 max,
1100 point;
1101
cristybb503372010-05-27 20:51:26 +00001102 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001103 i;
1104
cristy3ed852e2009-09-05 21:47:34 +00001105 SegmentInfo
1106 edge;
1107
cristy826a5472010-08-31 23:21:38 +00001108 ssize_t
1109 y;
1110
cristy3ed852e2009-09-05 21:47:34 +00001111 /*
1112 Determine bounding box.
1113 */
1114 assert(image != (Image *) NULL);
1115 assert(image->signature == MagickSignature);
1116 if (image->debug != MagickFalse)
1117 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1118 assert(source != (const Image *) NULL);
1119 assert(source->signature == MagickSignature);
1120 assert(affine != (AffineMatrix *) NULL);
1121 extent[0].x=0.0;
1122 extent[0].y=0.0;
1123 extent[1].x=(double) source->columns-1.0;
1124 extent[1].y=0.0;
1125 extent[2].x=(double) source->columns-1.0;
1126 extent[2].y=(double) source->rows-1.0;
1127 extent[3].x=0.0;
1128 extent[3].y=(double) source->rows-1.0;
1129 for (i=0; i < 4; i++)
1130 {
1131 point=extent[i];
1132 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1133 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1134 }
1135 min=extent[0];
1136 max=extent[0];
1137 for (i=1; i < 4; i++)
1138 {
1139 if (min.x > extent[i].x)
1140 min.x=extent[i].x;
1141 if (min.y > extent[i].y)
1142 min.y=extent[i].y;
1143 if (max.x < extent[i].x)
1144 max.x=extent[i].x;
1145 if (max.y < extent[i].y)
1146 max.y=extent[i].y;
1147 }
1148 /*
1149 Affine transform image.
1150 */
cristy874276b2011-08-05 19:29:22 +00001151 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +00001152 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001153 return(MagickFalse);
1154 status=MagickTrue;
1155 edge.x1=MagickMax(min.x,0.0);
1156 edge.y1=MagickMax(min.y,0.0);
1157 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1158 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1159 inverse_affine=InverseAffineMatrix(affine);
cristy4c08aed2011-07-01 19:47:50 +00001160 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +00001161 image_view=AcquireCacheView(image);
1162 source_view=AcquireCacheView(source);
cristyb5d5f722009-11-04 03:03:49 +00001163#if defined(MAGICKCORE_OPENMP_SUPPORT)
1164 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00001165#endif
cristybb503372010-05-27 20:51:26 +00001166 for (y=(ssize_t) ceil(edge.y1-0.5); y <= (ssize_t) floor(edge.y2+0.5); y++)
cristy3ed852e2009-09-05 21:47:34 +00001167 {
cristy4c08aed2011-07-01 19:47:50 +00001168 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001169 composite,
1170 pixel;
1171
1172 PointInfo
1173 point;
1174
cristybb503372010-05-27 20:51:26 +00001175 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001176 x;
1177
cristy4c08aed2011-07-01 19:47:50 +00001178 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001179 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001180
1181 SegmentInfo
1182 inverse_edge;
1183
cristy826a5472010-08-31 23:21:38 +00001184 ssize_t
1185 x_offset;
1186
cristy3ed852e2009-09-05 21:47:34 +00001187 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1188 if (inverse_edge.x2 < inverse_edge.x1)
1189 continue;
cristy6ebe97c2010-07-03 01:17:28 +00001190 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1191 0.5),y,(size_t) ((ssize_t) floor(inverse_edge.x2+0.5)-(ssize_t) floor(
cristy06609ee2010-03-17 20:21:27 +00001192 inverse_edge.x1+0.5)+1),1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001193 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001194 continue;
cristy3ed852e2009-09-05 21:47:34 +00001195 pixel=zero;
1196 composite=zero;
1197 x_offset=0;
cristybb503372010-05-27 20:51:26 +00001198 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 +00001199 {
1200 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1201 inverse_affine.tx;
1202 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1203 inverse_affine.ty;
cristy4c08aed2011-07-01 19:47:50 +00001204 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1205 point.x,point.y,&pixel,exception);
1206 SetPixelInfo(image,q,&composite);
1207 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1208 &composite);
1209 SetPixelPixelInfo(image,&composite,q);
cristy3ed852e2009-09-05 21:47:34 +00001210 x_offset++;
cristyed231572011-07-14 02:18:59 +00001211 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001212 }
1213 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1214 status=MagickFalse;
1215 }
cristy3ed852e2009-09-05 21:47:34 +00001216 source_view=DestroyCacheView(source_view);
1217 image_view=DestroyCacheView(image_view);
1218 return(status);
1219}
1220
1221/*
1222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1223% %
1224% %
1225% %
1226+ D r a w B o u n d i n g R e c t a n g l e s %
1227% %
1228% %
1229% %
1230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1231%
1232% DrawBoundingRectangles() draws the bounding rectangles on the image. This
1233% is only useful for developers debugging the rendering algorithm.
1234%
1235% The format of the DrawBoundingRectangles method is:
1236%
1237% void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1238% PolygonInfo *polygon_info)
1239%
1240% A description of each parameter follows:
1241%
1242% o image: the image.
1243%
1244% o draw_info: the draw info.
1245%
1246% o polygon_info: Specifies a pointer to a PolygonInfo structure.
1247%
1248*/
1249static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1250 const PolygonInfo *polygon_info)
1251{
1252 DrawInfo
1253 *clone_info;
1254
cristy3ed852e2009-09-05 21:47:34 +00001255 MagickRealType
1256 mid;
1257
1258 PointInfo
1259 end,
1260 resolution,
1261 start;
1262
1263 PrimitiveInfo
1264 primitive_info[6];
1265
cristybb503372010-05-27 20:51:26 +00001266 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001267 i;
1268
1269 SegmentInfo
1270 bounds;
1271
cristy826a5472010-08-31 23:21:38 +00001272 ssize_t
1273 coordinates;
1274
cristy3ed852e2009-09-05 21:47:34 +00001275 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
cristy9950d572011-10-01 18:22:35 +00001276 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
1277 &image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001278 resolution.x=DefaultResolution;
1279 resolution.y=DefaultResolution;
1280 if (clone_info->density != (char *) NULL)
1281 {
1282 GeometryInfo
1283 geometry_info;
1284
1285 MagickStatusType
1286 flags;
1287
1288 flags=ParseGeometry(clone_info->density,&geometry_info);
1289 resolution.x=geometry_info.rho;
1290 resolution.y=geometry_info.sigma;
1291 if ((flags & SigmaValue) == MagickFalse)
1292 resolution.y=resolution.x;
1293 }
1294 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1295 clone_info->stroke_width/2.0;
1296 bounds.x1=0.0;
1297 bounds.y1=0.0;
1298 bounds.x2=0.0;
1299 bounds.y2=0.0;
1300 if (polygon_info != (PolygonInfo *) NULL)
1301 {
1302 bounds=polygon_info->edges[0].bounds;
cristybb503372010-05-27 20:51:26 +00001303 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00001304 {
1305 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1306 bounds.x1=polygon_info->edges[i].bounds.x1;
1307 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1308 bounds.y1=polygon_info->edges[i].bounds.y1;
1309 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1310 bounds.x2=polygon_info->edges[i].bounds.x2;
1311 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1312 bounds.y2=polygon_info->edges[i].bounds.y2;
1313 }
1314 bounds.x1-=mid;
1315 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1316 image->columns ? (double) image->columns-1 : bounds.x1;
1317 bounds.y1-=mid;
1318 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1319 image->rows ? (double) image->rows-1 : bounds.y1;
1320 bounds.x2+=mid;
1321 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1322 image->columns ? (double) image->columns-1 : bounds.x2;
1323 bounds.y2+=mid;
1324 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1325 image->rows ? (double) image->rows-1 : bounds.y2;
cristybb503372010-05-27 20:51:26 +00001326 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00001327 {
1328 if (polygon_info->edges[i].direction != 0)
cristy9950d572011-10-01 18:22:35 +00001329 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
cristy3ed852e2009-09-05 21:47:34 +00001330 &image->exception);
1331 else
cristy9950d572011-10-01 18:22:35 +00001332 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
cristy3ed852e2009-09-05 21:47:34 +00001333 &image->exception);
1334 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1335 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1336 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1337 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1338 primitive_info[0].primitive=RectanglePrimitive;
1339 TraceRectangle(primitive_info,start,end);
1340 primitive_info[0].method=ReplaceMethod;
cristybb503372010-05-27 20:51:26 +00001341 coordinates=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00001342 primitive_info[coordinates].primitive=UndefinedPrimitive;
1343 (void) DrawPrimitive(image,clone_info,primitive_info);
1344 }
1345 }
cristy9950d572011-10-01 18:22:35 +00001346 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1347 &image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001348 start.x=(double) (bounds.x1-mid);
1349 start.y=(double) (bounds.y1-mid);
1350 end.x=(double) (bounds.x2+mid);
1351 end.y=(double) (bounds.y2+mid);
1352 primitive_info[0].primitive=RectanglePrimitive;
1353 TraceRectangle(primitive_info,start,end);
1354 primitive_info[0].method=ReplaceMethod;
cristybb503372010-05-27 20:51:26 +00001355 coordinates=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00001356 primitive_info[coordinates].primitive=UndefinedPrimitive;
1357 (void) DrawPrimitive(image,clone_info,primitive_info);
1358 clone_info=DestroyDrawInfo(clone_info);
1359}
1360
1361/*
1362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363% %
1364% %
1365% %
1366% D r a w C l i p P a t h %
1367% %
1368% %
1369% %
1370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371%
1372% DrawClipPath() draws the clip path on the image mask.
1373%
1374% The format of the DrawClipPath method is:
1375%
1376% MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
cristy018f07f2011-09-04 21:15:19 +00001377% const char *name,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001378%
1379% A description of each parameter follows:
1380%
1381% o image: the image.
1382%
1383% o draw_info: the draw info.
1384%
1385% o name: the name of the clip path.
1386%
cristy018f07f2011-09-04 21:15:19 +00001387% o exception: return any errors or warnings in this structure.
1388%
cristy3ed852e2009-09-05 21:47:34 +00001389*/
1390MagickExport MagickBooleanType DrawClipPath(Image *image,
cristy018f07f2011-09-04 21:15:19 +00001391 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001392{
1393 char
1394 clip_mask[MaxTextExtent];
1395
1396 const char
1397 *value;
1398
1399 DrawInfo
1400 *clone_info;
1401
1402 MagickStatusType
1403 status;
1404
1405 assert(image != (Image *) NULL);
1406 assert(image->signature == MagickSignature);
1407 if (image->debug != MagickFalse)
1408 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1409 assert(draw_info != (const DrawInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +00001410 (void) FormatLocaleString(clip_mask,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00001411 value=GetImageArtifact(image,clip_mask);
1412 if (value == (const char *) NULL)
1413 return(MagickFalse);
1414 if (image->clip_mask == (Image *) NULL)
1415 {
1416 Image
1417 *clip_mask;
1418
1419 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,
1420 &image->exception);
1421 if (clip_mask == (Image *) NULL)
1422 return(MagickFalse);
cristy018f07f2011-09-04 21:15:19 +00001423 (void) SetImageClipMask(image,clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001424 clip_mask=DestroyImage(clip_mask);
1425 }
cristy9950d572011-10-01 18:22:35 +00001426 (void) QueryColorCompliance("#00000000",AllCompliance,
1427 &image->clip_mask->background_color,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001428 image->clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00001429 (void) SetImageBackgroundColor(image->clip_mask);
1430 if (image->debug != MagickFalse)
1431 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1432 draw_info->clip_mask);
1433 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1434 (void) CloneString(&clone_info->primitive,value);
cristy9950d572011-10-01 18:22:35 +00001435 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1436 &image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001437 clone_info->clip_mask=(char *) NULL;
cristy018f07f2011-09-04 21:15:19 +00001438 status=DrawImage(image->clip_mask,clone_info,exception);
cristyb3e7c6c2011-07-24 01:43:55 +00001439 status|=NegateImage(image->clip_mask,MagickFalse,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001440 clone_info=DestroyDrawInfo(clone_info);
1441 if (image->debug != MagickFalse)
1442 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1443 return(status != 0 ? MagickTrue : MagickFalse);
1444}
1445
1446/*
1447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448% %
1449% %
1450% %
1451+ D r a w D a s h P o l y g o n %
1452% %
1453% %
1454% %
1455%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456%
1457% DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1458% image while respecting the dash offset and dash pattern attributes.
1459%
1460% The format of the DrawDashPolygon method is:
1461%
1462% MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1463% const PrimitiveInfo *primitive_info,Image *image)
1464%
1465% A description of each parameter follows:
1466%
1467% o draw_info: the draw info.
1468%
1469% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1470%
1471% o image: the image.
1472%
1473%
1474*/
1475static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1476 const PrimitiveInfo *primitive_info,Image *image)
1477{
1478 DrawInfo
1479 *clone_info;
1480
cristy3ed852e2009-09-05 21:47:34 +00001481 MagickRealType
1482 length,
1483 maximum_length,
1484 offset,
1485 scale,
1486 total_length;
1487
1488 MagickStatusType
1489 status;
1490
1491 PrimitiveInfo
1492 *dash_polygon;
1493
cristybb503372010-05-27 20:51:26 +00001494 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001495 i;
1496
1497 register MagickRealType
1498 dx,
1499 dy;
1500
cristybb503372010-05-27 20:51:26 +00001501 size_t
cristy3ed852e2009-09-05 21:47:34 +00001502 number_vertices;
1503
cristy826a5472010-08-31 23:21:38 +00001504 ssize_t
1505 j,
1506 n;
1507
cristy3ed852e2009-09-05 21:47:34 +00001508 assert(draw_info != (const DrawInfo *) NULL);
1509 if (image->debug != MagickFalse)
1510 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1511 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1512 clone_info->miterlimit=0;
1513 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
cristybb503372010-05-27 20:51:26 +00001514 number_vertices=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00001515 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1516 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1517 if (dash_polygon == (PrimitiveInfo *) NULL)
1518 return(MagickFalse);
1519 dash_polygon[0]=primitive_info[0];
1520 scale=ExpandAffine(&draw_info->affine);
1521 length=scale*(draw_info->dash_pattern[0]-0.5);
1522 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1523 j=1;
1524 for (n=0; offset > 0.0; j=0)
1525 {
1526 if (draw_info->dash_pattern[n] <= 0.0)
1527 break;
1528 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1529 if (offset > length)
1530 {
1531 offset-=length;
1532 n++;
1533 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1534 continue;
1535 }
1536 if (offset < length)
1537 {
1538 length-=offset;
1539 offset=0.0;
1540 break;
1541 }
1542 offset=0.0;
1543 n++;
1544 }
1545 status=MagickTrue;
1546 maximum_length=0.0;
1547 total_length=0.0;
cristybb503372010-05-27 20:51:26 +00001548 for (i=1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00001549 {
1550 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1551 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1552 maximum_length=hypot((double) dx,dy);
1553 if (length == 0.0)
1554 {
1555 n++;
1556 if (draw_info->dash_pattern[n] == 0.0)
1557 n=0;
1558 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1559 }
1560 for (total_length=0.0; (total_length+length) < maximum_length; )
1561 {
1562 total_length+=length;
1563 if ((n & 0x01) != 0)
1564 {
1565 dash_polygon[0]=primitive_info[0];
1566 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1567 total_length/maximum_length);
1568 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1569 total_length/maximum_length);
1570 j=1;
1571 }
1572 else
1573 {
cristybb503372010-05-27 20:51:26 +00001574 if ((j+1) > (ssize_t) (2*number_vertices))
cristy3ed852e2009-09-05 21:47:34 +00001575 break;
1576 dash_polygon[j]=primitive_info[i-1];
1577 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1578 total_length/maximum_length);
1579 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1580 total_length/maximum_length);
1581 dash_polygon[j].coordinates=1;
1582 j++;
cristybb503372010-05-27 20:51:26 +00001583 dash_polygon[0].coordinates=(size_t) j;
cristy3ed852e2009-09-05 21:47:34 +00001584 dash_polygon[j].primitive=UndefinedPrimitive;
1585 status|=DrawStrokePolygon(image,clone_info,dash_polygon);
1586 }
1587 n++;
1588 if (draw_info->dash_pattern[n] == 0.0)
1589 n=0;
1590 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1591 }
1592 length-=(maximum_length-total_length);
1593 if ((n & 0x01) != 0)
1594 continue;
1595 dash_polygon[j]=primitive_info[i];
1596 dash_polygon[j].coordinates=1;
1597 j++;
1598 }
1599 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1600 {
1601 dash_polygon[j]=primitive_info[i-1];
1602 dash_polygon[j].point.x+=MagickEpsilon;
1603 dash_polygon[j].point.y+=MagickEpsilon;
1604 dash_polygon[j].coordinates=1;
1605 j++;
cristybb503372010-05-27 20:51:26 +00001606 dash_polygon[0].coordinates=(size_t) j;
cristy3ed852e2009-09-05 21:47:34 +00001607 dash_polygon[j].primitive=UndefinedPrimitive;
1608 status|=DrawStrokePolygon(image,clone_info,dash_polygon);
1609 }
1610 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1611 clone_info=DestroyDrawInfo(clone_info);
1612 if (image->debug != MagickFalse)
1613 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1614 return(status != 0 ? MagickTrue : MagickFalse);
1615}
1616
1617/*
1618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619% %
1620% %
1621% %
1622% D r a w I m a g e %
1623% %
1624% %
1625% %
1626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627%
1628% DrawImage() draws a graphic primitive on your image. The primitive
1629% may be represented as a string or filename. Precede the filename with an
1630% "at" sign (@) and the contents of the file are drawn on the image. You
1631% can affect how text is drawn by setting one or more members of the draw
1632% info structure.
1633%
1634% The format of the DrawImage method is:
1635%
cristy018f07f2011-09-04 21:15:19 +00001636% MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1637% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001638%
1639% A description of each parameter follows:
1640%
1641% o image: the image.
1642%
1643% o draw_info: the draw info.
1644%
cristy018f07f2011-09-04 21:15:19 +00001645% o exception: return any errors or warnings in this structure.
1646%
cristy3ed852e2009-09-05 21:47:34 +00001647*/
1648
1649static inline MagickBooleanType IsPoint(const char *point)
1650{
1651 char
1652 *p;
1653
1654 double
1655 value;
1656
cristyc1acd842011-05-19 23:05:47 +00001657 value=InterpretLocaleValue(point,&p);
cristy3ed852e2009-09-05 21:47:34 +00001658 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1659}
1660
1661static inline void TracePoint(PrimitiveInfo *primitive_info,
1662 const PointInfo point)
1663{
1664 primitive_info->coordinates=1;
1665 primitive_info->point=point;
1666}
1667
cristy018f07f2011-09-04 21:15:19 +00001668MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1669 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001670{
1671#define RenderImageTag "Render/Image"
1672
1673 AffineMatrix
1674 affine,
1675 current;
1676
1677 char
1678 key[2*MaxTextExtent],
1679 keyword[MaxTextExtent],
1680 geometry[MaxTextExtent],
1681 name[MaxTextExtent],
1682 pattern[MaxTextExtent],
1683 *primitive,
1684 *token;
1685
1686 const char
1687 *q;
1688
1689 DrawInfo
1690 **graphic_context;
1691
cristy3ed852e2009-09-05 21:47:34 +00001692 MagickBooleanType
1693 proceed,
1694 status;
1695
1696 MagickRealType
1697 angle,
1698 factor,
1699 primitive_extent;
1700
1701 PointInfo
1702 point;
1703
1704 PixelPacket
1705 start_color;
1706
1707 PrimitiveInfo
1708 *primitive_info;
1709
1710 PrimitiveType
1711 primitive_type;
1712
1713 register const char
1714 *p;
1715
cristybb503372010-05-27 20:51:26 +00001716 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001717 i,
1718 x;
1719
1720 SegmentInfo
1721 bounds;
1722
1723 size_t
cristy826a5472010-08-31 23:21:38 +00001724 length,
cristy3ed852e2009-09-05 21:47:34 +00001725 number_points;
1726
cristy826a5472010-08-31 23:21:38 +00001727 ssize_t
1728 j,
1729 k,
1730 n;
1731
cristy3ed852e2009-09-05 21:47:34 +00001732 /*
1733 Ensure the annotation info is valid.
1734 */
1735 assert(image != (Image *) NULL);
1736 assert(image->signature == MagickSignature);
1737 if (image->debug != MagickFalse)
1738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1739 assert(draw_info != (DrawInfo *) NULL);
1740 assert(draw_info->signature == MagickSignature);
1741 if (image->debug != MagickFalse)
1742 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1743 if ((draw_info->primitive == (char *) NULL) ||
1744 (*draw_info->primitive == '\0'))
1745 return(MagickFalse);
1746 if (image->debug != MagickFalse)
1747 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1748 if (*draw_info->primitive != '@')
1749 primitive=AcquireString(draw_info->primitive);
1750 else
1751 primitive=FileToString(draw_info->primitive+1,~0,&image->exception);
1752 if (primitive == (char *) NULL)
1753 return(MagickFalse);
1754 primitive_extent=(MagickRealType) strlen(primitive);
1755 (void) SetImageArtifact(image,"MVG",primitive);
1756 n=0;
1757 /*
1758 Allocate primitive info memory.
1759 */
cristy73bd4a52010-10-05 11:24:23 +00001760 graphic_context=(DrawInfo **) AcquireMagickMemory(
cristyed110712010-03-23 01:16:38 +00001761 sizeof(*graphic_context));
cristy3ed852e2009-09-05 21:47:34 +00001762 if (graphic_context == (DrawInfo **) NULL)
1763 {
1764 primitive=DestroyString(primitive);
1765 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1766 image->filename);
1767 }
1768 number_points=2047;
1769 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1770 sizeof(*primitive_info));
1771 if (primitive_info == (PrimitiveInfo *) NULL)
1772 {
1773 primitive=DestroyString(primitive);
1774 for ( ; n >= 0; n--)
1775 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1776 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1777 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1778 image->filename);
1779 }
1780 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1781 graphic_context[n]->viewbox=image->page;
1782 if ((image->page.width == 0) || (image->page.height == 0))
1783 {
1784 graphic_context[n]->viewbox.width=image->columns;
1785 graphic_context[n]->viewbox.height=image->rows;
1786 }
1787 token=AcquireString(primitive);
cristy9950d572011-10-01 18:22:35 +00001788 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
1789 &image->exception);
cristy574cc262011-08-05 01:23:58 +00001790 if (SetImageStorageClass(image,DirectClass,&image->exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001791 return(MagickFalse);
1792 status=MagickTrue;
1793 for (q=primitive; *q != '\0'; )
1794 {
1795 /*
1796 Interpret graphic primitive.
1797 */
1798 GetMagickToken(q,&q,keyword);
1799 if (*keyword == '\0')
1800 break;
1801 if (*keyword == '#')
1802 {
1803 /*
1804 Comment.
1805 */
1806 while ((*q != '\n') && (*q != '\0'))
1807 q++;
1808 continue;
1809 }
1810 p=q-strlen(keyword)-1;
1811 primitive_type=UndefinedPrimitive;
1812 current=graphic_context[n]->affine;
1813 GetAffineMatrix(&affine);
1814 switch (*keyword)
1815 {
1816 case ';':
1817 break;
1818 case 'a':
1819 case 'A':
1820 {
1821 if (LocaleCompare("affine",keyword) == 0)
1822 {
1823 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001824 affine.sx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001825 GetMagickToken(q,&q,token);
1826 if (*token == ',')
1827 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001828 affine.rx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001829 GetMagickToken(q,&q,token);
1830 if (*token == ',')
1831 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00001832 affine.ry=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.sy=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.tx=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.ty=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001845 break;
1846 }
1847 if (LocaleCompare("arc",keyword) == 0)
1848 {
1849 primitive_type=ArcPrimitive;
1850 break;
1851 }
1852 status=MagickFalse;
1853 break;
1854 }
1855 case 'b':
1856 case 'B':
1857 {
1858 if (LocaleCompare("bezier",keyword) == 0)
1859 {
1860 primitive_type=BezierPrimitive;
1861 break;
1862 }
1863 if (LocaleCompare("border-color",keyword) == 0)
1864 {
1865 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00001866 (void) QueryColorCompliance(token,AllCompliance,
1867 &graphic_context[n]->border_color,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001868 break;
1869 }
1870 status=MagickFalse;
1871 break;
1872 }
1873 case 'c':
1874 case 'C':
1875 {
1876 if (LocaleCompare("clip-path",keyword) == 0)
1877 {
1878 /*
1879 Create clip mask.
1880 */
1881 GetMagickToken(q,&q,token);
1882 (void) CloneString(&graphic_context[n]->clip_mask,token);
1883 (void) DrawClipPath(image,graphic_context[n],
cristy018f07f2011-09-04 21:15:19 +00001884 graphic_context[n]->clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001885 break;
1886 }
1887 if (LocaleCompare("clip-rule",keyword) == 0)
1888 {
cristybb503372010-05-27 20:51:26 +00001889 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001890 fill_rule;
1891
1892 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001893 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001894 token);
1895 if (fill_rule == -1)
1896 {
1897 status=MagickFalse;
1898 break;
1899 }
1900 graphic_context[n]->fill_rule=(FillRule) fill_rule;
1901 break;
1902 }
1903 if (LocaleCompare("clip-units",keyword) == 0)
1904 {
cristybb503372010-05-27 20:51:26 +00001905 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001906 clip_units;
1907
1908 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001909 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001910 token);
1911 if (clip_units == -1)
1912 {
1913 status=MagickFalse;
1914 break;
1915 }
1916 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1917 if (clip_units == ObjectBoundingBox)
1918 {
1919 GetAffineMatrix(&current);
1920 affine.sx=draw_info->bounds.x2;
1921 affine.sy=draw_info->bounds.y2;
1922 affine.tx=draw_info->bounds.x1;
1923 affine.ty=draw_info->bounds.y1;
1924 break;
1925 }
1926 break;
1927 }
1928 if (LocaleCompare("circle",keyword) == 0)
1929 {
1930 primitive_type=CirclePrimitive;
1931 break;
1932 }
1933 if (LocaleCompare("color",keyword) == 0)
1934 {
1935 primitive_type=ColorPrimitive;
1936 break;
1937 }
1938 status=MagickFalse;
1939 break;
1940 }
1941 case 'd':
1942 case 'D':
1943 {
1944 if (LocaleCompare("decorate",keyword) == 0)
1945 {
cristybb503372010-05-27 20:51:26 +00001946 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001947 decorate;
1948
1949 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001950 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001951 token);
1952 if (decorate == -1)
1953 {
1954 status=MagickFalse;
1955 break;
1956 }
1957 graphic_context[n]->decorate=(DecorationType) decorate;
1958 break;
1959 }
1960 status=MagickFalse;
1961 break;
1962 }
1963 case 'e':
1964 case 'E':
1965 {
1966 if (LocaleCompare("ellipse",keyword) == 0)
1967 {
1968 primitive_type=EllipsePrimitive;
1969 break;
1970 }
1971 if (LocaleCompare("encoding",keyword) == 0)
1972 {
1973 GetMagickToken(q,&q,token);
1974 (void) CloneString(&graphic_context[n]->encoding,token);
1975 break;
1976 }
1977 status=MagickFalse;
1978 break;
1979 }
1980 case 'f':
1981 case 'F':
1982 {
1983 if (LocaleCompare("fill",keyword) == 0)
1984 {
1985 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00001986 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00001987 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1988 (void) DrawPatternPath(image,draw_info,token,
cristy018f07f2011-09-04 21:15:19 +00001989 &graphic_context[n]->fill_pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00001990 else
1991 {
cristy9950d572011-10-01 18:22:35 +00001992 status=QueryColorCompliance(token,AllCompliance,
1993 &graphic_context[n]->fill,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001994 if (status == MagickFalse)
1995 {
1996 ImageInfo
1997 *pattern_info;
1998
1999 pattern_info=AcquireImageInfo();
2000 (void) CopyMagickString(pattern_info->filename,token,
2001 MaxTextExtent);
2002 graphic_context[n]->fill_pattern=
2003 ReadImage(pattern_info,&image->exception);
2004 CatchException(&image->exception);
2005 pattern_info=DestroyImageInfo(pattern_info);
2006 }
2007 }
2008 break;
2009 }
2010 if (LocaleCompare("fill-opacity",keyword) == 0)
2011 {
2012 GetMagickToken(q,&q,token);
2013 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristy4c08aed2011-07-01 19:47:50 +00002014 graphic_context[n]->fill.alpha=ClampToQuantum((MagickRealType)
cristy1b542172011-07-09 16:04:35 +00002015 QuantumRange*factor*InterpretLocaleValue(token,
2016 (char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00002017 break;
2018 }
2019 if (LocaleCompare("fill-rule",keyword) == 0)
2020 {
cristybb503372010-05-27 20:51:26 +00002021 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002022 fill_rule;
2023
2024 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002025 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00002026 token);
2027 if (fill_rule == -1)
2028 {
2029 status=MagickFalse;
2030 break;
2031 }
2032 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2033 break;
2034 }
2035 if (LocaleCompare("font",keyword) == 0)
2036 {
2037 GetMagickToken(q,&q,token);
2038 (void) CloneString(&graphic_context[n]->font,token);
2039 if (LocaleCompare("none",token) == 0)
2040 graphic_context[n]->font=(char *)
2041 RelinquishMagickMemory(graphic_context[n]->font);
2042 break;
2043 }
2044 if (LocaleCompare("font-family",keyword) == 0)
2045 {
2046 GetMagickToken(q,&q,token);
2047 (void) CloneString(&graphic_context[n]->family,token);
2048 break;
2049 }
2050 if (LocaleCompare("font-size",keyword) == 0)
2051 {
2052 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002053 graphic_context[n]->pointsize=InterpretLocaleValue(token,
2054 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002055 break;
2056 }
2057 if (LocaleCompare("font-stretch",keyword) == 0)
2058 {
cristybb503372010-05-27 20:51:26 +00002059 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002060 stretch;
2061
2062 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002063 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002064 if (stretch == -1)
2065 {
2066 status=MagickFalse;
2067 break;
2068 }
2069 graphic_context[n]->stretch=(StretchType) stretch;
2070 break;
2071 }
2072 if (LocaleCompare("font-style",keyword) == 0)
2073 {
cristybb503372010-05-27 20:51:26 +00002074 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002075 style;
2076
2077 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002078 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002079 if (style == -1)
2080 {
2081 status=MagickFalse;
2082 break;
2083 }
2084 graphic_context[n]->style=(StyleType) style;
2085 break;
2086 }
2087 if (LocaleCompare("font-weight",keyword) == 0)
2088 {
2089 GetMagickToken(q,&q,token);
cristye27293e2009-12-18 02:53:20 +00002090 graphic_context[n]->weight=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00002091 if (LocaleCompare(token,"all") == 0)
2092 graphic_context[n]->weight=0;
2093 if (LocaleCompare(token,"bold") == 0)
2094 graphic_context[n]->weight=700;
2095 if (LocaleCompare(token,"bolder") == 0)
2096 if (graphic_context[n]->weight <= 800)
2097 graphic_context[n]->weight+=100;
2098 if (LocaleCompare(token,"lighter") == 0)
2099 if (graphic_context[n]->weight >= 100)
2100 graphic_context[n]->weight-=100;
2101 if (LocaleCompare(token,"normal") == 0)
2102 graphic_context[n]->weight=400;
2103 break;
2104 }
2105 status=MagickFalse;
2106 break;
2107 }
2108 case 'g':
2109 case 'G':
2110 {
2111 if (LocaleCompare("gradient-units",keyword) == 0)
2112 {
2113 GetMagickToken(q,&q,token);
2114 break;
2115 }
2116 if (LocaleCompare("gravity",keyword) == 0)
2117 {
cristybb503372010-05-27 20:51:26 +00002118 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002119 gravity;
2120
2121 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002122 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002123 if (gravity == -1)
2124 {
2125 status=MagickFalse;
2126 break;
2127 }
2128 graphic_context[n]->gravity=(GravityType) gravity;
2129 break;
2130 }
2131 status=MagickFalse;
2132 break;
2133 }
2134 case 'i':
2135 case 'I':
2136 {
2137 if (LocaleCompare("image",keyword) == 0)
2138 {
cristybb503372010-05-27 20:51:26 +00002139 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002140 compose;
2141
2142 primitive_type=ImagePrimitive;
2143 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002144 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002145 if (compose == -1)
2146 {
2147 status=MagickFalse;
2148 break;
2149 }
2150 graphic_context[n]->compose=(CompositeOperator) compose;
2151 break;
2152 }
cristyb32b90a2009-09-07 21:45:48 +00002153 if (LocaleCompare("interline-spacing",keyword) == 0)
2154 {
2155 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002156 graphic_context[n]->interline_spacing=InterpretLocaleValue(token,
2157 (char **) NULL);
cristyb32b90a2009-09-07 21:45:48 +00002158 break;
2159 }
cristy3ed852e2009-09-05 21:47:34 +00002160 if (LocaleCompare("interword-spacing",keyword) == 0)
2161 {
2162 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002163 graphic_context[n]->interword_spacing=InterpretLocaleValue(token,
2164 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002165 break;
2166 }
2167 status=MagickFalse;
2168 break;
2169 }
2170 case 'k':
2171 case 'K':
2172 {
2173 if (LocaleCompare("kerning",keyword) == 0)
2174 {
2175 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002176 graphic_context[n]->kerning=InterpretLocaleValue(token,
2177 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002178 break;
2179 }
2180 status=MagickFalse;
2181 break;
2182 }
2183 case 'l':
2184 case 'L':
2185 {
2186 if (LocaleCompare("line",keyword) == 0)
2187 {
2188 primitive_type=LinePrimitive;
2189 break;
2190 }
2191 status=MagickFalse;
2192 break;
2193 }
2194 case 'm':
2195 case 'M':
2196 {
2197 if (LocaleCompare("matte",keyword) == 0)
2198 {
2199 primitive_type=MattePrimitive;
2200 break;
2201 }
2202 status=MagickFalse;
2203 break;
2204 }
2205 case 'o':
2206 case 'O':
2207 {
2208 if (LocaleCompare("offset",keyword) == 0)
2209 {
2210 GetMagickToken(q,&q,token);
2211 break;
2212 }
2213 if (LocaleCompare("opacity",keyword) == 0)
2214 {
2215 GetMagickToken(q,&q,token);
2216 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristy4c08aed2011-07-01 19:47:50 +00002217 graphic_context[n]->alpha=ClampToQuantum((MagickRealType)
2218 QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->alpha)*
cristyc1acd842011-05-19 23:05:47 +00002219 factor*InterpretLocaleValue(token,(char **) NULL))));
cristy4c08aed2011-07-01 19:47:50 +00002220 graphic_context[n]->fill.alpha=graphic_context[n]->alpha;
2221 graphic_context[n]->stroke.alpha=graphic_context[n]->alpha;
cristy3ed852e2009-09-05 21:47:34 +00002222 break;
2223 }
2224 status=MagickFalse;
2225 break;
2226 }
2227 case 'p':
2228 case 'P':
2229 {
2230 if (LocaleCompare("path",keyword) == 0)
2231 {
2232 primitive_type=PathPrimitive;
2233 break;
2234 }
2235 if (LocaleCompare("point",keyword) == 0)
2236 {
2237 primitive_type=PointPrimitive;
2238 break;
2239 }
2240 if (LocaleCompare("polyline",keyword) == 0)
2241 {
2242 primitive_type=PolylinePrimitive;
2243 break;
2244 }
2245 if (LocaleCompare("polygon",keyword) == 0)
2246 {
2247 primitive_type=PolygonPrimitive;
2248 break;
2249 }
2250 if (LocaleCompare("pop",keyword) == 0)
2251 {
2252 GetMagickToken(q,&q,token);
2253 if (LocaleCompare("clip-path",token) == 0)
2254 break;
2255 if (LocaleCompare("defs",token) == 0)
2256 break;
2257 if (LocaleCompare("gradient",token) == 0)
2258 break;
2259 if (LocaleCompare("graphic-context",token) == 0)
2260 {
2261 if (n <= 0)
2262 {
2263 (void) ThrowMagickException(&image->exception,
2264 GetMagickModule(),DrawError,
2265 "UnbalancedGraphicContextPushPop","`%s'",token);
2266 n=0;
2267 break;
2268 }
2269 if (graphic_context[n]->clip_mask != (char *) NULL)
2270 if (LocaleCompare(graphic_context[n]->clip_mask,
2271 graphic_context[n-1]->clip_mask) != 0)
cristy018f07f2011-09-04 21:15:19 +00002272 (void) SetImageClipMask(image,(Image *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002273 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2274 n--;
2275 break;
2276 }
2277 if (LocaleCompare("pattern",token) == 0)
2278 break;
2279 status=MagickFalse;
2280 break;
2281 }
2282 if (LocaleCompare("push",keyword) == 0)
2283 {
2284 GetMagickToken(q,&q,token);
2285 if (LocaleCompare("clip-path",token) == 0)
2286 {
2287 char
2288 name[MaxTextExtent];
2289
2290 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00002291 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00002292 for (p=q; *q != '\0'; )
2293 {
2294 GetMagickToken(q,&q,token);
2295 if (LocaleCompare(token,"pop") != 0)
2296 continue;
2297 GetMagickToken(q,(const char **) NULL,token);
2298 if (LocaleCompare(token,"clip-path") != 0)
2299 continue;
2300 break;
2301 }
2302 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2303 (void) SetImageArtifact(image,name,token);
2304 GetMagickToken(q,&q,token);
2305 break;
2306 }
2307 if (LocaleCompare("gradient",token) == 0)
2308 {
2309 char
2310 key[2*MaxTextExtent],
2311 name[MaxTextExtent],
2312 type[MaxTextExtent];
2313
cristy3ed852e2009-09-05 21:47:34 +00002314 SegmentInfo
2315 segment;
2316
2317 GetMagickToken(q,&q,token);
2318 (void) CopyMagickString(name,token,MaxTextExtent);
2319 GetMagickToken(q,&q,token);
2320 (void) CopyMagickString(type,token,MaxTextExtent);
2321 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002322 segment.x1=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002323 GetMagickToken(q,&q,token);
2324 if (*token == ',')
2325 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002326 segment.y1=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002327 GetMagickToken(q,&q,token);
2328 if (*token == ',')
2329 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002330 segment.x2=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002331 GetMagickToken(q,&q,token);
2332 if (*token == ',')
2333 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002334 segment.y2=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002335 if (LocaleCompare(type,"radial") == 0)
2336 {
2337 GetMagickToken(q,&q,token);
2338 if (*token == ',')
2339 GetMagickToken(q,&q,token);
cristy3ed852e2009-09-05 21:47:34 +00002340 }
2341 for (p=q; *q != '\0'; )
2342 {
2343 GetMagickToken(q,&q,token);
2344 if (LocaleCompare(token,"pop") != 0)
2345 continue;
2346 GetMagickToken(q,(const char **) NULL,token);
2347 if (LocaleCompare(token,"gradient") != 0)
2348 continue;
2349 break;
2350 }
2351 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2352 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2353 graphic_context[n]->affine.ry*segment.y1+
2354 graphic_context[n]->affine.tx;
2355 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2356 graphic_context[n]->affine.sy*segment.y1+
2357 graphic_context[n]->affine.ty;
2358 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2359 graphic_context[n]->affine.ry*segment.y2+
2360 graphic_context[n]->affine.tx;
2361 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2362 graphic_context[n]->affine.sy*segment.y2+
2363 graphic_context[n]->affine.ty;
cristyb51dff52011-05-19 16:55:47 +00002364 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00002365 (void) SetImageArtifact(image,key,token);
cristyb51dff52011-05-19 16:55:47 +00002366 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2367 (void) FormatLocaleString(geometry,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002368 "%gx%g%+.15g%+.15g",
cristy3ed852e2009-09-05 21:47:34 +00002369 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2370 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2371 bounds.x1,bounds.y1);
2372 (void) SetImageArtifact(image,key,geometry);
2373 GetMagickToken(q,&q,token);
2374 break;
2375 }
2376 if (LocaleCompare("pattern",token) == 0)
2377 {
2378 RectangleInfo
2379 bounds;
2380
2381 GetMagickToken(q,&q,token);
2382 (void) CopyMagickString(name,token,MaxTextExtent);
2383 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002384 bounds.x=(ssize_t) ceil(InterpretLocaleValue(token,
2385 (char **) NULL)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002386 GetMagickToken(q,&q,token);
2387 if (*token == ',')
2388 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002389 bounds.y=(ssize_t) ceil(InterpretLocaleValue(token,
2390 (char **) NULL)-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002391 GetMagickToken(q,&q,token);
2392 if (*token == ',')
2393 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002394 bounds.width=(size_t) floor(InterpretLocaleValue(token,
2395 (char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002396 GetMagickToken(q,&q,token);
2397 if (*token == ',')
2398 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002399 bounds.height=(size_t) floor(InterpretLocaleValue(token,
2400 (char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002401 for (p=q; *q != '\0'; )
2402 {
2403 GetMagickToken(q,&q,token);
2404 if (LocaleCompare(token,"pop") != 0)
2405 continue;
2406 GetMagickToken(q,(const char **) NULL,token);
2407 if (LocaleCompare(token,"pattern") != 0)
2408 continue;
2409 break;
2410 }
2411 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
cristyb51dff52011-05-19 16:55:47 +00002412 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00002413 (void) SetImageArtifact(image,key,token);
cristyb51dff52011-05-19 16:55:47 +00002414 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2415 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002416 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002417 bounds.height,(double) bounds.x,(double) bounds.y);
cristy3ed852e2009-09-05 21:47:34 +00002418 (void) SetImageArtifact(image,key,geometry);
2419 GetMagickToken(q,&q,token);
2420 break;
2421 }
2422 if (LocaleCompare("graphic-context",token) == 0)
2423 {
2424 n++;
2425 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2426 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2427 if (graphic_context == (DrawInfo **) NULL)
2428 {
2429 (void) ThrowMagickException(&image->exception,
2430 GetMagickModule(),ResourceLimitError,
2431 "MemoryAllocationFailed","`%s'",image->filename);
2432 break;
2433 }
2434 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2435 graphic_context[n-1]);
2436 break;
2437 }
2438 if (LocaleCompare("defs",token) == 0)
2439 break;
2440 status=MagickFalse;
2441 break;
2442 }
2443 status=MagickFalse;
2444 break;
2445 }
2446 case 'r':
2447 case 'R':
2448 {
2449 if (LocaleCompare("rectangle",keyword) == 0)
2450 {
2451 primitive_type=RectanglePrimitive;
2452 break;
2453 }
2454 if (LocaleCompare("rotate",keyword) == 0)
2455 {
2456 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002457 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002458 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2459 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2460 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2461 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2462 break;
2463 }
2464 if (LocaleCompare("roundRectangle",keyword) == 0)
2465 {
2466 primitive_type=RoundRectanglePrimitive;
2467 break;
2468 }
2469 status=MagickFalse;
2470 break;
2471 }
2472 case 's':
2473 case 'S':
2474 {
2475 if (LocaleCompare("scale",keyword) == 0)
2476 {
2477 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002478 affine.sx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002479 GetMagickToken(q,&q,token);
2480 if (*token == ',')
2481 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002482 affine.sy=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002483 break;
2484 }
2485 if (LocaleCompare("skewX",keyword) == 0)
2486 {
2487 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002488 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002489 affine.ry=sin(DegreesToRadians(angle));
2490 break;
2491 }
2492 if (LocaleCompare("skewY",keyword) == 0)
2493 {
2494 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002495 angle=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002496 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2497 break;
2498 }
2499 if (LocaleCompare("stop-color",keyword) == 0)
2500 {
2501 PixelPacket
2502 stop_color;
2503
2504 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00002505 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2506 &image->exception);
cristy3ed852e2009-09-05 21:47:34 +00002507 (void) GradientImage(image,LinearGradient,ReflectSpread,
cristy189e84c2011-08-27 18:08:53 +00002508 &start_color,&stop_color,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00002509 start_color=stop_color;
2510 GetMagickToken(q,&q,token);
2511 break;
2512 }
2513 if (LocaleCompare("stroke",keyword) == 0)
2514 {
2515 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00002516 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00002517 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2518 (void) DrawPatternPath(image,draw_info,token,
cristy018f07f2011-09-04 21:15:19 +00002519 &graphic_context[n]->stroke_pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00002520 else
2521 {
cristy9950d572011-10-01 18:22:35 +00002522 status=QueryColorCompliance(token,AllCompliance,
2523 &graphic_context[n]->stroke,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00002524 if (status == MagickFalse)
2525 {
2526 ImageInfo
2527 *pattern_info;
2528
2529 pattern_info=AcquireImageInfo();
2530 (void) CopyMagickString(pattern_info->filename,token,
2531 MaxTextExtent);
2532 graphic_context[n]->stroke_pattern=
2533 ReadImage(pattern_info,&image->exception);
2534 CatchException(&image->exception);
2535 pattern_info=DestroyImageInfo(pattern_info);
2536 }
2537 }
2538 break;
2539 }
2540 if (LocaleCompare("stroke-antialias",keyword) == 0)
2541 {
2542 GetMagickToken(q,&q,token);
2543 graphic_context[n]->stroke_antialias=
cristyf2f27272009-12-17 14:48:46 +00002544 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002545 break;
2546 }
2547 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2548 {
2549 if (graphic_context[n]->dash_pattern != (double *) NULL)
2550 graphic_context[n]->dash_pattern=(double *)
2551 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2552 if (IsPoint(q) != MagickFalse)
2553 {
2554 const char
2555 *p;
2556
2557 p=q;
2558 GetMagickToken(p,&p,token);
2559 if (*token == ',')
2560 GetMagickToken(p,&p,token);
2561 for (x=0; IsPoint(token) != MagickFalse; x++)
2562 {
2563 GetMagickToken(p,&p,token);
2564 if (*token == ',')
2565 GetMagickToken(p,&p,token);
2566 }
2567 graphic_context[n]->dash_pattern=(double *)
2568 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2569 sizeof(*graphic_context[n]->dash_pattern));
2570 if (graphic_context[n]->dash_pattern == (double *) NULL)
2571 {
2572 (void) ThrowMagickException(&image->exception,
2573 GetMagickModule(),ResourceLimitError,
2574 "MemoryAllocationFailed","`%s'",image->filename);
2575 break;
2576 }
2577 for (j=0; j < x; j++)
2578 {
2579 GetMagickToken(q,&q,token);
2580 if (*token == ',')
2581 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002582 graphic_context[n]->dash_pattern[j]=InterpretLocaleValue(
2583 token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002584 }
2585 if ((x & 0x01) != 0)
2586 for ( ; j < (2*x); j++)
2587 graphic_context[n]->dash_pattern[j]=
2588 graphic_context[n]->dash_pattern[j-x];
2589 graphic_context[n]->dash_pattern[j]=0.0;
2590 break;
2591 }
2592 GetMagickToken(q,&q,token);
2593 break;
2594 }
2595 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2596 {
2597 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002598 graphic_context[n]->dash_offset=InterpretLocaleValue(token,
2599 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002600 break;
2601 }
2602 if (LocaleCompare("stroke-linecap",keyword) == 0)
2603 {
cristybb503372010-05-27 20:51:26 +00002604 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002605 linecap;
2606
2607 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002608 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002609 if (linecap == -1)
2610 {
2611 status=MagickFalse;
2612 break;
2613 }
2614 graphic_context[n]->linecap=(LineCap) linecap;
2615 break;
2616 }
2617 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2618 {
cristybb503372010-05-27 20:51:26 +00002619 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002620 linejoin;
2621
2622 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002623 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002624 if (linejoin == -1)
2625 {
2626 status=MagickFalse;
2627 break;
2628 }
2629 graphic_context[n]->linejoin=(LineJoin) linejoin;
2630 break;
2631 }
2632 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2633 {
2634 GetMagickToken(q,&q,token);
cristye27293e2009-12-18 02:53:20 +00002635 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00002636 break;
2637 }
2638 if (LocaleCompare("stroke-opacity",keyword) == 0)
2639 {
2640 GetMagickToken(q,&q,token);
2641 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristy4c08aed2011-07-01 19:47:50 +00002642 graphic_context[n]->stroke.alpha=ClampToQuantum((MagickRealType)
cristy1b542172011-07-09 16:04:35 +00002643 QuantumRange*factor*InterpretLocaleValue(token,
2644 (char **) NULL));
cristy3ed852e2009-09-05 21:47:34 +00002645 break;
2646 }
2647 if (LocaleCompare("stroke-width",keyword) == 0)
2648 {
2649 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002650 graphic_context[n]->stroke_width=InterpretLocaleValue(token,
2651 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002652 break;
2653 }
2654 status=MagickFalse;
2655 break;
2656 }
2657 case 't':
2658 case 'T':
2659 {
2660 if (LocaleCompare("text",keyword) == 0)
2661 {
2662 primitive_type=TextPrimitive;
2663 break;
2664 }
2665 if (LocaleCompare("text-align",keyword) == 0)
2666 {
cristybb503372010-05-27 20:51:26 +00002667 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002668 align;
2669
2670 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002671 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002672 if (align == -1)
2673 {
2674 status=MagickFalse;
2675 break;
2676 }
2677 graphic_context[n]->align=(AlignType) align;
2678 break;
2679 }
2680 if (LocaleCompare("text-anchor",keyword) == 0)
2681 {
cristybb503372010-05-27 20:51:26 +00002682 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002683 align;
2684
2685 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002686 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002687 if (align == -1)
2688 {
2689 status=MagickFalse;
2690 break;
2691 }
2692 graphic_context[n]->align=(AlignType) align;
2693 break;
2694 }
2695 if (LocaleCompare("text-antialias",keyword) == 0)
2696 {
2697 GetMagickToken(q,&q,token);
2698 graphic_context[n]->text_antialias=
cristyf2f27272009-12-17 14:48:46 +00002699 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002700 break;
2701 }
2702 if (LocaleCompare("text-undercolor",keyword) == 0)
2703 {
2704 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00002705 (void) QueryColorCompliance(token,AllCompliance,
2706 &graphic_context[n]->undercolor,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00002707 break;
2708 }
2709 if (LocaleCompare("translate",keyword) == 0)
2710 {
2711 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002712 affine.tx=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002713 GetMagickToken(q,&q,token);
2714 if (*token == ',')
2715 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002716 affine.ty=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002717 break;
2718 }
2719 status=MagickFalse;
2720 break;
2721 }
2722 case 'v':
2723 case 'V':
2724 {
2725 if (LocaleCompare("viewbox",keyword) == 0)
2726 {
2727 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002728 graphic_context[n]->viewbox.x=(ssize_t) ceil(InterpretLocaleValue(
2729 token,(char **) NULL)-0.5);
cristy06609ee2010-03-17 20:21:27 +00002730 GetMagickToken(q,&q,token);
2731 if (*token == ',')
2732 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002733 graphic_context[n]->viewbox.y=(ssize_t) ceil(InterpretLocaleValue(
2734 token,(char **) NULL)-0.5);
cristy06609ee2010-03-17 20:21:27 +00002735 GetMagickToken(q,&q,token);
2736 if (*token == ',')
2737 GetMagickToken(q,&q,token);
cristybb503372010-05-27 20:51:26 +00002738 graphic_context[n]->viewbox.width=(size_t) floor(
cristyc1acd842011-05-19 23:05:47 +00002739 InterpretLocaleValue(token,(char **) NULL)+0.5);
cristy06609ee2010-03-17 20:21:27 +00002740 GetMagickToken(q,&q,token);
2741 if (*token == ',')
2742 GetMagickToken(q,&q,token);
cristybb503372010-05-27 20:51:26 +00002743 graphic_context[n]->viewbox.height=(size_t) floor(
cristyc1acd842011-05-19 23:05:47 +00002744 InterpretLocaleValue(token,(char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002745 break;
2746 }
2747 status=MagickFalse;
2748 break;
2749 }
2750 default:
2751 {
2752 status=MagickFalse;
2753 break;
2754 }
2755 }
2756 if (status == MagickFalse)
2757 break;
2758 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2759 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2760 {
cristyfbd70092010-12-01 19:31:14 +00002761 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2762 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2763 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2764 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2765 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2766 current.tx;
2767 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2768 current.ty;
cristy3ed852e2009-09-05 21:47:34 +00002769 }
2770 if (primitive_type == UndefinedPrimitive)
2771 {
2772 if (image->debug != MagickFalse)
2773 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2774 (int) (q-p),p);
2775 continue;
2776 }
2777 /*
2778 Parse the primitive attributes.
2779 */
2780 i=0;
2781 j=0;
2782 primitive_info[0].point.x=0.0;
2783 primitive_info[0].point.y=0.0;
2784 for (x=0; *q != '\0'; x++)
2785 {
2786 /*
2787 Define points.
2788 */
2789 if (IsPoint(q) == MagickFalse)
2790 break;
2791 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002792 point.x=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002793 GetMagickToken(q,&q,token);
2794 if (*token == ',')
2795 GetMagickToken(q,&q,token);
cristyc1acd842011-05-19 23:05:47 +00002796 point.y=InterpretLocaleValue(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002797 GetMagickToken(q,(const char **) NULL,token);
2798 if (*token == ',')
2799 GetMagickToken(q,&q,token);
2800 primitive_info[i].primitive=primitive_type;
2801 primitive_info[i].point=point;
2802 primitive_info[i].coordinates=0;
2803 primitive_info[i].method=FloodfillMethod;
2804 i++;
cristybb503372010-05-27 20:51:26 +00002805 if (i < (ssize_t) number_points)
cristy3ed852e2009-09-05 21:47:34 +00002806 continue;
2807 number_points<<=1;
2808 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2809 (size_t) number_points,sizeof(*primitive_info));
2810 if (primitive_info == (PrimitiveInfo *) NULL)
2811 {
2812 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2813 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2814 break;
2815 }
2816 }
2817 primitive_info[j].primitive=primitive_type;
cristybb503372010-05-27 20:51:26 +00002818 primitive_info[j].coordinates=(size_t) x;
cristy3ed852e2009-09-05 21:47:34 +00002819 primitive_info[j].method=FloodfillMethod;
2820 primitive_info[j].text=(char *) NULL;
2821 /*
2822 Circumscribe primitive within a circle.
2823 */
2824 bounds.x1=primitive_info[j].point.x;
2825 bounds.y1=primitive_info[j].point.y;
2826 bounds.x2=primitive_info[j].point.x;
2827 bounds.y2=primitive_info[j].point.y;
cristybb503372010-05-27 20:51:26 +00002828 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
cristy3ed852e2009-09-05 21:47:34 +00002829 {
2830 point=primitive_info[j+k].point;
2831 if (point.x < bounds.x1)
2832 bounds.x1=point.x;
2833 if (point.y < bounds.y1)
2834 bounds.y1=point.y;
2835 if (point.x > bounds.x2)
2836 bounds.x2=point.x;
2837 if (point.y > bounds.y2)
2838 bounds.y2=point.y;
2839 }
2840 /*
2841 Speculate how many points our primitive might consume.
2842 */
2843 length=primitive_info[j].coordinates;
2844 switch (primitive_type)
2845 {
2846 case RectanglePrimitive:
2847 {
2848 length*=5;
2849 break;
2850 }
2851 case RoundRectanglePrimitive:
2852 {
cristy78817ad2010-05-07 12:25:34 +00002853 length*=5+8*BezierQuantum;
cristy3ed852e2009-09-05 21:47:34 +00002854 break;
2855 }
2856 case BezierPrimitive:
2857 {
2858 if (primitive_info[j].coordinates > 107)
2859 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2860 DrawError,"TooManyBezierCoordinates","`%s'",token);
2861 length=BezierQuantum*primitive_info[j].coordinates;
2862 break;
2863 }
2864 case PathPrimitive:
2865 {
2866 char
2867 *s,
2868 *t;
2869
2870 GetMagickToken(q,&q,token);
cristy40a08ad2010-02-09 02:27:44 +00002871 length=1;
cristy3ed852e2009-09-05 21:47:34 +00002872 t=token;
2873 for (s=token; *s != '\0'; s=t)
2874 {
cristy00976d82011-02-20 20:31:28 +00002875 double
2876 value;
2877
cristyc1acd842011-05-19 23:05:47 +00002878 value=InterpretLocaleValue(s,&t);
cristy00976d82011-02-20 20:31:28 +00002879 (void) value;
cristy3ed852e2009-09-05 21:47:34 +00002880 if (s == t)
2881 {
2882 t++;
2883 continue;
2884 }
cristyef7a6ce2011-05-14 15:01:11 +00002885 length++;
cristy3ed852e2009-09-05 21:47:34 +00002886 }
cristyd2f832c2011-06-28 12:35:24 +00002887 length=length*BezierQuantum/2;
cristy3ed852e2009-09-05 21:47:34 +00002888 break;
2889 }
2890 case CirclePrimitive:
2891 case ArcPrimitive:
2892 case EllipsePrimitive:
2893 {
2894 MagickRealType
2895 alpha,
2896 beta,
2897 radius;
2898
2899 alpha=bounds.x2-bounds.x1;
2900 beta=bounds.y2-bounds.y1;
2901 radius=hypot((double) alpha,(double) beta);
cristyf53f26f2011-05-16 00:56:05 +00002902 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
cristy3ed852e2009-09-05 21:47:34 +00002903 break;
2904 }
2905 default:
2906 break;
2907 }
cristybb503372010-05-27 20:51:26 +00002908 if ((size_t) (i+length) >= number_points)
cristy3ed852e2009-09-05 21:47:34 +00002909 {
2910 /*
2911 Resize based on speculative points required by primitive.
2912 */
cristy9ce61b92010-05-12 16:30:26 +00002913 number_points+=length+1;
cristy3ed852e2009-09-05 21:47:34 +00002914 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2915 (size_t) number_points,sizeof(*primitive_info));
2916 if (primitive_info == (PrimitiveInfo *) NULL)
2917 {
2918 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2919 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2920 image->filename);
2921 break;
2922 }
2923 }
2924 switch (primitive_type)
2925 {
2926 case PointPrimitive:
2927 default:
2928 {
2929 if (primitive_info[j].coordinates != 1)
2930 {
2931 status=MagickFalse;
2932 break;
2933 }
2934 TracePoint(primitive_info+j,primitive_info[j].point);
cristybb503372010-05-27 20:51:26 +00002935 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002936 break;
2937 }
2938 case LinePrimitive:
2939 {
2940 if (primitive_info[j].coordinates != 2)
2941 {
2942 status=MagickFalse;
2943 break;
2944 }
2945 TraceLine(primitive_info+j,primitive_info[j].point,
2946 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002947 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002948 break;
2949 }
2950 case RectanglePrimitive:
2951 {
2952 if (primitive_info[j].coordinates != 2)
2953 {
2954 status=MagickFalse;
2955 break;
2956 }
2957 TraceRectangle(primitive_info+j,primitive_info[j].point,
2958 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002959 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002960 break;
2961 }
2962 case RoundRectanglePrimitive:
2963 {
2964 if (primitive_info[j].coordinates != 3)
2965 {
2966 status=MagickFalse;
2967 break;
2968 }
2969 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2970 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002971 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002972 break;
2973 }
2974 case ArcPrimitive:
2975 {
2976 if (primitive_info[j].coordinates != 3)
2977 {
2978 primitive_type=UndefinedPrimitive;
2979 break;
2980 }
2981 TraceArc(primitive_info+j,primitive_info[j].point,
2982 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002983 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002984 break;
2985 }
2986 case EllipsePrimitive:
2987 {
2988 if (primitive_info[j].coordinates != 3)
2989 {
2990 status=MagickFalse;
2991 break;
2992 }
2993 TraceEllipse(primitive_info+j,primitive_info[j].point,
2994 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002995 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002996 break;
2997 }
2998 case CirclePrimitive:
2999 {
3000 if (primitive_info[j].coordinates != 2)
3001 {
3002 status=MagickFalse;
3003 break;
3004 }
3005 TraceCircle(primitive_info+j,primitive_info[j].point,
3006 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00003007 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00003008 break;
3009 }
3010 case PolylinePrimitive:
3011 break;
3012 case PolygonPrimitive:
3013 {
3014 primitive_info[i]=primitive_info[j];
3015 primitive_info[i].coordinates=0;
3016 primitive_info[j].coordinates++;
3017 i++;
3018 break;
3019 }
3020 case BezierPrimitive:
3021 {
3022 if (primitive_info[j].coordinates < 3)
3023 {
3024 status=MagickFalse;
3025 break;
3026 }
3027 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
cristybb503372010-05-27 20:51:26 +00003028 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00003029 break;
3030 }
3031 case PathPrimitive:
3032 {
cristybb503372010-05-27 20:51:26 +00003033 i=(ssize_t) (j+TracePath(primitive_info+j,token));
cristy3ed852e2009-09-05 21:47:34 +00003034 break;
3035 }
3036 case ColorPrimitive:
3037 case MattePrimitive:
3038 {
cristybb503372010-05-27 20:51:26 +00003039 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003040 method;
3041
3042 if (primitive_info[j].coordinates != 1)
3043 {
3044 status=MagickFalse;
3045 break;
3046 }
3047 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00003048 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00003049 if (method == -1)
3050 {
3051 status=MagickFalse;
3052 break;
3053 }
3054 primitive_info[j].method=(PaintMethod) method;
3055 break;
3056 }
3057 case TextPrimitive:
3058 {
3059 if (primitive_info[j].coordinates != 1)
3060 {
3061 status=MagickFalse;
3062 break;
3063 }
3064 if (*token != ',')
3065 GetMagickToken(q,&q,token);
3066 primitive_info[j].text=AcquireString(token);
3067 break;
3068 }
3069 case ImagePrimitive:
3070 {
3071 if (primitive_info[j].coordinates != 2)
3072 {
3073 status=MagickFalse;
3074 break;
3075 }
3076 GetMagickToken(q,&q,token);
3077 primitive_info[j].text=AcquireString(token);
3078 break;
3079 }
3080 }
3081 if (primitive_info == (PrimitiveInfo *) NULL)
3082 break;
3083 if (image->debug != MagickFalse)
3084 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3085 if (status == MagickFalse)
3086 break;
3087 primitive_info[i].primitive=UndefinedPrimitive;
3088 if (i == 0)
3089 continue;
3090 /*
3091 Transform points.
3092 */
3093 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3094 {
3095 point=primitive_info[i].point;
3096 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3097 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3098 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3099 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3100 point=primitive_info[i].point;
3101 if (point.x < graphic_context[n]->bounds.x1)
3102 graphic_context[n]->bounds.x1=point.x;
3103 if (point.y < graphic_context[n]->bounds.y1)
3104 graphic_context[n]->bounds.y1=point.y;
3105 if (point.x > graphic_context[n]->bounds.x2)
3106 graphic_context[n]->bounds.x2=point.x;
3107 if (point.y > graphic_context[n]->bounds.y2)
3108 graphic_context[n]->bounds.y2=point.y;
3109 if (primitive_info[i].primitive == ImagePrimitive)
3110 break;
cristybb503372010-05-27 20:51:26 +00003111 if (i >= (ssize_t) number_points)
cristy9ce61b92010-05-12 16:30:26 +00003112 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00003113 }
cristy3ed852e2009-09-05 21:47:34 +00003114 if (graphic_context[n]->render != MagickFalse)
3115 {
3116 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3117 (LocaleCompare(graphic_context[n]->clip_mask,
3118 graphic_context[n-1]->clip_mask) != 0))
3119 (void) DrawClipPath(image,graphic_context[n],
cristy018f07f2011-09-04 21:15:19 +00003120 graphic_context[n]->clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00003121 (void) DrawPrimitive(image,graphic_context[n],primitive_info);
3122 }
3123 if (primitive_info->text != (char *) NULL)
3124 primitive_info->text=(char *) RelinquishMagickMemory(
3125 primitive_info->text);
3126 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3127 primitive_extent);
3128 if (proceed == MagickFalse)
3129 break;
3130 }
3131 if (image->debug != MagickFalse)
3132 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3133 /*
3134 Relinquish resources.
3135 */
3136 token=DestroyString(token);
3137 if (primitive_info != (PrimitiveInfo *) NULL)
3138 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3139 primitive=DestroyString(primitive);
3140 for ( ; n >= 0; n--)
3141 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3142 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3143 if (status == MagickFalse)
3144 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3145 keyword);
3146 return(status);
3147}
3148
3149/*
3150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3151% %
3152% %
3153% %
3154% D r a w G r a d i e n t I m a g e %
3155% %
3156% %
3157% %
3158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3159%
3160% DrawGradientImage() draws a linear gradient on the image.
3161%
3162% The format of the DrawGradientImage method is:
3163%
3164% MagickBooleanType DrawGradientImage(Image *image,
3165% const DrawInfo *draw_info)
3166%
3167% A description of each parameter follows:
3168%
3169% o image: the image.
3170%
3171% o _info: the draw info.
3172%
3173*/
3174
3175static inline MagickRealType GetStopColorOffset(const GradientInfo *gradient,
cristybb503372010-05-27 20:51:26 +00003176 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00003177{
3178 switch (gradient->type)
3179 {
3180 case UndefinedGradient:
3181 case LinearGradient:
3182 {
3183 MagickRealType
3184 gamma,
3185 length,
3186 offset,
3187 scale;
3188
3189 PointInfo
3190 p,
3191 q;
3192
3193 const SegmentInfo
3194 *gradient_vector;
3195
3196 gradient_vector=(&gradient->gradient_vector);
3197 p.x=gradient_vector->x2-gradient_vector->x1;
3198 p.y=gradient_vector->y2-gradient_vector->y1;
3199 q.x=(double) x-gradient_vector->x1;
3200 q.y=(double) y-gradient_vector->y1;
3201 length=sqrt(q.x*q.x+q.y*q.y);
3202 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3203 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
3204 scale=p.x*q.x+p.y*q.y;
3205 offset=gamma*scale*length;
3206 return(offset);
3207 }
3208 case RadialGradient:
3209 {
3210 MagickRealType
3211 length,
3212 offset;
3213
3214 PointInfo
3215 v;
3216
3217 v.x=(double) x-gradient->center.x;
3218 v.y=(double) y-gradient->center.y;
3219 length=sqrt(v.x*v.x+v.y*v.y);
3220 if (gradient->spread == RepeatSpread)
3221 return(length);
3222 offset=length/gradient->radius;
3223 return(offset);
3224 }
3225 }
3226 return(0.0);
3227}
3228
3229MagickExport MagickBooleanType DrawGradientImage(Image *image,
3230 const DrawInfo *draw_info)
3231{
cristyc4c8d132010-01-07 01:58:38 +00003232 CacheView
3233 *image_view;
3234
cristy3ed852e2009-09-05 21:47:34 +00003235 const GradientInfo
3236 *gradient;
3237
3238 const SegmentInfo
3239 *gradient_vector;
3240
3241 ExceptionInfo
3242 *exception;
3243
cristy3ed852e2009-09-05 21:47:34 +00003244 MagickBooleanType
3245 status;
3246
cristy4c08aed2011-07-01 19:47:50 +00003247 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003248 zero;
3249
3250 MagickRealType
3251 length;
3252
3253 PointInfo
3254 point;
3255
3256 RectangleInfo
3257 bounding_box;
3258
cristy826a5472010-08-31 23:21:38 +00003259 ssize_t
3260 y;
3261
cristy3ed852e2009-09-05 21:47:34 +00003262 /*
3263 Draw linear or radial gradient on image.
3264 */
3265 assert(image != (Image *) NULL);
3266 assert(image->signature == MagickSignature);
3267 if (image->debug != MagickFalse)
3268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3269 assert(draw_info != (const DrawInfo *) NULL);
3270 gradient=(&draw_info->gradient);
3271 gradient_vector=(&gradient->gradient_vector);
3272 point.x=gradient_vector->x2-gradient_vector->x1;
3273 point.y=gradient_vector->y2-gradient_vector->y1;
3274 length=sqrt(point.x*point.x+point.y*point.y);
3275 bounding_box=gradient->bounding_box;
3276 status=MagickTrue;
3277 exception=(&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00003278 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +00003279 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003280#if defined(MAGICKCORE_OPENMP_SUPPORT)
3281 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003282#endif
cristybb503372010-05-27 20:51:26 +00003283 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
cristy3ed852e2009-09-05 21:47:34 +00003284 {
cristy4c08aed2011-07-01 19:47:50 +00003285 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003286 composite,
3287 pixel;
3288
3289 MagickRealType
3290 alpha,
3291 offset;
3292
cristy4c08aed2011-07-01 19:47:50 +00003293 register Quantum
3294 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003295
cristybb503372010-05-27 20:51:26 +00003296 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003297 i,
3298 x;
3299
cristy826a5472010-08-31 23:21:38 +00003300 ssize_t
3301 j;
3302
cristy3ed852e2009-09-05 21:47:34 +00003303 if (status == MagickFalse)
3304 continue;
3305 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003306 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003307 {
3308 status=MagickFalse;
3309 continue;
3310 }
cristy3ed852e2009-09-05 21:47:34 +00003311 pixel=zero;
3312 composite=zero;
3313 offset=GetStopColorOffset(gradient,0,y);
3314 if (gradient->type != RadialGradient)
3315 offset/=length;
cristybb503372010-05-27 20:51:26 +00003316 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
cristy3ed852e2009-09-05 21:47:34 +00003317 {
cristy4c08aed2011-07-01 19:47:50 +00003318 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00003319 switch (gradient->spread)
3320 {
3321 case UndefinedSpread:
3322 case PadSpread:
3323 {
cristybb503372010-05-27 20:51:26 +00003324 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3325 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003326 {
3327 offset=GetStopColorOffset(gradient,x,y);
3328 if (gradient->type != RadialGradient)
3329 offset/=length;
3330 }
cristybb503372010-05-27 20:51:26 +00003331 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003332 if (offset < gradient->stops[i].offset)
3333 break;
3334 if ((offset < 0.0) || (i == 0))
3335 composite=gradient->stops[0].color;
3336 else
cristybb503372010-05-27 20:51:26 +00003337 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
cristy3ed852e2009-09-05 21:47:34 +00003338 composite=gradient->stops[gradient->number_stops-1].color;
3339 else
3340 {
3341 j=i;
3342 i--;
3343 alpha=(offset-gradient->stops[i].offset)/
3344 (gradient->stops[j].offset-gradient->stops[i].offset);
cristy4c08aed2011-07-01 19:47:50 +00003345 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003346 &gradient->stops[j].color,alpha,&composite);
3347 }
3348 break;
3349 }
3350 case ReflectSpread:
3351 {
cristybb503372010-05-27 20:51:26 +00003352 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3353 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003354 {
3355 offset=GetStopColorOffset(gradient,x,y);
3356 if (gradient->type != RadialGradient)
3357 offset/=length;
3358 }
3359 if (offset < 0.0)
3360 offset=(-offset);
cristybb503372010-05-27 20:51:26 +00003361 if ((ssize_t) fmod(offset,2.0) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003362 offset=fmod(offset,1.0);
3363 else
3364 offset=1.0-fmod(offset,1.0);
cristybb503372010-05-27 20:51:26 +00003365 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003366 if (offset < gradient->stops[i].offset)
3367 break;
3368 if (i == 0)
3369 composite=gradient->stops[0].color;
3370 else
cristybb503372010-05-27 20:51:26 +00003371 if (i == (ssize_t) gradient->number_stops)
cristy3ed852e2009-09-05 21:47:34 +00003372 composite=gradient->stops[gradient->number_stops-1].color;
3373 else
3374 {
3375 j=i;
3376 i--;
3377 alpha=(offset-gradient->stops[i].offset)/
3378 (gradient->stops[j].offset-gradient->stops[i].offset);
cristy4c08aed2011-07-01 19:47:50 +00003379 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003380 &gradient->stops[j].color,alpha,&composite);
3381 }
3382 break;
3383 }
3384 case RepeatSpread:
3385 {
3386 MagickBooleanType
3387 antialias;
3388
3389 MagickRealType
3390 repeat;
3391
3392 antialias=MagickFalse;
3393 repeat=0.0;
cristybb503372010-05-27 20:51:26 +00003394 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3395 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003396 {
3397 offset=GetStopColorOffset(gradient,x,y);
3398 if (gradient->type == LinearGradient)
3399 {
3400 repeat=fmod(offset,length);
3401 if (repeat < 0.0)
3402 repeat=length-fmod(-repeat,length);
3403 else
3404 repeat=fmod(offset,length);
3405 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3406 MagickTrue : MagickFalse;
3407 offset=repeat/length;
3408 }
3409 else
3410 {
3411 repeat=fmod(offset,gradient->radius);
3412 if (repeat < 0.0)
3413 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3414 else
3415 repeat=fmod(offset,gradient->radius);
cristy4c08aed2011-07-01 19:47:50 +00003416 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3417 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003418 offset=repeat/gradient->radius;
3419 }
3420 }
cristybb503372010-05-27 20:51:26 +00003421 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003422 if (offset < gradient->stops[i].offset)
3423 break;
3424 if (i == 0)
3425 composite=gradient->stops[0].color;
3426 else
cristybb503372010-05-27 20:51:26 +00003427 if (i == (ssize_t) gradient->number_stops)
cristy3ed852e2009-09-05 21:47:34 +00003428 composite=gradient->stops[gradient->number_stops-1].color;
3429 else
3430 {
3431 j=i;
3432 i--;
3433 alpha=(offset-gradient->stops[i].offset)/
3434 (gradient->stops[j].offset-gradient->stops[i].offset);
3435 if (antialias != MagickFalse)
3436 {
3437 if (gradient->type == LinearGradient)
3438 alpha=length-repeat;
3439 else
3440 alpha=gradient->radius-repeat;
3441 i=0;
cristybb503372010-05-27 20:51:26 +00003442 j=(ssize_t) gradient->number_stops-1L;
cristy3ed852e2009-09-05 21:47:34 +00003443 }
cristy4c08aed2011-07-01 19:47:50 +00003444 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003445 &gradient->stops[j].color,alpha,&composite);
3446 }
3447 break;
3448 }
3449 }
cristy4c08aed2011-07-01 19:47:50 +00003450 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3451 &pixel);
3452 SetPixelPixelInfo(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003453 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003454 }
3455 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3456 status=MagickFalse;
3457 }
3458 image_view=DestroyCacheView(image_view);
3459 return(status);
3460}
3461
3462/*
3463%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3464% %
3465% %
3466% %
3467% D r a w P a t t e r n P a t h %
3468% %
3469% %
3470% %
3471%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3472%
3473% DrawPatternPath() draws a pattern.
3474%
3475% The format of the DrawPatternPath method is:
3476%
3477% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
cristy018f07f2011-09-04 21:15:19 +00003478% const char *name,Image **pattern,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003479%
3480% A description of each parameter follows:
3481%
3482% o image: the image.
3483%
3484% o draw_info: the draw info.
3485%
3486% o name: the pattern name.
3487%
3488% o image: the image.
3489%
cristy018f07f2011-09-04 21:15:19 +00003490% o exception: return any errors or warnings in this structure.
3491%
cristy3ed852e2009-09-05 21:47:34 +00003492*/
3493MagickExport MagickBooleanType DrawPatternPath(Image *image,
cristy018f07f2011-09-04 21:15:19 +00003494 const DrawInfo *draw_info,const char *name,Image **pattern,
3495 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003496{
3497 char
3498 property[MaxTextExtent];
3499
3500 const char
3501 *geometry,
3502 *path;
3503
3504 DrawInfo
3505 *clone_info;
3506
3507 ImageInfo
3508 *image_info;
3509
3510 MagickBooleanType
3511 status;
3512
3513 assert(image != (Image *) NULL);
3514 assert(image->signature == MagickSignature);
3515 if (image->debug != MagickFalse)
3516 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3517 assert(draw_info != (const DrawInfo *) NULL);
3518 assert(name != (const char *) NULL);
cristyb51dff52011-05-19 16:55:47 +00003519 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00003520 path=GetImageArtifact(image,property);
3521 if (path == (const char *) NULL)
3522 return(MagickFalse);
cristyb51dff52011-05-19 16:55:47 +00003523 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
cristy3ed852e2009-09-05 21:47:34 +00003524 geometry=GetImageArtifact(image,property);
3525 if (geometry == (const char *) NULL)
3526 return(MagickFalse);
3527 if ((*pattern) != (Image *) NULL)
3528 *pattern=DestroyImage(*pattern);
3529 image_info=AcquireImageInfo();
3530 image_info->size=AcquireString(geometry);
cristy9950d572011-10-01 18:22:35 +00003531 *pattern=AcquireImage(image_info,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00003532 image_info=DestroyImageInfo(image_info);
cristy9950d572011-10-01 18:22:35 +00003533 (void) QueryColorCompliance("#00000000",AllCompliance,
3534 &(*pattern)->background_color,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00003535 (void) SetImageBackgroundColor(*pattern);
3536 if (image->debug != MagickFalse)
3537 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3538 "begin pattern-path %s %s",name,geometry);
3539 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3540 clone_info->fill_pattern=NewImageList();
3541 clone_info->stroke_pattern=NewImageList();
3542 (void) CloneString(&clone_info->primitive,path);
cristy018f07f2011-09-04 21:15:19 +00003543 status=DrawImage(*pattern,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003544 clone_info=DestroyDrawInfo(clone_info);
3545 if (image->debug != MagickFalse)
3546 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3547 return(status);
3548}
3549
3550/*
3551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3552% %
3553% %
3554% %
3555+ D r a w P o l y g o n P r i m i t i v e %
3556% %
3557% %
3558% %
3559%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3560%
3561% DrawPolygonPrimitive() draws a polygon on the image.
3562%
3563% The format of the DrawPolygonPrimitive method is:
3564%
3565% MagickBooleanType DrawPolygonPrimitive(Image *image,
3566% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3567%
3568% A description of each parameter follows:
3569%
3570% o image: the image.
3571%
3572% o draw_info: the draw info.
3573%
3574% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3575%
3576*/
3577
3578static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3579{
cristybb503372010-05-27 20:51:26 +00003580 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003581 i;
3582
3583 assert(polygon_info != (PolygonInfo **) NULL);
cristybb503372010-05-27 20:51:26 +00003584 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy3ed852e2009-09-05 21:47:34 +00003585 if (polygon_info[i] != (PolygonInfo *) NULL)
3586 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
cristyb41ee102010-10-04 16:46:15 +00003587 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
cristy3ed852e2009-09-05 21:47:34 +00003588 return(polygon_info);
3589}
3590
3591static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3592 const PrimitiveInfo *primitive_info)
3593{
3594 PathInfo
cristyfa112112010-01-04 17:48:07 +00003595 *restrict path_info;
cristy3ed852e2009-09-05 21:47:34 +00003596
cristy3ed852e2009-09-05 21:47:34 +00003597 PolygonInfo
3598 **polygon_info;
3599
cristy826a5472010-08-31 23:21:38 +00003600 register ssize_t
3601 i;
3602
cristybb503372010-05-27 20:51:26 +00003603 size_t
cristy3ed852e2009-09-05 21:47:34 +00003604 number_threads;
3605
3606 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +00003607 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +00003608 sizeof(*polygon_info));
3609 if (polygon_info == (PolygonInfo **) NULL)
3610 return((PolygonInfo **) NULL);
3611 (void) ResetMagickMemory(polygon_info,0,GetOpenMPMaximumThreads()*
3612 sizeof(*polygon_info));
3613 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3614 if (path_info == (PathInfo *) NULL)
3615 return(DestroyPolygonThreadSet(polygon_info));
cristybb503372010-05-27 20:51:26 +00003616 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00003617 {
3618 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3619 if (polygon_info[i] == (PolygonInfo *) NULL)
3620 return(DestroyPolygonThreadSet(polygon_info));
3621 }
3622 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3623 return(polygon_info);
3624}
3625
cristy4c08aed2011-07-01 19:47:50 +00003626static MagickRealType GetPixelOpacity(PolygonInfo *polygon_info,
cristy3ed852e2009-09-05 21:47:34 +00003627 const MagickRealType mid,const MagickBooleanType fill,
cristy77f38fb2010-04-22 15:51:47 +00003628 const FillRule fill_rule,const double x,const double y,
cristy3ed852e2009-09-05 21:47:34 +00003629 MagickRealType *stroke_opacity)
3630{
cristy3ed852e2009-09-05 21:47:34 +00003631 MagickRealType
cristyb32b90a2009-09-07 21:45:48 +00003632 alpha,
3633 beta,
cristy3ed852e2009-09-05 21:47:34 +00003634 distance,
cristy3ed852e2009-09-05 21:47:34 +00003635 subpath_opacity;
3636
3637 PointInfo
cristyb32b90a2009-09-07 21:45:48 +00003638 delta;
cristy3ed852e2009-09-05 21:47:34 +00003639
3640 register EdgeInfo
3641 *p;
3642
cristyb32b90a2009-09-07 21:45:48 +00003643 register const PointInfo
3644 *q;
3645
cristybb503372010-05-27 20:51:26 +00003646 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003647 i;
3648
cristycee97112010-05-28 00:44:52 +00003649 ssize_t
3650 j,
3651 winding_number;
3652
cristy3ed852e2009-09-05 21:47:34 +00003653 /*
3654 Compute fill & stroke opacity for this (x,y) point.
3655 */
3656 *stroke_opacity=0.0;
3657 subpath_opacity=0.0;
cristy3ed852e2009-09-05 21:47:34 +00003658 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +00003659 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
cristy3ed852e2009-09-05 21:47:34 +00003660 {
cristyb32b90a2009-09-07 21:45:48 +00003661 if (y <= (p->bounds.y1-mid-0.5))
cristy3ed852e2009-09-05 21:47:34 +00003662 break;
cristyb32b90a2009-09-07 21:45:48 +00003663 if (y > (p->bounds.y2+mid+0.5))
cristy3ed852e2009-09-05 21:47:34 +00003664 {
cristybb503372010-05-27 20:51:26 +00003665 (void) DestroyEdge(polygon_info,(size_t) j);
cristy3ed852e2009-09-05 21:47:34 +00003666 continue;
3667 }
cristyb32b90a2009-09-07 21:45:48 +00003668 if ((x <= (p->bounds.x1-mid-0.5)) || (x > (p->bounds.x2+mid+0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003669 continue;
cristybb503372010-05-27 20:51:26 +00003670 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3671 for ( ; i < (ssize_t) p->number_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00003672 {
cristyb32b90a2009-09-07 21:45:48 +00003673 if (y <= (p->points[i-1].y-mid-0.5))
cristy3ed852e2009-09-05 21:47:34 +00003674 break;
cristyb32b90a2009-09-07 21:45:48 +00003675 if (y > (p->points[i].y+mid+0.5))
cristy3ed852e2009-09-05 21:47:34 +00003676 continue;
cristyb32b90a2009-09-07 21:45:48 +00003677 if (p->scanline != y)
cristy3ed852e2009-09-05 21:47:34 +00003678 {
cristyb32b90a2009-09-07 21:45:48 +00003679 p->scanline=y;
cristybb503372010-05-27 20:51:26 +00003680 p->highwater=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00003681 }
3682 /*
3683 Compute distance between a point and an edge.
3684 */
cristyb32b90a2009-09-07 21:45:48 +00003685 q=p->points+i-1;
3686 delta.x=(q+1)->x-q->x;
3687 delta.y=(q+1)->y-q->y;
3688 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
cristy3ed852e2009-09-05 21:47:34 +00003689 if (beta < 0.0)
3690 {
cristyb32b90a2009-09-07 21:45:48 +00003691 delta.x=x-q->x;
3692 delta.y=y-q->y;
cristy3ed852e2009-09-05 21:47:34 +00003693 distance=delta.x*delta.x+delta.y*delta.y;
3694 }
3695 else
3696 {
3697 alpha=delta.x*delta.x+delta.y*delta.y;
3698 if (beta > alpha)
3699 {
cristyb32b90a2009-09-07 21:45:48 +00003700 delta.x=x-(q+1)->x;
3701 delta.y=y-(q+1)->y;
cristy3ed852e2009-09-05 21:47:34 +00003702 distance=delta.x*delta.x+delta.y*delta.y;
3703 }
3704 else
3705 {
cristyb32b90a2009-09-07 21:45:48 +00003706 alpha=1.0/alpha;
3707 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3708 distance=alpha*beta*beta;
cristy3ed852e2009-09-05 21:47:34 +00003709 }
3710 }
3711 /*
3712 Compute stroke & subpath opacity.
3713 */
3714 beta=0.0;
3715 if (p->ghostline == MagickFalse)
3716 {
cristyb32b90a2009-09-07 21:45:48 +00003717 alpha=mid+0.5;
cristy3ed852e2009-09-05 21:47:34 +00003718 if ((*stroke_opacity < 1.0) &&
3719 (distance <= ((alpha+0.25)*(alpha+0.25))))
3720 {
3721 alpha=mid-0.5;
3722 if (distance <= ((alpha+0.25)*(alpha+0.25)))
3723 *stroke_opacity=1.0;
3724 else
3725 {
3726 beta=1.0;
3727 if (distance != 1.0)
3728 beta=sqrt((double) distance);
cristyb32b90a2009-09-07 21:45:48 +00003729 alpha=beta-mid-0.5;
cristy3ed852e2009-09-05 21:47:34 +00003730 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3731 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3732 }
3733 }
3734 }
3735 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3736 continue;
3737 if (distance <= 0.0)
3738 {
3739 subpath_opacity=1.0;
3740 continue;
3741 }
3742 if (distance > 1.0)
3743 continue;
3744 if (beta == 0.0)
3745 {
3746 beta=1.0;
3747 if (distance != 1.0)
cristyb32b90a2009-09-07 21:45:48 +00003748 beta=sqrt(distance);
cristy3ed852e2009-09-05 21:47:34 +00003749 }
3750 alpha=beta-1.0;
cristyb32b90a2009-09-07 21:45:48 +00003751 if (subpath_opacity < (alpha*alpha))
cristy3ed852e2009-09-05 21:47:34 +00003752 subpath_opacity=alpha*alpha;
3753 }
cristy3ed852e2009-09-05 21:47:34 +00003754 }
3755 /*
3756 Compute fill opacity.
3757 */
3758 if (fill == MagickFalse)
3759 return(0.0);
3760 if (subpath_opacity >= 1.0)
3761 return(1.0);
cristyb32b90a2009-09-07 21:45:48 +00003762 /*
3763 Determine winding number.
3764 */
3765 winding_number=0;
3766 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +00003767 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
cristyb32b90a2009-09-07 21:45:48 +00003768 {
3769 if (y <= p->bounds.y1)
3770 break;
3771 if ((y > p->bounds.y2) || (x <= p->bounds.x1))
3772 continue;
3773 if (x > p->bounds.x2)
3774 {
3775 winding_number+=p->direction ? 1 : -1;
3776 continue;
3777 }
cristybb503372010-05-27 20:51:26 +00003778 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3779 for ( ; i < (ssize_t) p->number_points; i++)
cristyb32b90a2009-09-07 21:45:48 +00003780 if (y <= p->points[i].y)
3781 break;
3782 q=p->points+i-1;
3783 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3784 winding_number+=p->direction ? 1 : -1;
3785 }
cristy3ed852e2009-09-05 21:47:34 +00003786 if (fill_rule != NonZeroRule)
3787 {
3788 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3789 return(1.0);
3790 }
3791 else
3792 if (MagickAbsoluteValue(winding_number) != 0)
3793 return(1.0);
3794 return(subpath_opacity);
3795}
3796
3797static MagickBooleanType DrawPolygonPrimitive(Image *image,
3798 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3799{
cristyfa112112010-01-04 17:48:07 +00003800 CacheView
3801 *image_view;
3802
cristy3ed852e2009-09-05 21:47:34 +00003803 ExceptionInfo
3804 *exception;
3805
cristy3ed852e2009-09-05 21:47:34 +00003806 MagickBooleanType
3807 fill,
3808 status;
3809
3810 MagickRealType
3811 mid;
3812
3813 PolygonInfo
cristyfa112112010-01-04 17:48:07 +00003814 **restrict polygon_info;
cristy3ed852e2009-09-05 21:47:34 +00003815
3816 register EdgeInfo
3817 *p;
3818
cristybb503372010-05-27 20:51:26 +00003819 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003820 i;
3821
3822 SegmentInfo
3823 bounds;
3824
cristy826a5472010-08-31 23:21:38 +00003825 ssize_t
3826 start,
3827 stop,
3828 y;
3829
cristy3ed852e2009-09-05 21:47:34 +00003830 /*
3831 Compute bounding box.
3832 */
3833 assert(image != (Image *) NULL);
3834 assert(image->signature == MagickSignature);
3835 if (image->debug != MagickFalse)
3836 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3837 assert(draw_info != (DrawInfo *) NULL);
3838 assert(draw_info->signature == MagickSignature);
3839 assert(primitive_info != (PrimitiveInfo *) NULL);
3840 if (primitive_info->coordinates == 0)
3841 return(MagickTrue);
3842 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3843 if (polygon_info == (PolygonInfo **) NULL)
3844 return(MagickFalse);
3845 if (0)
3846 DrawBoundingRectangles(image,draw_info,polygon_info[0]);
3847 if (image->debug != MagickFalse)
3848 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3849 fill=(primitive_info->method == FillToBorderMethod) ||
3850 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3851 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3852 bounds=polygon_info[0]->edges[0].bounds;
cristybb503372010-05-27 20:51:26 +00003853 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00003854 {
3855 p=polygon_info[0]->edges+i;
3856 if (p->bounds.x1 < bounds.x1)
3857 bounds.x1=p->bounds.x1;
3858 if (p->bounds.y1 < bounds.y1)
3859 bounds.y1=p->bounds.y1;
3860 if (p->bounds.x2 > bounds.x2)
3861 bounds.x2=p->bounds.x2;
3862 if (p->bounds.y2 > bounds.y2)
3863 bounds.y2=p->bounds.y2;
3864 }
3865 bounds.x1-=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003866 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003867 image->columns ? (double) image->columns-1.0 : bounds.x1;
3868 bounds.y1-=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003869 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003870 image->rows ? (double) image->rows-1.0 : bounds.y1;
3871 bounds.x2+=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003872 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003873 image->columns ? (double) image->columns-1.0 : bounds.x2;
3874 bounds.y2+=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003875 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
cristy3ed852e2009-09-05 21:47:34 +00003876 image->rows ? (double) image->rows-1.0 : bounds.y2;
3877 status=MagickTrue;
3878 exception=(&image->exception);
cristybb503372010-05-27 20:51:26 +00003879 start=(ssize_t) ceil(bounds.x1-0.5);
3880 stop=(ssize_t) floor(bounds.x2+0.5);
cristy3ed852e2009-09-05 21:47:34 +00003881 image_view=AcquireCacheView(image);
3882 if (primitive_info->coordinates == 1)
3883 {
3884 /*
3885 Draw point.
3886 */
cristyb5d5f722009-11-04 03:03:49 +00003887#if defined(MAGICKCORE_OPENMP_SUPPORT)
3888 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003889#endif
cristybb503372010-05-27 20:51:26 +00003890 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
cristy3ed852e2009-09-05 21:47:34 +00003891 {
3892 MagickBooleanType
3893 sync;
3894
cristy4c08aed2011-07-01 19:47:50 +00003895 PixelPacket
3896 pixel;
3897
cristybb503372010-05-27 20:51:26 +00003898 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003899 x;
3900
cristy4c08aed2011-07-01 19:47:50 +00003901 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003902 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003903
3904 if (status == MagickFalse)
3905 continue;
3906 x=start;
cristybb503372010-05-27 20:51:26 +00003907 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),
cristy3ed852e2009-09-05 21:47:34 +00003908 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003909 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003910 {
3911 status=MagickFalse;
3912 continue;
3913 }
3914 for ( ; x <= stop; x++)
3915 {
cristybb503372010-05-27 20:51:26 +00003916 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3917 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
cristy4c08aed2011-07-01 19:47:50 +00003918 {
3919 (void) GetStrokeColor(draw_info,x,y,&pixel);
3920 SetPixelPacket(image,&pixel,q);
3921 }
cristyed231572011-07-14 02:18:59 +00003922 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003923 }
3924 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3925 if (sync == MagickFalse)
3926 status=MagickFalse;
3927 }
3928 image_view=DestroyCacheView(image_view);
3929 polygon_info=DestroyPolygonThreadSet(polygon_info);
3930 if (image->debug != MagickFalse)
3931 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3932 " end draw-polygon");
3933 return(status);
3934 }
3935 /*
3936 Draw polygon or line.
3937 */
3938 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00003939 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristyb5d5f722009-11-04 03:03:49 +00003940#if defined(MAGICKCORE_OPENMP_SUPPORT)
3941 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003942#endif
cristybb503372010-05-27 20:51:26 +00003943 for (y=(ssize_t) ceil(bounds.y1-0.5); y <= (ssize_t) floor(bounds.y2+0.5); y++)
cristy3ed852e2009-09-05 21:47:34 +00003944 {
cristy5c9e6f22010-09-17 17:31:01 +00003945 const int
3946 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003947
cristy3ed852e2009-09-05 21:47:34 +00003948 MagickRealType
3949 fill_opacity,
3950 stroke_opacity;
3951
3952 PixelPacket
3953 fill_color,
3954 stroke_color;
3955
cristy4c08aed2011-07-01 19:47:50 +00003956 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003957 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003958
cristy826a5472010-08-31 23:21:38 +00003959 register ssize_t
3960 x;
3961
cristy3ed852e2009-09-05 21:47:34 +00003962 if (status == MagickFalse)
3963 continue;
cristybb503372010-05-27 20:51:26 +00003964 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-
cristy3ed852e2009-09-05 21:47:34 +00003965 start+1),1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003966 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003967 {
3968 status=MagickFalse;
3969 continue;
3970 }
cristy3ed852e2009-09-05 21:47:34 +00003971 for (x=start; x <= stop; x++)
3972 {
3973 /*
3974 Fill and/or stroke.
3975 */
cristy4c08aed2011-07-01 19:47:50 +00003976 fill_opacity=GetPixelOpacity(polygon_info[id],mid,fill,
cristy77f38fb2010-04-22 15:51:47 +00003977 draw_info->fill_rule,(double) x,(double) y,&stroke_opacity);
cristy3ed852e2009-09-05 21:47:34 +00003978 if (draw_info->stroke_antialias == MagickFalse)
3979 {
3980 fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
3981 stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
3982 }
3983 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +00003984 fill_opacity=fill_opacity*fill_color.alpha;
3985 CompositePixelOver(image,&fill_color,fill_opacity,q,(MagickRealType)
3986 GetPixelAlpha(image,q),q);
cristy3ed852e2009-09-05 21:47:34 +00003987 (void) GetStrokeColor(draw_info,x,y,&stroke_color);
cristy4c08aed2011-07-01 19:47:50 +00003988 stroke_opacity=stroke_opacity*stroke_color.alpha;
3989 CompositePixelOver(image,&stroke_color,stroke_opacity,q,(MagickRealType)
3990 GetPixelAlpha(image,q),q);
cristyed231572011-07-14 02:18:59 +00003991 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003992 }
3993 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3994 status=MagickFalse;
3995 }
3996 image_view=DestroyCacheView(image_view);
3997 polygon_info=DestroyPolygonThreadSet(polygon_info);
3998 if (image->debug != MagickFalse)
3999 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4000 return(status);
4001}
4002
4003/*
4004%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4005% %
4006% %
4007% %
4008% D r a w P r i m i t i v e %
4009% %
4010% %
4011% %
4012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4013%
4014% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4015%
4016% The format of the DrawPrimitive method is:
4017%
4018% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4019% PrimitiveInfo *primitive_info)
4020%
4021% A description of each parameter follows:
4022%
4023% o image: the image.
4024%
4025% o draw_info: the draw info.
4026%
4027% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4028%
4029*/
4030
4031static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4032{
4033 const char
4034 *methods[] =
4035 {
4036 "point",
4037 "replace",
4038 "floodfill",
4039 "filltoborder",
4040 "reset",
4041 "?"
4042 };
4043
cristy3ed852e2009-09-05 21:47:34 +00004044 PointInfo
4045 p,
4046 q,
4047 point;
4048
cristybb503372010-05-27 20:51:26 +00004049 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004050 i,
4051 x;
4052
cristy826a5472010-08-31 23:21:38 +00004053 ssize_t
4054 coordinates,
4055 y;
4056
cristybb503372010-05-27 20:51:26 +00004057 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4058 y=(ssize_t) ceil(primitive_info->point.y-0.5);
cristy3ed852e2009-09-05 21:47:34 +00004059 switch (primitive_info->primitive)
4060 {
4061 case PointPrimitive:
4062 {
4063 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004064 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004065 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004066 return;
4067 }
4068 case ColorPrimitive:
4069 {
4070 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004071 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004072 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004073 return;
4074 }
4075 case MattePrimitive:
4076 {
4077 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004078 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004079 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004080 return;
4081 }
4082 case TextPrimitive:
4083 {
4084 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004085 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00004086 return;
4087 }
4088 case ImagePrimitive:
4089 {
4090 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004091 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00004092 return;
4093 }
4094 default:
4095 break;
4096 }
4097 coordinates=0;
4098 p=primitive_info[0].point;
4099 q.x=(-1.0);
4100 q.y=(-1.0);
4101 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4102 {
4103 point=primitive_info[i].point;
4104 if (coordinates <= 0)
4105 {
cristybb503372010-05-27 20:51:26 +00004106 coordinates=(ssize_t) primitive_info[i].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00004107 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004108 " begin open (%.20g)",(double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004109 p=point;
4110 }
4111 point=primitive_info[i].point;
4112 if ((fabs(q.x-point.x) > MagickEpsilon) ||
4113 (fabs(q.y-point.y) > MagickEpsilon))
cristy8cd5b312010-01-07 01:10:24 +00004114 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004115 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
cristy3ed852e2009-09-05 21:47:34 +00004116 else
4117 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +00004118 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
cristy3ed852e2009-09-05 21:47:34 +00004119 q=point;
4120 coordinates--;
4121 if (coordinates > 0)
4122 continue;
4123 if ((fabs(p.x-point.x) > MagickEpsilon) ||
4124 (fabs(p.y-point.y) > MagickEpsilon))
cristye8c25f92010-06-03 00:53:06 +00004125 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4126 (double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004127 else
cristye8c25f92010-06-03 00:53:06 +00004128 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4129 (double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004130 }
4131}
4132
4133MagickExport MagickBooleanType DrawPrimitive(Image *image,
4134 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4135{
cristyc4c8d132010-01-07 01:58:38 +00004136 CacheView
4137 *image_view;
4138
cristy3ed852e2009-09-05 21:47:34 +00004139 ExceptionInfo
4140 *exception;
4141
cristy3ed852e2009-09-05 21:47:34 +00004142 MagickStatusType
4143 status;
4144
cristybb503372010-05-27 20:51:26 +00004145 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004146 i,
4147 x;
4148
cristy826a5472010-08-31 23:21:38 +00004149 ssize_t
4150 y;
4151
cristy3ed852e2009-09-05 21:47:34 +00004152 if (image->debug != MagickFalse)
4153 {
4154 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4155 " begin draw-primitive");
4156 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +00004157 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
cristy3ed852e2009-09-05 21:47:34 +00004158 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4159 draw_info->affine.tx,draw_info->affine.ty);
4160 }
4161 status=MagickTrue;
4162 exception=(&image->exception);
cristybb503372010-05-27 20:51:26 +00004163 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4164 y=(ssize_t) ceil(primitive_info->point.y-0.5);
cristy3ed852e2009-09-05 21:47:34 +00004165 image_view=AcquireCacheView(image);
4166 switch (primitive_info->primitive)
4167 {
4168 case PointPrimitive:
4169 {
4170 PixelPacket
4171 fill_color;
4172
cristy4c08aed2011-07-01 19:47:50 +00004173 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004174 *q;
4175
cristybb503372010-05-27 20:51:26 +00004176 if ((y < 0) || (y >= (ssize_t) image->rows))
cristyb32b90a2009-09-07 21:45:48 +00004177 break;
cristybb503372010-05-27 20:51:26 +00004178 if ((x < 0) || (x >= (ssize_t) image->columns))
cristyb32b90a2009-09-07 21:45:48 +00004179 break;
cristy3ed852e2009-09-05 21:47:34 +00004180 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004181 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004182 break;
4183 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +00004184 CompositePixelOver(image,&fill_color,(MagickRealType) fill_color.alpha,q,
4185 (MagickRealType) GetPixelAlpha(image,q),q);
cristy3ed852e2009-09-05 21:47:34 +00004186 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4187 break;
4188 }
4189 case ColorPrimitive:
4190 {
4191 switch (primitive_info->method)
4192 {
4193 case PointMethod:
4194 default:
4195 {
4196 PixelPacket
cristy4c08aed2011-07-01 19:47:50 +00004197 pixel;
4198
4199 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004200 *q;
4201
4202 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004203 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004204 break;
cristy4c08aed2011-07-01 19:47:50 +00004205 (void) GetFillColor(draw_info,x,y,&pixel);
4206 SetPixelPacket(image,&pixel,q);
cristy3ed852e2009-09-05 21:47:34 +00004207 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4208 break;
4209 }
4210 case ReplaceMethod:
4211 {
4212 MagickBooleanType
4213 sync;
4214
4215 PixelPacket
cristy4c08aed2011-07-01 19:47:50 +00004216 pixel,
cristy3ed852e2009-09-05 21:47:34 +00004217 target;
4218
4219 (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
cristybb503372010-05-27 20:51:26 +00004220 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004221 {
cristy4c08aed2011-07-01 19:47:50 +00004222 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004223 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004224
4225 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4226 exception);
cristy4c08aed2011-07-01 19:47:50 +00004227 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004228 break;
cristybb503372010-05-27 20:51:26 +00004229 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004230 {
cristy4c08aed2011-07-01 19:47:50 +00004231 GetPixelPacket(image,q,&pixel);
4232 if (IsFuzzyEquivalencePixelPacket(image,&pixel,&target) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004233 {
cristyed231572011-07-14 02:18:59 +00004234 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004235 continue;
4236 }
cristy4c08aed2011-07-01 19:47:50 +00004237 (void) GetFillColor(draw_info,x,y,&pixel);
cristy0e582272011-07-02 15:53:37 +00004238 SetPixelPacket(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00004239 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004240 }
4241 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4242 if (sync == MagickFalse)
4243 break;
4244 }
4245 break;
4246 }
4247 case FloodfillMethod:
4248 case FillToBorderMethod:
4249 {
cristy4c08aed2011-07-01 19:47:50 +00004250 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004251 target;
4252
4253 (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4254 if (primitive_info->method == FillToBorderMethod)
4255 {
4256 target.red=(MagickRealType) draw_info->border_color.red;
4257 target.green=(MagickRealType) draw_info->border_color.green;
4258 target.blue=(MagickRealType) draw_info->border_color.blue;
4259 }
cristyd42d9952011-07-08 14:21:50 +00004260 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
4261 primitive_info->method == FloodfillMethod ? MagickFalse :
cristy189e84c2011-08-27 18:08:53 +00004262 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004263 break;
4264 }
4265 case ResetMethod:
4266 {
4267 MagickBooleanType
4268 sync;
4269
cristy4c08aed2011-07-01 19:47:50 +00004270 PixelPacket
4271 pixel;
4272
cristybb503372010-05-27 20:51:26 +00004273 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004274 {
cristy4c08aed2011-07-01 19:47:50 +00004275 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004276 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004277
cristy826a5472010-08-31 23:21:38 +00004278 register ssize_t
4279 x;
4280
cristy3ed852e2009-09-05 21:47:34 +00004281 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4282 exception);
cristy4c08aed2011-07-01 19:47:50 +00004283 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004284 break;
cristybb503372010-05-27 20:51:26 +00004285 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004286 {
cristy4c08aed2011-07-01 19:47:50 +00004287 (void) GetFillColor(draw_info,x,y,&pixel);
4288 SetPixelPacket(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00004289 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004290 }
4291 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4292 if (sync == MagickFalse)
4293 break;
4294 }
4295 break;
4296 }
4297 }
4298 break;
4299 }
4300 case MattePrimitive:
4301 {
4302 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004303 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004304 switch (primitive_info->method)
4305 {
4306 case PointMethod:
4307 default:
4308 {
4309 PixelPacket
4310 pixel;
4311
cristy4c08aed2011-07-01 19:47:50 +00004312 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004313 *q;
4314
4315 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004316 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004317 break;
4318 (void) GetFillColor(draw_info,x,y,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00004319 SetPixelAlpha(image,pixel.alpha,q);
cristy3ed852e2009-09-05 21:47:34 +00004320 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4321 break;
4322 }
4323 case ReplaceMethod:
4324 {
4325 MagickBooleanType
4326 sync;
4327
4328 PixelPacket
4329 pixel,
4330 target;
4331
4332 (void) GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
cristybb503372010-05-27 20:51:26 +00004333 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004334 {
cristy4c08aed2011-07-01 19:47:50 +00004335 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004336 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004337
cristy826a5472010-08-31 23:21:38 +00004338 register ssize_t
4339 x;
4340
cristy3ed852e2009-09-05 21:47:34 +00004341 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4342 exception);
cristy4c08aed2011-07-01 19:47:50 +00004343 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004344 break;
cristybb503372010-05-27 20:51:26 +00004345 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004346 {
cristy4c08aed2011-07-01 19:47:50 +00004347 GetPixelPacket(image,q,&pixel);
4348 if (IsFuzzyEquivalencePixelPacket(image,&pixel,&target) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004349 {
cristyed231572011-07-14 02:18:59 +00004350 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004351 continue;
4352 }
4353 (void) GetFillColor(draw_info,x,y,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00004354 SetPixelAlpha(image,pixel.alpha,q);
cristyed231572011-07-14 02:18:59 +00004355 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004356 }
4357 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4358 if (sync == MagickFalse)
4359 break;
4360 }
4361 break;
4362 }
4363 case FloodfillMethod:
4364 case FillToBorderMethod:
4365 {
cristybd5a96c2011-08-21 00:04:26 +00004366 ChannelType
4367 channel_mask;
4368
cristy4c08aed2011-07-01 19:47:50 +00004369 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004370 target;
4371
4372 (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4373 if (primitive_info->method == FillToBorderMethod)
4374 {
4375 target.red=(MagickRealType) draw_info->border_color.red;
4376 target.green=(MagickRealType) draw_info->border_color.green;
4377 target.blue=(MagickRealType) draw_info->border_color.blue;
4378 }
cristybd5a96c2011-08-21 00:04:26 +00004379 channel_mask=SetPixelChannelMask(image,AlphaChannel);
cristyd42d9952011-07-08 14:21:50 +00004380 (void) FloodfillPaintImage(image,draw_info,&target,x,y,
cristy3ed852e2009-09-05 21:47:34 +00004381 primitive_info->method == FloodfillMethod ? MagickFalse :
cristy189e84c2011-08-27 18:08:53 +00004382 MagickTrue,exception);
cristybd5a96c2011-08-21 00:04:26 +00004383 (void) SetPixelChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004384 break;
4385 }
4386 case ResetMethod:
4387 {
4388 MagickBooleanType
4389 sync;
4390
4391 PixelPacket
4392 pixel;
4393
cristybb503372010-05-27 20:51:26 +00004394 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004395 {
cristy4c08aed2011-07-01 19:47:50 +00004396 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004397 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004398
cristy826a5472010-08-31 23:21:38 +00004399 register ssize_t
4400 x;
4401
cristy3ed852e2009-09-05 21:47:34 +00004402 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4403 exception);
cristy4c08aed2011-07-01 19:47:50 +00004404 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004405 break;
cristybb503372010-05-27 20:51:26 +00004406 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004407 {
4408 (void) GetFillColor(draw_info,x,y,&pixel);
cristy4c08aed2011-07-01 19:47:50 +00004409 SetPixelAlpha(image,pixel.alpha,q);
cristyed231572011-07-14 02:18:59 +00004410 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004411 }
4412 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4413 if (sync == MagickFalse)
4414 break;
4415 }
4416 break;
4417 }
4418 }
4419 break;
4420 }
4421 case TextPrimitive:
4422 {
4423 char
4424 geometry[MaxTextExtent];
4425
4426 DrawInfo
4427 *clone_info;
4428
4429 if (primitive_info->text == (char *) NULL)
4430 break;
4431 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4432 (void) CloneString(&clone_info->text,primitive_info->text);
cristyb51dff52011-05-19 16:55:47 +00004433 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
cristy3ed852e2009-09-05 21:47:34 +00004434 primitive_info->point.x,primitive_info->point.y);
4435 (void) CloneString(&clone_info->geometry,geometry);
cristy5cbc0162011-08-29 00:36:28 +00004436 status=AnnotateImage(image,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004437 clone_info=DestroyDrawInfo(clone_info);
4438 break;
4439 }
4440 case ImagePrimitive:
4441 {
4442 AffineMatrix
4443 affine;
4444
4445 char
4446 composite_geometry[MaxTextExtent];
4447
4448 Image
4449 *composite_image;
4450
4451 ImageInfo
4452 *clone_info;
4453
cristy826a5472010-08-31 23:21:38 +00004454 RectangleInfo
4455 geometry;
4456
cristybb503372010-05-27 20:51:26 +00004457 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004458 x1,
4459 y1;
4460
cristy3ed852e2009-09-05 21:47:34 +00004461 if (primitive_info->text == (char *) NULL)
4462 break;
4463 clone_info=AcquireImageInfo();
4464 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4465 composite_image=ReadInlineImage(clone_info,primitive_info->text,
4466 &image->exception);
4467 else
4468 {
4469 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4470 MaxTextExtent);
4471 composite_image=ReadImage(clone_info,&image->exception);
4472 }
4473 clone_info=DestroyImageInfo(clone_info);
4474 if (composite_image == (Image *) NULL)
4475 break;
4476 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4477 NULL,(void *) NULL);
cristybb503372010-05-27 20:51:26 +00004478 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4479 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4480 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4481 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
cristy3ed852e2009-09-05 21:47:34 +00004482 {
4483 char
4484 geometry[MaxTextExtent];
4485
4486 /*
4487 Resize image.
4488 */
cristyb51dff52011-05-19 16:55:47 +00004489 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
cristy3ed852e2009-09-05 21:47:34 +00004490 primitive_info[1].point.x,primitive_info[1].point.y);
4491 composite_image->filter=image->filter;
4492 (void) TransformImage(&composite_image,(char *) NULL,geometry);
4493 }
4494 if (composite_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00004495 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4496 exception);
cristy4c08aed2011-07-01 19:47:50 +00004497 if (draw_info->alpha != OpaqueAlpha)
4498 (void) SetImageOpacity(composite_image,draw_info->alpha);
cristy3ed852e2009-09-05 21:47:34 +00004499 SetGeometry(image,&geometry);
4500 image->gravity=draw_info->gravity;
4501 geometry.x=x;
4502 geometry.y=y;
cristyb51dff52011-05-19 16:55:47 +00004503 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00004504 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
cristye8c25f92010-06-03 00:53:06 +00004505 composite_image->rows,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00004506 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
4507 &image->exception);
4508 affine=draw_info->affine;
4509 affine.tx=(double) geometry.x;
4510 affine.ty=(double) geometry.y;
4511 composite_image->interpolate=image->interpolate;
cristyd5f3fc32011-04-26 14:44:36 +00004512 if (draw_info->compose == OverCompositeOp)
4513 (void) DrawAffineImage(image,composite_image,&affine);
4514 else
4515 (void) CompositeImage(image,draw_info->compose,composite_image,
4516 geometry.x,geometry.y);
cristy3ed852e2009-09-05 21:47:34 +00004517 composite_image=DestroyImage(composite_image);
4518 break;
4519 }
4520 default:
4521 {
4522 MagickRealType
4523 mid,
4524 scale;
4525
4526 DrawInfo
4527 *clone_info;
4528
4529 if (IsEventLogging() != MagickFalse)
4530 LogPrimitiveInfo(primitive_info);
4531 scale=ExpandAffine(&draw_info->affine);
4532 if ((draw_info->dash_pattern != (double *) NULL) &&
4533 (draw_info->dash_pattern[0] != 0.0) &&
4534 ((scale*draw_info->stroke_width) > MagickEpsilon) &&
cristy4c08aed2011-07-01 19:47:50 +00004535 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00004536 {
4537 /*
4538 Draw dash polygon.
4539 */
4540 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4541 clone_info->stroke_width=0.0;
cristy4c08aed2011-07-01 19:47:50 +00004542 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004543 status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4544 clone_info=DestroyDrawInfo(clone_info);
4545 (void) DrawDashPolygon(draw_info,primitive_info,image);
4546 break;
4547 }
4548 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4549 if ((mid > 1.0) &&
cristy4c08aed2011-07-01 19:47:50 +00004550 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00004551 {
4552 MagickBooleanType
4553 closed_path;
4554
4555 /*
4556 Draw strokes while respecting line cap/join attributes.
4557 */
4558 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4559 closed_path=
4560 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4561 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4562 MagickTrue : MagickFalse;
cristybb503372010-05-27 20:51:26 +00004563 i=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00004564 if ((((draw_info->linecap == RoundCap) ||
4565 (closed_path != MagickFalse)) &&
4566 (draw_info->linejoin == RoundJoin)) ||
4567 (primitive_info[i].primitive != UndefinedPrimitive))
4568 {
4569 (void) DrawPolygonPrimitive(image,draw_info,primitive_info);
4570 break;
4571 }
4572 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4573 clone_info->stroke_width=0.0;
cristy4c08aed2011-07-01 19:47:50 +00004574 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004575 status=DrawPolygonPrimitive(image,clone_info,primitive_info);
4576 clone_info=DestroyDrawInfo(clone_info);
4577 status|=DrawStrokePolygon(image,draw_info,primitive_info);
4578 break;
4579 }
4580 status=DrawPolygonPrimitive(image,draw_info,primitive_info);
4581 break;
4582 }
4583 }
4584 image_view=DestroyCacheView(image_view);
4585 if (image->debug != MagickFalse)
4586 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4587 return(status != 0 ? MagickTrue : MagickFalse);
4588}
4589
4590/*
4591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4592% %
4593% %
4594% %
4595+ D r a w S t r o k e P o l y g o n %
4596% %
4597% %
4598% %
4599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4600%
4601% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4602% the image while respecting the line cap and join attributes.
4603%
4604% The format of the DrawStrokePolygon method is:
4605%
4606% MagickBooleanType DrawStrokePolygon(Image *image,
4607% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4608%
4609% A description of each parameter follows:
4610%
4611% o image: the image.
4612%
4613% o draw_info: the draw info.
4614%
4615% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4616%
4617%
4618*/
4619
4620static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4621 const PrimitiveInfo *primitive_info)
4622{
4623 PrimitiveInfo
4624 linecap[5];
4625
cristybb503372010-05-27 20:51:26 +00004626 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004627 i;
4628
4629 for (i=0; i < 4; i++)
4630 linecap[i]=(*primitive_info);
4631 linecap[0].coordinates=4;
4632 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4633 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4634 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4635 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4636 linecap[4].primitive=UndefinedPrimitive;
4637 (void) DrawPolygonPrimitive(image,draw_info,linecap);
4638}
4639
4640static MagickBooleanType DrawStrokePolygon(Image *image,
4641 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4642{
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);
4672 status=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
4673 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 {
4679 DrawRoundLinecap(image,draw_info,p);
4680 DrawRoundLinecap(image,draw_info,q);
4681 }
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
4754 ImageInfo
4755 *clone_info;
4756
4757 /*
4758 Initialize draw attributes.
4759 */
4760 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4761 assert(draw_info != (DrawInfo *) NULL);
4762 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4763 clone_info=CloneImageInfo(image_info);
4764 GetAffineMatrix(&draw_info->affine);
4765 exception=AcquireExceptionInfo();
cristy9950d572011-10-01 18:22:35 +00004766 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4767 exception);
4768 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
4769 exception);
cristy3ed852e2009-09-05 21:47:34 +00004770 draw_info->stroke_antialias=clone_info->antialias;
4771 draw_info->stroke_width=1.0;
cristy4c08aed2011-07-01 19:47:50 +00004772 draw_info->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004773 draw_info->fill_rule=EvenOddRule;
4774 draw_info->linecap=ButtCap;
4775 draw_info->linejoin=MiterJoin;
4776 draw_info->miterlimit=10;
4777 draw_info->decorate=NoDecoration;
4778 if (clone_info->font != (char *) NULL)
4779 draw_info->font=AcquireString(clone_info->font);
4780 if (clone_info->density != (char *) NULL)
4781 draw_info->density=AcquireString(clone_info->density);
4782 draw_info->text_antialias=clone_info->antialias;
4783 draw_info->pointsize=12.0;
4784 if (clone_info->pointsize != 0.0)
4785 draw_info->pointsize=clone_info->pointsize;
cristy4c08aed2011-07-01 19:47:50 +00004786 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004787 draw_info->border_color=clone_info->border_color;
4788 draw_info->compose=OverCompositeOp;
4789 if (clone_info->server_name != (char *) NULL)
4790 draw_info->server_name=AcquireString(clone_info->server_name);
4791 draw_info->render=MagickTrue;
4792 draw_info->debug=IsEventLogging();
4793 option=GetImageOption(clone_info,"encoding");
4794 if (option != (const char *) NULL)
4795 (void) CloneString(&draw_info->encoding,option);
4796 option=GetImageOption(clone_info,"kerning");
4797 if (option != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00004798 draw_info->kerning=InterpretLocaleValue(option,(char **) NULL);
cristyb32b90a2009-09-07 21:45:48 +00004799 option=GetImageOption(clone_info,"interline-spacing");
4800 if (option != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00004801 draw_info->interline_spacing=InterpretLocaleValue(option,(char **) NULL);
cristy6ac8b332010-04-22 02:24:11 +00004802 draw_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +00004803 option=GetImageOption(clone_info,"interword-spacing");
4804 if (option != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00004805 draw_info->interword_spacing=InterpretLocaleValue(option,(char **) NULL);
cristyc9b12952010-03-28 01:12:28 +00004806 option=GetImageOption(clone_info,"direction");
4807 if (option != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +00004808 draw_info->direction=(DirectionType) ParseCommandOption(
cristyc9b12952010-03-28 01:12:28 +00004809 MagickDirectionOptions,MagickFalse,option);
cristy3ed852e2009-09-05 21:47:34 +00004810 option=GetImageOption(clone_info,"fill");
4811 if (option != (const char *) NULL)
cristy9950d572011-10-01 18:22:35 +00004812 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4813 exception);
cristy3ed852e2009-09-05 21:47:34 +00004814 option=GetImageOption(clone_info,"stroke");
4815 if (option != (const char *) NULL)
cristy9950d572011-10-01 18:22:35 +00004816 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4817 exception);
cristy3ed852e2009-09-05 21:47:34 +00004818 option=GetImageOption(clone_info,"strokewidth");
4819 if (option != (const char *) NULL)
cristyc1acd842011-05-19 23:05:47 +00004820 draw_info->stroke_width=InterpretLocaleValue(option,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00004821 option=GetImageOption(clone_info,"undercolor");
4822 if (option != (const char *) NULL)
cristy9950d572011-10-01 18:22:35 +00004823 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4824 exception);
cristy3ed852e2009-09-05 21:47:34 +00004825 option=GetImageOption(clone_info,"gravity");
4826 if (option != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +00004827 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
cristy3ed852e2009-09-05 21:47:34 +00004828 MagickFalse,option);
4829 exception=DestroyExceptionInfo(exception);
4830 draw_info->signature=MagickSignature;
4831 clone_info=DestroyImageInfo(clone_info);
4832}
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}