blob: 7b978ebf723197c72267b95620e915d10b1ffcc1 [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 %
cristyde984cd2013-12-01 14:49:27 +000017% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000018% July 1998 %
19% %
20% %
cristyfe676ee2013-11-18 13:03:38 +000021% Copyright 1999-2014 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"
cristy6a2180c2013-05-27 10:28:36 +000054#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/color.h"
cristy9b7a4fc2012-04-08 22:26:56 +000056#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000057#include "MagickCore/composite.h"
58#include "MagickCore/composite-private.h"
59#include "MagickCore/constitute.h"
60#include "MagickCore/draw.h"
61#include "MagickCore/draw-private.h"
62#include "MagickCore/enhance.h"
63#include "MagickCore/exception.h"
64#include "MagickCore/exception-private.h"
65#include "MagickCore/gem.h"
66#include "MagickCore/geometry.h"
67#include "MagickCore/image-private.h"
68#include "MagickCore/list.h"
69#include "MagickCore/log.h"
70#include "MagickCore/monitor.h"
71#include "MagickCore/monitor-private.h"
72#include "MagickCore/option.h"
73#include "MagickCore/paint.h"
74#include "MagickCore/pixel-accessor.h"
cristy35f15302012-06-07 14:59:02 +000075#include "MagickCore/pixel-private.h"
cristy4c08aed2011-07-01 19:47:50 +000076#include "MagickCore/property.h"
77#include "MagickCore/resample.h"
78#include "MagickCore/resample-private.h"
cristyac245f82012-05-05 17:13:57 +000079#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000080#include "MagickCore/string_.h"
81#include "MagickCore/string-private.h"
82#include "MagickCore/thread-private.h"
83#include "MagickCore/token.h"
84#include "MagickCore/transform.h"
85#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000086
87/*
88 Define declarations.
89*/
90#define BezierQuantum 200
91
92/*
93 Typedef declarations.
94*/
95typedef struct _EdgeInfo
96{
97 SegmentInfo
98 bounds;
99
cristya19f1d72012-08-07 18:24:38 +0000100 double
cristy3ed852e2009-09-05 21:47:34 +0000101 scanline;
102
103 PointInfo
104 *points;
105
cristybb503372010-05-27 20:51:26 +0000106 size_t
cristy3ed852e2009-09-05 21:47:34 +0000107 number_points;
108
cristybb503372010-05-27 20:51:26 +0000109 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000110 direction;
111
112 MagickBooleanType
113 ghostline;
114
cristybb503372010-05-27 20:51:26 +0000115 size_t
cristy3ed852e2009-09-05 21:47:34 +0000116 highwater;
117} EdgeInfo;
118
119typedef struct _ElementInfo
120{
cristya19f1d72012-08-07 18:24:38 +0000121 double
cristy3ed852e2009-09-05 21:47:34 +0000122 cx,
123 cy,
124 major,
125 minor,
126 angle;
127} ElementInfo;
128
129typedef struct _PolygonInfo
130{
131 EdgeInfo
132 *edges;
133
cristybb503372010-05-27 20:51:26 +0000134 size_t
cristy3ed852e2009-09-05 21:47:34 +0000135 number_edges;
136} PolygonInfo;
137
138typedef enum
139{
140 MoveToCode,
141 OpenCode,
142 GhostlineCode,
143 LineToCode,
144 EndCode
145} PathInfoCode;
146
147typedef struct _PathInfo
148{
149 PointInfo
150 point;
151
152 PathInfoCode
153 code;
154} PathInfo;
155
156/*
157 Forward declarations.
158*/
159static MagickBooleanType
cristy947cb4c2011-10-20 18:41:46 +0000160 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
161 ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000162
163static PrimitiveInfo
164 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
165
cristybb503372010-05-27 20:51:26 +0000166static size_t
cristy3ed852e2009-09-05 21:47:34 +0000167 TracePath(PrimitiveInfo *,const char *);
168
169static void
170 TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
171 TraceArcPath(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo,
cristya19f1d72012-08-07 18:24:38 +0000172 const double,const MagickBooleanType,const MagickBooleanType),
cristybb503372010-05-27 20:51:26 +0000173 TraceBezier(PrimitiveInfo *,const size_t),
cristy3ed852e2009-09-05 21:47:34 +0000174 TraceCircle(PrimitiveInfo *,const PointInfo,const PointInfo),
175 TraceEllipse(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
176 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
177 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
178 TraceRoundRectangle(PrimitiveInfo *,const PointInfo,const PointInfo,
179 PointInfo),
cristya19f1d72012-08-07 18:24:38 +0000180 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
cristy3ed852e2009-09-05 21:47:34 +0000181
182/*
183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184% %
185% %
186% %
187% A c q u i r e D r a w I n f o %
188% %
189% %
190% %
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192%
193% AcquireDrawInfo() returns a DrawInfo structure properly initialized.
194%
195% The format of the AcquireDrawInfo method is:
196%
197% DrawInfo *AcquireDrawInfo(void)
198%
199*/
200MagickExport DrawInfo *AcquireDrawInfo(void)
201{
202 DrawInfo
203 *draw_info;
204
cristy73bd4a52010-10-05 11:24:23 +0000205 draw_info=(DrawInfo *) AcquireMagickMemory(sizeof(*draw_info));
cristy3ed852e2009-09-05 21:47:34 +0000206 if (draw_info == (DrawInfo *) NULL)
207 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
208 GetDrawInfo((ImageInfo *) NULL,draw_info);
209 return(draw_info);
210}
211
212/*
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214% %
215% %
216% %
217% C l o n e D r a w I n f o %
218% %
219% %
220% %
221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222%
anthony36fe1752011-09-29 11:28:37 +0000223% CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
cristy430522c2012-09-03 00:07:16 +0000224% is specified, a new DrawInfo structure is created initialized to default
225% values.
cristy3ed852e2009-09-05 21:47:34 +0000226%
227% The format of the CloneDrawInfo method is:
228%
229% DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
230% const DrawInfo *draw_info)
231%
232% A description of each parameter follows:
233%
234% o image_info: the image info.
235%
236% o draw_info: the draw info.
237%
238*/
239MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
240 const DrawInfo *draw_info)
241{
242 DrawInfo
243 *clone_info;
244
cristy947cb4c2011-10-20 18:41:46 +0000245 ExceptionInfo
246 *exception;
247
cristy73bd4a52010-10-05 11:24:23 +0000248 clone_info=(DrawInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +0000249 if (clone_info == (DrawInfo *) NULL)
250 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
251 GetDrawInfo(image_info,clone_info);
252 if (draw_info == (DrawInfo *) NULL)
253 return(clone_info);
cristy947cb4c2011-10-20 18:41:46 +0000254 exception=AcquireExceptionInfo();
anthony42f6c202011-10-23 10:28:55 +0000255 (void) CloneString(&clone_info->primitive,draw_info->primitive);
256 (void) CloneString(&clone_info->geometry,draw_info->geometry);
cristy3ed852e2009-09-05 21:47:34 +0000257 clone_info->viewbox=draw_info->viewbox;
258 clone_info->affine=draw_info->affine;
259 clone_info->gravity=draw_info->gravity;
260 clone_info->fill=draw_info->fill;
261 clone_info->stroke=draw_info->stroke;
262 clone_info->stroke_width=draw_info->stroke_width;
263 if (draw_info->fill_pattern != (Image *) NULL)
264 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
cristy947cb4c2011-10-20 18:41:46 +0000265 exception);
cristy3ed852e2009-09-05 21:47:34 +0000266 if (draw_info->stroke_pattern != (Image *) NULL)
267 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
cristy947cb4c2011-10-20 18:41:46 +0000268 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000269 clone_info->stroke_antialias=draw_info->stroke_antialias;
270 clone_info->text_antialias=draw_info->text_antialias;
271 clone_info->fill_rule=draw_info->fill_rule;
272 clone_info->linecap=draw_info->linecap;
273 clone_info->linejoin=draw_info->linejoin;
274 clone_info->miterlimit=draw_info->miterlimit;
275 clone_info->dash_offset=draw_info->dash_offset;
276 clone_info->decorate=draw_info->decorate;
277 clone_info->compose=draw_info->compose;
anthony42f6c202011-10-23 10:28:55 +0000278 (void) CloneString(&clone_info->text,draw_info->text);
279 (void) CloneString(&clone_info->font,draw_info->font);
280 (void) CloneString(&clone_info->metrics,draw_info->metrics);
281 (void) CloneString(&clone_info->family,draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000282 clone_info->style=draw_info->style;
283 clone_info->stretch=draw_info->stretch;
284 clone_info->weight=draw_info->weight;
anthony42f6c202011-10-23 10:28:55 +0000285 (void) CloneString(&clone_info->encoding,draw_info->encoding);
cristy3ed852e2009-09-05 21:47:34 +0000286 clone_info->pointsize=draw_info->pointsize;
287 clone_info->kerning=draw_info->kerning;
cristyb32b90a2009-09-07 21:45:48 +0000288 clone_info->interline_spacing=draw_info->interline_spacing;
cristy3ed852e2009-09-05 21:47:34 +0000289 clone_info->interword_spacing=draw_info->interword_spacing;
cristyc9b12952010-03-28 01:12:28 +0000290 clone_info->direction=draw_info->direction;
anthony42f6c202011-10-23 10:28:55 +0000291 (void) CloneString(&clone_info->density,draw_info->density);
cristy3ed852e2009-09-05 21:47:34 +0000292 clone_info->align=draw_info->align;
293 clone_info->undercolor=draw_info->undercolor;
294 clone_info->border_color=draw_info->border_color;
anthony42f6c202011-10-23 10:28:55 +0000295 (void) CloneString(&clone_info->server_name,draw_info->server_name);
cristy3ed852e2009-09-05 21:47:34 +0000296 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 }
anthony42f6c202011-10-23 10:28:55 +0000326 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
cristy3ed852e2009-09-05 21:47:34 +0000327 clone_info->bounds=draw_info->bounds;
328 clone_info->clip_units=draw_info->clip_units;
329 clone_info->render=draw_info->render;
cristy4c08aed2011-07-01 19:47:50 +0000330 clone_info->alpha=draw_info->alpha;
cristy3ed852e2009-09-05 21:47:34 +0000331 clone_info->element_reference=draw_info->element_reference;
332 clone_info->debug=IsEventLogging();
cristy947cb4c2011-10-20 18:41:46 +0000333 exception=DestroyExceptionInfo(exception);
cristy3ed852e2009-09-05 21:47:34 +0000334 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;
cristy53aed702012-07-08 00:21:25 +0000383 if ((p->points[0].y-MagickEpsilon) > q->points[0].y)
cristy3ed852e2009-09-05 21:47:34 +0000384 return(1);
cristy53aed702012-07-08 00:21:25 +0000385 if ((p->points[0].y+MagickEpsilon) < q->points[0].y)
cristy3ed852e2009-09-05 21:47:34 +0000386 return(-1);
cristy53aed702012-07-08 00:21:25 +0000387 if ((p->points[0].x-MagickEpsilon) > q->points[0].x)
cristy3ed852e2009-09-05 21:47:34 +0000388 return(1);
cristy53aed702012-07-08 00:21:25 +0000389 if ((p->points[0].x+MagickEpsilon) < q->points[0].x)
cristy3ed852e2009-09-05 21:47:34 +0000390 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 */
cristy53aed702012-07-08 00:21:25 +0000754 if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
755 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +0000756 {
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;
cristy53aed702012-07-08 00:21:25 +0000764 if ((fabs(p.x-primitive_info[i].point.x) < MagickEpsilon) &&
765 (fabs(p.y-primitive_info[i].point.y) < MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +0000766 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,
cristy947cb4c2011-10-20 18:41:46 +0000939% const AffineMatrix *affine,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000940%
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%
cristy947cb4c2011-10-20 18:41:46 +0000949% o exception: return any errors or warnings in this structure.
950%
cristy3ed852e2009-09-05 21:47:34 +0000951*/
cristyefa9e8a2011-11-07 01:56:51 +0000952
cristy3ed852e2009-09-05 21:47:34 +0000953static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
954 const double y,const SegmentInfo *edge)
955{
956 double
957 intercept,
958 z;
959
960 register double
961 x;
962
963 SegmentInfo
964 inverse_edge;
965
966 /*
967 Determine left and right edges.
968 */
969 inverse_edge.x1=edge->x1;
970 inverse_edge.y1=edge->y1;
971 inverse_edge.x2=edge->x2;
972 inverse_edge.y2=edge->y2;
973 z=affine->ry*y+affine->tx;
cristy53aed702012-07-08 00:21:25 +0000974 if (affine->sx >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +0000975 {
976 intercept=(-z/affine->sx);
cristyefa9e8a2011-11-07 01:56:51 +0000977 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +0000978 if (x > inverse_edge.x1)
979 inverse_edge.x1=x;
980 intercept=(-z+(double) image->columns)/affine->sx;
cristyefa9e8a2011-11-07 01:56:51 +0000981 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +0000982 if (x < inverse_edge.x2)
983 inverse_edge.x2=x;
984 }
985 else
cristy53aed702012-07-08 00:21:25 +0000986 if (affine->sx < -MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +0000987 {
988 intercept=(-z+(double) image->columns)/affine->sx;
cristyefa9e8a2011-11-07 01:56:51 +0000989 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +0000990 if (x > inverse_edge.x1)
991 inverse_edge.x1=x;
992 intercept=(-z/affine->sx);
cristyefa9e8a2011-11-07 01:56:51 +0000993 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +0000994 if (x < inverse_edge.x2)
995 inverse_edge.x2=x;
996 }
997 else
cristybb503372010-05-27 20:51:26 +0000998 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000999 {
1000 inverse_edge.x2=edge->x1;
1001 return(inverse_edge);
1002 }
1003 /*
1004 Determine top and bottom edges.
1005 */
1006 z=affine->sy*y+affine->ty;
cristy53aed702012-07-08 00:21:25 +00001007 if (affine->rx >= MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00001008 {
1009 intercept=(-z/affine->rx);
cristyefa9e8a2011-11-07 01:56:51 +00001010 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +00001011 if (x > inverse_edge.x1)
1012 inverse_edge.x1=x;
1013 intercept=(-z+(double) image->rows)/affine->rx;
cristyefa9e8a2011-11-07 01:56:51 +00001014 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +00001015 if (x < inverse_edge.x2)
1016 inverse_edge.x2=x;
1017 }
1018 else
cristy53aed702012-07-08 00:21:25 +00001019 if (affine->rx < -MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00001020 {
1021 intercept=(-z+(double) image->rows)/affine->rx;
cristyefa9e8a2011-11-07 01:56:51 +00001022 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +00001023 if (x > inverse_edge.x1)
1024 inverse_edge.x1=x;
1025 intercept=(-z/affine->rx);
cristyefa9e8a2011-11-07 01:56:51 +00001026 x=intercept;
cristy3ed852e2009-09-05 21:47:34 +00001027 if (x < inverse_edge.x2)
1028 inverse_edge.x2=x;
1029 }
1030 else
cristybb503372010-05-27 20:51:26 +00001031 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001032 {
1033 inverse_edge.x2=edge->x2;
1034 return(inverse_edge);
1035 }
1036 return(inverse_edge);
1037}
1038
1039static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1040{
1041 AffineMatrix
1042 inverse_affine;
1043
1044 double
1045 determinant;
1046
cristy3e3ec3a2012-11-03 23:11:06 +00001047 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
cristy35f15302012-06-07 14:59:02 +00001048 affine->ry);
cristy3ed852e2009-09-05 21:47:34 +00001049 inverse_affine.sx=determinant*affine->sy;
1050 inverse_affine.rx=determinant*(-affine->rx);
1051 inverse_affine.ry=determinant*(-affine->ry);
1052 inverse_affine.sy=determinant*affine->sx;
1053 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1054 inverse_affine.ry;
1055 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1056 inverse_affine.sy;
1057 return(inverse_affine);
1058}
1059
cristybb503372010-05-27 20:51:26 +00001060static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +00001061{
1062 if (x < 0)
1063 return(-x);
1064 return(x);
1065}
1066
1067static inline double MagickMax(const double x,const double y)
1068{
1069 if (x > y)
1070 return(x);
1071 return(y);
1072}
1073
1074static inline double MagickMin(const double x,const double y)
1075{
1076 if (x < y)
1077 return(x);
1078 return(y);
1079}
1080
1081MagickExport MagickBooleanType DrawAffineImage(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00001082 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001083{
1084 AffineMatrix
1085 inverse_affine;
1086
cristyfa112112010-01-04 17:48:07 +00001087 CacheView
1088 *image_view,
1089 *source_view;
1090
cristy3ed852e2009-09-05 21:47:34 +00001091 MagickBooleanType
1092 status;
1093
cristy4c08aed2011-07-01 19:47:50 +00001094 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001095 zero;
1096
1097 PointInfo
1098 extent[4],
1099 min,
1100 max,
1101 point;
1102
cristybb503372010-05-27 20:51:26 +00001103 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001104 i;
1105
cristy3ed852e2009-09-05 21:47:34 +00001106 SegmentInfo
1107 edge;
1108
cristy826a5472010-08-31 23:21:38 +00001109 ssize_t
cristy564a5692012-01-20 23:56:26 +00001110 start,
1111 stop,
cristy826a5472010-08-31 23:21:38 +00001112 y;
1113
cristy3ed852e2009-09-05 21:47:34 +00001114 /*
1115 Determine bounding box.
1116 */
1117 assert(image != (Image *) NULL);
1118 assert(image->signature == MagickSignature);
1119 if (image->debug != MagickFalse)
1120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1121 assert(source != (const Image *) NULL);
1122 assert(source->signature == MagickSignature);
1123 assert(affine != (AffineMatrix *) NULL);
1124 extent[0].x=0.0;
1125 extent[0].y=0.0;
1126 extent[1].x=(double) source->columns-1.0;
1127 extent[1].y=0.0;
1128 extent[2].x=(double) source->columns-1.0;
1129 extent[2].y=(double) source->rows-1.0;
1130 extent[3].x=0.0;
1131 extent[3].y=(double) source->rows-1.0;
1132 for (i=0; i < 4; i++)
1133 {
1134 point=extent[i];
1135 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1136 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1137 }
1138 min=extent[0];
1139 max=extent[0];
1140 for (i=1; i < 4; i++)
1141 {
1142 if (min.x > extent[i].x)
1143 min.x=extent[i].x;
1144 if (min.y > extent[i].y)
1145 min.y=extent[i].y;
1146 if (max.x < extent[i].x)
1147 max.x=extent[i].x;
1148 if (max.y < extent[i].y)
1149 max.y=extent[i].y;
1150 }
1151 /*
1152 Affine transform image.
1153 */
cristy574cc262011-08-05 01:23:58 +00001154 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001155 return(MagickFalse);
1156 status=MagickTrue;
1157 edge.x1=MagickMax(min.x,0.0);
1158 edge.y1=MagickMax(min.y,0.0);
1159 edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1160 edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1161 inverse_affine=InverseAffineMatrix(affine);
cristy4c08aed2011-07-01 19:47:50 +00001162 GetPixelInfo(image,&zero);
cristy564a5692012-01-20 23:56:26 +00001163 start=(ssize_t) ceil(edge.y1-0.5);
cristy8071c472012-09-24 12:41:06 +00001164 stop=(ssize_t) floor(edge.y2+0.5);
cristy46ff2672012-12-14 15:32:26 +00001165 source_view=AcquireVirtualCacheView(source,exception);
1166 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001167#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001168 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00001169 magick_threads(source,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +00001170#endif
cristy564a5692012-01-20 23:56:26 +00001171 for (y=start; y <= stop; y++)
cristy3ed852e2009-09-05 21:47:34 +00001172 {
cristy4c08aed2011-07-01 19:47:50 +00001173 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001174 composite,
1175 pixel;
1176
1177 PointInfo
1178 point;
1179
cristybb503372010-05-27 20:51:26 +00001180 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001181 x;
1182
cristy4c08aed2011-07-01 19:47:50 +00001183 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001184 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001185
1186 SegmentInfo
1187 inverse_edge;
1188
cristy826a5472010-08-31 23:21:38 +00001189 ssize_t
1190 x_offset;
1191
cristy3ed852e2009-09-05 21:47:34 +00001192 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1193 if (inverse_edge.x2 < inverse_edge.x1)
1194 continue;
cristy6ebe97c2010-07-03 01:17:28 +00001195 q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
cristy592aefd2013-02-03 15:41:39 +00001196 0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1197 1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001198 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001199 continue;
cristy3ed852e2009-09-05 21:47:34 +00001200 pixel=zero;
1201 composite=zero;
1202 x_offset=0;
cristy8071c472012-09-24 12:41:06 +00001203 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 +00001204 {
1205 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1206 inverse_affine.tx;
1207 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1208 inverse_affine.ty;
cristy4c08aed2011-07-01 19:47:50 +00001209 (void) InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1210 point.x,point.y,&pixel,exception);
cristy803640d2011-11-17 02:11:32 +00001211 GetPixelInfoPixel(image,q,&composite);
cristy4c08aed2011-07-01 19:47:50 +00001212 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1213 &composite);
cristy803640d2011-11-17 02:11:32 +00001214 SetPixelInfoPixel(image,&composite,q);
cristy3ed852e2009-09-05 21:47:34 +00001215 x_offset++;
cristyed231572011-07-14 02:18:59 +00001216 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001217 }
1218 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1219 status=MagickFalse;
1220 }
cristy3ed852e2009-09-05 21:47:34 +00001221 source_view=DestroyCacheView(source_view);
1222 image_view=DestroyCacheView(image_view);
1223 return(status);
1224}
1225
1226/*
1227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1228% %
1229% %
1230% %
1231+ D r a w B o u n d i n g R e c t a n g l e s %
1232% %
1233% %
1234% %
1235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1236%
1237% DrawBoundingRectangles() draws the bounding rectangles on the image. This
1238% is only useful for developers debugging the rendering algorithm.
1239%
1240% The format of the DrawBoundingRectangles method is:
1241%
1242% void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001243% PolygonInfo *polygon_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001244%
1245% A description of each parameter follows:
1246%
1247% o image: the image.
1248%
1249% o draw_info: the draw info.
1250%
1251% o polygon_info: Specifies a pointer to a PolygonInfo structure.
1252%
cristy947cb4c2011-10-20 18:41:46 +00001253% o exception: return any errors or warnings in this structure.
1254%
cristy3ed852e2009-09-05 21:47:34 +00001255*/
1256static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001257 const PolygonInfo *polygon_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001258{
1259 DrawInfo
1260 *clone_info;
1261
cristya19f1d72012-08-07 18:24:38 +00001262 double
cristy3ed852e2009-09-05 21:47:34 +00001263 mid;
1264
1265 PointInfo
1266 end,
1267 resolution,
1268 start;
1269
1270 PrimitiveInfo
1271 primitive_info[6];
1272
cristybb503372010-05-27 20:51:26 +00001273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001274 i;
1275
1276 SegmentInfo
1277 bounds;
1278
cristy826a5472010-08-31 23:21:38 +00001279 ssize_t
1280 coordinates;
1281
cristy3ed852e2009-09-05 21:47:34 +00001282 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
cristyfad60c92012-01-19 18:32:39 +00001283 (void) QueryColorCompliance("#0000",AllCompliance,&clone_info->fill,
cristy947cb4c2011-10-20 18:41:46 +00001284 exception);
cristy3ed852e2009-09-05 21:47:34 +00001285 resolution.x=DefaultResolution;
1286 resolution.y=DefaultResolution;
1287 if (clone_info->density != (char *) NULL)
1288 {
1289 GeometryInfo
1290 geometry_info;
1291
1292 MagickStatusType
1293 flags;
1294
1295 flags=ParseGeometry(clone_info->density,&geometry_info);
1296 resolution.x=geometry_info.rho;
1297 resolution.y=geometry_info.sigma;
1298 if ((flags & SigmaValue) == MagickFalse)
1299 resolution.y=resolution.x;
1300 }
1301 mid=(resolution.x/72.0)*ExpandAffine(&clone_info->affine)*
1302 clone_info->stroke_width/2.0;
1303 bounds.x1=0.0;
1304 bounds.y1=0.0;
1305 bounds.x2=0.0;
1306 bounds.y2=0.0;
1307 if (polygon_info != (PolygonInfo *) NULL)
1308 {
1309 bounds=polygon_info->edges[0].bounds;
cristybb503372010-05-27 20:51:26 +00001310 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00001311 {
1312 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1313 bounds.x1=polygon_info->edges[i].bounds.x1;
1314 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1315 bounds.y1=polygon_info->edges[i].bounds.y1;
1316 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1317 bounds.x2=polygon_info->edges[i].bounds.x2;
1318 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1319 bounds.y2=polygon_info->edges[i].bounds.y2;
1320 }
1321 bounds.x1-=mid;
1322 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
cristy6c5494a2012-09-22 00:30:37 +00001323 image->columns ? (double) image->columns-1 : bounds.x1;
cristy3ed852e2009-09-05 21:47:34 +00001324 bounds.y1-=mid;
1325 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
cristy6c5494a2012-09-22 00:30:37 +00001326 image->rows ? (double) image->rows-1 : bounds.y1;
cristy3ed852e2009-09-05 21:47:34 +00001327 bounds.x2+=mid;
1328 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
cristy6c5494a2012-09-22 00:30:37 +00001329 image->columns ? (double) image->columns-1 : bounds.x2;
cristy3ed852e2009-09-05 21:47:34 +00001330 bounds.y2+=mid;
1331 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
cristy6c5494a2012-09-22 00:30:37 +00001332 image->rows ? (double) image->rows-1 : bounds.y2;
cristybb503372010-05-27 20:51:26 +00001333 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00001334 {
1335 if (polygon_info->edges[i].direction != 0)
cristy9950d572011-10-01 18:22:35 +00001336 (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
cristy947cb4c2011-10-20 18:41:46 +00001337 exception);
cristy3ed852e2009-09-05 21:47:34 +00001338 else
cristy9950d572011-10-01 18:22:35 +00001339 (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
cristy947cb4c2011-10-20 18:41:46 +00001340 exception);
cristy3ed852e2009-09-05 21:47:34 +00001341 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1342 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1343 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1344 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1345 primitive_info[0].primitive=RectanglePrimitive;
1346 TraceRectangle(primitive_info,start,end);
1347 primitive_info[0].method=ReplaceMethod;
cristybb503372010-05-27 20:51:26 +00001348 coordinates=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00001349 primitive_info[coordinates].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00001350 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001351 }
1352 }
cristy9950d572011-10-01 18:22:35 +00001353 (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
cristy947cb4c2011-10-20 18:41:46 +00001354 exception);
cristy3ed852e2009-09-05 21:47:34 +00001355 start.x=(double) (bounds.x1-mid);
1356 start.y=(double) (bounds.y1-mid);
1357 end.x=(double) (bounds.x2+mid);
1358 end.y=(double) (bounds.y2+mid);
1359 primitive_info[0].primitive=RectanglePrimitive;
1360 TraceRectangle(primitive_info,start,end);
1361 primitive_info[0].method=ReplaceMethod;
cristybb503372010-05-27 20:51:26 +00001362 coordinates=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00001363 primitive_info[coordinates].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00001364 (void) DrawPrimitive(image,clone_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001365 clone_info=DestroyDrawInfo(clone_info);
1366}
1367
1368/*
1369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370% %
1371% %
1372% %
1373% D r a w C l i p P a t h %
1374% %
1375% %
1376% %
1377%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378%
1379% DrawClipPath() draws the clip path on the image mask.
1380%
1381% The format of the DrawClipPath method is:
1382%
1383% MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
cristy018f07f2011-09-04 21:15:19 +00001384% const char *name,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001385%
1386% A description of each parameter follows:
1387%
1388% o image: the image.
1389%
1390% o draw_info: the draw info.
1391%
1392% o name: the name of the clip path.
1393%
cristy018f07f2011-09-04 21:15:19 +00001394% o exception: return any errors or warnings in this structure.
1395%
cristy3ed852e2009-09-05 21:47:34 +00001396*/
1397MagickExport MagickBooleanType DrawClipPath(Image *image,
cristy018f07f2011-09-04 21:15:19 +00001398 const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001399{
1400 char
cristy10a6c612012-01-29 21:41:05 +00001401 filename[MaxTextExtent];
1402
1403 Image
1404 *clip_mask;
cristy3ed852e2009-09-05 21:47:34 +00001405
1406 const char
1407 *value;
1408
1409 DrawInfo
1410 *clone_info;
1411
1412 MagickStatusType
1413 status;
1414
1415 assert(image != (Image *) NULL);
1416 assert(image->signature == MagickSignature);
1417 if (image->debug != MagickFalse)
1418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1419 assert(draw_info != (const DrawInfo *) NULL);
cristy10a6c612012-01-29 21:41:05 +00001420 (void) FormatLocaleString(filename,MaxTextExtent,"%s",name);
1421 value=GetImageArtifact(image,filename);
cristy3ed852e2009-09-05 21:47:34 +00001422 if (value == (const char *) NULL)
1423 return(MagickFalse);
cristy10a6c612012-01-29 21:41:05 +00001424 clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1425 if (clip_mask == (Image *) NULL)
1426 return(MagickFalse);
cristyfad60c92012-01-19 18:32:39 +00001427 (void) QueryColorCompliance("#0000",AllCompliance,
cristy10a6c612012-01-29 21:41:05 +00001428 &clip_mask->background_color,exception);
1429 clip_mask->background_color.alpha=(Quantum) TransparentAlpha;
1430 (void) SetImageBackgroundColor(clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001431 if (image->debug != MagickFalse)
1432 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1433 draw_info->clip_mask);
1434 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1435 (void) CloneString(&clone_info->primitive,value);
cristy9950d572011-10-01 18:22:35 +00001436 (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
cristyea1a8aa2011-10-20 13:24:06 +00001437 exception);
cristy3ed852e2009-09-05 21:47:34 +00001438 clone_info->clip_mask=(char *) NULL;
cristy0c901882012-01-30 15:58:59 +00001439 status=NegateImage(clip_mask,MagickFalse,exception);
cristy10a6c612012-01-29 21:41:05 +00001440 (void) SetImageMask(image,clip_mask,exception);
1441 clip_mask=DestroyImage(clip_mask);
cristy8a77f142013-08-14 12:44:38 +00001442 status&=DrawImage(image,clone_info,exception);
cristy8e508182012-09-22 00:21:17 +00001443 clone_info=DestroyDrawInfo(clone_info);
cristy3ed852e2009-09-05 21:47:34 +00001444 if (image->debug != MagickFalse)
1445 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1446 return(status != 0 ? MagickTrue : MagickFalse);
1447}
1448
1449/*
1450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451% %
1452% %
1453% %
1454+ D r a w D a s h P o l y g o n %
1455% %
1456% %
1457% %
1458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459%
1460% DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1461% image while respecting the dash offset and dash pattern attributes.
1462%
1463% The format of the DrawDashPolygon method is:
1464%
1465% MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001466% const PrimitiveInfo *primitive_info,Image *image,
1467% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001468%
1469% A description of each parameter follows:
1470%
1471% o draw_info: the draw info.
1472%
1473% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1474%
1475% o image: the image.
1476%
cristy947cb4c2011-10-20 18:41:46 +00001477% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00001478%
1479*/
1480static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00001481 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001482{
1483 DrawInfo
1484 *clone_info;
1485
cristya19f1d72012-08-07 18:24:38 +00001486 double
cristy3ed852e2009-09-05 21:47:34 +00001487 length,
1488 maximum_length,
1489 offset,
1490 scale,
1491 total_length;
1492
1493 MagickStatusType
1494 status;
1495
1496 PrimitiveInfo
1497 *dash_polygon;
1498
cristybb503372010-05-27 20:51:26 +00001499 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001500 i;
1501
cristya19f1d72012-08-07 18:24:38 +00001502 register double
cristy3ed852e2009-09-05 21:47:34 +00001503 dx,
1504 dy;
1505
cristybb503372010-05-27 20:51:26 +00001506 size_t
cristy3ed852e2009-09-05 21:47:34 +00001507 number_vertices;
1508
cristy826a5472010-08-31 23:21:38 +00001509 ssize_t
1510 j,
1511 n;
1512
cristy3ed852e2009-09-05 21:47:34 +00001513 assert(draw_info != (const DrawInfo *) NULL);
1514 if (image->debug != MagickFalse)
1515 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1516 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1517 clone_info->miterlimit=0;
1518 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
cristybb503372010-05-27 20:51:26 +00001519 number_vertices=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00001520 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1521 (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1522 if (dash_polygon == (PrimitiveInfo *) NULL)
1523 return(MagickFalse);
1524 dash_polygon[0]=primitive_info[0];
1525 scale=ExpandAffine(&draw_info->affine);
1526 length=scale*(draw_info->dash_pattern[0]-0.5);
1527 offset=draw_info->dash_offset != 0.0 ? scale*draw_info->dash_offset : 0.0;
1528 j=1;
1529 for (n=0; offset > 0.0; j=0)
1530 {
1531 if (draw_info->dash_pattern[n] <= 0.0)
1532 break;
1533 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1534 if (offset > length)
1535 {
1536 offset-=length;
1537 n++;
1538 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1539 continue;
1540 }
1541 if (offset < length)
1542 {
1543 length-=offset;
1544 offset=0.0;
1545 break;
1546 }
1547 offset=0.0;
1548 n++;
1549 }
1550 status=MagickTrue;
1551 maximum_length=0.0;
1552 total_length=0.0;
cristybb503372010-05-27 20:51:26 +00001553 for (i=1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00001554 {
1555 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1556 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1557 maximum_length=hypot((double) dx,dy);
1558 if (length == 0.0)
1559 {
1560 n++;
1561 if (draw_info->dash_pattern[n] == 0.0)
1562 n=0;
1563 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1564 }
cristycbda6112012-05-27 20:57:16 +00001565 for (total_length=0.0; (total_length+length) <= maximum_length; )
cristy3ed852e2009-09-05 21:47:34 +00001566 {
1567 total_length+=length;
1568 if ((n & 0x01) != 0)
1569 {
1570 dash_polygon[0]=primitive_info[0];
1571 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1572 total_length/maximum_length);
1573 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1574 total_length/maximum_length);
1575 j=1;
1576 }
1577 else
1578 {
cristybb503372010-05-27 20:51:26 +00001579 if ((j+1) > (ssize_t) (2*number_vertices))
cristy3ed852e2009-09-05 21:47:34 +00001580 break;
1581 dash_polygon[j]=primitive_info[i-1];
1582 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1583 total_length/maximum_length);
1584 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1585 total_length/maximum_length);
1586 dash_polygon[j].coordinates=1;
1587 j++;
cristybb503372010-05-27 20:51:26 +00001588 dash_polygon[0].coordinates=(size_t) j;
cristy3ed852e2009-09-05 21:47:34 +00001589 dash_polygon[j].primitive=UndefinedPrimitive;
cristy8a77f142013-08-14 12:44:38 +00001590 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
cristy3ed852e2009-09-05 21:47:34 +00001591 }
1592 n++;
1593 if (draw_info->dash_pattern[n] == 0.0)
1594 n=0;
1595 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1596 }
1597 length-=(maximum_length-total_length);
1598 if ((n & 0x01) != 0)
1599 continue;
1600 dash_polygon[j]=primitive_info[i];
1601 dash_polygon[j].coordinates=1;
1602 j++;
1603 }
cristycbda6112012-05-27 20:57:16 +00001604 if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
cristy3ed852e2009-09-05 21:47:34 +00001605 {
1606 dash_polygon[j]=primitive_info[i-1];
cristy53aed702012-07-08 00:21:25 +00001607 dash_polygon[j].point.x+=MagickEpsilon;
1608 dash_polygon[j].point.y+=MagickEpsilon;
cristy3ed852e2009-09-05 21:47:34 +00001609 dash_polygon[j].coordinates=1;
1610 j++;
cristybb503372010-05-27 20:51:26 +00001611 dash_polygon[0].coordinates=(size_t) j;
cristy3ed852e2009-09-05 21:47:34 +00001612 dash_polygon[j].primitive=UndefinedPrimitive;
cristy8a77f142013-08-14 12:44:38 +00001613 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
cristy3ed852e2009-09-05 21:47:34 +00001614 }
1615 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1616 clone_info=DestroyDrawInfo(clone_info);
1617 if (image->debug != MagickFalse)
1618 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1619 return(status != 0 ? MagickTrue : MagickFalse);
1620}
1621
1622/*
1623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624% %
1625% %
1626% %
1627% D r a w I m a g e %
1628% %
1629% %
1630% %
1631%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632%
1633% DrawImage() draws a graphic primitive on your image. The primitive
1634% may be represented as a string or filename. Precede the filename with an
1635% "at" sign (@) and the contents of the file are drawn on the image. You
1636% can affect how text is drawn by setting one or more members of the draw
1637% info structure.
1638%
1639% The format of the DrawImage method is:
1640%
cristy018f07f2011-09-04 21:15:19 +00001641% MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1642% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001643%
1644% A description of each parameter follows:
1645%
1646% o image: the image.
1647%
1648% o draw_info: the draw info.
1649%
cristy018f07f2011-09-04 21:15:19 +00001650% o exception: return any errors or warnings in this structure.
1651%
cristy3ed852e2009-09-05 21:47:34 +00001652*/
1653
1654static inline MagickBooleanType IsPoint(const char *point)
1655{
1656 char
1657 *p;
1658
1659 double
1660 value;
1661
cristydbdd0e32011-11-04 23:29:40 +00001662 value=StringToDouble(point,&p);
cristy3ed852e2009-09-05 21:47:34 +00001663 return((value == 0.0) && (p == point) ? MagickFalse : MagickTrue);
1664}
1665
1666static inline void TracePoint(PrimitiveInfo *primitive_info,
1667 const PointInfo point)
1668{
1669 primitive_info->coordinates=1;
1670 primitive_info->point=point;
1671}
1672
cristy018f07f2011-09-04 21:15:19 +00001673MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1674 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001675{
1676#define RenderImageTag "Render/Image"
1677
1678 AffineMatrix
1679 affine,
1680 current;
1681
1682 char
1683 key[2*MaxTextExtent],
1684 keyword[MaxTextExtent],
1685 geometry[MaxTextExtent],
1686 name[MaxTextExtent],
1687 pattern[MaxTextExtent],
1688 *primitive,
1689 *token;
1690
1691 const char
1692 *q;
1693
1694 DrawInfo
1695 **graphic_context;
1696
cristy3ed852e2009-09-05 21:47:34 +00001697 MagickBooleanType
cristy8a77f142013-08-14 12:44:38 +00001698 proceed;
1699
1700 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +00001701 status;
1702
cristya19f1d72012-08-07 18:24:38 +00001703 double
cristy3ed852e2009-09-05 21:47:34 +00001704 angle,
1705 factor,
1706 primitive_extent;
1707
1708 PointInfo
1709 point;
1710
cristy101ab702011-10-13 13:06:32 +00001711 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001712 start_color;
1713
1714 PrimitiveInfo
1715 *primitive_info;
1716
1717 PrimitiveType
1718 primitive_type;
1719
1720 register const char
1721 *p;
1722
cristybb503372010-05-27 20:51:26 +00001723 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001724 i,
1725 x;
1726
1727 SegmentInfo
1728 bounds;
1729
1730 size_t
cristy826a5472010-08-31 23:21:38 +00001731 length,
cristy3ed852e2009-09-05 21:47:34 +00001732 number_points;
1733
cristy826a5472010-08-31 23:21:38 +00001734 ssize_t
1735 j,
1736 k,
1737 n;
1738
cristy3ed852e2009-09-05 21:47:34 +00001739 /*
1740 Ensure the annotation info is valid.
1741 */
1742 assert(image != (Image *) NULL);
1743 assert(image->signature == MagickSignature);
1744 if (image->debug != MagickFalse)
1745 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1746 assert(draw_info != (DrawInfo *) NULL);
1747 assert(draw_info->signature == MagickSignature);
1748 if (image->debug != MagickFalse)
1749 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1750 if ((draw_info->primitive == (char *) NULL) ||
1751 (*draw_info->primitive == '\0'))
1752 return(MagickFalse);
1753 if (image->debug != MagickFalse)
1754 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1755 if (*draw_info->primitive != '@')
1756 primitive=AcquireString(draw_info->primitive);
1757 else
cristy3a5987c2013-11-07 14:18:46 +00001758 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001759 if (primitive == (char *) NULL)
1760 return(MagickFalse);
cristya19f1d72012-08-07 18:24:38 +00001761 primitive_extent=(double) strlen(primitive);
cristy3ed852e2009-09-05 21:47:34 +00001762 (void) SetImageArtifact(image,"MVG",primitive);
1763 n=0;
1764 /*
1765 Allocate primitive info memory.
1766 */
cristy73bd4a52010-10-05 11:24:23 +00001767 graphic_context=(DrawInfo **) AcquireMagickMemory(
cristyed110712010-03-23 01:16:38 +00001768 sizeof(*graphic_context));
cristy3ed852e2009-09-05 21:47:34 +00001769 if (graphic_context == (DrawInfo **) NULL)
1770 {
1771 primitive=DestroyString(primitive);
1772 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1773 image->filename);
1774 }
cristy430522c2012-09-03 00:07:16 +00001775 number_points=6553;
cristy3ed852e2009-09-05 21:47:34 +00001776 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1777 sizeof(*primitive_info));
1778 if (primitive_info == (PrimitiveInfo *) NULL)
1779 {
1780 primitive=DestroyString(primitive);
1781 for ( ; n >= 0; n--)
1782 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1783 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1784 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1785 image->filename);
1786 }
1787 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1788 graphic_context[n]->viewbox=image->page;
1789 if ((image->page.width == 0) || (image->page.height == 0))
1790 {
1791 graphic_context[n]->viewbox.width=image->columns;
1792 graphic_context[n]->viewbox.height=image->rows;
1793 }
1794 token=AcquireString(primitive);
cristy9950d572011-10-01 18:22:35 +00001795 (void) QueryColorCompliance("#000000",AllCompliance,&start_color,
cristy947cb4c2011-10-20 18:41:46 +00001796 exception);
1797 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001798 return(MagickFalse);
1799 status=MagickTrue;
1800 for (q=primitive; *q != '\0'; )
1801 {
1802 /*
1803 Interpret graphic primitive.
1804 */
1805 GetMagickToken(q,&q,keyword);
1806 if (*keyword == '\0')
1807 break;
1808 if (*keyword == '#')
1809 {
1810 /*
1811 Comment.
1812 */
1813 while ((*q != '\n') && (*q != '\0'))
1814 q++;
1815 continue;
1816 }
1817 p=q-strlen(keyword)-1;
1818 primitive_type=UndefinedPrimitive;
1819 current=graphic_context[n]->affine;
1820 GetAffineMatrix(&affine);
1821 switch (*keyword)
1822 {
1823 case ';':
1824 break;
1825 case 'a':
1826 case 'A':
1827 {
1828 if (LocaleCompare("affine",keyword) == 0)
1829 {
1830 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00001831 affine.sx=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001832 GetMagickToken(q,&q,token);
1833 if (*token == ',')
1834 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00001835 affine.rx=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001836 GetMagickToken(q,&q,token);
1837 if (*token == ',')
1838 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00001839 affine.ry=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001840 GetMagickToken(q,&q,token);
1841 if (*token == ',')
1842 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00001843 affine.sy=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001844 GetMagickToken(q,&q,token);
1845 if (*token == ',')
1846 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00001847 affine.tx=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001848 GetMagickToken(q,&q,token);
1849 if (*token == ',')
1850 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00001851 affine.ty=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001852 break;
1853 }
1854 if (LocaleCompare("arc",keyword) == 0)
1855 {
1856 primitive_type=ArcPrimitive;
1857 break;
1858 }
1859 status=MagickFalse;
1860 break;
1861 }
1862 case 'b':
1863 case 'B':
1864 {
1865 if (LocaleCompare("bezier",keyword) == 0)
1866 {
1867 primitive_type=BezierPrimitive;
1868 break;
1869 }
1870 if (LocaleCompare("border-color",keyword) == 0)
1871 {
1872 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00001873 (void) QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00001874 &graphic_context[n]->border_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00001875 break;
1876 }
1877 status=MagickFalse;
1878 break;
1879 }
1880 case 'c':
1881 case 'C':
1882 {
1883 if (LocaleCompare("clip-path",keyword) == 0)
1884 {
1885 /*
1886 Create clip mask.
1887 */
1888 GetMagickToken(q,&q,token);
1889 (void) CloneString(&graphic_context[n]->clip_mask,token);
1890 (void) DrawClipPath(image,graphic_context[n],
cristy018f07f2011-09-04 21:15:19 +00001891 graphic_context[n]->clip_mask,exception);
cristy3ed852e2009-09-05 21:47:34 +00001892 break;
1893 }
1894 if (LocaleCompare("clip-rule",keyword) == 0)
1895 {
cristybb503372010-05-27 20:51:26 +00001896 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001897 fill_rule;
1898
1899 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001900 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001901 token);
1902 if (fill_rule == -1)
anthony2a021472011-10-08 11:29:29 +00001903 status=MagickFalse;
1904 else
1905 graphic_context[n]->fill_rule=(FillRule) fill_rule;
cristy3ed852e2009-09-05 21:47:34 +00001906 break;
1907 }
1908 if (LocaleCompare("clip-units",keyword) == 0)
1909 {
cristybb503372010-05-27 20:51:26 +00001910 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001911 clip_units;
1912
1913 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001914 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001915 token);
1916 if (clip_units == -1)
1917 {
1918 status=MagickFalse;
1919 break;
1920 }
1921 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1922 if (clip_units == ObjectBoundingBox)
1923 {
1924 GetAffineMatrix(&current);
1925 affine.sx=draw_info->bounds.x2;
1926 affine.sy=draw_info->bounds.y2;
1927 affine.tx=draw_info->bounds.x1;
1928 affine.ty=draw_info->bounds.y1;
1929 break;
1930 }
1931 break;
1932 }
1933 if (LocaleCompare("circle",keyword) == 0)
1934 {
1935 primitive_type=CirclePrimitive;
1936 break;
1937 }
1938 if (LocaleCompare("color",keyword) == 0)
1939 {
1940 primitive_type=ColorPrimitive;
1941 break;
1942 }
1943 status=MagickFalse;
1944 break;
1945 }
1946 case 'd':
1947 case 'D':
1948 {
1949 if (LocaleCompare("decorate",keyword) == 0)
1950 {
cristybb503372010-05-27 20:51:26 +00001951 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001952 decorate;
1953
1954 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00001955 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001956 token);
1957 if (decorate == -1)
anthony2a021472011-10-08 11:29:29 +00001958 status=MagickFalse;
1959 else
1960 graphic_context[n]->decorate=(DecorationType) decorate;
cristy3ed852e2009-09-05 21:47:34 +00001961 break;
1962 }
1963 status=MagickFalse;
1964 break;
1965 }
1966 case 'e':
1967 case 'E':
1968 {
1969 if (LocaleCompare("ellipse",keyword) == 0)
1970 {
1971 primitive_type=EllipsePrimitive;
1972 break;
1973 }
1974 if (LocaleCompare("encoding",keyword) == 0)
1975 {
1976 GetMagickToken(q,&q,token);
1977 (void) CloneString(&graphic_context[n]->encoding,token);
1978 break;
1979 }
1980 status=MagickFalse;
1981 break;
1982 }
1983 case 'f':
1984 case 'F':
1985 {
1986 if (LocaleCompare("fill",keyword) == 0)
1987 {
1988 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00001989 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00001990 if (GetImageArtifact(image,pattern) != (const char *) NULL)
1991 (void) DrawPatternPath(image,draw_info,token,
cristy018f07f2011-09-04 21:15:19 +00001992 &graphic_context[n]->fill_pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00001993 else
1994 {
cristy8a77f142013-08-14 12:44:38 +00001995 status&=QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00001996 &graphic_context[n]->fill,exception);
cristy3ed852e2009-09-05 21:47:34 +00001997 if (status == MagickFalse)
1998 {
1999 ImageInfo
2000 *pattern_info;
2001
2002 pattern_info=AcquireImageInfo();
2003 (void) CopyMagickString(pattern_info->filename,token,
2004 MaxTextExtent);
cristy947cb4c2011-10-20 18:41:46 +00002005 graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2006 exception);
2007 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00002008 pattern_info=DestroyImageInfo(pattern_info);
2009 }
2010 }
2011 break;
2012 }
cristy69fcc592012-04-28 18:39:59 +00002013 if (LocaleCompare("fill-alpha",keyword) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002014 {
2015 GetMagickToken(q,&q,token);
2016 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristya19f1d72012-08-07 18:24:38 +00002017 graphic_context[n]->fill.alpha=(double) QuantumRange*
cristydbdd0e32011-11-04 23:29:40 +00002018 factor*StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002019 break;
2020 }
2021 if (LocaleCompare("fill-rule",keyword) == 0)
2022 {
cristybb503372010-05-27 20:51:26 +00002023 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002024 fill_rule;
2025
2026 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002027 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00002028 token);
2029 if (fill_rule == -1)
anthony2a021472011-10-08 11:29:29 +00002030 status=MagickFalse;
2031 else
2032 graphic_context[n]->fill_rule=(FillRule) fill_rule;
cristy3ed852e2009-09-05 21:47:34 +00002033 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);
cristy9b34e302011-11-05 02:15:45 +00002053 graphic_context[n]->pointsize=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002054 break;
2055 }
2056 if (LocaleCompare("font-stretch",keyword) == 0)
2057 {
cristybb503372010-05-27 20:51:26 +00002058 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002059 stretch;
2060
2061 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002062 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002063 if (stretch == -1)
anthony2a021472011-10-08 11:29:29 +00002064 status=MagickFalse;
2065 else
2066 graphic_context[n]->stretch=(StretchType) stretch;
cristy3ed852e2009-09-05 21:47:34 +00002067 break;
2068 }
2069 if (LocaleCompare("font-style",keyword) == 0)
2070 {
cristybb503372010-05-27 20:51:26 +00002071 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002072 style;
2073
2074 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002075 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002076 if (style == -1)
anthony2a021472011-10-08 11:29:29 +00002077 status=MagickFalse;
2078 else
2079 graphic_context[n]->style=(StyleType) style;
cristy3ed852e2009-09-05 21:47:34 +00002080 break;
2081 }
2082 if (LocaleCompare("font-weight",keyword) == 0)
2083 {
2084 GetMagickToken(q,&q,token);
cristye27293e2009-12-18 02:53:20 +00002085 graphic_context[n]->weight=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00002086 if (LocaleCompare(token,"all") == 0)
2087 graphic_context[n]->weight=0;
2088 if (LocaleCompare(token,"bold") == 0)
2089 graphic_context[n]->weight=700;
2090 if (LocaleCompare(token,"bolder") == 0)
2091 if (graphic_context[n]->weight <= 800)
2092 graphic_context[n]->weight+=100;
2093 if (LocaleCompare(token,"lighter") == 0)
2094 if (graphic_context[n]->weight >= 100)
2095 graphic_context[n]->weight-=100;
2096 if (LocaleCompare(token,"normal") == 0)
2097 graphic_context[n]->weight=400;
2098 break;
2099 }
2100 status=MagickFalse;
2101 break;
2102 }
2103 case 'g':
2104 case 'G':
2105 {
2106 if (LocaleCompare("gradient-units",keyword) == 0)
2107 {
2108 GetMagickToken(q,&q,token);
2109 break;
2110 }
2111 if (LocaleCompare("gravity",keyword) == 0)
2112 {
cristybb503372010-05-27 20:51:26 +00002113 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002114 gravity;
2115
2116 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002117 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002118 if (gravity == -1)
anthony2a021472011-10-08 11:29:29 +00002119 status=MagickFalse;
2120 else
2121 graphic_context[n]->gravity=(GravityType) gravity;
cristy3ed852e2009-09-05 21:47:34 +00002122 break;
2123 }
2124 status=MagickFalse;
2125 break;
2126 }
2127 case 'i':
2128 case 'I':
2129 {
2130 if (LocaleCompare("image",keyword) == 0)
2131 {
cristybb503372010-05-27 20:51:26 +00002132 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002133 compose;
2134
2135 primitive_type=ImagePrimitive;
2136 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002137 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002138 if (compose == -1)
anthony2a021472011-10-08 11:29:29 +00002139 status=MagickFalse;
2140 else
2141 graphic_context[n]->compose=(CompositeOperator) compose;
cristy3ed852e2009-09-05 21:47:34 +00002142 break;
2143 }
cristyb32b90a2009-09-07 21:45:48 +00002144 if (LocaleCompare("interline-spacing",keyword) == 0)
2145 {
2146 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002147 graphic_context[n]->interline_spacing=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +00002148 (char **) NULL);
cristyb32b90a2009-09-07 21:45:48 +00002149 break;
2150 }
cristy3ed852e2009-09-05 21:47:34 +00002151 if (LocaleCompare("interword-spacing",keyword) == 0)
2152 {
2153 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002154 graphic_context[n]->interword_spacing=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +00002155 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002156 break;
2157 }
2158 status=MagickFalse;
2159 break;
2160 }
2161 case 'k':
2162 case 'K':
2163 {
2164 if (LocaleCompare("kerning",keyword) == 0)
2165 {
2166 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002167 graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002168 break;
2169 }
2170 status=MagickFalse;
2171 break;
2172 }
2173 case 'l':
2174 case 'L':
2175 {
2176 if (LocaleCompare("line",keyword) == 0)
anthony2a021472011-10-08 11:29:29 +00002177 primitive_type=LinePrimitive;
2178 else
2179 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002180 break;
2181 }
2182 case 'm':
2183 case 'M':
2184 {
2185 if (LocaleCompare("matte",keyword) == 0)
anthony2a021472011-10-08 11:29:29 +00002186 primitive_type=MattePrimitive;
2187 else
2188 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002189 break;
2190 }
2191 case 'o':
2192 case 'O':
2193 {
2194 if (LocaleCompare("offset",keyword) == 0)
2195 {
2196 GetMagickToken(q,&q,token);
2197 break;
2198 }
2199 if (LocaleCompare("opacity",keyword) == 0)
2200 {
2201 GetMagickToken(q,&q,token);
2202 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristy8cd03c32012-07-07 18:57:59 +00002203 graphic_context[n]->alpha=ClampToQuantum(QuantumRange*(1.0-((1.0-
2204 QuantumScale*graphic_context[n]->alpha)*factor*
2205 StringToDouble(token,(char **) NULL))));
cristyda1f9c12011-10-02 21:39:49 +00002206 graphic_context[n]->fill.alpha=(double) graphic_context[n]->alpha;
2207 graphic_context[n]->stroke.alpha=(double) graphic_context[n]->alpha;
cristy3ed852e2009-09-05 21:47:34 +00002208 break;
2209 }
2210 status=MagickFalse;
2211 break;
2212 }
2213 case 'p':
2214 case 'P':
2215 {
2216 if (LocaleCompare("path",keyword) == 0)
2217 {
2218 primitive_type=PathPrimitive;
2219 break;
2220 }
2221 if (LocaleCompare("point",keyword) == 0)
2222 {
2223 primitive_type=PointPrimitive;
2224 break;
2225 }
2226 if (LocaleCompare("polyline",keyword) == 0)
2227 {
2228 primitive_type=PolylinePrimitive;
2229 break;
2230 }
2231 if (LocaleCompare("polygon",keyword) == 0)
2232 {
2233 primitive_type=PolygonPrimitive;
2234 break;
2235 }
2236 if (LocaleCompare("pop",keyword) == 0)
2237 {
2238 GetMagickToken(q,&q,token);
2239 if (LocaleCompare("clip-path",token) == 0)
2240 break;
2241 if (LocaleCompare("defs",token) == 0)
2242 break;
2243 if (LocaleCompare("gradient",token) == 0)
2244 break;
2245 if (LocaleCompare("graphic-context",token) == 0)
2246 {
2247 if (n <= 0)
2248 {
cristy947cb4c2011-10-20 18:41:46 +00002249 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002250 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00002251 n=0;
2252 break;
2253 }
2254 if (graphic_context[n]->clip_mask != (char *) NULL)
2255 if (LocaleCompare(graphic_context[n]->clip_mask,
2256 graphic_context[n-1]->clip_mask) != 0)
cristye42f6582012-02-11 17:59:50 +00002257 (void) SetImageMask(image,(Image *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002258 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2259 n--;
2260 break;
2261 }
2262 if (LocaleCompare("pattern",token) == 0)
2263 break;
2264 status=MagickFalse;
2265 break;
2266 }
2267 if (LocaleCompare("push",keyword) == 0)
2268 {
2269 GetMagickToken(q,&q,token);
2270 if (LocaleCompare("clip-path",token) == 0)
2271 {
2272 char
2273 name[MaxTextExtent];
2274
2275 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00002276 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00002277 for (p=q; *q != '\0'; )
2278 {
2279 GetMagickToken(q,&q,token);
2280 if (LocaleCompare(token,"pop") != 0)
2281 continue;
2282 GetMagickToken(q,(const char **) NULL,token);
2283 if (LocaleCompare(token,"clip-path") != 0)
2284 continue;
2285 break;
2286 }
2287 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2288 (void) SetImageArtifact(image,name,token);
2289 GetMagickToken(q,&q,token);
2290 break;
2291 }
2292 if (LocaleCompare("gradient",token) == 0)
2293 {
2294 char
2295 key[2*MaxTextExtent],
2296 name[MaxTextExtent],
2297 type[MaxTextExtent];
2298
cristy3ed852e2009-09-05 21:47:34 +00002299 SegmentInfo
2300 segment;
2301
2302 GetMagickToken(q,&q,token);
2303 (void) CopyMagickString(name,token,MaxTextExtent);
2304 GetMagickToken(q,&q,token);
2305 (void) CopyMagickString(type,token,MaxTextExtent);
2306 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002307 segment.x1=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002308 GetMagickToken(q,&q,token);
2309 if (*token == ',')
2310 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002311 segment.y1=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002312 GetMagickToken(q,&q,token);
2313 if (*token == ',')
2314 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002315 segment.x2=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002316 GetMagickToken(q,&q,token);
2317 if (*token == ',')
2318 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002319 segment.y2=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002320 if (LocaleCompare(type,"radial") == 0)
2321 {
2322 GetMagickToken(q,&q,token);
2323 if (*token == ',')
2324 GetMagickToken(q,&q,token);
cristy3ed852e2009-09-05 21:47:34 +00002325 }
2326 for (p=q; *q != '\0'; )
2327 {
2328 GetMagickToken(q,&q,token);
2329 if (LocaleCompare(token,"pop") != 0)
2330 continue;
2331 GetMagickToken(q,(const char **) NULL,token);
2332 if (LocaleCompare(token,"gradient") != 0)
2333 continue;
2334 break;
2335 }
2336 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2337 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2338 graphic_context[n]->affine.ry*segment.y1+
2339 graphic_context[n]->affine.tx;
2340 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2341 graphic_context[n]->affine.sy*segment.y1+
2342 graphic_context[n]->affine.ty;
2343 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2344 graphic_context[n]->affine.ry*segment.y2+
2345 graphic_context[n]->affine.tx;
2346 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2347 graphic_context[n]->affine.sy*segment.y2+
2348 graphic_context[n]->affine.ty;
cristyb51dff52011-05-19 16:55:47 +00002349 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00002350 (void) SetImageArtifact(image,key,token);
cristyb51dff52011-05-19 16:55:47 +00002351 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2352 (void) FormatLocaleString(geometry,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002353 "%gx%g%+.15g%+.15g",
cristy3ed852e2009-09-05 21:47:34 +00002354 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2355 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2356 bounds.x1,bounds.y1);
2357 (void) SetImageArtifact(image,key,geometry);
2358 GetMagickToken(q,&q,token);
2359 break;
2360 }
2361 if (LocaleCompare("pattern",token) == 0)
2362 {
2363 RectangleInfo
2364 bounds;
2365
2366 GetMagickToken(q,&q,token);
2367 (void) CopyMagickString(name,token,MaxTextExtent);
2368 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002369 bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2370 0.5);
cristy3ed852e2009-09-05 21:47:34 +00002371 GetMagickToken(q,&q,token);
2372 if (*token == ',')
2373 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002374 bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2375 0.5);
cristy3ed852e2009-09-05 21:47:34 +00002376 GetMagickToken(q,&q,token);
2377 if (*token == ',')
2378 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002379 bounds.width=(size_t) floor(StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +00002380 (char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002381 GetMagickToken(q,&q,token);
2382 if (*token == ',')
2383 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002384 bounds.height=(size_t) floor(StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +00002385 (char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002386 for (p=q; *q != '\0'; )
2387 {
2388 GetMagickToken(q,&q,token);
2389 if (LocaleCompare(token,"pop") != 0)
2390 continue;
2391 GetMagickToken(q,(const char **) NULL,token);
2392 if (LocaleCompare(token,"pattern") != 0)
2393 continue;
2394 break;
2395 }
2396 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
cristyb51dff52011-05-19 16:55:47 +00002397 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00002398 (void) SetImageArtifact(image,key,token);
cristyb51dff52011-05-19 16:55:47 +00002399 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2400 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002401 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
cristye8c25f92010-06-03 00:53:06 +00002402 bounds.height,(double) bounds.x,(double) bounds.y);
cristy3ed852e2009-09-05 21:47:34 +00002403 (void) SetImageArtifact(image,key,geometry);
2404 GetMagickToken(q,&q,token);
2405 break;
2406 }
2407 if (LocaleCompare("graphic-context",token) == 0)
2408 {
2409 n++;
2410 graphic_context=(DrawInfo **) ResizeQuantumMemory(
2411 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2412 if (graphic_context == (DrawInfo **) NULL)
2413 {
cristy947cb4c2011-10-20 18:41:46 +00002414 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002415 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy947cb4c2011-10-20 18:41:46 +00002416 image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002417 break;
2418 }
2419 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2420 graphic_context[n-1]);
2421 break;
2422 }
2423 if (LocaleCompare("defs",token) == 0)
2424 break;
2425 status=MagickFalse;
2426 break;
2427 }
2428 status=MagickFalse;
2429 break;
2430 }
2431 case 'r':
2432 case 'R':
2433 {
2434 if (LocaleCompare("rectangle",keyword) == 0)
2435 {
2436 primitive_type=RectanglePrimitive;
2437 break;
2438 }
2439 if (LocaleCompare("rotate",keyword) == 0)
2440 {
2441 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002442 angle=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002443 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2444 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2445 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2446 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2447 break;
2448 }
2449 if (LocaleCompare("roundRectangle",keyword) == 0)
2450 {
2451 primitive_type=RoundRectanglePrimitive;
2452 break;
2453 }
2454 status=MagickFalse;
2455 break;
2456 }
2457 case 's':
2458 case 'S':
2459 {
2460 if (LocaleCompare("scale",keyword) == 0)
2461 {
2462 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002463 affine.sx=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002464 GetMagickToken(q,&q,token);
2465 if (*token == ',')
2466 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002467 affine.sy=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002468 break;
2469 }
2470 if (LocaleCompare("skewX",keyword) == 0)
2471 {
2472 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002473 angle=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002474 affine.ry=sin(DegreesToRadians(angle));
2475 break;
2476 }
2477 if (LocaleCompare("skewY",keyword) == 0)
2478 {
2479 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002480 angle=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002481 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2482 break;
2483 }
2484 if (LocaleCompare("stop-color",keyword) == 0)
2485 {
cristy101ab702011-10-13 13:06:32 +00002486 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00002487 stop_color;
2488
2489 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00002490 (void) QueryColorCompliance(token,AllCompliance,&stop_color,
cristy947cb4c2011-10-20 18:41:46 +00002491 exception);
cristy3ed852e2009-09-05 21:47:34 +00002492 (void) GradientImage(image,LinearGradient,ReflectSpread,
cristy947cb4c2011-10-20 18:41:46 +00002493 &start_color,&stop_color,exception);
cristy3ed852e2009-09-05 21:47:34 +00002494 start_color=stop_color;
2495 GetMagickToken(q,&q,token);
2496 break;
2497 }
2498 if (LocaleCompare("stroke",keyword) == 0)
2499 {
2500 GetMagickToken(q,&q,token);
cristyb51dff52011-05-19 16:55:47 +00002501 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
cristy3ed852e2009-09-05 21:47:34 +00002502 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2503 (void) DrawPatternPath(image,draw_info,token,
cristy018f07f2011-09-04 21:15:19 +00002504 &graphic_context[n]->stroke_pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00002505 else
2506 {
cristy8a77f142013-08-14 12:44:38 +00002507 status&=QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00002508 &graphic_context[n]->stroke,exception);
cristy3ed852e2009-09-05 21:47:34 +00002509 if (status == MagickFalse)
2510 {
2511 ImageInfo
2512 *pattern_info;
2513
2514 pattern_info=AcquireImageInfo();
2515 (void) CopyMagickString(pattern_info->filename,token,
2516 MaxTextExtent);
cristy947cb4c2011-10-20 18:41:46 +00002517 graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2518 exception);
2519 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00002520 pattern_info=DestroyImageInfo(pattern_info);
2521 }
2522 }
2523 break;
2524 }
2525 if (LocaleCompare("stroke-antialias",keyword) == 0)
2526 {
2527 GetMagickToken(q,&q,token);
2528 graphic_context[n]->stroke_antialias=
cristyf2f27272009-12-17 14:48:46 +00002529 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002530 break;
2531 }
2532 if (LocaleCompare("stroke-dasharray",keyword) == 0)
2533 {
2534 if (graphic_context[n]->dash_pattern != (double *) NULL)
2535 graphic_context[n]->dash_pattern=(double *)
2536 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2537 if (IsPoint(q) != MagickFalse)
2538 {
2539 const char
2540 *p;
2541
2542 p=q;
2543 GetMagickToken(p,&p,token);
2544 if (*token == ',')
2545 GetMagickToken(p,&p,token);
2546 for (x=0; IsPoint(token) != MagickFalse; x++)
2547 {
2548 GetMagickToken(p,&p,token);
2549 if (*token == ',')
2550 GetMagickToken(p,&p,token);
2551 }
2552 graphic_context[n]->dash_pattern=(double *)
2553 AcquireQuantumMemory((size_t) (2UL*x+1UL),
2554 sizeof(*graphic_context[n]->dash_pattern));
2555 if (graphic_context[n]->dash_pattern == (double *) NULL)
2556 {
cristy947cb4c2011-10-20 18:41:46 +00002557 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002558 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy947cb4c2011-10-20 18:41:46 +00002559 image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002560 break;
2561 }
2562 for (j=0; j < x; j++)
2563 {
2564 GetMagickToken(q,&q,token);
2565 if (*token == ',')
2566 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002567 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2568 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002569 }
2570 if ((x & 0x01) != 0)
2571 for ( ; j < (2*x); j++)
2572 graphic_context[n]->dash_pattern[j]=
2573 graphic_context[n]->dash_pattern[j-x];
2574 graphic_context[n]->dash_pattern[j]=0.0;
2575 break;
2576 }
2577 GetMagickToken(q,&q,token);
2578 break;
2579 }
2580 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2581 {
2582 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002583 graphic_context[n]->dash_offset=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +00002584 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002585 break;
2586 }
2587 if (LocaleCompare("stroke-linecap",keyword) == 0)
2588 {
cristybb503372010-05-27 20:51:26 +00002589 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002590 linecap;
2591
2592 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002593 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002594 if (linecap == -1)
anthony2a021472011-10-08 11:29:29 +00002595 status=MagickFalse;
2596 else
2597 graphic_context[n]->linecap=(LineCap) linecap;
cristy3ed852e2009-09-05 21:47:34 +00002598 break;
2599 }
2600 if (LocaleCompare("stroke-linejoin",keyword) == 0)
2601 {
cristybb503372010-05-27 20:51:26 +00002602 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002603 linejoin;
2604
2605 GetMagickToken(q,&q,token);
cristy7118edf2011-10-08 13:33:25 +00002606 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
2607 token);
cristy3ed852e2009-09-05 21:47:34 +00002608 if (linejoin == -1)
anthony2a021472011-10-08 11:29:29 +00002609 status=MagickFalse;
2610 else
2611 graphic_context[n]->linejoin=(LineJoin) linejoin;
cristy3ed852e2009-09-05 21:47:34 +00002612 break;
2613 }
2614 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2615 {
2616 GetMagickToken(q,&q,token);
cristye27293e2009-12-18 02:53:20 +00002617 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00002618 break;
2619 }
2620 if (LocaleCompare("stroke-opacity",keyword) == 0)
2621 {
2622 GetMagickToken(q,&q,token);
2623 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
cristya19f1d72012-08-07 18:24:38 +00002624 graphic_context[n]->stroke.alpha=(double) QuantumRange*
cristydbdd0e32011-11-04 23:29:40 +00002625 factor*StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002626 break;
2627 }
2628 if (LocaleCompare("stroke-width",keyword) == 0)
2629 {
2630 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002631 graphic_context[n]->stroke_width=StringToDouble(token,
cristyc1acd842011-05-19 23:05:47 +00002632 (char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002633 break;
2634 }
2635 status=MagickFalse;
2636 break;
2637 }
2638 case 't':
2639 case 'T':
2640 {
2641 if (LocaleCompare("text",keyword) == 0)
2642 {
2643 primitive_type=TextPrimitive;
2644 break;
2645 }
2646 if (LocaleCompare("text-align",keyword) == 0)
2647 {
cristybb503372010-05-27 20:51:26 +00002648 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002649 align;
2650
2651 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002652 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002653 if (align == -1)
anthony2a021472011-10-08 11:29:29 +00002654 status=MagickFalse;
2655 else
2656 graphic_context[n]->align=(AlignType) align;
cristy3ed852e2009-09-05 21:47:34 +00002657 break;
2658 }
2659 if (LocaleCompare("text-anchor",keyword) == 0)
2660 {
cristybb503372010-05-27 20:51:26 +00002661 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002662 align;
2663
2664 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00002665 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00002666 if (align == -1)
anthony2a021472011-10-08 11:29:29 +00002667 status=MagickFalse;
2668 else
2669 graphic_context[n]->align=(AlignType) align;
cristy3ed852e2009-09-05 21:47:34 +00002670 break;
2671 }
2672 if (LocaleCompare("text-antialias",keyword) == 0)
2673 {
2674 GetMagickToken(q,&q,token);
2675 graphic_context[n]->text_antialias=
cristyf2f27272009-12-17 14:48:46 +00002676 StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002677 break;
2678 }
2679 if (LocaleCompare("text-undercolor",keyword) == 0)
2680 {
2681 GetMagickToken(q,&q,token);
cristy9950d572011-10-01 18:22:35 +00002682 (void) QueryColorCompliance(token,AllCompliance,
cristy947cb4c2011-10-20 18:41:46 +00002683 &graphic_context[n]->undercolor,exception);
cristy3ed852e2009-09-05 21:47:34 +00002684 break;
2685 }
2686 if (LocaleCompare("translate",keyword) == 0)
2687 {
2688 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002689 affine.tx=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002690 GetMagickToken(q,&q,token);
2691 if (*token == ',')
2692 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002693 affine.ty=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002694 break;
2695 }
2696 status=MagickFalse;
2697 break;
2698 }
2699 case 'v':
2700 case 'V':
2701 {
2702 if (LocaleCompare("viewbox",keyword) == 0)
2703 {
2704 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002705 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2706 (char **) NULL)-0.5);
cristy06609ee2010-03-17 20:21:27 +00002707 GetMagickToken(q,&q,token);
2708 if (*token == ',')
2709 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002710 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2711 (char **) NULL)-0.5);
cristy06609ee2010-03-17 20:21:27 +00002712 GetMagickToken(q,&q,token);
2713 if (*token == ',')
2714 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002715 graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2716 token,(char **) NULL)+0.5);
cristy06609ee2010-03-17 20:21:27 +00002717 GetMagickToken(q,&q,token);
2718 if (*token == ',')
2719 GetMagickToken(q,&q,token);
cristy9b34e302011-11-05 02:15:45 +00002720 graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2721 token,(char **) NULL)+0.5);
cristy3ed852e2009-09-05 21:47:34 +00002722 break;
2723 }
2724 status=MagickFalse;
2725 break;
2726 }
2727 default:
2728 {
2729 status=MagickFalse;
2730 break;
2731 }
2732 }
2733 if (status == MagickFalse)
2734 break;
2735 if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2736 (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2737 {
cristyfbd70092010-12-01 19:31:14 +00002738 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2739 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2740 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2741 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2742 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2743 current.tx;
2744 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2745 current.ty;
cristy3ed852e2009-09-05 21:47:34 +00002746 }
2747 if (primitive_type == UndefinedPrimitive)
2748 {
2749 if (image->debug != MagickFalse)
2750 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2751 (int) (q-p),p);
2752 continue;
2753 }
2754 /*
2755 Parse the primitive attributes.
2756 */
2757 i=0;
2758 j=0;
2759 primitive_info[0].point.x=0.0;
2760 primitive_info[0].point.y=0.0;
2761 for (x=0; *q != '\0'; x++)
2762 {
2763 /*
2764 Define points.
2765 */
2766 if (IsPoint(q) == MagickFalse)
2767 break;
2768 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002769 point.x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002770 GetMagickToken(q,&q,token);
2771 if (*token == ',')
2772 GetMagickToken(q,&q,token);
cristydbdd0e32011-11-04 23:29:40 +00002773 point.y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002774 GetMagickToken(q,(const char **) NULL,token);
2775 if (*token == ',')
2776 GetMagickToken(q,&q,token);
2777 primitive_info[i].primitive=primitive_type;
2778 primitive_info[i].point=point;
2779 primitive_info[i].coordinates=0;
2780 primitive_info[i].method=FloodfillMethod;
2781 i++;
cristybb503372010-05-27 20:51:26 +00002782 if (i < (ssize_t) number_points)
cristy3ed852e2009-09-05 21:47:34 +00002783 continue;
2784 number_points<<=1;
2785 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2786 (size_t) number_points,sizeof(*primitive_info));
2787 if (primitive_info == (PrimitiveInfo *) NULL)
2788 {
cristy947cb4c2011-10-20 18:41:46 +00002789 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002790 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002791 break;
2792 }
2793 }
2794 primitive_info[j].primitive=primitive_type;
cristybb503372010-05-27 20:51:26 +00002795 primitive_info[j].coordinates=(size_t) x;
cristy3ed852e2009-09-05 21:47:34 +00002796 primitive_info[j].method=FloodfillMethod;
2797 primitive_info[j].text=(char *) NULL;
2798 /*
2799 Circumscribe primitive within a circle.
2800 */
2801 bounds.x1=primitive_info[j].point.x;
2802 bounds.y1=primitive_info[j].point.y;
2803 bounds.x2=primitive_info[j].point.x;
2804 bounds.y2=primitive_info[j].point.y;
cristybb503372010-05-27 20:51:26 +00002805 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
cristy3ed852e2009-09-05 21:47:34 +00002806 {
2807 point=primitive_info[j+k].point;
2808 if (point.x < bounds.x1)
2809 bounds.x1=point.x;
2810 if (point.y < bounds.y1)
2811 bounds.y1=point.y;
2812 if (point.x > bounds.x2)
2813 bounds.x2=point.x;
2814 if (point.y > bounds.y2)
2815 bounds.y2=point.y;
2816 }
2817 /*
2818 Speculate how many points our primitive might consume.
2819 */
2820 length=primitive_info[j].coordinates;
2821 switch (primitive_type)
2822 {
2823 case RectanglePrimitive:
2824 {
2825 length*=5;
2826 break;
2827 }
2828 case RoundRectanglePrimitive:
2829 {
cristy78817ad2010-05-07 12:25:34 +00002830 length*=5+8*BezierQuantum;
cristy3ed852e2009-09-05 21:47:34 +00002831 break;
2832 }
2833 case BezierPrimitive:
2834 {
2835 if (primitive_info[j].coordinates > 107)
cristy947cb4c2011-10-20 18:41:46 +00002836 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
cristyefe601c2013-01-05 17:51:12 +00002837 "TooManyBezierCoordinates","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00002838 length=BezierQuantum*primitive_info[j].coordinates;
2839 break;
2840 }
2841 case PathPrimitive:
2842 {
2843 char
2844 *s,
2845 *t;
2846
2847 GetMagickToken(q,&q,token);
cristy40a08ad2010-02-09 02:27:44 +00002848 length=1;
cristy3ed852e2009-09-05 21:47:34 +00002849 t=token;
2850 for (s=token; *s != '\0'; s=t)
2851 {
cristy00976d82011-02-20 20:31:28 +00002852 double
2853 value;
2854
cristydbdd0e32011-11-04 23:29:40 +00002855 value=StringToDouble(s,&t);
cristy00976d82011-02-20 20:31:28 +00002856 (void) value;
cristy3ed852e2009-09-05 21:47:34 +00002857 if (s == t)
2858 {
2859 t++;
2860 continue;
2861 }
cristyef7a6ce2011-05-14 15:01:11 +00002862 length++;
cristy3ed852e2009-09-05 21:47:34 +00002863 }
cristyd2f832c2011-06-28 12:35:24 +00002864 length=length*BezierQuantum/2;
cristy3ed852e2009-09-05 21:47:34 +00002865 break;
2866 }
2867 case CirclePrimitive:
2868 case ArcPrimitive:
2869 case EllipsePrimitive:
2870 {
cristya19f1d72012-08-07 18:24:38 +00002871 double
cristy3ed852e2009-09-05 21:47:34 +00002872 alpha,
2873 beta,
2874 radius;
2875
2876 alpha=bounds.x2-bounds.x1;
2877 beta=bounds.y2-bounds.y1;
2878 radius=hypot((double) alpha,(double) beta);
cristyf53f26f2011-05-16 00:56:05 +00002879 length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
cristy3ed852e2009-09-05 21:47:34 +00002880 break;
2881 }
2882 default:
2883 break;
2884 }
cristybb503372010-05-27 20:51:26 +00002885 if ((size_t) (i+length) >= number_points)
cristy3ed852e2009-09-05 21:47:34 +00002886 {
2887 /*
2888 Resize based on speculative points required by primitive.
2889 */
cristy9ce61b92010-05-12 16:30:26 +00002890 number_points+=length+1;
cristy3ed852e2009-09-05 21:47:34 +00002891 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2892 (size_t) number_points,sizeof(*primitive_info));
2893 if (primitive_info == (PrimitiveInfo *) NULL)
2894 {
cristy947cb4c2011-10-20 18:41:46 +00002895 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00002896 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00002897 image->filename);
2898 break;
2899 }
2900 }
2901 switch (primitive_type)
2902 {
2903 case PointPrimitive:
2904 default:
2905 {
2906 if (primitive_info[j].coordinates != 1)
2907 {
2908 status=MagickFalse;
2909 break;
2910 }
2911 TracePoint(primitive_info+j,primitive_info[j].point);
cristybb503372010-05-27 20:51:26 +00002912 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002913 break;
2914 }
2915 case LinePrimitive:
2916 {
2917 if (primitive_info[j].coordinates != 2)
2918 {
2919 status=MagickFalse;
2920 break;
2921 }
2922 TraceLine(primitive_info+j,primitive_info[j].point,
2923 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002924 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002925 break;
2926 }
2927 case RectanglePrimitive:
2928 {
2929 if (primitive_info[j].coordinates != 2)
2930 {
2931 status=MagickFalse;
2932 break;
2933 }
2934 TraceRectangle(primitive_info+j,primitive_info[j].point,
2935 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002936 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002937 break;
2938 }
2939 case RoundRectanglePrimitive:
2940 {
2941 if (primitive_info[j].coordinates != 3)
2942 {
2943 status=MagickFalse;
2944 break;
2945 }
2946 TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2947 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002948 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002949 break;
2950 }
2951 case ArcPrimitive:
2952 {
2953 if (primitive_info[j].coordinates != 3)
2954 {
2955 primitive_type=UndefinedPrimitive;
2956 break;
2957 }
2958 TraceArc(primitive_info+j,primitive_info[j].point,
2959 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002960 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002961 break;
2962 }
2963 case EllipsePrimitive:
2964 {
2965 if (primitive_info[j].coordinates != 3)
2966 {
2967 status=MagickFalse;
2968 break;
2969 }
2970 TraceEllipse(primitive_info+j,primitive_info[j].point,
2971 primitive_info[j+1].point,primitive_info[j+2].point);
cristybb503372010-05-27 20:51:26 +00002972 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002973 break;
2974 }
2975 case CirclePrimitive:
2976 {
2977 if (primitive_info[j].coordinates != 2)
2978 {
2979 status=MagickFalse;
2980 break;
2981 }
2982 TraceCircle(primitive_info+j,primitive_info[j].point,
2983 primitive_info[j+1].point);
cristybb503372010-05-27 20:51:26 +00002984 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00002985 break;
2986 }
2987 case PolylinePrimitive:
2988 break;
2989 case PolygonPrimitive:
2990 {
2991 primitive_info[i]=primitive_info[j];
2992 primitive_info[i].coordinates=0;
2993 primitive_info[j].coordinates++;
2994 i++;
2995 break;
2996 }
2997 case BezierPrimitive:
2998 {
2999 if (primitive_info[j].coordinates < 3)
3000 {
3001 status=MagickFalse;
3002 break;
3003 }
3004 TraceBezier(primitive_info+j,primitive_info[j].coordinates);
cristybb503372010-05-27 20:51:26 +00003005 i=(ssize_t) (j+primitive_info[j].coordinates);
cristy3ed852e2009-09-05 21:47:34 +00003006 break;
3007 }
3008 case PathPrimitive:
3009 {
cristybb503372010-05-27 20:51:26 +00003010 i=(ssize_t) (j+TracePath(primitive_info+j,token));
cristy3ed852e2009-09-05 21:47:34 +00003011 break;
3012 }
3013 case ColorPrimitive:
3014 case MattePrimitive:
3015 {
cristybb503372010-05-27 20:51:26 +00003016 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003017 method;
3018
3019 if (primitive_info[j].coordinates != 1)
3020 {
3021 status=MagickFalse;
3022 break;
3023 }
3024 GetMagickToken(q,&q,token);
cristy042ee782011-04-22 18:48:30 +00003025 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
cristy3ed852e2009-09-05 21:47:34 +00003026 if (method == -1)
anthony2a021472011-10-08 11:29:29 +00003027 status=MagickFalse;
3028 else
3029 primitive_info[j].method=(PaintMethod) method;
cristy3ed852e2009-09-05 21:47:34 +00003030 break;
3031 }
3032 case TextPrimitive:
3033 {
3034 if (primitive_info[j].coordinates != 1)
3035 {
3036 status=MagickFalse;
3037 break;
3038 }
3039 if (*token != ',')
3040 GetMagickToken(q,&q,token);
3041 primitive_info[j].text=AcquireString(token);
3042 break;
3043 }
3044 case ImagePrimitive:
3045 {
3046 if (primitive_info[j].coordinates != 2)
3047 {
3048 status=MagickFalse;
3049 break;
3050 }
3051 GetMagickToken(q,&q,token);
3052 primitive_info[j].text=AcquireString(token);
3053 break;
3054 }
3055 }
3056 if (primitive_info == (PrimitiveInfo *) NULL)
3057 break;
3058 if (image->debug != MagickFalse)
3059 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3060 if (status == MagickFalse)
3061 break;
3062 primitive_info[i].primitive=UndefinedPrimitive;
3063 if (i == 0)
3064 continue;
3065 /*
3066 Transform points.
3067 */
3068 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3069 {
3070 point=primitive_info[i].point;
3071 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3072 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3073 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3074 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3075 point=primitive_info[i].point;
3076 if (point.x < graphic_context[n]->bounds.x1)
3077 graphic_context[n]->bounds.x1=point.x;
3078 if (point.y < graphic_context[n]->bounds.y1)
3079 graphic_context[n]->bounds.y1=point.y;
3080 if (point.x > graphic_context[n]->bounds.x2)
3081 graphic_context[n]->bounds.x2=point.x;
3082 if (point.y > graphic_context[n]->bounds.y2)
3083 graphic_context[n]->bounds.y2=point.y;
3084 if (primitive_info[i].primitive == ImagePrimitive)
3085 break;
cristybb503372010-05-27 20:51:26 +00003086 if (i >= (ssize_t) number_points)
cristy9ce61b92010-05-12 16:30:26 +00003087 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00003088 }
cristy3ed852e2009-09-05 21:47:34 +00003089 if (graphic_context[n]->render != MagickFalse)
3090 {
3091 if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3092 (LocaleCompare(graphic_context[n]->clip_mask,
3093 graphic_context[n-1]->clip_mask) != 0))
cristy8a77f142013-08-14 12:44:38 +00003094 status&=DrawClipPath(image,graphic_context[n],
cristy018f07f2011-09-04 21:15:19 +00003095 graphic_context[n]->clip_mask,exception);
cristy8a77f142013-08-14 12:44:38 +00003096 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3097 exception);
cristy3ed852e2009-09-05 21:47:34 +00003098 }
3099 if (primitive_info->text != (char *) NULL)
3100 primitive_info->text=(char *) RelinquishMagickMemory(
3101 primitive_info->text);
3102 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3103 primitive_extent);
3104 if (proceed == MagickFalse)
3105 break;
cristy8a77f142013-08-14 12:44:38 +00003106 if (status == 0)
3107 break;
cristy3ed852e2009-09-05 21:47:34 +00003108 }
3109 if (image->debug != MagickFalse)
3110 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3111 /*
3112 Relinquish resources.
3113 */
3114 token=DestroyString(token);
3115 if (primitive_info != (PrimitiveInfo *) NULL)
3116 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3117 primitive=DestroyString(primitive);
3118 for ( ; n >= 0; n--)
3119 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3120 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3121 if (status == MagickFalse)
3122 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3123 keyword);
cristy8a77f142013-08-14 12:44:38 +00003124 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00003125}
3126
3127/*
3128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129% %
3130% %
3131% %
3132% D r a w G r a d i e n t I m a g e %
3133% %
3134% %
3135% %
3136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3137%
3138% DrawGradientImage() draws a linear gradient on the image.
3139%
3140% The format of the DrawGradientImage method is:
3141%
3142% MagickBooleanType DrawGradientImage(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003143% const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003144%
3145% A description of each parameter follows:
3146%
3147% o image: the image.
3148%
anthony2a021472011-10-08 11:29:29 +00003149% o draw_info: the draw info.
cristy3ed852e2009-09-05 21:47:34 +00003150%
cristy947cb4c2011-10-20 18:41:46 +00003151% o exception: return any errors or warnings in this structure.
3152%
cristy3ed852e2009-09-05 21:47:34 +00003153*/
3154
cristya19f1d72012-08-07 18:24:38 +00003155static inline double GetStopColorOffset(const GradientInfo *gradient,
cristybb503372010-05-27 20:51:26 +00003156 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00003157{
3158 switch (gradient->type)
3159 {
3160 case UndefinedGradient:
3161 case LinearGradient:
3162 {
cristya19f1d72012-08-07 18:24:38 +00003163 double
cristy3ed852e2009-09-05 21:47:34 +00003164 gamma,
3165 length,
3166 offset,
3167 scale;
3168
3169 PointInfo
3170 p,
3171 q;
3172
3173 const SegmentInfo
3174 *gradient_vector;
3175
3176 gradient_vector=(&gradient->gradient_vector);
3177 p.x=gradient_vector->x2-gradient_vector->x1;
3178 p.y=gradient_vector->y2-gradient_vector->y1;
3179 q.x=(double) x-gradient_vector->x1;
3180 q.y=(double) y-gradient_vector->y1;
3181 length=sqrt(q.x*q.x+q.y*q.y);
3182 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
cristy3e3ec3a2012-11-03 23:11:06 +00003183 gamma=PerceptibleReciprocal(gamma);
cristy3ed852e2009-09-05 21:47:34 +00003184 scale=p.x*q.x+p.y*q.y;
3185 offset=gamma*scale*length;
3186 return(offset);
3187 }
3188 case RadialGradient:
3189 {
cristya19f1d72012-08-07 18:24:38 +00003190 double
cristy3ed852e2009-09-05 21:47:34 +00003191 length,
3192 offset;
3193
3194 PointInfo
3195 v;
3196
3197 v.x=(double) x-gradient->center.x;
3198 v.y=(double) y-gradient->center.y;
3199 length=sqrt(v.x*v.x+v.y*v.y);
3200 if (gradient->spread == RepeatSpread)
3201 return(length);
3202 offset=length/gradient->radius;
3203 return(offset);
3204 }
3205 }
3206 return(0.0);
3207}
3208
3209MagickExport MagickBooleanType DrawGradientImage(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003210 const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003211{
cristyc4c8d132010-01-07 01:58:38 +00003212 CacheView
3213 *image_view;
3214
cristy3ed852e2009-09-05 21:47:34 +00003215 const GradientInfo
3216 *gradient;
3217
3218 const SegmentInfo
3219 *gradient_vector;
3220
cristy75c658f2012-12-30 16:09:36 +00003221 double
3222 length;
3223
cristy3ed852e2009-09-05 21:47:34 +00003224 MagickBooleanType
3225 status;
3226
cristy4c08aed2011-07-01 19:47:50 +00003227 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003228 zero;
3229
cristy3ed852e2009-09-05 21:47:34 +00003230 PointInfo
3231 point;
3232
3233 RectangleInfo
3234 bounding_box;
3235
cristy826a5472010-08-31 23:21:38 +00003236 ssize_t
3237 y;
3238
cristy3ed852e2009-09-05 21:47:34 +00003239 /*
3240 Draw linear or radial gradient on image.
3241 */
3242 assert(image != (Image *) NULL);
3243 assert(image->signature == MagickSignature);
3244 if (image->debug != MagickFalse)
3245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3246 assert(draw_info != (const DrawInfo *) NULL);
3247 gradient=(&draw_info->gradient);
3248 gradient_vector=(&gradient->gradient_vector);
3249 point.x=gradient_vector->x2-gradient_vector->x1;
3250 point.y=gradient_vector->y2-gradient_vector->y1;
3251 length=sqrt(point.x*point.x+point.y*point.y);
3252 bounding_box=gradient->bounding_box;
3253 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00003254 GetPixelInfo(image,&zero);
cristy46ff2672012-12-14 15:32:26 +00003255 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003256#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00003257 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00003258 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003259#endif
cristybb503372010-05-27 20:51:26 +00003260 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
cristy3ed852e2009-09-05 21:47:34 +00003261 {
cristy4c08aed2011-07-01 19:47:50 +00003262 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003263 composite,
3264 pixel;
3265
cristya19f1d72012-08-07 18:24:38 +00003266 double
cristy3ed852e2009-09-05 21:47:34 +00003267 alpha,
3268 offset;
3269
cristy4c08aed2011-07-01 19:47:50 +00003270 register Quantum
3271 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003272
cristybb503372010-05-27 20:51:26 +00003273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003274 i,
3275 x;
3276
cristy826a5472010-08-31 23:21:38 +00003277 ssize_t
3278 j;
3279
cristy3ed852e2009-09-05 21:47:34 +00003280 if (status == MagickFalse)
3281 continue;
3282 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003283 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003284 {
3285 status=MagickFalse;
3286 continue;
3287 }
cristy3ed852e2009-09-05 21:47:34 +00003288 pixel=zero;
3289 composite=zero;
3290 offset=GetStopColorOffset(gradient,0,y);
3291 if (gradient->type != RadialGradient)
3292 offset/=length;
cristybb503372010-05-27 20:51:26 +00003293 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
cristy3ed852e2009-09-05 21:47:34 +00003294 {
cristy803640d2011-11-17 02:11:32 +00003295 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00003296 switch (gradient->spread)
3297 {
3298 case UndefinedSpread:
3299 case PadSpread:
3300 {
cristybb503372010-05-27 20:51:26 +00003301 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3302 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003303 {
3304 offset=GetStopColorOffset(gradient,x,y);
3305 if (gradient->type != RadialGradient)
3306 offset/=length;
3307 }
cristybb503372010-05-27 20:51:26 +00003308 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003309 if (offset < gradient->stops[i].offset)
3310 break;
3311 if ((offset < 0.0) || (i == 0))
3312 composite=gradient->stops[0].color;
3313 else
cristybb503372010-05-27 20:51:26 +00003314 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
cristy3ed852e2009-09-05 21:47:34 +00003315 composite=gradient->stops[gradient->number_stops-1].color;
3316 else
3317 {
3318 j=i;
3319 i--;
3320 alpha=(offset-gradient->stops[i].offset)/
3321 (gradient->stops[j].offset-gradient->stops[i].offset);
cristy4c08aed2011-07-01 19:47:50 +00003322 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003323 &gradient->stops[j].color,alpha,&composite);
3324 }
3325 break;
3326 }
3327 case ReflectSpread:
3328 {
cristybb503372010-05-27 20:51:26 +00003329 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3330 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003331 {
3332 offset=GetStopColorOffset(gradient,x,y);
3333 if (gradient->type != RadialGradient)
3334 offset/=length;
3335 }
3336 if (offset < 0.0)
3337 offset=(-offset);
cristybb503372010-05-27 20:51:26 +00003338 if ((ssize_t) fmod(offset,2.0) == 0)
cristy3ed852e2009-09-05 21:47:34 +00003339 offset=fmod(offset,1.0);
3340 else
3341 offset=1.0-fmod(offset,1.0);
cristybb503372010-05-27 20:51:26 +00003342 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003343 if (offset < gradient->stops[i].offset)
3344 break;
3345 if (i == 0)
3346 composite=gradient->stops[0].color;
3347 else
cristybb503372010-05-27 20:51:26 +00003348 if (i == (ssize_t) gradient->number_stops)
cristy3ed852e2009-09-05 21:47:34 +00003349 composite=gradient->stops[gradient->number_stops-1].color;
3350 else
3351 {
3352 j=i;
3353 i--;
3354 alpha=(offset-gradient->stops[i].offset)/
3355 (gradient->stops[j].offset-gradient->stops[i].offset);
cristy4c08aed2011-07-01 19:47:50 +00003356 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003357 &gradient->stops[j].color,alpha,&composite);
3358 }
3359 break;
3360 }
3361 case RepeatSpread:
3362 {
3363 MagickBooleanType
3364 antialias;
3365
cristya19f1d72012-08-07 18:24:38 +00003366 double
cristy3ed852e2009-09-05 21:47:34 +00003367 repeat;
3368
3369 antialias=MagickFalse;
3370 repeat=0.0;
cristybb503372010-05-27 20:51:26 +00003371 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3372 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003373 {
3374 offset=GetStopColorOffset(gradient,x,y);
3375 if (gradient->type == LinearGradient)
3376 {
3377 repeat=fmod(offset,length);
3378 if (repeat < 0.0)
3379 repeat=length-fmod(-repeat,length);
3380 else
3381 repeat=fmod(offset,length);
3382 antialias=(repeat < length) && ((repeat+1.0) > length) ?
3383 MagickTrue : MagickFalse;
3384 offset=repeat/length;
3385 }
3386 else
3387 {
3388 repeat=fmod(offset,gradient->radius);
3389 if (repeat < 0.0)
3390 repeat=gradient->radius-fmod(-repeat,gradient->radius);
3391 else
3392 repeat=fmod(offset,gradient->radius);
cristy4c08aed2011-07-01 19:47:50 +00003393 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3394 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003395 offset=repeat/gradient->radius;
3396 }
3397 }
cristybb503372010-05-27 20:51:26 +00003398 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy3ed852e2009-09-05 21:47:34 +00003399 if (offset < gradient->stops[i].offset)
3400 break;
3401 if (i == 0)
3402 composite=gradient->stops[0].color;
3403 else
cristybb503372010-05-27 20:51:26 +00003404 if (i == (ssize_t) gradient->number_stops)
cristy3ed852e2009-09-05 21:47:34 +00003405 composite=gradient->stops[gradient->number_stops-1].color;
3406 else
3407 {
3408 j=i;
3409 i--;
3410 alpha=(offset-gradient->stops[i].offset)/
3411 (gradient->stops[j].offset-gradient->stops[i].offset);
3412 if (antialias != MagickFalse)
3413 {
3414 if (gradient->type == LinearGradient)
3415 alpha=length-repeat;
3416 else
3417 alpha=gradient->radius-repeat;
3418 i=0;
cristybb503372010-05-27 20:51:26 +00003419 j=(ssize_t) gradient->number_stops-1L;
cristy3ed852e2009-09-05 21:47:34 +00003420 }
cristy4c08aed2011-07-01 19:47:50 +00003421 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
cristy3ed852e2009-09-05 21:47:34 +00003422 &gradient->stops[j].color,alpha,&composite);
3423 }
3424 break;
3425 }
3426 }
cristy4c08aed2011-07-01 19:47:50 +00003427 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3428 &pixel);
cristy803640d2011-11-17 02:11:32 +00003429 SetPixelInfoPixel(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00003430 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003431 }
3432 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3433 status=MagickFalse;
3434 }
3435 image_view=DestroyCacheView(image_view);
3436 return(status);
3437}
3438
3439/*
3440%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3441% %
3442% %
3443% %
3444% D r a w P a t t e r n P a t h %
3445% %
3446% %
3447% %
3448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3449%
3450% DrawPatternPath() draws a pattern.
3451%
3452% The format of the DrawPatternPath method is:
3453%
3454% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
cristy018f07f2011-09-04 21:15:19 +00003455% const char *name,Image **pattern,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003456%
3457% A description of each parameter follows:
3458%
3459% o image: the image.
3460%
3461% o draw_info: the draw info.
3462%
3463% o name: the pattern name.
3464%
3465% o image: the image.
3466%
cristy018f07f2011-09-04 21:15:19 +00003467% o exception: return any errors or warnings in this structure.
3468%
cristy3ed852e2009-09-05 21:47:34 +00003469*/
3470MagickExport MagickBooleanType DrawPatternPath(Image *image,
cristy018f07f2011-09-04 21:15:19 +00003471 const DrawInfo *draw_info,const char *name,Image **pattern,
3472 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003473{
3474 char
3475 property[MaxTextExtent];
3476
3477 const char
3478 *geometry,
3479 *path;
3480
3481 DrawInfo
3482 *clone_info;
3483
3484 ImageInfo
3485 *image_info;
3486
3487 MagickBooleanType
3488 status;
3489
3490 assert(image != (Image *) NULL);
3491 assert(image->signature == MagickSignature);
3492 if (image->debug != MagickFalse)
3493 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3494 assert(draw_info != (const DrawInfo *) NULL);
3495 assert(name != (const char *) NULL);
cristyb51dff52011-05-19 16:55:47 +00003496 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
cristy3ed852e2009-09-05 21:47:34 +00003497 path=GetImageArtifact(image,property);
3498 if (path == (const char *) NULL)
3499 return(MagickFalse);
cristyb51dff52011-05-19 16:55:47 +00003500 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
cristy3ed852e2009-09-05 21:47:34 +00003501 geometry=GetImageArtifact(image,property);
3502 if (geometry == (const char *) NULL)
3503 return(MagickFalse);
3504 if ((*pattern) != (Image *) NULL)
3505 *pattern=DestroyImage(*pattern);
3506 image_info=AcquireImageInfo();
3507 image_info->size=AcquireString(geometry);
cristy947cb4c2011-10-20 18:41:46 +00003508 *pattern=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003509 image_info=DestroyImageInfo(image_info);
cristyca611542013-02-19 00:54:03 +00003510 (void) QueryColorCompliance("#000000ff",AllCompliance,
cristyea1a8aa2011-10-20 13:24:06 +00003511 &(*pattern)->background_color,exception);
3512 (void) SetImageBackgroundColor(*pattern,exception);
cristy3ed852e2009-09-05 21:47:34 +00003513 if (image->debug != MagickFalse)
3514 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3515 "begin pattern-path %s %s",name,geometry);
3516 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3517 clone_info->fill_pattern=NewImageList();
3518 clone_info->stroke_pattern=NewImageList();
3519 (void) CloneString(&clone_info->primitive,path);
cristy018f07f2011-09-04 21:15:19 +00003520 status=DrawImage(*pattern,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003521 clone_info=DestroyDrawInfo(clone_info);
3522 if (image->debug != MagickFalse)
3523 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3524 return(status);
3525}
3526
3527/*
3528%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3529% %
3530% %
3531% %
3532+ D r a w P o l y g o n P r i m i t i v e %
3533% %
3534% %
3535% %
3536%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3537%
3538% DrawPolygonPrimitive() draws a polygon on the image.
3539%
3540% The format of the DrawPolygonPrimitive method is:
3541%
3542% MagickBooleanType DrawPolygonPrimitive(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003543% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3544% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003545%
3546% A description of each parameter follows:
3547%
3548% o image: the image.
3549%
3550% o draw_info: the draw info.
3551%
3552% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3553%
cristy947cb4c2011-10-20 18:41:46 +00003554% o exception: return any errors or warnings in this structure.
3555%
cristy3ed852e2009-09-05 21:47:34 +00003556*/
3557
3558static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
3559{
cristybb503372010-05-27 20:51:26 +00003560 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003561 i;
3562
3563 assert(polygon_info != (PolygonInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00003564 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy3ed852e2009-09-05 21:47:34 +00003565 if (polygon_info[i] != (PolygonInfo *) NULL)
3566 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
cristyb41ee102010-10-04 16:46:15 +00003567 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
cristy3ed852e2009-09-05 21:47:34 +00003568 return(polygon_info);
3569}
3570
3571static PolygonInfo **AcquirePolygonThreadSet(const DrawInfo *draw_info,
3572 const PrimitiveInfo *primitive_info)
3573{
3574 PathInfo
cristyfa112112010-01-04 17:48:07 +00003575 *restrict path_info;
cristy3ed852e2009-09-05 21:47:34 +00003576
cristy3ed852e2009-09-05 21:47:34 +00003577 PolygonInfo
3578 **polygon_info;
3579
cristy826a5472010-08-31 23:21:38 +00003580 register ssize_t
3581 i;
3582
cristybb503372010-05-27 20:51:26 +00003583 size_t
cristy3ed852e2009-09-05 21:47:34 +00003584 number_threads;
3585
cristy9357bdd2012-07-30 12:28:34 +00003586 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristyb41ee102010-10-04 16:46:15 +00003587 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +00003588 sizeof(*polygon_info));
3589 if (polygon_info == (PolygonInfo **) NULL)
3590 return((PolygonInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00003591 (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
cristy3ed852e2009-09-05 21:47:34 +00003592 path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3593 if (path_info == (PathInfo *) NULL)
3594 return(DestroyPolygonThreadSet(polygon_info));
cristybb503372010-05-27 20:51:26 +00003595 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +00003596 {
3597 polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3598 if (polygon_info[i] == (PolygonInfo *) NULL)
3599 return(DestroyPolygonThreadSet(polygon_info));
3600 }
3601 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3602 return(polygon_info);
3603}
3604
cristy884d1622012-09-03 18:10:10 +00003605static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
cristye4e47ae2012-09-04 23:36:43 +00003606 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3607 const ssize_t y,double *stroke_alpha)
cristy3ed852e2009-09-05 21:47:34 +00003608{
cristya19f1d72012-08-07 18:24:38 +00003609 double
cristyb32b90a2009-09-07 21:45:48 +00003610 alpha,
3611 beta,
cristy3ed852e2009-09-05 21:47:34 +00003612 distance,
cristyb2e6b992011-12-17 16:42:29 +00003613 subpath_alpha;
cristy3ed852e2009-09-05 21:47:34 +00003614
3615 PointInfo
cristyb32b90a2009-09-07 21:45:48 +00003616 delta;
cristy3ed852e2009-09-05 21:47:34 +00003617
cristyb32b90a2009-09-07 21:45:48 +00003618 register const PointInfo
3619 *q;
3620
cristy884d1622012-09-03 18:10:10 +00003621 register EdgeInfo
3622 *p;
3623
cristybb503372010-05-27 20:51:26 +00003624 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003625 i;
3626
cristycee97112010-05-28 00:44:52 +00003627 ssize_t
3628 j,
3629 winding_number;
3630
cristy3ed852e2009-09-05 21:47:34 +00003631 /*
3632 Compute fill & stroke opacity for this (x,y) point.
3633 */
cristyb2e6b992011-12-17 16:42:29 +00003634 *stroke_alpha=0.0;
3635 subpath_alpha=0.0;
cristy3ed852e2009-09-05 21:47:34 +00003636 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +00003637 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
cristy3ed852e2009-09-05 21:47:34 +00003638 {
cristye4e47ae2012-09-04 23:36:43 +00003639 if ((double) y <= (p->bounds.y1-mid-0.5))
cristy3ed852e2009-09-05 21:47:34 +00003640 break;
cristye4e47ae2012-09-04 23:36:43 +00003641 if ((double) y > (p->bounds.y2+mid+0.5))
cristy3ed852e2009-09-05 21:47:34 +00003642 {
cristybb503372010-05-27 20:51:26 +00003643 (void) DestroyEdge(polygon_info,(size_t) j);
cristy3ed852e2009-09-05 21:47:34 +00003644 continue;
3645 }
cristye4e47ae2012-09-04 23:36:43 +00003646 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3647 ((double) x > (p->bounds.x2+mid+0.5)))
cristy3ed852e2009-09-05 21:47:34 +00003648 continue;
cristybb503372010-05-27 20:51:26 +00003649 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3650 for ( ; i < (ssize_t) p->number_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00003651 {
cristye4e47ae2012-09-04 23:36:43 +00003652 if ((double) y <= (p->points[i-1].y-mid-0.5))
cristy3ed852e2009-09-05 21:47:34 +00003653 break;
cristye4e47ae2012-09-04 23:36:43 +00003654 if ((double) y > (p->points[i].y+mid+0.5))
cristy3ed852e2009-09-05 21:47:34 +00003655 continue;
cristye4e47ae2012-09-04 23:36:43 +00003656 if (p->scanline != (double) y)
cristy3ed852e2009-09-05 21:47:34 +00003657 {
cristye4e47ae2012-09-04 23:36:43 +00003658 p->scanline=(double) y;
cristybb503372010-05-27 20:51:26 +00003659 p->highwater=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +00003660 }
3661 /*
3662 Compute distance between a point and an edge.
3663 */
cristyb32b90a2009-09-07 21:45:48 +00003664 q=p->points+i-1;
3665 delta.x=(q+1)->x-q->x;
3666 delta.y=(q+1)->y-q->y;
3667 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
cristy3ed852e2009-09-05 21:47:34 +00003668 if (beta < 0.0)
3669 {
cristye4e47ae2012-09-04 23:36:43 +00003670 delta.x=(double) x-q->x;
3671 delta.y=(double) y-q->y;
cristy3ed852e2009-09-05 21:47:34 +00003672 distance=delta.x*delta.x+delta.y*delta.y;
3673 }
3674 else
3675 {
3676 alpha=delta.x*delta.x+delta.y*delta.y;
3677 if (beta > alpha)
3678 {
cristye4e47ae2012-09-04 23:36:43 +00003679 delta.x=(double) x-(q+1)->x;
3680 delta.y=(double) y-(q+1)->y;
cristy3ed852e2009-09-05 21:47:34 +00003681 distance=delta.x*delta.x+delta.y*delta.y;
3682 }
3683 else
3684 {
cristy592aefd2013-02-03 15:41:39 +00003685 alpha=1.0/alpha;
cristyb32b90a2009-09-07 21:45:48 +00003686 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
cristy4b67d752012-07-07 23:50:17 +00003687 distance=alpha*beta*beta;
cristy3ed852e2009-09-05 21:47:34 +00003688 }
3689 }
3690 /*
3691 Compute stroke & subpath opacity.
3692 */
3693 beta=0.0;
3694 if (p->ghostline == MagickFalse)
3695 {
cristyb32b90a2009-09-07 21:45:48 +00003696 alpha=mid+0.5;
cristyb2e6b992011-12-17 16:42:29 +00003697 if ((*stroke_alpha < 1.0) &&
cristy3ed852e2009-09-05 21:47:34 +00003698 (distance <= ((alpha+0.25)*(alpha+0.25))))
3699 {
3700 alpha=mid-0.5;
3701 if (distance <= ((alpha+0.25)*(alpha+0.25)))
cristyb2e6b992011-12-17 16:42:29 +00003702 *stroke_alpha=1.0;
cristy3ed852e2009-09-05 21:47:34 +00003703 else
3704 {
3705 beta=1.0;
3706 if (distance != 1.0)
3707 beta=sqrt((double) distance);
cristyb32b90a2009-09-07 21:45:48 +00003708 alpha=beta-mid-0.5;
cristyb2e6b992011-12-17 16:42:29 +00003709 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3710 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
cristy3ed852e2009-09-05 21:47:34 +00003711 }
3712 }
3713 }
cristyb2e6b992011-12-17 16:42:29 +00003714 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
cristy3ed852e2009-09-05 21:47:34 +00003715 continue;
3716 if (distance <= 0.0)
3717 {
cristyb2e6b992011-12-17 16:42:29 +00003718 subpath_alpha=1.0;
cristy3ed852e2009-09-05 21:47:34 +00003719 continue;
3720 }
3721 if (distance > 1.0)
3722 continue;
3723 if (beta == 0.0)
3724 {
3725 beta=1.0;
3726 if (distance != 1.0)
cristyb32b90a2009-09-07 21:45:48 +00003727 beta=sqrt(distance);
cristy3ed852e2009-09-05 21:47:34 +00003728 }
3729 alpha=beta-1.0;
cristyb2e6b992011-12-17 16:42:29 +00003730 if (subpath_alpha < (alpha*alpha))
3731 subpath_alpha=alpha*alpha;
cristy3ed852e2009-09-05 21:47:34 +00003732 }
cristy3ed852e2009-09-05 21:47:34 +00003733 }
3734 /*
3735 Compute fill opacity.
3736 */
3737 if (fill == MagickFalse)
3738 return(0.0);
cristyb2e6b992011-12-17 16:42:29 +00003739 if (subpath_alpha >= 1.0)
cristy3ed852e2009-09-05 21:47:34 +00003740 return(1.0);
cristyb32b90a2009-09-07 21:45:48 +00003741 /*
3742 Determine winding number.
3743 */
3744 winding_number=0;
3745 p=polygon_info->edges;
cristybb503372010-05-27 20:51:26 +00003746 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
cristyb32b90a2009-09-07 21:45:48 +00003747 {
cristye4e47ae2012-09-04 23:36:43 +00003748 if ((double) y <= p->bounds.y1)
cristyb32b90a2009-09-07 21:45:48 +00003749 break;
cristye4e47ae2012-09-04 23:36:43 +00003750 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
cristyb32b90a2009-09-07 21:45:48 +00003751 continue;
cristye4e47ae2012-09-04 23:36:43 +00003752 if ((double) x > p->bounds.x2)
cristyb32b90a2009-09-07 21:45:48 +00003753 {
3754 winding_number+=p->direction ? 1 : -1;
3755 continue;
3756 }
cristybb503372010-05-27 20:51:26 +00003757 i=(ssize_t) MagickMax((double) p->highwater,1.0);
3758 for ( ; i < (ssize_t) p->number_points; i++)
cristye4e47ae2012-09-04 23:36:43 +00003759 if ((double) y <= p->points[i].y)
cristyb32b90a2009-09-07 21:45:48 +00003760 break;
3761 q=p->points+i-1;
3762 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3763 winding_number+=p->direction ? 1 : -1;
3764 }
cristy3ed852e2009-09-05 21:47:34 +00003765 if (fill_rule != NonZeroRule)
3766 {
3767 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3768 return(1.0);
3769 }
3770 else
3771 if (MagickAbsoluteValue(winding_number) != 0)
3772 return(1.0);
cristyb2e6b992011-12-17 16:42:29 +00003773 return(subpath_alpha);
cristy3ed852e2009-09-05 21:47:34 +00003774}
3775
3776static MagickBooleanType DrawPolygonPrimitive(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00003777 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3778 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003779{
cristyfa112112010-01-04 17:48:07 +00003780 CacheView
3781 *image_view;
3782
cristy3ed852e2009-09-05 21:47:34 +00003783 MagickBooleanType
3784 fill,
3785 status;
3786
cristya19f1d72012-08-07 18:24:38 +00003787 double
cristy3ed852e2009-09-05 21:47:34 +00003788 mid;
3789
3790 PolygonInfo
cristyfa112112010-01-04 17:48:07 +00003791 **restrict polygon_info;
cristy3ed852e2009-09-05 21:47:34 +00003792
3793 register EdgeInfo
3794 *p;
3795
cristybb503372010-05-27 20:51:26 +00003796 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003797 i;
3798
3799 SegmentInfo
3800 bounds;
3801
cristy826a5472010-08-31 23:21:38 +00003802 ssize_t
3803 start,
3804 stop,
3805 y;
3806
cristy3ed852e2009-09-05 21:47:34 +00003807 /*
3808 Compute bounding box.
3809 */
3810 assert(image != (Image *) NULL);
3811 assert(image->signature == MagickSignature);
3812 if (image->debug != MagickFalse)
3813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3814 assert(draw_info != (DrawInfo *) NULL);
3815 assert(draw_info->signature == MagickSignature);
3816 assert(primitive_info != (PrimitiveInfo *) NULL);
3817 if (primitive_info->coordinates == 0)
3818 return(MagickTrue);
3819 polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3820 if (polygon_info == (PolygonInfo **) NULL)
3821 return(MagickFalse);
dirk93b02b72013-11-16 16:03:36 +00003822DisableMSCWarning(4127)
cristy3ed852e2009-09-05 21:47:34 +00003823 if (0)
cristy947cb4c2011-10-20 18:41:46 +00003824 DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
dirk93b02b72013-11-16 16:03:36 +00003825RestoreMSCWarning
cristy3ed852e2009-09-05 21:47:34 +00003826 if (image->debug != MagickFalse)
3827 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3828 fill=(primitive_info->method == FillToBorderMethod) ||
3829 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3830 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3831 bounds=polygon_info[0]->edges[0].bounds;
cristybb503372010-05-27 20:51:26 +00003832 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
cristy3ed852e2009-09-05 21:47:34 +00003833 {
3834 p=polygon_info[0]->edges+i;
3835 if (p->bounds.x1 < bounds.x1)
3836 bounds.x1=p->bounds.x1;
3837 if (p->bounds.y1 < bounds.y1)
3838 bounds.y1=p->bounds.y1;
3839 if (p->bounds.x2 > bounds.x2)
3840 bounds.x2=p->bounds.x2;
3841 if (p->bounds.y2 > bounds.y2)
3842 bounds.y2=p->bounds.y2;
3843 }
3844 bounds.x1-=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003845 bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
cristy6c5494a2012-09-22 00:30:37 +00003846 image->columns ? (double) image->columns-1 : bounds.x1;
cristy3ed852e2009-09-05 21:47:34 +00003847 bounds.y1-=(mid+1.0);
cristybb503372010-05-27 20:51:26 +00003848 bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
cristy6c5494a2012-09-22 00:30:37 +00003849 image->rows ? (double) image->rows-1 : bounds.y1;
cristy3ed852e2009-09-05 21:47:34 +00003850 bounds.x2+=(mid+1.0);
cristy8071c472012-09-24 12:41:06 +00003851 bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
cristy6c5494a2012-09-22 00:30:37 +00003852 image->columns ? (double) image->columns-1 : bounds.x2;
cristy3ed852e2009-09-05 21:47:34 +00003853 bounds.y2+=(mid+1.0);
cristy8071c472012-09-24 12:41:06 +00003854 bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
cristy6c5494a2012-09-22 00:30:37 +00003855 image->rows ? (double) image->rows-1 : bounds.y2;
cristy3ed852e2009-09-05 21:47:34 +00003856 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00003857 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003858 if (primitive_info->coordinates == 1)
3859 {
3860 /*
3861 Draw point.
3862 */
cristy564a5692012-01-20 23:56:26 +00003863 start=(ssize_t) ceil(bounds.y1-0.5);
cristy8071c472012-09-24 12:41:06 +00003864 stop=(ssize_t) floor(bounds.y2+0.5);
cristyb5d5f722009-11-04 03:03:49 +00003865#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00003866 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00003867 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003868#endif
cristy564a5692012-01-20 23:56:26 +00003869 for (y=start; y <= stop; y++)
cristy3ed852e2009-09-05 21:47:34 +00003870 {
3871 MagickBooleanType
3872 sync;
3873
cristy101ab702011-10-13 13:06:32 +00003874 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00003875 pixel;
3876
cristybb503372010-05-27 20:51:26 +00003877 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003878 x;
3879
cristy4c08aed2011-07-01 19:47:50 +00003880 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003881 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003882
cristy564a5692012-01-20 23:56:26 +00003883 ssize_t
3884 start,
3885 stop;
3886
cristy3ed852e2009-09-05 21:47:34 +00003887 if (status == MagickFalse)
3888 continue;
cristy564a5692012-01-20 23:56:26 +00003889 start=(ssize_t) ceil(bounds.x1-0.5);
cristy8071c472012-09-24 12:41:06 +00003890 stop=(ssize_t) floor(bounds.x2+0.5);
cristy3ed852e2009-09-05 21:47:34 +00003891 x=start;
cristyf707b302012-09-04 23:51:26 +00003892 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop-x+1),1,
3893 exception);
cristy4c08aed2011-07-01 19:47:50 +00003894 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003895 {
3896 status=MagickFalse;
3897 continue;
3898 }
cristy101ab702011-10-13 13:06:32 +00003899 GetPixelInfo(image,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00003900 for ( ; x <= stop; x++)
3901 {
cristybb503372010-05-27 20:51:26 +00003902 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3903 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
cristy4c08aed2011-07-01 19:47:50 +00003904 {
cristy2ed42f62011-10-02 19:49:57 +00003905 (void) GetStrokeColor(draw_info,x,y,&pixel,exception);
cristy803640d2011-11-17 02:11:32 +00003906 SetPixelInfoPixel(image,&pixel,q);
cristy4c08aed2011-07-01 19:47:50 +00003907 }
cristyed231572011-07-14 02:18:59 +00003908 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003909 }
3910 sync=SyncCacheViewAuthenticPixels(image_view,exception);
3911 if (sync == MagickFalse)
3912 status=MagickFalse;
3913 }
3914 image_view=DestroyCacheView(image_view);
3915 polygon_info=DestroyPolygonThreadSet(polygon_info);
3916 if (image->debug != MagickFalse)
3917 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
3918 " end draw-polygon");
3919 return(status);
3920 }
3921 /*
3922 Draw polygon or line.
3923 */
cristy8a46d822012-08-28 23:32:39 +00003924 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00003925 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy564a5692012-01-20 23:56:26 +00003926 start=(ssize_t) ceil(bounds.y1-0.5);
cristy8071c472012-09-24 12:41:06 +00003927 stop=(ssize_t) floor(bounds.y2+0.5);
cristyb5d5f722009-11-04 03:03:49 +00003928#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00003929 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00003930 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +00003931#endif
cristy564a5692012-01-20 23:56:26 +00003932 for (y=start; y <= stop; y++)
cristy3ed852e2009-09-05 21:47:34 +00003933 {
cristy5c9e6f22010-09-17 17:31:01 +00003934 const int
3935 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00003936
cristya19f1d72012-08-07 18:24:38 +00003937 double
cristyb2e6b992011-12-17 16:42:29 +00003938 fill_alpha,
3939 stroke_alpha;
cristy3ed852e2009-09-05 21:47:34 +00003940
cristy101ab702011-10-13 13:06:32 +00003941 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003942 fill_color,
3943 stroke_color;
3944
cristy4c08aed2011-07-01 19:47:50 +00003945 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003946 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003947
cristy826a5472010-08-31 23:21:38 +00003948 register ssize_t
3949 x;
3950
cristy564a5692012-01-20 23:56:26 +00003951 ssize_t
3952 start,
3953 stop;
3954
cristy3ed852e2009-09-05 21:47:34 +00003955 if (status == MagickFalse)
3956 continue;
cristy564a5692012-01-20 23:56:26 +00003957 start=(ssize_t) ceil(bounds.x1-0.5);
cristy8071c472012-09-24 12:41:06 +00003958 stop=(ssize_t) floor(bounds.x2+0.5);
cristyb2e6b992011-12-17 16:42:29 +00003959 q=GetCacheViewAuthenticPixels(image_view,start,y,(size_t) (stop-start+1),1,
3960 exception);
cristy4c08aed2011-07-01 19:47:50 +00003961 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003962 {
3963 status=MagickFalse;
3964 continue;
3965 }
cristy3ed852e2009-09-05 21:47:34 +00003966 for (x=start; x <= stop; x++)
3967 {
3968 /*
3969 Fill and/or stroke.
3970 */
cristyb2e6b992011-12-17 16:42:29 +00003971 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
cristye4e47ae2012-09-04 23:36:43 +00003972 x,y,&stroke_alpha);
cristy3ed852e2009-09-05 21:47:34 +00003973 if (draw_info->stroke_antialias == MagickFalse)
3974 {
cristyb2e6b992011-12-17 16:42:29 +00003975 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
3976 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
cristy3ed852e2009-09-05 21:47:34 +00003977 }
cristy2ed42f62011-10-02 19:49:57 +00003978 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristyb2e6b992011-12-17 16:42:29 +00003979 fill_alpha=fill_alpha*fill_color.alpha;
cristya19f1d72012-08-07 18:24:38 +00003980 CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
cristy4c08aed2011-07-01 19:47:50 +00003981 GetPixelAlpha(image,q),q);
cristy2ed42f62011-10-02 19:49:57 +00003982 (void) GetStrokeColor(draw_info,x,y,&stroke_color,exception);
cristyb2e6b992011-12-17 16:42:29 +00003983 stroke_alpha=stroke_alpha*stroke_color.alpha;
cristya19f1d72012-08-07 18:24:38 +00003984 CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
cristy4c08aed2011-07-01 19:47:50 +00003985 GetPixelAlpha(image,q),q);
cristyed231572011-07-14 02:18:59 +00003986 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003987 }
3988 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3989 status=MagickFalse;
3990 }
3991 image_view=DestroyCacheView(image_view);
3992 polygon_info=DestroyPolygonThreadSet(polygon_info);
3993 if (image->debug != MagickFalse)
3994 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
3995 return(status);
3996}
3997
3998/*
3999%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4000% %
4001% %
4002% %
4003% D r a w P r i m i t i v e %
4004% %
4005% %
4006% %
4007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4008%
4009% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4010%
4011% The format of the DrawPrimitive method is:
4012%
4013% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00004014% PrimitiveInfo *primitive_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004015%
4016% A description of each parameter follows:
4017%
4018% o image: the image.
4019%
4020% o draw_info: the draw info.
4021%
4022% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4023%
cristy947cb4c2011-10-20 18:41:46 +00004024% o exception: return any errors or warnings in this structure.
4025%
cristy3ed852e2009-09-05 21:47:34 +00004026*/
4027
4028static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4029{
4030 const char
4031 *methods[] =
4032 {
4033 "point",
4034 "replace",
4035 "floodfill",
4036 "filltoborder",
4037 "reset",
4038 "?"
4039 };
4040
cristy3ed852e2009-09-05 21:47:34 +00004041 PointInfo
4042 p,
4043 q,
4044 point;
4045
cristybb503372010-05-27 20:51:26 +00004046 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004047 i,
4048 x;
4049
cristy826a5472010-08-31 23:21:38 +00004050 ssize_t
4051 coordinates,
4052 y;
4053
cristybb503372010-05-27 20:51:26 +00004054 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4055 y=(ssize_t) ceil(primitive_info->point.y-0.5);
cristy3ed852e2009-09-05 21:47:34 +00004056 switch (primitive_info->primitive)
4057 {
4058 case PointPrimitive:
4059 {
4060 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004061 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004062 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004063 return;
4064 }
4065 case ColorPrimitive:
4066 {
4067 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004068 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004069 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004070 return;
4071 }
4072 case MattePrimitive:
4073 {
4074 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004075 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
cristyf2faecf2010-05-28 19:19:36 +00004076 methods[primitive_info->method]);
cristy3ed852e2009-09-05 21:47:34 +00004077 return;
4078 }
4079 case TextPrimitive:
4080 {
4081 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004082 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00004083 return;
4084 }
4085 case ImagePrimitive:
4086 {
4087 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004088 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
cristy3ed852e2009-09-05 21:47:34 +00004089 return;
4090 }
4091 default:
4092 break;
4093 }
4094 coordinates=0;
4095 p=primitive_info[0].point;
4096 q.x=(-1.0);
4097 q.y=(-1.0);
4098 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4099 {
4100 point=primitive_info[i].point;
4101 if (coordinates <= 0)
4102 {
cristybb503372010-05-27 20:51:26 +00004103 coordinates=(ssize_t) primitive_info[i].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00004104 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004105 " begin open (%.20g)",(double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004106 p=point;
4107 }
4108 point=primitive_info[i].point;
cristy53aed702012-07-08 00:21:25 +00004109 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4110 (fabs(q.y-point.y) >= MagickEpsilon))
cristy8cd5b312010-01-07 01:10:24 +00004111 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004112 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
cristy3ed852e2009-09-05 21:47:34 +00004113 else
4114 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +00004115 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
cristy3ed852e2009-09-05 21:47:34 +00004116 q=point;
4117 coordinates--;
4118 if (coordinates > 0)
4119 continue;
cristy53aed702012-07-08 00:21:25 +00004120 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4121 (fabs(p.y-point.y) >= MagickEpsilon))
cristye8c25f92010-06-03 00:53:06 +00004122 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4123 (double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004124 else
cristye8c25f92010-06-03 00:53:06 +00004125 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4126 (double) coordinates);
cristy3ed852e2009-09-05 21:47:34 +00004127 }
4128}
4129
4130MagickExport MagickBooleanType DrawPrimitive(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00004131 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4132 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004133{
cristyc4c8d132010-01-07 01:58:38 +00004134 CacheView
4135 *image_view;
4136
cristy3ed852e2009-09-05 21:47:34 +00004137 MagickStatusType
4138 status;
4139
cristybb503372010-05-27 20:51:26 +00004140 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004141 i,
4142 x;
4143
cristy826a5472010-08-31 23:21:38 +00004144 ssize_t
4145 y;
4146
cristy3ed852e2009-09-05 21:47:34 +00004147 if (image->debug != MagickFalse)
4148 {
4149 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4150 " begin draw-primitive");
4151 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
cristy14388de2011-05-15 14:57:16 +00004152 " affine: %g %g %g %g %g %g",draw_info->affine.sx,
cristy3ed852e2009-09-05 21:47:34 +00004153 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4154 draw_info->affine.tx,draw_info->affine.ty);
4155 }
cristya6400b12013-03-15 12:20:18 +00004156 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
cristy4dab3802013-03-15 22:08:15 +00004157 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4158 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
cristy0c81d062013-04-21 15:22:02 +00004159 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00004160 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +00004161 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4162 y=(ssize_t) ceil(primitive_info->point.y-0.5);
cristy46ff2672012-12-14 15:32:26 +00004163 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00004164 switch (primitive_info->primitive)
4165 {
4166 case PointPrimitive:
4167 {
cristy101ab702011-10-13 13:06:32 +00004168 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004169 fill_color;
4170
cristy4c08aed2011-07-01 19:47:50 +00004171 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004172 *q;
4173
cristybb503372010-05-27 20:51:26 +00004174 if ((y < 0) || (y >= (ssize_t) image->rows))
cristyb32b90a2009-09-07 21:45:48 +00004175 break;
cristybb503372010-05-27 20:51:26 +00004176 if ((x < 0) || (x >= (ssize_t) image->columns))
cristyb32b90a2009-09-07 21:45:48 +00004177 break;
cristy3ed852e2009-09-05 21:47:34 +00004178 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004179 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004180 break;
cristy2ed42f62011-10-02 19:49:57 +00004181 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristya19f1d72012-08-07 18:24:38 +00004182 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4183 (double) GetPixelAlpha(image,q),q);
cristy3ed852e2009-09-05 21:47:34 +00004184 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4185 break;
4186 }
4187 case ColorPrimitive:
4188 {
4189 switch (primitive_info->method)
4190 {
4191 case PointMethod:
4192 default:
4193 {
cristy101ab702011-10-13 13:06:32 +00004194 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00004195 pixel;
4196
4197 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004198 *q;
4199
4200 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004201 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004202 break;
cristy101ab702011-10-13 13:06:32 +00004203 GetPixelInfo(image,&pixel);
cristy2ed42f62011-10-02 19:49:57 +00004204 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristy803640d2011-11-17 02:11:32 +00004205 SetPixelInfoPixel(image,&pixel,q);
cristy3ed852e2009-09-05 21:47:34 +00004206 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4207 break;
4208 }
4209 case ReplaceMethod:
4210 {
4211 MagickBooleanType
4212 sync;
4213
cristy101ab702011-10-13 13:06:32 +00004214 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00004215 pixel,
cristy3ed852e2009-09-05 21:47:34 +00004216 target;
4217
cristyf05d4942012-03-17 16:26:09 +00004218 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
cristy2ed42f62011-10-02 19:49:57 +00004219 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 {
cristy101ab702011-10-13 13:06:32 +00004231 GetPixelInfoPixel(image,q,&pixel);
4232 if (IsFuzzyEquivalencePixelInfo(&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 }
cristy2ed42f62011-10-02 19:49:57 +00004237 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristy803640d2011-11-17 02:11:32 +00004238 SetPixelInfoPixel(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
cristy3aa93752011-12-18 15:54:24 +00004253 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
cristy52010022011-10-21 18:07:37 +00004254 &target,exception);
cristy3ed852e2009-09-05 21:47:34 +00004255 if (primitive_info->method == FillToBorderMethod)
4256 {
cristya19f1d72012-08-07 18:24:38 +00004257 target.red=(double) draw_info->border_color.red;
4258 target.green=(double) draw_info->border_color.green;
4259 target.blue=(double) draw_info->border_color.blue;
cristy3ed852e2009-09-05 21:47:34 +00004260 }
cristy8a77f142013-08-14 12:44:38 +00004261 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
cristyd42d9952011-07-08 14:21:50 +00004262 primitive_info->method == FloodfillMethod ? MagickFalse :
cristy189e84c2011-08-27 18:08:53 +00004263 MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00004264 break;
4265 }
4266 case ResetMethod:
4267 {
4268 MagickBooleanType
4269 sync;
4270
cristy101ab702011-10-13 13:06:32 +00004271 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +00004272 pixel;
4273
cristy101ab702011-10-13 13:06:32 +00004274 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00004275 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004276 {
cristy4c08aed2011-07-01 19:47:50 +00004277 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004278 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004279
cristy826a5472010-08-31 23:21:38 +00004280 register ssize_t
4281 x;
4282
cristy3ed852e2009-09-05 21:47:34 +00004283 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4284 exception);
cristy4c08aed2011-07-01 19:47:50 +00004285 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004286 break;
cristybb503372010-05-27 20:51:26 +00004287 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004288 {
cristy2ed42f62011-10-02 19:49:57 +00004289 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristy803640d2011-11-17 02:11:32 +00004290 SetPixelInfoPixel(image,&pixel,q);
cristyed231572011-07-14 02:18:59 +00004291 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004292 }
4293 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4294 if (sync == MagickFalse)
4295 break;
4296 }
4297 break;
4298 }
4299 }
4300 break;
4301 }
4302 case MattePrimitive:
4303 {
cristy8a46d822012-08-28 23:32:39 +00004304 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004305 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00004306 switch (primitive_info->method)
4307 {
4308 case PointMethod:
4309 default:
4310 {
cristy101ab702011-10-13 13:06:32 +00004311 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004312 pixel;
4313
cristy4c08aed2011-07-01 19:47:50 +00004314 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00004315 *q;
4316
4317 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004318 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004319 break;
cristy2ed42f62011-10-02 19:49:57 +00004320 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristyda1f9c12011-10-02 21:39:49 +00004321 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00004322 (void) SyncCacheViewAuthenticPixels(image_view,exception);
4323 break;
4324 }
4325 case ReplaceMethod:
4326 {
4327 MagickBooleanType
4328 sync;
4329
cristy101ab702011-10-13 13:06:32 +00004330 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004331 pixel,
4332 target;
4333
cristyf05d4942012-03-17 16:26:09 +00004334 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
cristy2ed42f62011-10-02 19:49:57 +00004335 exception);
cristybb503372010-05-27 20:51:26 +00004336 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004337 {
cristy4c08aed2011-07-01 19:47:50 +00004338 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004339 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004340
cristy826a5472010-08-31 23:21:38 +00004341 register ssize_t
4342 x;
4343
cristy3ed852e2009-09-05 21:47:34 +00004344 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4345 exception);
cristy4c08aed2011-07-01 19:47:50 +00004346 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004347 break;
cristybb503372010-05-27 20:51:26 +00004348 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004349 {
cristy101ab702011-10-13 13:06:32 +00004350 GetPixelInfoPixel(image,q,&pixel);
4351 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00004352 {
cristyed231572011-07-14 02:18:59 +00004353 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004354 continue;
4355 }
cristy2ed42f62011-10-02 19:49:57 +00004356 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristyda1f9c12011-10-02 21:39:49 +00004357 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004358 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004359 }
4360 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4361 if (sync == MagickFalse)
4362 break;
4363 }
4364 break;
4365 }
4366 case FloodfillMethod:
4367 case FillToBorderMethod:
4368 {
cristybd5a96c2011-08-21 00:04:26 +00004369 ChannelType
4370 channel_mask;
4371
cristy4c08aed2011-07-01 19:47:50 +00004372 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004373 target;
4374
cristy3aa93752011-12-18 15:54:24 +00004375 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
cristy52010022011-10-21 18:07:37 +00004376 &target,exception);
cristy3ed852e2009-09-05 21:47:34 +00004377 if (primitive_info->method == FillToBorderMethod)
4378 {
cristya19f1d72012-08-07 18:24:38 +00004379 target.red=(double) draw_info->border_color.red;
4380 target.green=(double) draw_info->border_color.green;
4381 target.blue=(double) draw_info->border_color.blue;
cristy3ed852e2009-09-05 21:47:34 +00004382 }
cristycf1296e2012-08-26 23:40:49 +00004383 channel_mask=SetImageChannelMask(image,AlphaChannel);
cristy8a77f142013-08-14 12:44:38 +00004384 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
cristy3ed852e2009-09-05 21:47:34 +00004385 primitive_info->method == FloodfillMethod ? MagickFalse :
cristy189e84c2011-08-27 18:08:53 +00004386 MagickTrue,exception);
cristycf1296e2012-08-26 23:40:49 +00004387 (void) SetImageChannelMask(image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +00004388 break;
4389 }
4390 case ResetMethod:
4391 {
4392 MagickBooleanType
4393 sync;
4394
cristy101ab702011-10-13 13:06:32 +00004395 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004396 pixel;
4397
cristybb503372010-05-27 20:51:26 +00004398 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004399 {
cristy4c08aed2011-07-01 19:47:50 +00004400 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00004401 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00004402
cristy826a5472010-08-31 23:21:38 +00004403 register ssize_t
4404 x;
4405
cristy3ed852e2009-09-05 21:47:34 +00004406 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4407 exception);
cristy4c08aed2011-07-01 19:47:50 +00004408 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004409 break;
cristybb503372010-05-27 20:51:26 +00004410 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00004411 {
cristy2ed42f62011-10-02 19:49:57 +00004412 (void) GetFillColor(draw_info,x,y,&pixel,exception);
cristyda1f9c12011-10-02 21:39:49 +00004413 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00004414 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00004415 }
4416 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4417 if (sync == MagickFalse)
4418 break;
4419 }
4420 break;
4421 }
4422 }
4423 break;
4424 }
4425 case TextPrimitive:
4426 {
4427 char
4428 geometry[MaxTextExtent];
4429
4430 DrawInfo
4431 *clone_info;
4432
4433 if (primitive_info->text == (char *) NULL)
4434 break;
4435 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4436 (void) CloneString(&clone_info->text,primitive_info->text);
cristyb51dff52011-05-19 16:55:47 +00004437 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
cristy3ed852e2009-09-05 21:47:34 +00004438 primitive_info->point.x,primitive_info->point.y);
4439 (void) CloneString(&clone_info->geometry,geometry);
cristy8a77f142013-08-14 12:44:38 +00004440 status&=AnnotateImage(image,clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004441 clone_info=DestroyDrawInfo(clone_info);
4442 break;
4443 }
4444 case ImagePrimitive:
4445 {
4446 AffineMatrix
4447 affine;
4448
4449 char
4450 composite_geometry[MaxTextExtent];
4451
4452 Image
4453 *composite_image;
4454
4455 ImageInfo
4456 *clone_info;
4457
cristy826a5472010-08-31 23:21:38 +00004458 RectangleInfo
4459 geometry;
4460
cristybb503372010-05-27 20:51:26 +00004461 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004462 x1,
4463 y1;
4464
cristy3ed852e2009-09-05 21:47:34 +00004465 if (primitive_info->text == (char *) NULL)
4466 break;
4467 clone_info=AcquireImageInfo();
4468 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4469 composite_image=ReadInlineImage(clone_info,primitive_info->text,
cristy947cb4c2011-10-20 18:41:46 +00004470 exception);
cristy3ed852e2009-09-05 21:47:34 +00004471 else
4472 {
4473 (void) CopyMagickString(clone_info->filename,primitive_info->text,
4474 MaxTextExtent);
cristy947cb4c2011-10-20 18:41:46 +00004475 composite_image=ReadImage(clone_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004476 }
4477 clone_info=DestroyImageInfo(clone_info);
4478 if (composite_image == (Image *) NULL)
4479 break;
4480 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4481 NULL,(void *) NULL);
cristybb503372010-05-27 20:51:26 +00004482 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4483 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4484 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4485 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
cristy3ed852e2009-09-05 21:47:34 +00004486 {
4487 char
4488 geometry[MaxTextExtent];
4489
4490 /*
4491 Resize image.
4492 */
cristyb51dff52011-05-19 16:55:47 +00004493 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
cristy3ed852e2009-09-05 21:47:34 +00004494 primitive_info[1].point.x,primitive_info[1].point.y);
4495 composite_image->filter=image->filter;
cristye941a752011-10-15 01:52:48 +00004496 (void) TransformImage(&composite_image,(char *) NULL,geometry,
4497 exception);
cristy3ed852e2009-09-05 21:47:34 +00004498 }
cristy8a46d822012-08-28 23:32:39 +00004499 if (composite_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00004500 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4501 exception);
cristy4c08aed2011-07-01 19:47:50 +00004502 if (draw_info->alpha != OpaqueAlpha)
cristye941a752011-10-15 01:52:48 +00004503 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
cristy3ed852e2009-09-05 21:47:34 +00004504 SetGeometry(image,&geometry);
4505 image->gravity=draw_info->gravity;
4506 geometry.x=x;
4507 geometry.y=y;
cristyb51dff52011-05-19 16:55:47 +00004508 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00004509 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
cristye8c25f92010-06-03 00:53:06 +00004510 composite_image->rows,(double) geometry.x,(double) geometry.y);
cristy947cb4c2011-10-20 18:41:46 +00004511 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
cristy3ed852e2009-09-05 21:47:34 +00004512 affine=draw_info->affine;
4513 affine.tx=(double) geometry.x;
4514 affine.ty=(double) geometry.y;
4515 composite_image->interpolate=image->interpolate;
cristyd5f3fc32011-04-26 14:44:36 +00004516 if (draw_info->compose == OverCompositeOp)
cristy947cb4c2011-10-20 18:41:46 +00004517 (void) DrawAffineImage(image,composite_image,&affine,exception);
cristyd5f3fc32011-04-26 14:44:36 +00004518 else
cristyfeb3e962012-03-29 17:25:55 +00004519 (void) CompositeImage(image,composite_image,draw_info->compose,
cristy39172402012-03-30 13:04:39 +00004520 MagickTrue,geometry.x,geometry.y,exception);
cristy3ed852e2009-09-05 21:47:34 +00004521 composite_image=DestroyImage(composite_image);
4522 break;
4523 }
4524 default:
4525 {
cristya19f1d72012-08-07 18:24:38 +00004526 double
cristy3ed852e2009-09-05 21:47:34 +00004527 mid,
4528 scale;
4529
4530 DrawInfo
4531 *clone_info;
4532
4533 if (IsEventLogging() != MagickFalse)
4534 LogPrimitiveInfo(primitive_info);
4535 scale=ExpandAffine(&draw_info->affine);
4536 if ((draw_info->dash_pattern != (double *) NULL) &&
4537 (draw_info->dash_pattern[0] != 0.0) &&
cristy53aed702012-07-08 00:21:25 +00004538 ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
cristy4c08aed2011-07-01 19:47:50 +00004539 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00004540 {
4541 /*
4542 Draw dash polygon.
4543 */
4544 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4545 clone_info->stroke_width=0.0;
cristy4c08aed2011-07-01 19:47:50 +00004546 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy8a77f142013-08-14 12:44:38 +00004547 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
cristy947cb4c2011-10-20 18:41:46 +00004548 exception);
cristy3ed852e2009-09-05 21:47:34 +00004549 clone_info=DestroyDrawInfo(clone_info);
cristy947cb4c2011-10-20 18:41:46 +00004550 (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00004551 break;
4552 }
4553 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4554 if ((mid > 1.0) &&
cristy4c08aed2011-07-01 19:47:50 +00004555 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00004556 {
4557 MagickBooleanType
4558 closed_path;
4559
4560 /*
4561 Draw strokes while respecting line cap/join attributes.
4562 */
4563 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4564 closed_path=
4565 (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4566 (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4567 MagickTrue : MagickFalse;
cristybb503372010-05-27 20:51:26 +00004568 i=(ssize_t) primitive_info[0].coordinates;
cristy3ed852e2009-09-05 21:47:34 +00004569 if ((((draw_info->linecap == RoundCap) ||
4570 (closed_path != MagickFalse)) &&
4571 (draw_info->linejoin == RoundJoin)) ||
4572 (primitive_info[i].primitive != UndefinedPrimitive))
4573 {
cristy947cb4c2011-10-20 18:41:46 +00004574 (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4575 exception);
cristy3ed852e2009-09-05 21:47:34 +00004576 break;
4577 }
4578 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4579 clone_info->stroke_width=0.0;
cristy4c08aed2011-07-01 19:47:50 +00004580 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy8a77f142013-08-14 12:44:38 +00004581 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
cristy947cb4c2011-10-20 18:41:46 +00004582 exception);
cristy3ed852e2009-09-05 21:47:34 +00004583 clone_info=DestroyDrawInfo(clone_info);
cristy8a77f142013-08-14 12:44:38 +00004584 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004585 break;
4586 }
cristy8a77f142013-08-14 12:44:38 +00004587 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004588 break;
4589 }
4590 }
4591 image_view=DestroyCacheView(image_view);
4592 if (image->debug != MagickFalse)
4593 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4594 return(status != 0 ? MagickTrue : MagickFalse);
4595}
4596
4597/*
4598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4599% %
4600% %
4601% %
4602+ D r a w S t r o k e P o l y g o n %
4603% %
4604% %
4605% %
4606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4607%
4608% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4609% the image while respecting the line cap and join attributes.
4610%
4611% The format of the DrawStrokePolygon method is:
4612%
4613% MagickBooleanType DrawStrokePolygon(Image *image,
4614% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4615%
4616% A description of each parameter follows:
4617%
4618% o image: the image.
4619%
4620% o draw_info: the draw info.
4621%
4622% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4623%
4624%
4625*/
4626
4627static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
cristy947cb4c2011-10-20 18:41:46 +00004628 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004629{
4630 PrimitiveInfo
4631 linecap[5];
4632
cristybb503372010-05-27 20:51:26 +00004633 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004634 i;
4635
4636 for (i=0; i < 4; i++)
4637 linecap[i]=(*primitive_info);
4638 linecap[0].coordinates=4;
cristy53aed702012-07-08 00:21:25 +00004639 linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4640 linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4641 linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4642 linecap[3].point.y+=(double) (10.0*MagickEpsilon);
cristy3ed852e2009-09-05 21:47:34 +00004643 linecap[4].primitive=UndefinedPrimitive;
cristy947cb4c2011-10-20 18:41:46 +00004644 (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
cristy3ed852e2009-09-05 21:47:34 +00004645}
4646
4647static MagickBooleanType DrawStrokePolygon(Image *image,
cristy947cb4c2011-10-20 18:41:46 +00004648 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4649 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00004650{
4651 DrawInfo
4652 *clone_info;
4653
4654 MagickBooleanType
cristy8a77f142013-08-14 12:44:38 +00004655 closed_path;
4656
4657 MagickStatusType
cristy3ed852e2009-09-05 21:47:34 +00004658 status;
4659
4660 PrimitiveInfo
4661 *stroke_polygon;
4662
4663 register const PrimitiveInfo
4664 *p,
4665 *q;
4666
4667 /*
4668 Draw stroked polygon.
4669 */
4670 if (image->debug != MagickFalse)
4671 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4672 " begin draw-stroke-polygon");
4673 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4674 clone_info->fill=draw_info->stroke;
cristy2d2d5622012-01-19 19:11:29 +00004675 if (clone_info->fill_pattern != (Image *) NULL)
4676 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
cristy2195b752012-01-19 16:04:04 +00004677 if (clone_info->stroke_pattern != (Image *) NULL)
cristy2d2d5622012-01-19 19:11:29 +00004678 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4679 MagickTrue,exception);
cristy4c08aed2011-07-01 19:47:50 +00004680 clone_info->stroke.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004681 clone_info->stroke_width=0.0;
4682 clone_info->fill_rule=NonZeroRule;
4683 status=MagickTrue;
4684 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4685 {
4686 stroke_polygon=TraceStrokePolygon(draw_info,p);
cristy8a77f142013-08-14 12:44:38 +00004687 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4688 if (status == 0)
4689 break;
cristy3ed852e2009-09-05 21:47:34 +00004690 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4691 q=p+p->coordinates-1;
4692 closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4693 MagickTrue : MagickFalse;
4694 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4695 {
cristy947cb4c2011-10-20 18:41:46 +00004696 DrawRoundLinecap(image,draw_info,p,exception);
4697 DrawRoundLinecap(image,draw_info,q,exception);
cristy3ed852e2009-09-05 21:47:34 +00004698 }
4699 }
4700 clone_info=DestroyDrawInfo(clone_info);
4701 if (image->debug != MagickFalse)
4702 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4703 " end draw-stroke-polygon");
cristy8a77f142013-08-14 12:44:38 +00004704 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00004705}
4706
4707/*
4708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4709% %
4710% %
4711% %
4712% G e t A f f i n e M a t r i x %
4713% %
4714% %
4715% %
4716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4717%
4718% GetAffineMatrix() returns an AffineMatrix initialized to the identity
4719% matrix.
4720%
4721% The format of the GetAffineMatrix method is:
4722%
4723% void GetAffineMatrix(AffineMatrix *affine_matrix)
4724%
4725% A description of each parameter follows:
4726%
4727% o affine_matrix: the affine matrix.
4728%
4729*/
4730MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
4731{
4732 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4733 assert(affine_matrix != (AffineMatrix *) NULL);
4734 (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4735 affine_matrix->sx=1.0;
4736 affine_matrix->sy=1.0;
4737}
4738
4739/*
4740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4741% %
4742% %
4743% %
4744+ G e t D r a w I n f o %
4745% %
4746% %
4747% %
4748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4749%
anthony9c88e8f2011-09-29 11:31:53 +00004750% GetDrawInfo() initializes draw_info to default values from image_info.
cristy3ed852e2009-09-05 21:47:34 +00004751%
4752% The format of the GetDrawInfo method is:
4753%
4754% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4755%
4756% A description of each parameter follows:
4757%
4758% o image_info: the image info..
4759%
4760% o draw_info: the draw info.
4761%
4762*/
4763MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4764{
4765 const char
4766 *option;
4767
4768 ExceptionInfo
4769 *exception;
4770
cristy3ed852e2009-09-05 21:47:34 +00004771 /*
4772 Initialize draw attributes.
4773 */
4774 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4775 assert(draw_info != (DrawInfo *) NULL);
4776 (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
cristy3ed852e2009-09-05 21:47:34 +00004777 GetAffineMatrix(&draw_info->affine);
4778 exception=AcquireExceptionInfo();
cristyfad60c92012-01-19 18:32:39 +00004779 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
cristy9950d572011-10-01 18:22:35 +00004780 exception);
dirke191b322013-09-23 23:37:07 +00004781 (void) QueryColorCompliance("#FFFF",AllCompliance,&draw_info->stroke,
cristy9950d572011-10-01 18:22:35 +00004782 exception);
cristy3ed852e2009-09-05 21:47:34 +00004783 draw_info->stroke_width=1.0;
cristy4c08aed2011-07-01 19:47:50 +00004784 draw_info->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004785 draw_info->fill_rule=EvenOddRule;
4786 draw_info->linecap=ButtCap;
4787 draw_info->linejoin=MiterJoin;
4788 draw_info->miterlimit=10;
4789 draw_info->decorate=NoDecoration;
cristy3ed852e2009-09-05 21:47:34 +00004790 draw_info->pointsize=12.0;
cristy4c08aed2011-07-01 19:47:50 +00004791 draw_info->undercolor.alpha=(Quantum) TransparentAlpha;
cristy3ed852e2009-09-05 21:47:34 +00004792 draw_info->compose=OverCompositeOp;
cristy3ed852e2009-09-05 21:47:34 +00004793 draw_info->render=MagickTrue;
4794 draw_info->debug=IsEventLogging();
cristy0245b5d2011-10-05 12:03:49 +00004795 if (image_info != (ImageInfo *) NULL)
4796 {
4797 draw_info->stroke_antialias=image_info->antialias;
4798 if (image_info->font != (char *) NULL)
4799 draw_info->font=AcquireString(image_info->font);
4800 if (image_info->density != (char *) NULL)
4801 draw_info->density=AcquireString(image_info->density);
4802 draw_info->text_antialias=image_info->antialias;
4803 if (image_info->pointsize != 0.0)
4804 draw_info->pointsize=image_info->pointsize;
4805 draw_info->border_color=image_info->border_color;
4806 if (image_info->server_name != (char *) NULL)
4807 draw_info->server_name=AcquireString(image_info->server_name);
4808 option=GetImageOption(image_info,"encoding");
4809 if (option != (const char *) NULL)
4810 (void) CloneString(&draw_info->encoding,option);
4811 option=GetImageOption(image_info,"kerning");
4812 if (option != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00004813 draw_info->kerning=StringToDouble(option,(char **) NULL);
cristy0245b5d2011-10-05 12:03:49 +00004814 option=GetImageOption(image_info,"interline-spacing");
4815 if (option != (const char *) NULL)
cristy9b34e302011-11-05 02:15:45 +00004816 draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
cristy0245b5d2011-10-05 12:03:49 +00004817 option=GetImageOption(image_info,"interword-spacing");
4818 if (option != (const char *) NULL)
cristy9b34e302011-11-05 02:15:45 +00004819 draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
cristy0245b5d2011-10-05 12:03:49 +00004820 option=GetImageOption(image_info,"direction");
4821 if (option != (const char *) NULL)
4822 draw_info->direction=(DirectionType) ParseCommandOption(
4823 MagickDirectionOptions,MagickFalse,option);
anthony42f6c202011-10-23 10:28:55 +00004824 else
4825 draw_info->direction=UndefinedDirection;
cristy0245b5d2011-10-05 12:03:49 +00004826 option=GetImageOption(image_info,"fill");
4827 if (option != (const char *) NULL)
4828 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
4829 exception);
4830 option=GetImageOption(image_info,"stroke");
4831 if (option != (const char *) NULL)
4832 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
4833 exception);
4834 option=GetImageOption(image_info,"strokewidth");
4835 if (option != (const char *) NULL)
cristydbdd0e32011-11-04 23:29:40 +00004836 draw_info->stroke_width=StringToDouble(option,(char **) NULL);
cristy0245b5d2011-10-05 12:03:49 +00004837 option=GetImageOption(image_info,"undercolor");
4838 if (option != (const char *) NULL)
4839 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
4840 exception);
4841 option=GetImageOption(image_info,"gravity");
4842 if (option != (const char *) NULL)
4843 draw_info->gravity=(GravityType) ParseCommandOption(
4844 MagickGravityOptions,MagickFalse,option);
4845 }
cristy3ed852e2009-09-05 21:47:34 +00004846 exception=DestroyExceptionInfo(exception);
4847 draw_info->signature=MagickSignature;
cristy3ed852e2009-09-05 21:47:34 +00004848}
4849
4850/*
4851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4852% %
4853% %
4854% %
4855+ P e r m u t a t e %
4856% %
4857% %
4858% %
4859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4860%
4861% Permutate() returns the permuation of the (n,k).
4862%
4863% The format of the Permutate method is:
4864%
cristybb503372010-05-27 20:51:26 +00004865% void Permutate(ssize_t n,ssize_t k)
cristy3ed852e2009-09-05 21:47:34 +00004866%
4867% A description of each parameter follows:
4868%
4869% o n:
4870%
4871% o k:
4872%
4873%
4874*/
cristya19f1d72012-08-07 18:24:38 +00004875static inline double Permutate(const ssize_t n,const ssize_t k)
cristy3ed852e2009-09-05 21:47:34 +00004876{
cristya19f1d72012-08-07 18:24:38 +00004877 double
cristy3ed852e2009-09-05 21:47:34 +00004878 r;
4879
cristybb503372010-05-27 20:51:26 +00004880 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004881 i;
4882
4883 r=1.0;
4884 for (i=k+1; i <= n; i++)
4885 r*=i;
4886 for (i=1; i <= (n-k); i++)
4887 r/=i;
4888 return(r);
4889}
4890
4891/*
4892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4893% %
4894% %
4895% %
4896+ T r a c e P r i m i t i v e %
4897% %
4898% %
4899% %
4900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4901%
4902% TracePrimitive is a collection of methods for generating graphic
4903% primitives such as arcs, ellipses, paths, etc.
4904%
4905*/
4906
4907static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4908 const PointInfo end,const PointInfo degrees)
4909{
4910 PointInfo
4911 center,
4912 radii;
4913
4914 center.x=0.5*(end.x+start.x);
4915 center.y=0.5*(end.y+start.y);
4916 radii.x=fabs(center.x-start.x);
4917 radii.y=fabs(center.y-start.y);
4918 TraceEllipse(primitive_info,center,radii,degrees);
4919}
4920
4921static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
cristya19f1d72012-08-07 18:24:38 +00004922 const PointInfo end,const PointInfo arc,const double angle,
cristy3ed852e2009-09-05 21:47:34 +00004923 const MagickBooleanType large_arc,const MagickBooleanType sweep)
4924{
cristya19f1d72012-08-07 18:24:38 +00004925 double
cristy3ed852e2009-09-05 21:47:34 +00004926 alpha,
4927 beta,
4928 delta,
4929 factor,
4930 gamma,
4931 theta;
4932
4933 PointInfo
4934 center,
4935 points[3],
4936 radii;
4937
cristya19f1d72012-08-07 18:24:38 +00004938 register double
cristy3ed852e2009-09-05 21:47:34 +00004939 cosine,
4940 sine;
4941
4942 register PrimitiveInfo
4943 *p;
4944
cristybb503372010-05-27 20:51:26 +00004945 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004946 i;
4947
cristybb503372010-05-27 20:51:26 +00004948 size_t
cristy3ed852e2009-09-05 21:47:34 +00004949 arc_segments;
4950
4951 if ((start.x == end.x) && (start.y == end.y))
4952 {
4953 TracePoint(primitive_info,end);
4954 return;
4955 }
4956 radii.x=fabs(arc.x);
4957 radii.y=fabs(arc.y);
4958 if ((radii.x == 0.0) || (radii.y == 0.0))
4959 {
4960 TraceLine(primitive_info,start,end);
4961 return;
4962 }
4963 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
4964 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
4965 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
4966 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
4967 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
4968 (radii.y*radii.y);
cristy53aed702012-07-08 00:21:25 +00004969 if (delta < MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00004970 {
4971 TraceLine(primitive_info,start,end);
4972 return;
4973 }
4974 if (delta > 1.0)
4975 {
4976 radii.x*=sqrt((double) delta);
4977 radii.y*=sqrt((double) delta);
4978 }
4979 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
4980 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
4981 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
4982 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
4983 alpha=points[1].x-points[0].x;
4984 beta=points[1].y-points[0].y;
cristy3e3ec3a2012-11-03 23:11:06 +00004985 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
cristy3ed852e2009-09-05 21:47:34 +00004986 if (factor <= 0.0)
4987 factor=0.0;
4988 else
4989 {
4990 factor=sqrt((double) factor);
4991 if (sweep == large_arc)
4992 factor=(-factor);
4993 }
4994 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
4995 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
4996 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
4997 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
4998 if ((theta < 0.0) && (sweep != MagickFalse))
cristya19f1d72012-08-07 18:24:38 +00004999 theta+=(double) (2.0*MagickPI);
cristy3ed852e2009-09-05 21:47:34 +00005000 else
5001 if ((theta > 0.0) && (sweep == MagickFalse))
cristya19f1d72012-08-07 18:24:38 +00005002 theta-=(double) (2.0*MagickPI);
cristybb503372010-05-27 20:51:26 +00005003 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
cristy53aed702012-07-08 00:21:25 +00005004 MagickEpsilon))));
cristy3ed852e2009-09-05 21:47:34 +00005005 p=primitive_info;
cristybb503372010-05-27 20:51:26 +00005006 for (i=0; i < (ssize_t) arc_segments; i++)
cristy3ed852e2009-09-05 21:47:34 +00005007 {
5008 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5009 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5010 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5011 sin(fmod((double) beta,DegreesToRadians(360.0)));
5012 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5013 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5014 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5015 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5016 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5017 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5018 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5019 theta/arc_segments),DegreesToRadians(360.0))));
5020 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5021 theta/arc_segments),DegreesToRadians(360.0))));
5022 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5023 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5024 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5025 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5026 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5027 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5028 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5029 points[0].y);
5030 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5031 points[0].y);
5032 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5033 points[1].y);
5034 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5035 points[1].y);
5036 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5037 points[2].y);
5038 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5039 points[2].y);
cristybb503372010-05-27 20:51:26 +00005040 if (i == (ssize_t) (arc_segments-1))
cristy3ed852e2009-09-05 21:47:34 +00005041 (p+3)->point=end;
5042 TraceBezier(p,4);
5043 p+=p->coordinates;
5044 }
cristybb503372010-05-27 20:51:26 +00005045 primitive_info->coordinates=(size_t) (p-primitive_info);
5046 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005047 {
5048 p->primitive=primitive_info->primitive;
5049 p--;
5050 }
5051}
5052
5053static void TraceBezier(PrimitiveInfo *primitive_info,
cristybb503372010-05-27 20:51:26 +00005054 const size_t number_coordinates)
cristy3ed852e2009-09-05 21:47:34 +00005055{
cristya19f1d72012-08-07 18:24:38 +00005056 double
cristy3ed852e2009-09-05 21:47:34 +00005057 alpha,
5058 *coefficients,
5059 weight;
5060
5061 PointInfo
5062 end,
5063 point,
5064 *points;
5065
cristy826a5472010-08-31 23:21:38 +00005066 register PrimitiveInfo
5067 *p;
5068
cristybb503372010-05-27 20:51:26 +00005069 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005070 i,
5071 j;
5072
cristybb503372010-05-27 20:51:26 +00005073 size_t
cristy3ed852e2009-09-05 21:47:34 +00005074 control_points,
5075 quantum;
5076
5077 /*
5078 Allocate coeficients.
5079 */
5080 quantum=number_coordinates;
cristybb503372010-05-27 20:51:26 +00005081 for (i=0; i < (ssize_t) number_coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005082 {
cristybb503372010-05-27 20:51:26 +00005083 for (j=i+1; j < (ssize_t) number_coordinates; j++)
cristy3ed852e2009-09-05 21:47:34 +00005084 {
5085 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
cristya19f1d72012-08-07 18:24:38 +00005086 if (alpha > (double) quantum)
cristybb503372010-05-27 20:51:26 +00005087 quantum=(size_t) alpha;
cristy3ed852e2009-09-05 21:47:34 +00005088 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
cristya19f1d72012-08-07 18:24:38 +00005089 if (alpha > (double) quantum)
cristybb503372010-05-27 20:51:26 +00005090 quantum=(size_t) alpha;
cristy3ed852e2009-09-05 21:47:34 +00005091 }
5092 }
cristybb503372010-05-27 20:51:26 +00005093 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
cristy3ed852e2009-09-05 21:47:34 +00005094 (double) BezierQuantum);
5095 control_points=quantum*number_coordinates;
cristya19f1d72012-08-07 18:24:38 +00005096 coefficients=(double *) AcquireQuantumMemory((size_t)
cristy3ed852e2009-09-05 21:47:34 +00005097 number_coordinates,sizeof(*coefficients));
5098 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5099 sizeof(*points));
cristya19f1d72012-08-07 18:24:38 +00005100 if ((coefficients == (double *) NULL) ||
cristy3ed852e2009-09-05 21:47:34 +00005101 (points == (PointInfo *) NULL))
5102 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5103 /*
5104 Compute bezier points.
5105 */
5106 end=primitive_info[number_coordinates-1].point;
cristybb503372010-05-27 20:51:26 +00005107 for (i=0; i < (ssize_t) number_coordinates; i++)
5108 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
cristy3ed852e2009-09-05 21:47:34 +00005109 weight=0.0;
cristybb503372010-05-27 20:51:26 +00005110 for (i=0; i < (ssize_t) control_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00005111 {
5112 p=primitive_info;
5113 point.x=0.0;
5114 point.y=0.0;
5115 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
cristybb503372010-05-27 20:51:26 +00005116 for (j=0; j < (ssize_t) number_coordinates; j++)
cristy3ed852e2009-09-05 21:47:34 +00005117 {
5118 point.x+=alpha*coefficients[j]*p->point.x;
5119 point.y+=alpha*coefficients[j]*p->point.y;
5120 alpha*=weight/(1.0-weight);
5121 p++;
5122 }
5123 points[i]=point;
5124 weight+=1.0/control_points;
5125 }
5126 /*
5127 Bezier curves are just short segmented polys.
5128 */
5129 p=primitive_info;
cristybb503372010-05-27 20:51:26 +00005130 for (i=0; i < (ssize_t) control_points; i++)
cristy3ed852e2009-09-05 21:47:34 +00005131 {
5132 TracePoint(p,points[i]);
5133 p+=p->coordinates;
5134 }
5135 TracePoint(p,end);
5136 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005137 primitive_info->coordinates=(size_t) (p-primitive_info);
5138 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005139 {
5140 p->primitive=primitive_info->primitive;
5141 p--;
5142 }
5143 points=(PointInfo *) RelinquishMagickMemory(points);
cristya19f1d72012-08-07 18:24:38 +00005144 coefficients=(double *) RelinquishMagickMemory(coefficients);
cristy3ed852e2009-09-05 21:47:34 +00005145}
5146
5147static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5148 const PointInfo end)
5149{
cristya19f1d72012-08-07 18:24:38 +00005150 double
cristy3ed852e2009-09-05 21:47:34 +00005151 alpha,
5152 beta,
5153 radius;
5154
5155 PointInfo
5156 offset,
5157 degrees;
5158
5159 alpha=end.x-start.x;
5160 beta=end.y-start.y;
5161 radius=hypot((double) alpha,(double) beta);
5162 offset.x=(double) radius;
5163 offset.y=(double) radius;
5164 degrees.x=0.0;
5165 degrees.y=360.0;
5166 TraceEllipse(primitive_info,start,offset,degrees);
5167}
5168
5169static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5170 const PointInfo stop,const PointInfo degrees)
5171{
cristya19f1d72012-08-07 18:24:38 +00005172 double
cristy3ed852e2009-09-05 21:47:34 +00005173 delta,
5174 step,
5175 y;
5176
5177 PointInfo
5178 angle,
5179 point;
5180
5181 register PrimitiveInfo
5182 *p;
5183
cristybb503372010-05-27 20:51:26 +00005184 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005185 i;
5186
5187 /*
5188 Ellipses are just short segmented polys.
5189 */
5190 if ((stop.x == 0.0) && (stop.y == 0.0))
5191 {
5192 TracePoint(primitive_info,start);
5193 return;
5194 }
5195 delta=2.0/MagickMax(stop.x,stop.y);
cristya19f1d72012-08-07 18:24:38 +00005196 step=(double) (MagickPI/8.0);
5197 if ((delta >= 0.0) && (delta < (double) (MagickPI/8.0)))
5198 step=(double) (MagickPI/(4*(MagickPI/delta/2+0.5)));
cristy3ed852e2009-09-05 21:47:34 +00005199 angle.x=DegreesToRadians(degrees.x);
5200 y=degrees.y;
5201 while (y < degrees.x)
5202 y+=360.0;
cristya855f3d2012-12-01 13:02:13 +00005203 angle.y=(double) DegreesToRadians(y);
cristy3ed852e2009-09-05 21:47:34 +00005204 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5205 {
5206 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5207 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5208 TracePoint(p,point);
5209 p+=p->coordinates;
5210 }
5211 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5212 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5213 TracePoint(p,point);
5214 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005215 primitive_info->coordinates=(size_t) (p-primitive_info);
5216 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005217 {
5218 p->primitive=primitive_info->primitive;
5219 p--;
5220 }
5221}
5222
5223static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5224 const PointInfo end)
5225{
5226 TracePoint(primitive_info,start);
cristy53aed702012-07-08 00:21:25 +00005227 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5228 (fabs(start.y-end.y) < MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +00005229 {
5230 primitive_info->primitive=PointPrimitive;
5231 primitive_info->coordinates=1;
5232 return;
5233 }
5234 TracePoint(primitive_info+1,end);
5235 (primitive_info+1)->primitive=primitive_info->primitive;
5236 primitive_info->coordinates=2;
5237}
5238
cristybb503372010-05-27 20:51:26 +00005239static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
cristy3ed852e2009-09-05 21:47:34 +00005240{
5241 char
5242 token[MaxTextExtent];
5243
5244 const char
5245 *p;
5246
5247 int
5248 attribute,
5249 last_attribute;
5250
cristya19f1d72012-08-07 18:24:38 +00005251 double
cristy3ed852e2009-09-05 21:47:34 +00005252 x,
5253 y;
5254
5255 PointInfo
5256 end,
5257 points[4],
5258 point,
5259 start;
5260
5261 PrimitiveType
5262 primitive_type;
5263
5264 register PrimitiveInfo
5265 *q;
5266
cristybb503372010-05-27 20:51:26 +00005267 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005268 i;
5269
cristybb503372010-05-27 20:51:26 +00005270 size_t
cristy3ed852e2009-09-05 21:47:34 +00005271 number_coordinates,
5272 z_count;
5273
5274 attribute=0;
cristy206c26e2013-11-07 18:04:21 +00005275 end.x=0.0;
5276 end.y=0.0;
cristy3ed852e2009-09-05 21:47:34 +00005277 point.x=0.0;
5278 point.y=0.0;
5279 start.x=0.0;
5280 start.y=0.0;
5281 number_coordinates=0;
5282 z_count=0;
5283 primitive_type=primitive_info->primitive;
5284 q=primitive_info;
5285 for (p=path; *p != '\0'; )
5286 {
5287 while (isspace((int) ((unsigned char) *p)) != 0)
5288 p++;
5289 if (*p == '\0')
5290 break;
5291 last_attribute=attribute;
5292 attribute=(int) (*p++);
5293 switch (attribute)
5294 {
5295 case 'a':
5296 case 'A':
5297 {
5298 MagickBooleanType
5299 large_arc,
5300 sweep;
5301
cristya19f1d72012-08-07 18:24:38 +00005302 double
cristy3ed852e2009-09-05 21:47:34 +00005303 angle;
5304
5305 PointInfo
5306 arc;
5307
5308 /*
5309 Compute arc points.
5310 */
5311 do
5312 {
5313 GetMagickToken(p,&p,token);
5314 if (*token == ',')
5315 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005316 arc.x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005317 GetMagickToken(p,&p,token);
5318 if (*token == ',')
5319 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005320 arc.y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005321 GetMagickToken(p,&p,token);
5322 if (*token == ',')
5323 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005324 angle=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005325 GetMagickToken(p,&p,token);
5326 if (*token == ',')
5327 GetMagickToken(p,&p,token);
cristyf2f27272009-12-17 14:48:46 +00005328 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00005329 GetMagickToken(p,&p,token);
5330 if (*token == ',')
5331 GetMagickToken(p,&p,token);
cristyf2f27272009-12-17 14:48:46 +00005332 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00005333 GetMagickToken(p,&p,token);
5334 if (*token == ',')
5335 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005336 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005337 GetMagickToken(p,&p,token);
5338 if (*token == ',')
5339 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005340 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005341 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5342 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5343 TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5344 q+=q->coordinates;
5345 point=end;
cristy671fcfc2011-12-13 13:28:31 +00005346 while (isspace((int) ((unsigned char) *p)) != 0)
5347 p++;
5348 if (*p == ',')
5349 p++;
cristy3ed852e2009-09-05 21:47:34 +00005350 } while (IsPoint(p) != MagickFalse);
5351 break;
5352 }
5353 case 'c':
5354 case 'C':
5355 {
5356 /*
5357 Compute bezier points.
5358 */
5359 do
5360 {
5361 points[0]=point;
5362 for (i=1; i < 4; i++)
5363 {
5364 GetMagickToken(p,&p,token);
5365 if (*token == ',')
5366 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005367 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005368 GetMagickToken(p,&p,token);
5369 if (*token == ',')
5370 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005371 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005372 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
5373 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
5374 points[i]=end;
5375 }
5376 for (i=0; i < 4; i++)
5377 (q+i)->point=points[i];
5378 TraceBezier(q,4);
5379 q+=q->coordinates;
5380 point=end;
5381 } while (IsPoint(p) != MagickFalse);
5382 break;
5383 }
5384 case 'H':
5385 case 'h':
5386 {
5387 do
5388 {
5389 GetMagickToken(p,&p,token);
5390 if (*token == ',')
5391 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005392 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005393 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
5394 TracePoint(q,point);
5395 q+=q->coordinates;
5396 } while (IsPoint(p) != MagickFalse);
5397 break;
5398 }
5399 case 'l':
5400 case 'L':
5401 {
5402 do
5403 {
5404 GetMagickToken(p,&p,token);
5405 if (*token == ',')
5406 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005407 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005408 GetMagickToken(p,&p,token);
5409 if (*token == ',')
5410 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005411 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005412 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
5413 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
5414 TracePoint(q,point);
5415 q+=q->coordinates;
5416 } while (IsPoint(p) != MagickFalse);
5417 break;
5418 }
5419 case 'M':
5420 case 'm':
5421 {
5422 if (q != primitive_info)
5423 {
cristybb503372010-05-27 20:51:26 +00005424 primitive_info->coordinates=(size_t) (q-primitive_info);
cristy3ed852e2009-09-05 21:47:34 +00005425 number_coordinates+=primitive_info->coordinates;
5426 primitive_info=q;
5427 }
5428 i=0;
5429 do
5430 {
5431 GetMagickToken(p,&p,token);
5432 if (*token == ',')
5433 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005434 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005435 GetMagickToken(p,&p,token);
5436 if (*token == ',')
5437 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005438 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005439 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
5440 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
5441 if (i == 0)
5442 start=point;
5443 i++;
5444 TracePoint(q,point);
5445 q+=q->coordinates;
cristy826a5472010-08-31 23:21:38 +00005446 if ((i != 0) && (attribute == (int) 'M'))
cristy3ed852e2009-09-05 21:47:34 +00005447 {
5448 TracePoint(q,point);
5449 q+=q->coordinates;
5450 }
5451 } while (IsPoint(p) != MagickFalse);
5452 break;
5453 }
5454 case 'q':
5455 case 'Q':
5456 {
5457 /*
5458 Compute bezier points.
5459 */
5460 do
5461 {
5462 points[0]=point;
5463 for (i=1; i < 3; i++)
5464 {
5465 GetMagickToken(p,&p,token);
5466 if (*token == ',')
5467 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005468 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005469 GetMagickToken(p,&p,token);
5470 if (*token == ',')
5471 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005472 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005473 if (*p == ',')
5474 p++;
5475 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
5476 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
5477 points[i]=end;
5478 }
5479 for (i=0; i < 3; i++)
5480 (q+i)->point=points[i];
5481 TraceBezier(q,3);
5482 q+=q->coordinates;
5483 point=end;
5484 } while (IsPoint(p) != MagickFalse);
5485 break;
5486 }
5487 case 's':
5488 case 'S':
5489 {
5490 /*
5491 Compute bezier points.
5492 */
5493 do
5494 {
5495 points[0]=points[3];
5496 points[1].x=2.0*points[3].x-points[2].x;
5497 points[1].y=2.0*points[3].y-points[2].y;
5498 for (i=2; i < 4; i++)
5499 {
5500 GetMagickToken(p,&p,token);
5501 if (*token == ',')
5502 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005503 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005504 GetMagickToken(p,&p,token);
5505 if (*token == ',')
5506 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005507 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005508 if (*p == ',')
5509 p++;
5510 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
5511 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
5512 points[i]=end;
5513 }
5514 if (strchr("CcSs",last_attribute) == (char *) NULL)
5515 {
5516 points[0]=points[2];
5517 points[1]=points[3];
5518 }
5519 for (i=0; i < 4; i++)
5520 (q+i)->point=points[i];
5521 TraceBezier(q,4);
5522 q+=q->coordinates;
5523 point=end;
5524 } while (IsPoint(p) != MagickFalse);
5525 break;
5526 }
5527 case 't':
5528 case 'T':
5529 {
5530 /*
5531 Compute bezier points.
5532 */
5533 do
5534 {
5535 points[0]=points[2];
5536 points[1].x=2.0*points[2].x-points[1].x;
5537 points[1].y=2.0*points[2].y-points[1].y;
5538 for (i=2; i < 3; i++)
5539 {
5540 GetMagickToken(p,&p,token);
5541 if (*token == ',')
5542 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005543 x=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005544 GetMagickToken(p,&p,token);
5545 if (*token == ',')
5546 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005547 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005548 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
5549 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
5550 points[i]=end;
5551 }
5552 if (strchr("QqTt",last_attribute) == (char *) NULL)
5553 {
5554 points[0]=points[2];
5555 points[1]=points[3];
5556 }
5557 for (i=0; i < 3; i++)
5558 (q+i)->point=points[i];
5559 TraceBezier(q,3);
5560 q+=q->coordinates;
5561 point=end;
5562 } while (IsPoint(p) != MagickFalse);
5563 break;
5564 }
5565 case 'v':
5566 case 'V':
5567 {
5568 do
5569 {
5570 GetMagickToken(p,&p,token);
5571 if (*token == ',')
5572 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00005573 y=StringToDouble(token,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00005574 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
5575 TracePoint(q,point);
5576 q+=q->coordinates;
5577 } while (IsPoint(p) != MagickFalse);
5578 break;
5579 }
5580 case 'z':
5581 case 'Z':
5582 {
5583 point=start;
5584 TracePoint(q,point);
5585 q+=q->coordinates;
cristybb503372010-05-27 20:51:26 +00005586 primitive_info->coordinates=(size_t) (q-primitive_info);
cristy3ed852e2009-09-05 21:47:34 +00005587 number_coordinates+=primitive_info->coordinates;
5588 primitive_info=q;
5589 z_count++;
5590 break;
5591 }
5592 default:
5593 {
5594 if (isalpha((int) ((unsigned char) attribute)) != 0)
cristy1e604812011-05-19 18:07:50 +00005595 (void) FormatLocaleFile(stderr,"attribute not recognized: %c\n",
5596 attribute);
cristy3ed852e2009-09-05 21:47:34 +00005597 break;
5598 }
5599 }
5600 }
cristybb503372010-05-27 20:51:26 +00005601 primitive_info->coordinates=(size_t) (q-primitive_info);
cristy3ed852e2009-09-05 21:47:34 +00005602 number_coordinates+=primitive_info->coordinates;
cristybb503372010-05-27 20:51:26 +00005603 for (i=0; i < (ssize_t) number_coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005604 {
5605 q--;
5606 q->primitive=primitive_type;
5607 if (z_count > 1)
5608 q->method=FillToBorderMethod;
5609 }
5610 q=primitive_info;
5611 return(number_coordinates);
5612}
5613
5614static void TraceRectangle(PrimitiveInfo *primitive_info,const PointInfo start,
5615 const PointInfo end)
5616{
5617 PointInfo
5618 point;
5619
5620 register PrimitiveInfo
5621 *p;
5622
cristybb503372010-05-27 20:51:26 +00005623 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005624 i;
5625
5626 p=primitive_info;
5627 TracePoint(p,start);
5628 p+=p->coordinates;
5629 point.x=start.x;
5630 point.y=end.y;
5631 TracePoint(p,point);
5632 p+=p->coordinates;
5633 TracePoint(p,end);
5634 p+=p->coordinates;
5635 point.x=end.x;
5636 point.y=start.y;
5637 TracePoint(p,point);
5638 p+=p->coordinates;
5639 TracePoint(p,start);
5640 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005641 primitive_info->coordinates=(size_t) (p-primitive_info);
5642 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005643 {
5644 p->primitive=primitive_info->primitive;
5645 p--;
5646 }
5647}
5648
5649static void TraceRoundRectangle(PrimitiveInfo *primitive_info,
5650 const PointInfo start,const PointInfo end,PointInfo arc)
5651{
5652 PointInfo
5653 degrees,
5654 offset,
5655 point;
5656
5657 register PrimitiveInfo
5658 *p;
5659
cristybb503372010-05-27 20:51:26 +00005660 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005661 i;
5662
5663 p=primitive_info;
5664 offset.x=fabs(end.x-start.x);
5665 offset.y=fabs(end.y-start.y);
5666 if (arc.x > (0.5*offset.x))
5667 arc.x=0.5*offset.x;
5668 if (arc.y > (0.5*offset.y))
5669 arc.y=0.5*offset.y;
5670 point.x=start.x+offset.x-arc.x;
5671 point.y=start.y+arc.y;
5672 degrees.x=270.0;
5673 degrees.y=360.0;
5674 TraceEllipse(p,point,arc,degrees);
5675 p+=p->coordinates;
5676 point.x=start.x+offset.x-arc.x;
5677 point.y=start.y+offset.y-arc.y;
5678 degrees.x=0.0;
5679 degrees.y=90.0;
5680 TraceEllipse(p,point,arc,degrees);
5681 p+=p->coordinates;
5682 point.x=start.x+arc.x;
5683 point.y=start.y+offset.y-arc.y;
5684 degrees.x=90.0;
5685 degrees.y=180.0;
5686 TraceEllipse(p,point,arc,degrees);
5687 p+=p->coordinates;
5688 point.x=start.x+arc.x;
5689 point.y=start.y+arc.y;
5690 degrees.x=180.0;
5691 degrees.y=270.0;
5692 TraceEllipse(p,point,arc,degrees);
5693 p+=p->coordinates;
5694 TracePoint(p,primitive_info->point);
5695 p+=p->coordinates;
cristybb503372010-05-27 20:51:26 +00005696 primitive_info->coordinates=(size_t) (p-primitive_info);
5697 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
cristy3ed852e2009-09-05 21:47:34 +00005698 {
5699 p->primitive=primitive_info->primitive;
5700 p--;
5701 }
5702}
5703
5704static void TraceSquareLinecap(PrimitiveInfo *primitive_info,
cristya19f1d72012-08-07 18:24:38 +00005705 const size_t number_vertices,const double offset)
cristy3ed852e2009-09-05 21:47:34 +00005706{
cristya19f1d72012-08-07 18:24:38 +00005707 double
cristy3ed852e2009-09-05 21:47:34 +00005708 distance;
5709
cristya19f1d72012-08-07 18:24:38 +00005710 register double
cristy3ed852e2009-09-05 21:47:34 +00005711 dx,
5712 dy;
5713
cristybb503372010-05-27 20:51:26 +00005714 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005715 i;
5716
cristy826a5472010-08-31 23:21:38 +00005717 ssize_t
5718 j;
5719
cristy3ed852e2009-09-05 21:47:34 +00005720 dx=0.0;
5721 dy=0.0;
cristybb503372010-05-27 20:51:26 +00005722 for (i=1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00005723 {
5724 dx=primitive_info[0].point.x-primitive_info[i].point.x;
5725 dy=primitive_info[0].point.y-primitive_info[i].point.y;
cristy53aed702012-07-08 00:21:25 +00005726 if ((fabs((double) dx) >= MagickEpsilon) ||
5727 (fabs((double) dy) >= MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +00005728 break;
5729 }
cristybb503372010-05-27 20:51:26 +00005730 if (i == (ssize_t) number_vertices)
5731 i=(ssize_t) number_vertices-1L;
cristy3ed852e2009-09-05 21:47:34 +00005732 distance=hypot((double) dx,(double) dy);
5733 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
5734 dx*(distance+offset)/distance);
5735 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
5736 dy*(distance+offset)/distance);
cristybb503372010-05-27 20:51:26 +00005737 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +00005738 {
5739 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
5740 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
cristy53aed702012-07-08 00:21:25 +00005741 if ((fabs((double) dx) >= MagickEpsilon) ||
5742 (fabs((double) dy) >= MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +00005743 break;
5744 }
5745 distance=hypot((double) dx,(double) dy);
5746 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
5747 dx*(distance+offset)/distance);
5748 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
5749 dy*(distance+offset)/distance);
5750}
5751
cristya19f1d72012-08-07 18:24:38 +00005752static inline double DrawEpsilonReciprocal(const double x)
cristy53aed702012-07-08 00:21:25 +00005753{
cristya19f1d72012-08-07 18:24:38 +00005754#define DrawEpsilon ((double) 1.0e-10)
cristy53aed702012-07-08 00:21:25 +00005755
cristya19f1d72012-08-07 18:24:38 +00005756 double sign = x < (double) 0.0 ? (double) -1.0 :
5757 (double) 1.0;
5758 return((sign*x) >= DrawEpsilon ? (double) 1.0/x : sign*(
5759 (double) 1.0/DrawEpsilon));
cristy53aed702012-07-08 00:21:25 +00005760}
5761
cristy3ed852e2009-09-05 21:47:34 +00005762static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
5763 const PrimitiveInfo *primitive_info)
5764{
5765 typedef struct _LineSegment
5766 {
5767 double
5768 p,
5769 q;
5770 } LineSegment;
5771
5772 LineSegment
5773 dx,
5774 dy,
5775 inverse_slope,
5776 slope,
5777 theta;
5778
cristy3ed852e2009-09-05 21:47:34 +00005779 MagickBooleanType
5780 closed_path;
5781
cristya19f1d72012-08-07 18:24:38 +00005782 double
cristy3ed852e2009-09-05 21:47:34 +00005783 delta_theta,
5784 dot_product,
5785 mid,
5786 miterlimit;
5787
5788 PointInfo
5789 box_p[5],
5790 box_q[5],
5791 center,
5792 offset,
5793 *path_p,
5794 *path_q;
5795
5796 PrimitiveInfo
5797 *polygon_primitive,
5798 *stroke_polygon;
5799
cristybb503372010-05-27 20:51:26 +00005800 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005801 i;
5802
cristybb503372010-05-27 20:51:26 +00005803 size_t
cristy3ed852e2009-09-05 21:47:34 +00005804 arc_segments,
5805 max_strokes,
5806 number_vertices;
5807
cristy826a5472010-08-31 23:21:38 +00005808 ssize_t
5809 j,
5810 n,
5811 p,
5812 q;
5813
cristy3ed852e2009-09-05 21:47:34 +00005814 /*
5815 Allocate paths.
5816 */
5817 number_vertices=primitive_info->coordinates;
5818 max_strokes=2*number_vertices+6*BezierQuantum+360;
5819 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5820 sizeof(*path_p));
5821 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
5822 sizeof(*path_q));
5823 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
5824 number_vertices+2UL,sizeof(*polygon_primitive));
5825 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL) ||
5826 (polygon_primitive == (PrimitiveInfo *) NULL))
5827 return((PrimitiveInfo *) NULL);
5828 (void) CopyMagickMemory(polygon_primitive,primitive_info,(size_t)
5829 number_vertices*sizeof(*polygon_primitive));
5830 closed_path=
5831 (primitive_info[number_vertices-1].point.x == primitive_info[0].point.x) &&
5832 (primitive_info[number_vertices-1].point.y == primitive_info[0].point.y) ?
5833 MagickTrue : MagickFalse;
5834 if ((draw_info->linejoin == RoundJoin) ||
5835 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
5836 {
5837 polygon_primitive[number_vertices]=primitive_info[1];
5838 number_vertices++;
5839 }
5840 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
5841 /*
5842 Compute the slope for the first line segment, p.
5843 */
5844 dx.p=0.0;
5845 dy.p=0.0;
cristybb503372010-05-27 20:51:26 +00005846 for (n=1; n < (ssize_t) number_vertices; n++)
cristy3ed852e2009-09-05 21:47:34 +00005847 {
5848 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
5849 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
cristy53aed702012-07-08 00:21:25 +00005850 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +00005851 break;
5852 }
cristybb503372010-05-27 20:51:26 +00005853 if (n == (ssize_t) number_vertices)
5854 n=(ssize_t) number_vertices-1L;
cristy66fce5b2012-06-29 15:58:17 +00005855 slope.p=DrawEpsilonReciprocal(dx.p)*dy.p;
5856 inverse_slope.p=(-1.0*DrawEpsilonReciprocal(slope.p));
cristy3ed852e2009-09-05 21:47:34 +00005857 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
cristya19f1d72012-08-07 18:24:38 +00005858 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*
cristy3ed852e2009-09-05 21:47:34 +00005859 mid*mid);
5860 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
5861 TraceSquareLinecap(polygon_primitive,number_vertices,mid);
5862 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
5863 offset.y=(double) (offset.x*inverse_slope.p);
5864 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
5865 {
5866 box_p[0].x=polygon_primitive[0].point.x-offset.x;
5867 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
5868 box_p[1].x=polygon_primitive[n].point.x-offset.x;
5869 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
5870 box_q[0].x=polygon_primitive[0].point.x+offset.x;
5871 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
5872 box_q[1].x=polygon_primitive[n].point.x+offset.x;
5873 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
5874 }
5875 else
5876 {
5877 box_p[0].x=polygon_primitive[0].point.x+offset.x;
5878 box_p[0].y=polygon_primitive[0].point.y+offset.y;
5879 box_p[1].x=polygon_primitive[n].point.x+offset.x;
5880 box_p[1].y=polygon_primitive[n].point.y+offset.y;
5881 box_q[0].x=polygon_primitive[0].point.x-offset.x;
5882 box_q[0].y=polygon_primitive[0].point.y-offset.y;
5883 box_q[1].x=polygon_primitive[n].point.x-offset.x;
5884 box_q[1].y=polygon_primitive[n].point.y-offset.y;
5885 }
5886 /*
5887 Create strokes for the line join attribute: bevel, miter, round.
5888 */
5889 p=0;
5890 q=0;
5891 path_q[p++]=box_q[0];
5892 path_p[q++]=box_p[0];
cristybb503372010-05-27 20:51:26 +00005893 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
cristy3ed852e2009-09-05 21:47:34 +00005894 {
5895 /*
5896 Compute the slope for this line segment, q.
5897 */
5898 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
5899 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
5900 dot_product=dx.q*dx.q+dy.q*dy.q;
5901 if (dot_product < 0.25)
5902 continue;
cristy66fce5b2012-06-29 15:58:17 +00005903 slope.q=DrawEpsilonReciprocal(dx.q)*dy.q;
5904 inverse_slope.q=(-1.0*DrawEpsilonReciprocal(slope.q));
cristy3ed852e2009-09-05 21:47:34 +00005905 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
5906 offset.y=(double) (offset.x*inverse_slope.q);
5907 dot_product=dy.q*offset.x-dx.q*offset.y;
5908 if (dot_product > 0.0)
5909 {
5910 box_p[2].x=polygon_primitive[n].point.x-offset.x;
5911 box_p[2].y=polygon_primitive[n].point.y-offset.y;
5912 box_p[3].x=polygon_primitive[i].point.x-offset.x;
5913 box_p[3].y=polygon_primitive[i].point.y-offset.y;
5914 box_q[2].x=polygon_primitive[n].point.x+offset.x;
5915 box_q[2].y=polygon_primitive[n].point.y+offset.y;
5916 box_q[3].x=polygon_primitive[i].point.x+offset.x;
5917 box_q[3].y=polygon_primitive[i].point.y+offset.y;
5918 }
5919 else
5920 {
5921 box_p[2].x=polygon_primitive[n].point.x+offset.x;
5922 box_p[2].y=polygon_primitive[n].point.y+offset.y;
5923 box_p[3].x=polygon_primitive[i].point.x+offset.x;
5924 box_p[3].y=polygon_primitive[i].point.y+offset.y;
5925 box_q[2].x=polygon_primitive[n].point.x-offset.x;
5926 box_q[2].y=polygon_primitive[n].point.y-offset.y;
5927 box_q[3].x=polygon_primitive[i].point.x-offset.x;
5928 box_q[3].y=polygon_primitive[i].point.y-offset.y;
5929 }
cristy53aed702012-07-08 00:21:25 +00005930 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +00005931 {
5932 box_p[4]=box_p[1];
5933 box_q[4]=box_q[1];
5934 }
5935 else
5936 {
5937 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
5938 box_p[3].y)/(slope.p-slope.q));
5939 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
5940 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
5941 box_q[3].y)/(slope.p-slope.q));
5942 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
5943 }
cristybb503372010-05-27 20:51:26 +00005944 if (q >= (ssize_t) (max_strokes-6*BezierQuantum-360))
cristy3ed852e2009-09-05 21:47:34 +00005945 {
5946 max_strokes+=6*BezierQuantum+360;
5947 path_p=(PointInfo *) ResizeQuantumMemory(path_p,(size_t) max_strokes,
5948 sizeof(*path_p));
5949 path_q=(PointInfo *) ResizeQuantumMemory(path_q,(size_t) max_strokes,
5950 sizeof(*path_q));
5951 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL))
5952 {
5953 polygon_primitive=(PrimitiveInfo *)
5954 RelinquishMagickMemory(polygon_primitive);
5955 return((PrimitiveInfo *) NULL);
5956 }
5957 }
5958 dot_product=dx.q*dy.p-dx.p*dy.q;
5959 if (dot_product <= 0.0)
5960 switch (draw_info->linejoin)
5961 {
5962 case BevelJoin:
5963 {
5964 path_q[q++]=box_q[1];
5965 path_q[q++]=box_q[2];
5966 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5967 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5968 if (dot_product <= miterlimit)
5969 path_p[p++]=box_p[4];
5970 else
5971 {
5972 path_p[p++]=box_p[1];
5973 path_p[p++]=box_p[2];
5974 }
5975 break;
5976 }
5977 case MiterJoin:
5978 {
5979 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5980 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5981 if (dot_product <= miterlimit)
5982 {
5983 path_q[q++]=box_q[4];
5984 path_p[p++]=box_p[4];
5985 }
5986 else
5987 {
5988 path_q[q++]=box_q[1];
5989 path_q[q++]=box_q[2];
5990 path_p[p++]=box_p[1];
5991 path_p[p++]=box_p[2];
5992 }
5993 break;
5994 }
5995 case RoundJoin:
5996 {
5997 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
5998 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
5999 if (dot_product <= miterlimit)
6000 path_p[p++]=box_p[4];
6001 else
6002 {
6003 path_p[p++]=box_p[1];
6004 path_p[p++]=box_p[2];
6005 }
6006 center=polygon_primitive[n].point;
6007 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
6008 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
6009 if (theta.q < theta.p)
cristya19f1d72012-08-07 18:24:38 +00006010 theta.q+=(double) (2.0*MagickPI);
cristybb503372010-05-27 20:51:26 +00006011 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
cristy3ed852e2009-09-05 21:47:34 +00006012 (2.0*sqrt((double) (1.0/mid)))));
6013 path_q[q].x=box_q[1].x;
6014 path_q[q].y=box_q[1].y;
6015 q++;
cristybb503372010-05-27 20:51:26 +00006016 for (j=1; j < (ssize_t) arc_segments; j++)
cristy3ed852e2009-09-05 21:47:34 +00006017 {
cristya19f1d72012-08-07 18:24:38 +00006018 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
cristy3ed852e2009-09-05 21:47:34 +00006019 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
6020 (theta.p+delta_theta),DegreesToRadians(360.0))));
6021 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
6022 (theta.p+delta_theta),DegreesToRadians(360.0))));
6023 q++;
6024 }
6025 path_q[q++]=box_q[2];
6026 break;
6027 }
6028 default:
6029 break;
6030 }
6031 else
6032 switch (draw_info->linejoin)
6033 {
6034 case BevelJoin:
6035 {
6036 path_p[p++]=box_p[1];
6037 path_p[p++]=box_p[2];
6038 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6039 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6040 if (dot_product <= miterlimit)
6041 path_q[q++]=box_q[4];
6042 else
6043 {
6044 path_q[q++]=box_q[1];
6045 path_q[q++]=box_q[2];
6046 }
6047 break;
6048 }
6049 case MiterJoin:
6050 {
6051 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6052 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6053 if (dot_product <= miterlimit)
6054 {
6055 path_q[q++]=box_q[4];
6056 path_p[p++]=box_p[4];
6057 }
6058 else
6059 {
6060 path_q[q++]=box_q[1];
6061 path_q[q++]=box_q[2];
6062 path_p[p++]=box_p[1];
6063 path_p[p++]=box_p[2];
6064 }
6065 break;
6066 }
6067 case RoundJoin:
6068 {
6069 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
6070 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
6071 if (dot_product <= miterlimit)
6072 path_q[q++]=box_q[4];
6073 else
6074 {
6075 path_q[q++]=box_q[1];
6076 path_q[q++]=box_q[2];
6077 }
6078 center=polygon_primitive[n].point;
6079 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
6080 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
6081 if (theta.p < theta.q)
cristya19f1d72012-08-07 18:24:38 +00006082 theta.p+=(double) (2.0*MagickPI);
cristybb503372010-05-27 20:51:26 +00006083 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
cristy3ed852e2009-09-05 21:47:34 +00006084 (2.0*sqrt((double) (1.0/mid)))));
6085 path_p[p++]=box_p[1];
cristybb503372010-05-27 20:51:26 +00006086 for (j=1; j < (ssize_t) arc_segments; j++)
cristy3ed852e2009-09-05 21:47:34 +00006087 {
cristya19f1d72012-08-07 18:24:38 +00006088 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
cristy3ed852e2009-09-05 21:47:34 +00006089 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
6090 (theta.p+delta_theta),DegreesToRadians(360.0))));
6091 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
6092 (theta.p+delta_theta),DegreesToRadians(360.0))));
6093 p++;
6094 }
6095 path_p[p++]=box_p[2];
6096 break;
6097 }
6098 default:
6099 break;
6100 }
6101 slope.p=slope.q;
6102 inverse_slope.p=inverse_slope.q;
6103 box_p[0]=box_p[2];
6104 box_p[1]=box_p[3];
6105 box_q[0]=box_q[2];
6106 box_q[1]=box_q[3];
6107 dx.p=dx.q;
6108 dy.p=dy.q;
6109 n=i;
6110 }
6111 path_p[p++]=box_p[1];
6112 path_q[q++]=box_q[1];
6113 /*
6114 Trace stroked polygon.
6115 */
6116 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
6117 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
6118 if (stroke_polygon != (PrimitiveInfo *) NULL)
6119 {
cristybb503372010-05-27 20:51:26 +00006120 for (i=0; i < (ssize_t) p; i++)
cristy3ed852e2009-09-05 21:47:34 +00006121 {
6122 stroke_polygon[i]=polygon_primitive[0];
6123 stroke_polygon[i].point=path_p[i];
6124 }
6125 if (closed_path != MagickFalse)
6126 {
6127 stroke_polygon[i]=polygon_primitive[0];
6128 stroke_polygon[i].point=stroke_polygon[0].point;
6129 i++;
6130 }
cristybb503372010-05-27 20:51:26 +00006131 for ( ; i < (ssize_t) (p+q+closed_path); i++)
cristy3ed852e2009-09-05 21:47:34 +00006132 {
6133 stroke_polygon[i]=polygon_primitive[0];
6134 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
6135 }
6136 if (closed_path != MagickFalse)
6137 {
6138 stroke_polygon[i]=polygon_primitive[0];
6139 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
6140 i++;
6141 }
6142 stroke_polygon[i]=polygon_primitive[0];
6143 stroke_polygon[i].point=stroke_polygon[0].point;
6144 i++;
6145 stroke_polygon[i].primitive=UndefinedPrimitive;
cristybb503372010-05-27 20:51:26 +00006146 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
cristy3ed852e2009-09-05 21:47:34 +00006147 }
6148 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
6149 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
6150 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
6151 return(stroke_polygon);
6152}