MagickCore  7.0.7
Convert, Edit, Or Compose Bitmap Images
draw.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % DDDD RRRR AAA W W %
7 % D D R R A A W W %
8 % D D RRRR AAAAA W W W %
9 % D D R RN A A WW WW %
10 % DDDD R R A A W W %
11 % %
12 % %
13 % MagickCore Image Drawing Methods %
14 % %
15 % %
16 % Software Design %
17 % Cristy %
18 % July 1998 %
19 % %
20 % %
21 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
23 % %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
26 % %
27 % https://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 */
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
54 #include "MagickCore/cache-view.h"
55 #include "MagickCore/channel.h"
56 #include "MagickCore/color.h"
58 #include "MagickCore/composite.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/draw.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/geometry.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/log.h"
72 #include "MagickCore/monitor.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/paint.h"
78 #include "MagickCore/property.h"
79 #include "MagickCore/resample.h"
81 #include "MagickCore/resource_.h"
82 #include "MagickCore/string_.h"
85 #include "MagickCore/token.h"
87 #include "MagickCore/utility.h"
88 
89 /*
90  Define declarations.
91 */
92 #define BezierQuantum 200
93 #define DrawEpsilon (1.0e-10)
94 
95 
96 /*
97  Typedef declarations.
98 */
99 typedef struct _EdgeInfo
100 {
103 
104  double
106 
107  PointInfo
109 
110  size_t
112 
113  ssize_t
115 
118 
119  size_t
121 } EdgeInfo;
122 
123 typedef struct _ElementInfo
124 {
125  double
126  cx,
127  cy,
128  major,
129  minor,
130  angle;
131 } ElementInfo;
132 
133 typedef struct _PolygonInfo
134 {
135  EdgeInfo
137 
138  size_t
140 } PolygonInfo;
141 
142 typedef enum
143 {
149 } PathInfoCode;
150 
151 typedef struct _PathInfo
152 {
153  PointInfo
155 
158 } PathInfo;
159 
160 /*
161  Forward declarations.
162 */
163 static MagickBooleanType
164  DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
165  ExceptionInfo *);
166 
167 static PrimitiveInfo
168  *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *);
169 
170 static size_t
171  TracePath(PrimitiveInfo *,const char *);
172 
173 static void
174  TraceArc(PrimitiveInfo *,const PointInfo,const PointInfo,const PointInfo),
176  const double,const MagickBooleanType,const MagickBooleanType),
177  TraceBezier(PrimitiveInfo *,const size_t),
180  const PointInfo),
184  PointInfo),
185  TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
186 
187 /*
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % %
190 % %
191 % %
192 % A c q u i r e D r a w I n f o %
193 % %
194 % %
195 % %
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 %
198 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
199 %
200 % The format of the AcquireDrawInfo method is:
201 %
202 % DrawInfo *AcquireDrawInfo(void)
203 %
204 */
206 {
207  DrawInfo
208  *draw_info;
209 
210  draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
211  GetDrawInfo((ImageInfo *) NULL,draw_info);
212  return(draw_info);
213 }
214 
215 /*
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % %
218 % %
219 % %
220 % C l o n e D r a w I n f o %
221 % %
222 % %
223 % %
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 %
226 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
227 % is specified, a new DrawInfo structure is created initialized to default
228 % values.
229 %
230 % The format of the CloneDrawInfo method is:
231 %
232 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
233 % const DrawInfo *draw_info)
234 %
235 % A description of each parameter follows:
236 %
237 % o image_info: the image info.
238 %
239 % o draw_info: the draw info.
240 %
241 */
243  const DrawInfo *draw_info)
244 {
245  DrawInfo
246  *clone_info;
247 
249  *exception;
250 
251  clone_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*clone_info));
252  GetDrawInfo(image_info,clone_info);
253  if (draw_info == (DrawInfo *) NULL)
254  return(clone_info);
255  exception=AcquireExceptionInfo();
256  if (clone_info->primitive != (char *) NULL)
257  (void) CloneString(&clone_info->primitive,draw_info->primitive);
258  if (draw_info->geometry != (char *) NULL)
259  (void) CloneString(&clone_info->geometry,draw_info->geometry);
260  clone_info->viewbox=draw_info->viewbox;
261  clone_info->affine=draw_info->affine;
262  clone_info->gravity=draw_info->gravity;
263  clone_info->fill=draw_info->fill;
264  clone_info->stroke=draw_info->stroke;
265  clone_info->stroke_width=draw_info->stroke_width;
266  if (draw_info->fill_pattern != (Image *) NULL)
267  clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
268  exception);
269  if (draw_info->stroke_pattern != (Image *) NULL)
270  clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
271  MagickTrue,exception);
272  clone_info->stroke_antialias=draw_info->stroke_antialias;
273  clone_info->text_antialias=draw_info->text_antialias;
274  clone_info->fill_rule=draw_info->fill_rule;
275  clone_info->linecap=draw_info->linecap;
276  clone_info->linejoin=draw_info->linejoin;
277  clone_info->miterlimit=draw_info->miterlimit;
278  clone_info->dash_offset=draw_info->dash_offset;
279  clone_info->decorate=draw_info->decorate;
280  clone_info->compose=draw_info->compose;
281  if (draw_info->text != (char *) NULL)
282  (void) CloneString(&clone_info->text,draw_info->text);
283  if (draw_info->font != (char *) NULL)
284  (void) CloneString(&clone_info->font,draw_info->font);
285  if (draw_info->metrics != (char *) NULL)
286  (void) CloneString(&clone_info->metrics,draw_info->metrics);
287  if (draw_info->family != (char *) NULL)
288  (void) CloneString(&clone_info->family,draw_info->family);
289  clone_info->style=draw_info->style;
290  clone_info->stretch=draw_info->stretch;
291  clone_info->weight=draw_info->weight;
292  if (draw_info->encoding != (char *) NULL)
293  (void) CloneString(&clone_info->encoding,draw_info->encoding);
294  clone_info->pointsize=draw_info->pointsize;
295  clone_info->kerning=draw_info->kerning;
296  clone_info->interline_spacing=draw_info->interline_spacing;
297  clone_info->interword_spacing=draw_info->interword_spacing;
298  clone_info->direction=draw_info->direction;
299  if (draw_info->density != (char *) NULL)
300  (void) CloneString(&clone_info->density,draw_info->density);
301  clone_info->align=draw_info->align;
302  clone_info->undercolor=draw_info->undercolor;
303  clone_info->border_color=draw_info->border_color;
304  if (draw_info->server_name != (char *) NULL)
305  (void) CloneString(&clone_info->server_name,draw_info->server_name);
306  if (draw_info->dash_pattern != (double *) NULL)
307  {
308  register ssize_t
309  x;
310 
311  for (x=0; fabs(draw_info->dash_pattern[x]) >= DrawEpsilon; x++) ;
312  clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) x+1UL,
313  sizeof(*clone_info->dash_pattern));
314  if (clone_info->dash_pattern == (double *) NULL)
316  "UnableToAllocateDashPattern");
317  (void) CopyMagickMemory(clone_info->dash_pattern,draw_info->dash_pattern,
318  (size_t) (x+1)*sizeof(*clone_info->dash_pattern));
319  }
320  clone_info->gradient=draw_info->gradient;
321  if (draw_info->gradient.stops != (StopInfo *) NULL)
322  {
323  size_t
324  number_stops;
325 
326  number_stops=clone_info->gradient.number_stops;
327  clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
328  number_stops,sizeof(*clone_info->gradient.stops));
329  if (clone_info->gradient.stops == (StopInfo *) NULL)
331  "UnableToAllocateDashPattern");
332  (void) CopyMagickMemory(clone_info->gradient.stops,
333  draw_info->gradient.stops,(size_t) number_stops*
334  sizeof(*clone_info->gradient.stops));
335  }
336  if (draw_info->clip_mask != (char *) NULL)
337  (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
338  clone_info->bounds=draw_info->bounds;
339  clone_info->clip_units=draw_info->clip_units;
340  clone_info->render=draw_info->render;
341  clone_info->fill_alpha=draw_info->fill_alpha;
342  clone_info->stroke_alpha=draw_info->stroke_alpha;
343  clone_info->element_reference=draw_info->element_reference;
344  clone_info->debug=IsEventLogging();
345  exception=DestroyExceptionInfo(exception);
346  return(clone_info);
347 }
348 
349 /*
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 % %
352 % %
353 % %
354 + C o n v e r t P a t h T o P o l y g o n %
355 % %
356 % %
357 % %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 %
360 % ConvertPathToPolygon() converts a path to the more efficient sorted
361 % rendering form.
362 %
363 % The format of the ConvertPathToPolygon method is:
364 %
365 % PolygonInfo *ConvertPathToPolygon(const DrawInfo *draw_info,
366 % const PathInfo *path_info)
367 %
368 % A description of each parameter follows:
369 %
370 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
371 % rendering form of type PolygonInfo.
372 %
373 % o draw_info: Specifies a pointer to an DrawInfo structure.
374 %
375 % o path_info: Specifies a pointer to an PathInfo structure.
376 %
377 %
378 */
379 
380 #if defined(__cplusplus) || defined(c_plusplus)
381 extern "C" {
382 #endif
383 
384 static int CompareEdges(const void *x,const void *y)
385 {
386  register const EdgeInfo
387  *p,
388  *q;
389 
390  /*
391  Compare two edges.
392  */
393  p=(const EdgeInfo *) x;
394  q=(const EdgeInfo *) y;
395  if ((p->points[0].y-DrawEpsilon) > q->points[0].y)
396  return(1);
397  if ((p->points[0].y+DrawEpsilon) < q->points[0].y)
398  return(-1);
399  if ((p->points[0].x-DrawEpsilon) > q->points[0].x)
400  return(1);
401  if ((p->points[0].x+DrawEpsilon) < q->points[0].x)
402  return(-1);
403  if (((p->points[1].x-p->points[0].x)*(q->points[1].y-q->points[0].y)-
404  (p->points[1].y-p->points[0].y)*(q->points[1].x-q->points[0].x)) > 0.0)
405  return(1);
406  return(-1);
407 }
408 
409 #if defined(__cplusplus) || defined(c_plusplus)
410 }
411 #endif
412 
413 static void LogPolygonInfo(const PolygonInfo *polygon_info)
414 {
415  register EdgeInfo
416  *p;
417 
418  register ssize_t
419  i,
420  j;
421 
422  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
423  p=polygon_info->edges;
424  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
425  {
426  (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
427  (double) i);
428  (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
429  p->direction != MagickFalse ? "down" : "up");
430  (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
431  p->ghostline != MagickFalse ? "transparent" : "opaque");
433  " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
434  p->bounds.x2,p->bounds.y2);
435  for (j=0; j < (ssize_t) p->number_points; j++)
436  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
437  p->points[j].x,p->points[j].y);
438  p++;
439  }
440  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
441 }
442 
443 static void ReversePoints(PointInfo *points,const size_t number_points)
444 {
445  PointInfo
446  point;
447 
448  register ssize_t
449  i;
450 
451  for (i=0; i < (ssize_t) (number_points >> 1); i++)
452  {
453  point=points[i];
454  points[i]=points[number_points-(i+1)];
455  points[number_points-(i+1)]=point;
456  }
457 }
458 
459 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
460 {
461  long
462  direction,
463  next_direction;
464 
465  PointInfo
466  point,
467  *points;
468 
470  *polygon_info;
471 
473  bounds;
474 
475  register ssize_t
476  i,
477  n;
478 
480  ghostline;
481 
482  size_t
483  edge,
484  number_edges,
486 
487  /*
488  Convert a path to the more efficient sorted rendering form.
489  */
490  polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
491  if (polygon_info == (PolygonInfo *) NULL)
492  return((PolygonInfo *) NULL);
493  number_edges=16;
494  polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
495  sizeof(*polygon_info->edges));
496  if (polygon_info->edges == (EdgeInfo *) NULL)
497  return((PolygonInfo *) NULL);
498  (void) ResetMagickMemory(polygon_info->edges,0,number_edges*
499  sizeof(*polygon_info->edges));
500  direction=0;
501  edge=0;
502  ghostline=MagickFalse;
503  n=0;
504  number_points=0;
505  points=(PointInfo *) NULL;
506  (void) ResetMagickMemory(&point,0,sizeof(point));
507  (void) ResetMagickMemory(&bounds,0,sizeof(bounds));
508  for (i=0; path_info[i].code != EndCode; i++)
509  {
510  if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
511  (path_info[i].code == GhostlineCode))
512  {
513  /*
514  Move to.
515  */
516  if ((points != (PointInfo *) NULL) && (n >= 2))
517  {
518  if (edge == number_edges)
519  {
520  number_edges<<=1;
521  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
522  polygon_info->edges,(size_t) number_edges,
523  sizeof(*polygon_info->edges));
524  if (polygon_info->edges == (EdgeInfo *) NULL)
525  return((PolygonInfo *) NULL);
526  }
527  polygon_info->edges[edge].number_points=(size_t) n;
528  polygon_info->edges[edge].scanline=(-1.0);
529  polygon_info->edges[edge].highwater=0;
530  polygon_info->edges[edge].ghostline=ghostline;
531  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
532  if (direction < 0)
533  ReversePoints(points,(size_t) n);
534  polygon_info->edges[edge].points=points;
535  polygon_info->edges[edge].bounds=bounds;
536  polygon_info->edges[edge].bounds.y1=points[0].y;
537  polygon_info->edges[edge].bounds.y2=points[n-1].y;
538  points=(PointInfo *) NULL;
539  ghostline=MagickFalse;
540  edge++;
541  }
542  if (points == (PointInfo *) NULL)
543  {
544  number_points=16;
545  points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
546  sizeof(*points));
547  if (points == (PointInfo *) NULL)
548  return((PolygonInfo *) NULL);
549  }
550  ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
551  point=path_info[i].point;
552  points[0]=point;
553  bounds.x1=point.x;
554  bounds.x2=point.x;
555  direction=0;
556  n=1;
557  continue;
558  }
559  /*
560  Line to.
561  */
562  next_direction=((path_info[i].point.y > point.y) ||
563  ((fabs(path_info[i].point.y-point.y) < DrawEpsilon) &&
564  (path_info[i].point.x > point.x))) ? 1 : -1;
565  if ((points != (PointInfo *) NULL) && (direction != 0) &&
566  (direction != next_direction))
567  {
568  /*
569  New edge.
570  */
571  point=points[n-1];
572  if (edge == number_edges)
573  {
574  number_edges<<=1;
575  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
576  polygon_info->edges,(size_t) number_edges,
577  sizeof(*polygon_info->edges));
578  if (polygon_info->edges == (EdgeInfo *) NULL)
579  return((PolygonInfo *) NULL);
580  }
581  polygon_info->edges[edge].number_points=(size_t) n;
582  polygon_info->edges[edge].scanline=(-1.0);
583  polygon_info->edges[edge].highwater=0;
584  polygon_info->edges[edge].ghostline=ghostline;
585  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
586  if (direction < 0)
587  ReversePoints(points,(size_t) n);
588  polygon_info->edges[edge].points=points;
589  polygon_info->edges[edge].bounds=bounds;
590  polygon_info->edges[edge].bounds.y1=points[0].y;
591  polygon_info->edges[edge].bounds.y2=points[n-1].y;
592  number_points=16;
593  points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
594  sizeof(*points));
595  if (points == (PointInfo *) NULL)
596  return((PolygonInfo *) NULL);
597  n=1;
598  ghostline=MagickFalse;
599  points[0]=point;
600  bounds.x1=point.x;
601  bounds.x2=point.x;
602  edge++;
603  }
604  direction=next_direction;
605  if (points == (PointInfo *) NULL)
606  continue;
607  if (n == (ssize_t) number_points)
608  {
609  number_points<<=1;
610  points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
611  sizeof(*points));
612  if (points == (PointInfo *) NULL)
613  return((PolygonInfo *) NULL);
614  }
615  point=path_info[i].point;
616  points[n]=point;
617  if (point.x < bounds.x1)
618  bounds.x1=point.x;
619  if (point.x > bounds.x2)
620  bounds.x2=point.x;
621  n++;
622  }
623  if (points != (PointInfo *) NULL)
624  {
625  if (n < 2)
626  points=(PointInfo *) RelinquishMagickMemory(points);
627  else
628  {
629  if (edge == number_edges)
630  {
631  number_edges<<=1;
632  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
633  polygon_info->edges,(size_t) number_edges,
634  sizeof(*polygon_info->edges));
635  if (polygon_info->edges == (EdgeInfo *) NULL)
636  return((PolygonInfo *) NULL);
637  }
638  polygon_info->edges[edge].number_points=(size_t) n;
639  polygon_info->edges[edge].scanline=(-1.0);
640  polygon_info->edges[edge].highwater=0;
641  polygon_info->edges[edge].ghostline=ghostline;
642  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
643  if (direction < 0)
644  ReversePoints(points,(size_t) n);
645  polygon_info->edges[edge].points=points;
646  polygon_info->edges[edge].bounds=bounds;
647  polygon_info->edges[edge].bounds.y1=points[0].y;
648  polygon_info->edges[edge].bounds.y2=points[n-1].y;
649  ghostline=MagickFalse;
650  edge++;
651  }
652  }
653  polygon_info->number_edges=edge;
654  qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
655  sizeof(*polygon_info->edges),CompareEdges);
656  if (IsEventLogging() != MagickFalse)
657  LogPolygonInfo(polygon_info);
658  return(polygon_info);
659 }
660 
661 /*
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 % %
664 % %
665 % %
666 + C o n v e r t P r i m i t i v e T o P a t h %
667 % %
668 % %
669 % %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 %
672 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
673 % path structure.
674 %
675 % The format of the ConvertPrimitiveToPath method is:
676 %
677 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
678 % const PrimitiveInfo *primitive_info)
679 %
680 % A description of each parameter follows:
681 %
682 % o Method ConvertPrimitiveToPath returns a vector path structure of type
683 % PathInfo.
684 %
685 % o draw_info: a structure of type DrawInfo.
686 %
687 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
688 %
689 %
690 */
691 
692 static void LogPathInfo(const PathInfo *path_info)
693 {
694  register const PathInfo
695  *p;
696 
697  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
698  for (p=path_info; p->code != EndCode; p++)
700  " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
701  "moveto ghostline" : p->code == OpenCode ? "moveto open" :
702  p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
703  "?");
704  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
705 }
706 
707 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
708 {
709  PathInfo
710  *path_info;
711 
713  code;
714 
715  PointInfo
716  p,
717  q;
718 
719  register ssize_t
720  i,
721  n;
722 
723  ssize_t
724  coordinates,
725  start;
726 
727  /*
728  Converts a PrimitiveInfo structure into a vector path structure.
729  */
730  switch (primitive_info->primitive)
731  {
732  case AlphaPrimitive:
733  case ColorPrimitive:
734  case ImagePrimitive:
735  case PointPrimitive:
736  case TextPrimitive:
737  return((PathInfo *) NULL);
738  default:
739  break;
740  }
741  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
742  path_info=(PathInfo *) AcquireQuantumMemory((size_t) (2UL*i+3UL),
743  sizeof(*path_info));
744  if (path_info == (PathInfo *) NULL)
745  return((PathInfo *) NULL);
746  coordinates=0;
747  n=0;
748  p.x=(-1.0);
749  p.y=(-1.0);
750  q.x=(-1.0);
751  q.y=(-1.0);
752  start=0;
753  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
754  {
755  code=LineToCode;
756  if (coordinates <= 0)
757  {
758  coordinates=(ssize_t) primitive_info[i].coordinates;
759  p=primitive_info[i].point;
760  start=n;
761  code=MoveToCode;
762  }
763  coordinates--;
764  /*
765  Eliminate duplicate points.
766  */
767  if ((i == 0) || (fabs(q.x-primitive_info[i].point.x) >= DrawEpsilon) ||
768  (fabs(q.y-primitive_info[i].point.y) >= DrawEpsilon))
769  {
770  path_info[n].code=code;
771  path_info[n].point=primitive_info[i].point;
772  q=primitive_info[i].point;
773  n++;
774  }
775  if (coordinates > 0)
776  continue;
777  if ((fabs(p.x-primitive_info[i].point.x) < DrawEpsilon) &&
778  (fabs(p.y-primitive_info[i].point.y) < DrawEpsilon))
779  continue;
780  /*
781  Mark the p point as open if it does not match the q.
782  */
783  path_info[start].code=OpenCode;
784  path_info[n].code=GhostlineCode;
785  path_info[n].point=primitive_info[i].point;
786  n++;
787  path_info[n].code=LineToCode;
788  path_info[n].point=p;
789  n++;
790  }
791  path_info[n].code=EndCode;
792  path_info[n].point.x=0.0;
793  path_info[n].point.y=0.0;
794  if (IsEventLogging() != MagickFalse)
795  LogPathInfo(path_info);
796  return(path_info);
797 }
798 
799 /*
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % %
802 % %
803 % %
804 % D e s t r o y D r a w I n f o %
805 % %
806 % %
807 % %
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 %
810 % DestroyDrawInfo() deallocates memory associated with an DrawInfo
811 % structure.
812 %
813 % The format of the DestroyDrawInfo method is:
814 %
815 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
816 %
817 % A description of each parameter follows:
818 %
819 % o draw_info: the draw info.
820 %
821 */
823 {
824  if (draw_info->debug != MagickFalse)
825  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
826  assert(draw_info != (DrawInfo *) NULL);
827  assert(draw_info->signature == MagickCoreSignature);
828  if (draw_info->primitive != (char *) NULL)
829  draw_info->primitive=DestroyString(draw_info->primitive);
830  if (draw_info->text != (char *) NULL)
831  draw_info->text=DestroyString(draw_info->text);
832  if (draw_info->geometry != (char *) NULL)
833  draw_info->geometry=DestroyString(draw_info->geometry);
834  if (draw_info->fill_pattern != (Image *) NULL)
835  draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
836  if (draw_info->stroke_pattern != (Image *) NULL)
837  draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
838  if (draw_info->font != (char *) NULL)
839  draw_info->font=DestroyString(draw_info->font);
840  if (draw_info->metrics != (char *) NULL)
841  draw_info->metrics=DestroyString(draw_info->metrics);
842  if (draw_info->family != (char *) NULL)
843  draw_info->family=DestroyString(draw_info->family);
844  if (draw_info->encoding != (char *) NULL)
845  draw_info->encoding=DestroyString(draw_info->encoding);
846  if (draw_info->density != (char *) NULL)
847  draw_info->density=DestroyString(draw_info->density);
848  if (draw_info->server_name != (char *) NULL)
849  draw_info->server_name=(char *)
851  if (draw_info->dash_pattern != (double *) NULL)
852  draw_info->dash_pattern=(double *) RelinquishMagickMemory(
853  draw_info->dash_pattern);
854  if (draw_info->gradient.stops != (StopInfo *) NULL)
856  draw_info->gradient.stops);
857  if (draw_info->clip_mask != (char *) NULL)
858  draw_info->clip_mask=DestroyString(draw_info->clip_mask);
859  draw_info->signature=(~MagickCoreSignature);
860  draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
861  return(draw_info);
862 }
863 
864 /*
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866 % %
867 % %
868 % %
869 + D e s t r o y E d g e %
870 % %
871 % %
872 % %
873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 %
875 % DestroyEdge() destroys the specified polygon edge.
876 %
877 % The format of the DestroyEdge method is:
878 %
879 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
880 %
881 % A description of each parameter follows:
882 %
883 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
884 %
885 % o edge: the polygon edge number to destroy.
886 %
887 */
888 static size_t DestroyEdge(PolygonInfo *polygon_info,
889  const size_t edge)
890 {
891  assert(edge < polygon_info->number_edges);
892  polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
893  polygon_info->edges[edge].points);
894  polygon_info->number_edges--;
895  if (edge < polygon_info->number_edges)
896  (void) CopyMagickMemory(polygon_info->edges+edge,polygon_info->edges+edge+1,
897  (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
898  return(polygon_info->number_edges);
899 }
900 
901 /*
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 % %
904 % %
905 % %
906 + D e s t r o y P o l y g o n I n f o %
907 % %
908 % %
909 % %
910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 %
912 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
913 %
914 % The format of the DestroyPolygonInfo method is:
915 %
916 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
917 %
918 % A description of each parameter follows:
919 %
920 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
921 %
922 */
924 {
925  register ssize_t
926  i;
927 
928  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
929  polygon_info->edges[i].points=(PointInfo *)
930  RelinquishMagickMemory(polygon_info->edges[i].points);
931  polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
932  return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
933 }
934 
935 /*
936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 % %
938 % %
939 % %
940 % D r a w A f f i n e I m a g e %
941 % %
942 % %
943 % %
944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
945 %
946 % DrawAffineImage() composites the source over the destination image as
947 % dictated by the affine transform.
948 %
949 % The format of the DrawAffineImage method is:
950 %
951 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
952 % const AffineMatrix *affine,ExceptionInfo *exception)
953 %
954 % A description of each parameter follows:
955 %
956 % o image: the image.
957 %
958 % o source: the source image.
959 %
960 % o affine: the affine transform.
961 %
962 % o exception: return any errors or warnings in this structure.
963 %
964 */
965 
966 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
967  const double y,const SegmentInfo *edge)
968 {
969  double
970  intercept,
971  z;
972 
973  register double
974  x;
975 
977  inverse_edge;
978 
979  /*
980  Determine left and right edges.
981  */
982  inverse_edge.x1=edge->x1;
983  inverse_edge.y1=edge->y1;
984  inverse_edge.x2=edge->x2;
985  inverse_edge.y2=edge->y2;
986  z=affine->ry*y+affine->tx;
987  if (affine->sx >= DrawEpsilon)
988  {
989  intercept=(-z/affine->sx);
990  x=intercept;
991  if (x > inverse_edge.x1)
992  inverse_edge.x1=x;
993  intercept=(-z+(double) image->columns)/affine->sx;
994  x=intercept;
995  if (x < inverse_edge.x2)
996  inverse_edge.x2=x;
997  }
998  else
999  if (affine->sx < -DrawEpsilon)
1000  {
1001  intercept=(-z+(double) image->columns)/affine->sx;
1002  x=intercept;
1003  if (x > inverse_edge.x1)
1004  inverse_edge.x1=x;
1005  intercept=(-z/affine->sx);
1006  x=intercept;
1007  if (x < inverse_edge.x2)
1008  inverse_edge.x2=x;
1009  }
1010  else
1011  if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1012  {
1013  inverse_edge.x2=edge->x1;
1014  return(inverse_edge);
1015  }
1016  /*
1017  Determine top and bottom edges.
1018  */
1019  z=affine->sy*y+affine->ty;
1020  if (affine->rx >= DrawEpsilon)
1021  {
1022  intercept=(-z/affine->rx);
1023  x=intercept;
1024  if (x > inverse_edge.x1)
1025  inverse_edge.x1=x;
1026  intercept=(-z+(double) image->rows)/affine->rx;
1027  x=intercept;
1028  if (x < inverse_edge.x2)
1029  inverse_edge.x2=x;
1030  }
1031  else
1032  if (affine->rx < -DrawEpsilon)
1033  {
1034  intercept=(-z+(double) image->rows)/affine->rx;
1035  x=intercept;
1036  if (x > inverse_edge.x1)
1037  inverse_edge.x1=x;
1038  intercept=(-z/affine->rx);
1039  x=intercept;
1040  if (x < inverse_edge.x2)
1041  inverse_edge.x2=x;
1042  }
1043  else
1044  if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1045  {
1046  inverse_edge.x2=edge->x2;
1047  return(inverse_edge);
1048  }
1049  return(inverse_edge);
1050 }
1051 
1053 {
1054  AffineMatrix
1055  inverse_affine;
1056 
1057  double
1058  determinant;
1059 
1060  determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1061  affine->ry);
1062  inverse_affine.sx=determinant*affine->sy;
1063  inverse_affine.rx=determinant*(-affine->rx);
1064  inverse_affine.ry=determinant*(-affine->ry);
1065  inverse_affine.sy=determinant*affine->sx;
1066  inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1067  inverse_affine.ry;
1068  inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1069  inverse_affine.sy;
1070  return(inverse_affine);
1071 }
1072 
1074  const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1075 {
1076  AffineMatrix
1077  inverse_affine;
1078 
1079  CacheView
1080  *image_view,
1081  *source_view;
1082 
1084  status;
1085 
1086  PixelInfo
1087  zero;
1088 
1089  PointInfo
1090  extent[4],
1091  min,
1092  max;
1093 
1094  register ssize_t
1095  i;
1096 
1097  SegmentInfo
1098  edge;
1099 
1100  ssize_t
1101  start,
1102  stop,
1103  y;
1104 
1105  /*
1106  Determine bounding box.
1107  */
1108  assert(image != (Image *) NULL);
1109  assert(image->signature == MagickCoreSignature);
1110  if (image->debug != MagickFalse)
1111  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1112  assert(source != (const Image *) NULL);
1113  assert(source->signature == MagickCoreSignature);
1114  assert(affine != (AffineMatrix *) NULL);
1115  extent[0].x=0.0;
1116  extent[0].y=0.0;
1117  extent[1].x=(double) source->columns-1.0;
1118  extent[1].y=0.0;
1119  extent[2].x=(double) source->columns-1.0;
1120  extent[2].y=(double) source->rows-1.0;
1121  extent[3].x=0.0;
1122  extent[3].y=(double) source->rows-1.0;
1123  for (i=0; i < 4; i++)
1124  {
1125  PointInfo
1126  point;
1127 
1128  point=extent[i];
1129  extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1130  extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1131  }
1132  min=extent[0];
1133  max=extent[0];
1134  for (i=1; i < 4; i++)
1135  {
1136  if (min.x > extent[i].x)
1137  min.x=extent[i].x;
1138  if (min.y > extent[i].y)
1139  min.y=extent[i].y;
1140  if (max.x < extent[i].x)
1141  max.x=extent[i].x;
1142  if (max.y < extent[i].y)
1143  max.y=extent[i].y;
1144  }
1145  /*
1146  Affine transform image.
1147  */
1148  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1149  return(MagickFalse);
1150  status=MagickTrue;
1151  edge.x1=MagickMax(min.x,0.0);
1152  edge.y1=MagickMax(min.y,0.0);
1153  edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1154  edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1155  inverse_affine=InverseAffineMatrix(affine);
1156  GetPixelInfo(image,&zero);
1157  start=(ssize_t) ceil(edge.y1-0.5);
1158  stop=(ssize_t) floor(edge.y2+0.5);
1159  source_view=AcquireVirtualCacheView(source,exception);
1160  image_view=AcquireAuthenticCacheView(image,exception);
1161 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1162  #pragma omp parallel for schedule(static,4) shared(status) \
1163  magick_number_threads(source,image,stop-start,1)
1164 #endif
1165  for (y=start; y <= stop; y++)
1166  {
1167  PixelInfo
1168  composite,
1169  pixel;
1170 
1171  PointInfo
1172  point;
1173 
1174  register ssize_t
1175  x;
1176 
1177  register Quantum
1178  *magick_restrict q;
1179 
1180  SegmentInfo
1181  inverse_edge;
1182 
1183  ssize_t
1184  x_offset;
1185 
1186  inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1187  if (inverse_edge.x2 < inverse_edge.x1)
1188  continue;
1189  q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1190  0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1191  1,exception);
1192  if (q == (Quantum *) NULL)
1193  continue;
1194  pixel=zero;
1195  composite=zero;
1196  x_offset=0;
1197  for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1198  {
1199  point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1200  inverse_affine.tx;
1201  point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1202  inverse_affine.ty;
1203  status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1204  point.x,point.y,&pixel,exception);
1205  if (status == MagickFalse)
1206  break;
1207  GetPixelInfoPixel(image,q,&composite);
1208  CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1209  &composite);
1210  SetPixelViaPixelInfo(image,&composite,q);
1211  x_offset++;
1212  q+=GetPixelChannels(image);
1213  }
1214  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1215  status=MagickFalse;
1216  }
1217  source_view=DestroyCacheView(source_view);
1218  image_view=DestroyCacheView(image_view);
1219  return(status);
1220 }
1221 
1222 /*
1223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1224 % %
1225 % %
1226 % %
1227 + D r a w B o u n d i n g R e c t a n g l e s %
1228 % %
1229 % %
1230 % %
1231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 %
1233 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1234 % is only useful for developers debugging the rendering algorithm.
1235 %
1236 % The format of the DrawBoundingRectangles method is:
1237 %
1238 % void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1239 % PolygonInfo *polygon_info,ExceptionInfo *exception)
1240 %
1241 % A description of each parameter follows:
1242 %
1243 % o image: the image.
1244 %
1245 % o draw_info: the draw info.
1246 %
1247 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1248 %
1249 % o exception: return any errors or warnings in this structure.
1250 %
1251 */
1252 static void DrawBoundingRectangles(Image *image,const DrawInfo *draw_info,
1253  const PolygonInfo *polygon_info,ExceptionInfo *exception)
1254 {
1255  DrawInfo
1256  *clone_info;
1257 
1258  double
1259  mid;
1260 
1261  PointInfo
1262  end,
1263  resolution,
1264  start;
1265 
1267  primitive_info[6];
1268 
1269  register ssize_t
1270  i;
1271 
1272  SegmentInfo
1273  bounds;
1274 
1275  ssize_t
1276  coordinates;
1277 
1278  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1279  (void) QueryColorCompliance("#000F",AllCompliance,&clone_info->fill,
1280  exception);
1281  resolution.x=96.0;
1282  resolution.y=96.0;
1283  if (clone_info->density != (char *) NULL)
1284  {
1285  GeometryInfo
1286  geometry_info;
1287 
1289  flags;
1290 
1291  flags=ParseGeometry(clone_info->density,&geometry_info);
1292  resolution.x=geometry_info.rho;
1293  resolution.y=geometry_info.sigma;
1294  if ((flags & SigmaValue) == MagickFalse)
1295  resolution.y=resolution.x;
1296  }
1297  mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1298  clone_info->stroke_width/2.0;
1299  bounds.x1=0.0;
1300  bounds.y1=0.0;
1301  bounds.x2=0.0;
1302  bounds.y2=0.0;
1303  if (polygon_info != (PolygonInfo *) NULL)
1304  {
1305  bounds=polygon_info->edges[0].bounds;
1306  for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1307  {
1308  if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1309  bounds.x1=polygon_info->edges[i].bounds.x1;
1310  if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1311  bounds.y1=polygon_info->edges[i].bounds.y1;
1312  if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1313  bounds.x2=polygon_info->edges[i].bounds.x2;
1314  if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1315  bounds.y2=polygon_info->edges[i].bounds.y2;
1316  }
1317  bounds.x1-=mid;
1318  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1319  image->columns ? (double) image->columns-1 : bounds.x1;
1320  bounds.y1-=mid;
1321  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1322  image->rows ? (double) image->rows-1 : bounds.y1;
1323  bounds.x2+=mid;
1324  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1325  image->columns ? (double) image->columns-1 : bounds.x2;
1326  bounds.y2+=mid;
1327  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1328  image->rows ? (double) image->rows-1 : bounds.y2;
1329  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1330  {
1331  if (polygon_info->edges[i].direction != 0)
1332  (void) QueryColorCompliance("red",AllCompliance,&clone_info->stroke,
1333  exception);
1334  else
1335  (void) QueryColorCompliance("green",AllCompliance,&clone_info->stroke,
1336  exception);
1337  start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1338  start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1339  end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1340  end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1341  primitive_info[0].primitive=RectanglePrimitive;
1342  TraceRectangle(primitive_info,start,end);
1343  primitive_info[0].method=ReplaceMethod;
1344  coordinates=(ssize_t) primitive_info[0].coordinates;
1345  primitive_info[coordinates].primitive=UndefinedPrimitive;
1346  (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1347  }
1348  }
1349  (void) QueryColorCompliance("blue",AllCompliance,&clone_info->stroke,
1350  exception);
1351  start.x=(double) (bounds.x1-mid);
1352  start.y=(double) (bounds.y1-mid);
1353  end.x=(double) (bounds.x2+mid);
1354  end.y=(double) (bounds.y2+mid);
1355  primitive_info[0].primitive=RectanglePrimitive;
1356  TraceRectangle(primitive_info,start,end);
1357  primitive_info[0].method=ReplaceMethod;
1358  coordinates=(ssize_t) primitive_info[0].coordinates;
1359  primitive_info[coordinates].primitive=UndefinedPrimitive;
1360  (void) DrawPrimitive(image,clone_info,primitive_info,exception);
1361  clone_info=DestroyDrawInfo(clone_info);
1362 }
1363 
1364 /*
1365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 % %
1367 % %
1368 % %
1369 % D r a w C l i p P a t h %
1370 % %
1371 % %
1372 % %
1373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 %
1375 % DrawClipPath() draws the clip path on the image mask.
1376 %
1377 % The format of the DrawClipPath method is:
1378 %
1379 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1380 % const char *name,ExceptionInfo *exception)
1381 %
1382 % A description of each parameter follows:
1383 %
1384 % o image: the image.
1385 %
1386 % o draw_info: the draw info.
1387 %
1388 % o name: the name of the clip path.
1389 %
1390 % o exception: return any errors or warnings in this structure.
1391 %
1392 */
1394  const DrawInfo *draw_info,const char *name,ExceptionInfo *exception)
1395 {
1396  char
1397  filename[MagickPathExtent];
1398 
1399  Image
1400  *clip_mask;
1401 
1402  const char
1403  *value;
1404 
1405  DrawInfo
1406  *clone_info;
1407 
1409  status;
1410 
1411  assert(image != (Image *) NULL);
1412  assert(image->signature == MagickCoreSignature);
1413  if (image->debug != MagickFalse)
1414  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1415  assert(draw_info != (const DrawInfo *) NULL);
1416  (void) FormatLocaleString(filename,MagickPathExtent,"%s",name);
1417  value=GetImageArtifact(image,filename);
1418  if (value == (const char *) NULL)
1419  return(MagickFalse);
1420  clip_mask=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1421  if (clip_mask == (Image *) NULL)
1422  return(MagickFalse);
1423  (void) QueryColorCompliance("#0000",AllCompliance,
1424  &clip_mask->background_color,exception);
1426  (void) SetImageBackgroundColor(clip_mask,exception);
1427  if (image->debug != MagickFalse)
1428  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1429  draw_info->clip_mask);
1430  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1431  (void) CloneString(&clone_info->primitive,value);
1432  (void) QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1433  exception);
1434  clone_info->clip_mask=(char *) NULL;
1435  status=NegateImage(clip_mask,MagickFalse,exception);
1436  (void) SetImageMask(image,ReadPixelMask,clip_mask,exception);
1437  clip_mask=DestroyImage(clip_mask);
1438  status&=DrawImage(image,clone_info,exception);
1439  clone_info=DestroyDrawInfo(clone_info);
1440  if (image->debug != MagickFalse)
1441  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1442  return(status != 0 ? MagickTrue : MagickFalse);
1443 }
1444 
1445 /*
1446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % %
1448 % %
1449 % %
1450 + D r a w D a s h P o l y g o n %
1451 % %
1452 % %
1453 % %
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1455 %
1456 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1457 % image while respecting the dash offset and dash pattern attributes.
1458 %
1459 % The format of the DrawDashPolygon method is:
1460 %
1461 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1462 % const PrimitiveInfo *primitive_info,Image *image,
1463 % ExceptionInfo *exception)
1464 %
1465 % A description of each parameter follows:
1466 %
1467 % o draw_info: the draw info.
1468 %
1469 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1470 %
1471 % o image: the image.
1472 %
1473 % o exception: return any errors or warnings in this structure.
1474 %
1475 */
1477  const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1478 {
1479  DrawInfo
1480  *clone_info;
1481 
1482  double
1483  length,
1484  maximum_length,
1485  offset,
1486  scale,
1487  total_length;
1488 
1490  status;
1491 
1493  *dash_polygon;
1494 
1495  register ssize_t
1496  i;
1497 
1498  register double
1499  dx,
1500  dy;
1501 
1502  size_t
1503  number_vertices;
1504 
1505  ssize_t
1506  j,
1507  n;
1508 
1509  assert(draw_info != (const DrawInfo *) NULL);
1510  if (image->debug != MagickFalse)
1511  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1512  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1513  number_vertices=(size_t) i;
1514  dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1515  (2UL*number_vertices+1UL),sizeof(*dash_polygon));
1516  if (dash_polygon == (PrimitiveInfo *) NULL)
1517  return(MagickFalse);
1518  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1519  clone_info->miterlimit=0;
1520  dash_polygon[0]=primitive_info[0];
1521  scale=ExpandAffine(&draw_info->affine);
1522  length=scale*(draw_info->dash_pattern[0]-0.5);
1523  offset=fabs(draw_info->dash_offset) >= DrawEpsilon ?
1524  scale*draw_info->dash_offset : 0.0;
1525  j=1;
1526  for (n=0; offset > 0.0; j=0)
1527  {
1528  if (draw_info->dash_pattern[n] <= 0.0)
1529  break;
1530  length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1531  if (offset > length)
1532  {
1533  offset-=length;
1534  n++;
1535  length=scale*(draw_info->dash_pattern[n]+0.5);
1536  continue;
1537  }
1538  if (offset < length)
1539  {
1540  length-=offset;
1541  offset=0.0;
1542  break;
1543  }
1544  offset=0.0;
1545  n++;
1546  }
1547  status=MagickTrue;
1548  maximum_length=0.0;
1549  total_length=0.0;
1550  for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1551  {
1552  dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1553  dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1554  maximum_length=hypot((double) dx,dy);
1555  if (fabs(length) < DrawEpsilon)
1556  {
1557  n++;
1558  if (fabs(draw_info->dash_pattern[n]) < DrawEpsilon)
1559  n=0;
1560  length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1561  }
1562  for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1563  {
1564  total_length+=length;
1565  if ((n & 0x01) != 0)
1566  {
1567  dash_polygon[0]=primitive_info[0];
1568  dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1569  total_length/maximum_length);
1570  dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1571  total_length/maximum_length);
1572  j=1;
1573  }
1574  else
1575  {
1576  if ((j+1) > (ssize_t) (2*number_vertices))
1577  break;
1578  dash_polygon[j]=primitive_info[i-1];
1579  dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1580  total_length/maximum_length);
1581  dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1582  total_length/maximum_length);
1583  dash_polygon[j].coordinates=1;
1584  j++;
1585  dash_polygon[0].coordinates=(size_t) j;
1586  dash_polygon[j].primitive=UndefinedPrimitive;
1587  status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1588  }
1589  n++;
1590  if (fabs(draw_info->dash_pattern[n]) < DrawEpsilon)
1591  n=0;
1592  length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1593  }
1594  length-=(maximum_length-total_length);
1595  if ((n & 0x01) != 0)
1596  continue;
1597  dash_polygon[j]=primitive_info[i];
1598  dash_polygon[j].coordinates=1;
1599  j++;
1600  }
1601  if ((total_length <= maximum_length) && ((n & 0x01) == 0) && (j > 1))
1602  {
1603  dash_polygon[j]=primitive_info[i-1];
1604  dash_polygon[j].point.x+=DrawEpsilon;
1605  dash_polygon[j].point.y+=DrawEpsilon;
1606  dash_polygon[j].coordinates=1;
1607  j++;
1608  dash_polygon[0].coordinates=(size_t) j;
1609  dash_polygon[j].primitive=UndefinedPrimitive;
1610  status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1611  }
1612  dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1613  clone_info=DestroyDrawInfo(clone_info);
1614  if (image->debug != MagickFalse)
1615  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1616  return(status != 0 ? MagickTrue : MagickFalse);
1617 }
1618 
1619 /*
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 % %
1622 % %
1623 % %
1624 % D r a w I m a g e %
1625 % %
1626 % %
1627 % %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629 %
1630 % DrawImage() draws a graphic primitive on your image. The primitive
1631 % may be represented as a string or filename. Precede the filename with an
1632 % "at" sign (@) and the contents of the file are drawn on the image. You
1633 % can affect how text is drawn by setting one or more members of the draw
1634 % info structure.
1635 %
1636 % The format of the DrawImage method is:
1637 %
1638 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
1639 % ExceptionInfo *exception)
1640 %
1641 % A description of each parameter follows:
1642 %
1643 % o image: the image.
1644 %
1645 % o draw_info: the draw info.
1646 %
1647 % o exception: return any errors or warnings in this structure.
1648 %
1649 */
1650 
1651 static inline MagickBooleanType IsPoint(const char *point)
1652 {
1653  char
1654  *p;
1655 
1656  double
1657  value;
1658 
1659  value=StringToDouble(point,&p);
1660  return((fabs(value) < DrawEpsilon) && (p == point) ? MagickFalse : MagickTrue);
1661 }
1662 
1663 static inline void TracePoint(PrimitiveInfo *primitive_info,
1664  const PointInfo point)
1665 {
1666  primitive_info->coordinates=1;
1667  primitive_info->point=point;
1668 }
1669 
1671  ExceptionInfo *exception)
1672 {
1673 #define RenderImageTag "Render/Image"
1674 
1675  AffineMatrix
1676  affine,
1677  current;
1678 
1679  char
1680  keyword[MagickPathExtent],
1681  geometry[MagickPathExtent],
1682  *next_token,
1683  pattern[MagickPathExtent],
1684  *primitive,
1685  *token;
1686 
1687  const char
1688  *q;
1689 
1690  double
1691  angle,
1692  factor,
1693  points_extent,
1694  primitive_extent;
1695 
1696  DrawInfo
1697  **graphic_context;
1698 
1700  proceed;
1701 
1703  number_points;
1704 
1706  status;
1707 
1708  PointInfo
1709  point;
1710 
1712  *primitive_info;
1713 
1715  primitive_type;
1716 
1717  register const char
1718  *p;
1719 
1720  register ssize_t
1721  i,
1722  x;
1723 
1724  SegmentInfo
1725  bounds;
1726 
1727  size_t
1728  extent,
1729  number_stops;
1730 
1731  ssize_t
1732  j,
1733  k,
1734  n;
1735 
1736  StopInfo
1737  *stops;
1738 
1739  assert(image != (Image *) NULL);
1740  assert(image->signature == MagickCoreSignature);
1741  if (image->debug != MagickFalse)
1742  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1743  assert(draw_info != (DrawInfo *) NULL);
1744  assert(draw_info->signature == MagickCoreSignature);
1745  if (image->debug != MagickFalse)
1746  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1747  if ((draw_info->primitive == (char *) NULL) ||
1748  (*draw_info->primitive == '\0'))
1749  return(MagickFalse);
1750  if (image->debug != MagickFalse)
1751  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
1752  if (*draw_info->primitive != '@')
1753  primitive=AcquireString(draw_info->primitive);
1754  else
1755  primitive=FileToString(draw_info->primitive+1,~0UL,exception);
1756  if (primitive == (char *) NULL)
1757  return(MagickFalse);
1758  primitive_extent=(double) strlen(primitive);
1759  (void) SetImageArtifact(image,"MVG",primitive);
1760  n=0;
1761  number_stops=0;
1762  stops=(StopInfo *) NULL;
1763  /*
1764  Allocate primitive info memory.
1765  */
1766  graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
1767  if (graphic_context == (DrawInfo **) NULL)
1768  {
1769  primitive=DestroyString(primitive);
1770  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1771  image->filename);
1772  }
1773  number_points=6553;
1774  primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
1775  sizeof(*primitive_info));
1776  if (primitive_info == (PrimitiveInfo *) NULL)
1777  {
1778  primitive=DestroyString(primitive);
1779  for ( ; n >= 0; n--)
1780  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
1781  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
1782  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1783  image->filename);
1784  }
1785  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1786  graphic_context[n]->viewbox=image->page;
1787  if ((image->page.width == 0) || (image->page.height == 0))
1788  {
1789  graphic_context[n]->viewbox.width=image->columns;
1790  graphic_context[n]->viewbox.height=image->rows;
1791  }
1792  token=AcquireString(primitive);
1793  extent=strlen(token)+MagickPathExtent;
1794  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1795  return(MagickFalse);
1796  status=MagickTrue;
1797  for (q=primitive; *q != '\0'; )
1798  {
1799  /*
1800  Interpret graphic primitive.
1801  */
1802  GetNextToken(q,&q,MagickPathExtent,keyword);
1803  if (*keyword == '\0')
1804  break;
1805  if (*keyword == '#')
1806  {
1807  /*
1808  Comment.
1809  */
1810  while ((*q != '\n') && (*q != '\0'))
1811  q++;
1812  continue;
1813  }
1814  p=q-strlen(keyword)-1;
1815  primitive_type=UndefinedPrimitive;
1816  current=graphic_context[n]->affine;
1817  GetAffineMatrix(&affine);
1818  switch (*keyword)
1819  {
1820  case ';':
1821  break;
1822  case 'a':
1823  case 'A':
1824  {
1825  if (LocaleCompare("affine",keyword) == 0)
1826  {
1827  GetNextToken(q,&q,extent,token);
1828  affine.sx=StringToDouble(token,&next_token);
1829  if (token == next_token)
1830  status=MagickFalse;
1831  GetNextToken(q,&q,extent,token);
1832  if (*token == ',')
1833  GetNextToken(q,&q,extent,token);
1834  affine.rx=StringToDouble(token,&next_token);
1835  if (token == next_token)
1836  status=MagickFalse;
1837  GetNextToken(q,&q,extent,token);
1838  if (*token == ',')
1839  GetNextToken(q,&q,extent,token);
1840  affine.ry=StringToDouble(token,&next_token);
1841  if (token == next_token)
1842  status=MagickFalse;
1843  GetNextToken(q,&q,extent,token);
1844  if (*token == ',')
1845  GetNextToken(q,&q,extent,token);
1846  affine.sy=StringToDouble(token,&next_token);
1847  if (token == next_token)
1848  status=MagickFalse;
1849  GetNextToken(q,&q,extent,token);
1850  if (*token == ',')
1851  GetNextToken(q,&q,extent,token);
1852  affine.tx=StringToDouble(token,&next_token);
1853  if (token == next_token)
1854  status=MagickFalse;
1855  GetNextToken(q,&q,extent,token);
1856  if (*token == ',')
1857  GetNextToken(q,&q,extent,token);
1858  affine.ty=StringToDouble(token,&next_token);
1859  if (token == next_token)
1860  status=MagickFalse;
1861  break;
1862  }
1863  if (LocaleCompare("alpha",keyword) == 0)
1864  {
1865  primitive_type=AlphaPrimitive;
1866  break;
1867  }
1868  if (LocaleCompare("arc",keyword) == 0)
1869  {
1870  primitive_type=ArcPrimitive;
1871  break;
1872  }
1873  status=MagickFalse;
1874  break;
1875  }
1876  case 'b':
1877  case 'B':
1878  {
1879  if (LocaleCompare("bezier",keyword) == 0)
1880  {
1881  primitive_type=BezierPrimitive;
1882  break;
1883  }
1884  if (LocaleCompare("border-color",keyword) == 0)
1885  {
1886  GetNextToken(q,&q,extent,token);
1887  (void) QueryColorCompliance(token,AllCompliance,
1888  &graphic_context[n]->border_color,exception);
1889  break;
1890  }
1891  status=MagickFalse;
1892  break;
1893  }
1894  case 'c':
1895  case 'C':
1896  {
1897  if (LocaleCompare("clip-path",keyword) == 0)
1898  {
1899  /*
1900  Create clip mask.
1901  */
1902  GetNextToken(q,&q,extent,token);
1903  (void) CloneString(&graphic_context[n]->clip_mask,token);
1904  (void) DrawClipPath(image,graphic_context[n],
1905  graphic_context[n]->clip_mask,exception);
1906  break;
1907  }
1908  if (LocaleCompare("clip-rule",keyword) == 0)
1909  {
1910  ssize_t
1911  fill_rule;
1912 
1913  GetNextToken(q,&q,extent,token);
1915  token);
1916  if (fill_rule == -1)
1917  status=MagickFalse;
1918  else
1919  graphic_context[n]->fill_rule=(FillRule) fill_rule;
1920  break;
1921  }
1922  if (LocaleCompare("clip-units",keyword) == 0)
1923  {
1924  ssize_t
1925  clip_units;
1926 
1927  GetNextToken(q,&q,extent,token);
1929  token);
1930  if (clip_units == -1)
1931  {
1932  status=MagickFalse;
1933  break;
1934  }
1935  graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
1936  if (clip_units == ObjectBoundingBox)
1937  {
1938  GetAffineMatrix(&current);
1939  affine.sx=draw_info->bounds.x2;
1940  affine.sy=draw_info->bounds.y2;
1941  affine.tx=draw_info->bounds.x1;
1942  affine.ty=draw_info->bounds.y1;
1943  break;
1944  }
1945  break;
1946  }
1947  if (LocaleCompare("circle",keyword) == 0)
1948  {
1949  primitive_type=CirclePrimitive;
1950  break;
1951  }
1952  if (LocaleCompare("color",keyword) == 0)
1953  {
1954  primitive_type=ColorPrimitive;
1955  break;
1956  }
1957  status=MagickFalse;
1958  break;
1959  }
1960  case 'd':
1961  case 'D':
1962  {
1963  if (LocaleCompare("decorate",keyword) == 0)
1964  {
1965  ssize_t
1966  decorate;
1967 
1968  GetNextToken(q,&q,extent,token);
1970  token);
1971  if (decorate == -1)
1972  status=MagickFalse;
1973  else
1974  graphic_context[n]->decorate=(DecorationType) decorate;
1975  break;
1976  }
1977  if (LocaleCompare("density",keyword) == 0)
1978  {
1979  GetNextToken(q,&q,extent,token);
1980  (void) CloneString(&graphic_context[n]->density,token);
1981  break;
1982  }
1983  if (LocaleCompare("direction",keyword) == 0)
1984  {
1985  ssize_t
1986  direction;
1987 
1988  GetNextToken(q,&q,extent,token);
1990  token);
1991  if (direction == -1)
1992  status=MagickFalse;
1993  else
1994  graphic_context[n]->direction=(DirectionType) direction;
1995  break;
1996  }
1997  status=MagickFalse;
1998  break;
1999  }
2000  case 'e':
2001  case 'E':
2002  {
2003  if (LocaleCompare("ellipse",keyword) == 0)
2004  {
2005  primitive_type=EllipsePrimitive;
2006  break;
2007  }
2008  if (LocaleCompare("encoding",keyword) == 0)
2009  {
2010  GetNextToken(q,&q,extent,token);
2011  (void) CloneString(&graphic_context[n]->encoding,token);
2012  break;
2013  }
2014  status=MagickFalse;
2015  break;
2016  }
2017  case 'f':
2018  case 'F':
2019  {
2020  if (LocaleCompare("fill",keyword) == 0)
2021  {
2022  GetNextToken(q,&q,extent,token);
2023  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2024  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2025  (void) DrawPatternPath(image,draw_info,token,
2026  &graphic_context[n]->fill_pattern,exception);
2027  else
2028  {
2029  status&=QueryColorCompliance(token,AllCompliance,
2030  &graphic_context[n]->fill,exception);
2031  if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2032  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2033  if (status == MagickFalse)
2034  {
2035  ImageInfo
2036  *pattern_info;
2037 
2038  pattern_info=AcquireImageInfo();
2039  (void) CopyMagickString(pattern_info->filename,token,
2041  graphic_context[n]->fill_pattern=ReadImage(pattern_info,
2042  exception);
2043  CatchException(exception);
2044  pattern_info=DestroyImageInfo(pattern_info);
2045  }
2046  }
2047  break;
2048  }
2049  if (LocaleCompare("fill-opacity",keyword) == 0)
2050  {
2051  GetNextToken(q,&q,extent,token);
2052  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2053  graphic_context[n]->fill.alpha=(MagickRealType) (QuantumRange-
2055  StringToDouble(token,&next_token))));
2056  if (token == next_token)
2057  status=MagickFalse;
2058  break;
2059  }
2060  if (LocaleCompare("fill-rule",keyword) == 0)
2061  {
2062  ssize_t
2063  fill_rule;
2064 
2065  GetNextToken(q,&q,extent,token);
2067  token);
2068  if (fill_rule == -1)
2069  status=MagickFalse;
2070  else
2071  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2072  break;
2073  }
2074  if (LocaleCompare("font",keyword) == 0)
2075  {
2076  GetNextToken(q,&q,extent,token);
2077  (void) CloneString(&graphic_context[n]->font,token);
2078  if (LocaleCompare("none",token) == 0)
2079  graphic_context[n]->font=(char *) RelinquishMagickMemory(
2080  graphic_context[n]->font);
2081  break;
2082  }
2083  if (LocaleCompare("font-family",keyword) == 0)
2084  {
2085  GetNextToken(q,&q,extent,token);
2086  (void) CloneString(&graphic_context[n]->family,token);
2087  break;
2088  }
2089  if (LocaleCompare("font-size",keyword) == 0)
2090  {
2091  GetNextToken(q,&q,extent,token);
2092  graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2093  if (token == next_token)
2094  status=MagickFalse;
2095  break;
2096  }
2097  if (LocaleCompare("font-stretch",keyword) == 0)
2098  {
2099  ssize_t
2100  stretch;
2101 
2102  GetNextToken(q,&q,extent,token);
2104  if (stretch == -1)
2105  status=MagickFalse;
2106  else
2107  graphic_context[n]->stretch=(StretchType) stretch;
2108  break;
2109  }
2110  if (LocaleCompare("font-style",keyword) == 0)
2111  {
2112  ssize_t
2113  style;
2114 
2115  GetNextToken(q,&q,extent,token);
2117  if (style == -1)
2118  status=MagickFalse;
2119  else
2120  graphic_context[n]->style=(StyleType) style;
2121  break;
2122  }
2123  if (LocaleCompare("font-weight",keyword) == 0)
2124  {
2125  ssize_t
2126  weight;
2127 
2128  GetNextToken(q,&q,extent,token);
2130  if (weight == -1)
2131  weight=(ssize_t) StringToUnsignedLong(token);
2132  graphic_context[n]->weight=(size_t) weight;
2133  break;
2134  }
2135  status=MagickFalse;
2136  break;
2137  }
2138  case 'g':
2139  case 'G':
2140  {
2141  if (LocaleCompare("gradient-units",keyword) == 0)
2142  {
2143  GetNextToken(q,&q,extent,token);
2144  break;
2145  }
2146  if (LocaleCompare("gravity",keyword) == 0)
2147  {
2148  ssize_t
2149  gravity;
2150 
2151  GetNextToken(q,&q,extent,token);
2153  if (gravity == -1)
2154  status=MagickFalse;
2155  else
2156  graphic_context[n]->gravity=(GravityType) gravity;
2157  break;
2158  }
2159  status=MagickFalse;
2160  break;
2161  }
2162  case 'i':
2163  case 'I':
2164  {
2165  if (LocaleCompare("image",keyword) == 0)
2166  {
2167  ssize_t
2168  compose;
2169 
2170  primitive_type=ImagePrimitive;
2171  GetNextToken(q,&q,extent,token);
2173  if (compose == -1)
2174  status=MagickFalse;
2175  else
2176  graphic_context[n]->compose=(CompositeOperator) compose;
2177  break;
2178  }
2179  if (LocaleCompare("interline-spacing",keyword) == 0)
2180  {
2181  GetNextToken(q,&q,extent,token);
2182  graphic_context[n]->interline_spacing=StringToDouble(token,
2183  &next_token);
2184  if (token == next_token)
2185  status=MagickFalse;
2186  break;
2187  }
2188  if (LocaleCompare("interword-spacing",keyword) == 0)
2189  {
2190  GetNextToken(q,&q,extent,token);
2191  graphic_context[n]->interword_spacing=StringToDouble(token,
2192  &next_token);
2193  if (token == next_token)
2194  status=MagickFalse;
2195  break;
2196  }
2197  status=MagickFalse;
2198  break;
2199  }
2200  case 'k':
2201  case 'K':
2202  {
2203  if (LocaleCompare("kerning",keyword) == 0)
2204  {
2205  GetNextToken(q,&q,extent,token);
2206  graphic_context[n]->kerning=StringToDouble(token,&next_token);
2207  if (token == next_token)
2208  status=MagickFalse;
2209  break;
2210  }
2211  status=MagickFalse;
2212  break;
2213  }
2214  case 'l':
2215  case 'L':
2216  {
2217  if (LocaleCompare("line",keyword) == 0)
2218  primitive_type=LinePrimitive;
2219  else
2220  status=MagickFalse;
2221  break;
2222  }
2223  case 'o':
2224  case 'O':
2225  {
2226  if (LocaleCompare("offset",keyword) == 0)
2227  {
2228  GetNextToken(q,&q,extent,token);
2229  break;
2230  }
2231  if (LocaleCompare("opacity",keyword) == 0)
2232  {
2233  GetNextToken(q,&q,extent,token);
2234  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2235  graphic_context[n]->alpha=(Quantum) (QuantumRange*(1.0-
2236  (QuantumScale*graphic_context[n]->alpha*(1.0-factor*
2237  StringToDouble(token,&next_token)))));
2238  graphic_context[n]->fill_alpha=QuantumRange*(1.0-(QuantumScale*
2239  graphic_context[n]->fill_alpha*(1.0-factor*StringToDouble(token,
2240  &next_token))));
2241  graphic_context[n]->stroke_alpha=QuantumRange*(1.0-(QuantumScale*
2242  graphic_context[n]->stroke_alpha*(1.0-factor*StringToDouble(token,
2243  &next_token))));
2244  if (token == next_token)
2245  status=MagickFalse;
2246  break;
2247  }
2248  status=MagickFalse;
2249  break;
2250  }
2251  case 'p':
2252  case 'P':
2253  {
2254  if (LocaleCompare("path",keyword) == 0)
2255  {
2256  primitive_type=PathPrimitive;
2257  break;
2258  }
2259  if (LocaleCompare("point",keyword) == 0)
2260  {
2261  primitive_type=PointPrimitive;
2262  break;
2263  }
2264  if (LocaleCompare("polyline",keyword) == 0)
2265  {
2266  primitive_type=PolylinePrimitive;
2267  break;
2268  }
2269  if (LocaleCompare("polygon",keyword) == 0)
2270  {
2271  primitive_type=PolygonPrimitive;
2272  break;
2273  }
2274  if (LocaleCompare("pop",keyword) == 0)
2275  {
2276  GetNextToken(q,&q,extent,token);
2277  if (LocaleCompare("clip-path",token) == 0)
2278  break;
2279  if (LocaleCompare("defs",token) == 0)
2280  break;
2281  if (LocaleCompare("gradient",token) == 0)
2282  break;
2283  if (LocaleCompare("graphic-context",token) == 0)
2284  {
2285  if (n <= 0)
2286  {
2287  (void) ThrowMagickException(exception,GetMagickModule(),
2288  DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
2289  status=MagickFalse;
2290  n=0;
2291  break;
2292  }
2293  if (graphic_context[n]->clip_mask != (char *) NULL)
2294  if (LocaleCompare(graphic_context[n]->clip_mask,
2295  graphic_context[n-1]->clip_mask) != 0)
2296  (void) SetImageMask(image,ReadPixelMask,(Image *) NULL,
2297  exception);
2298  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2299  n--;
2300  break;
2301  }
2302  if (LocaleCompare("pattern",token) == 0)
2303  break;
2304  status=MagickFalse;
2305  break;
2306  }
2307  if (LocaleCompare("push",keyword) == 0)
2308  {
2309  GetNextToken(q,&q,extent,token);
2310  if (LocaleCompare("clip-path",token) == 0)
2311  {
2312  char
2314 
2315  GetNextToken(q,&q,extent,token);
2316  (void) FormatLocaleString(name,MagickPathExtent,"%s",token);
2317  for (p=q; *q != '\0'; )
2318  {
2319  GetNextToken(q,&q,extent,token);
2320  if (LocaleCompare(token,"pop") != 0)
2321  continue;
2322  GetNextToken(q,(const char **) NULL,extent,token);
2323  if (LocaleCompare(token,"clip-path") != 0)
2324  continue;
2325  break;
2326  }
2327  if ((size_t) (q-p-4+1) > 0)
2328  {
2329  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2330  (void) SetImageArtifact(image,name,token);
2331  }
2332  GetNextToken(q,&q,extent,token);
2333  break;
2334  }
2335  if (LocaleCompare("gradient",token) == 0)
2336  {
2337  char
2338  key[2*MagickPathExtent],
2340  type[MagickPathExtent];
2341 
2342  SegmentInfo
2343  segment;
2344 
2345  GetNextToken(q,&q,extent,token);
2346  (void) CopyMagickString(name,token,MagickPathExtent);
2347  GetNextToken(q,&q,extent,token);
2348  (void) CopyMagickString(type,token,MagickPathExtent);
2349  GetNextToken(q,&q,extent,token);
2350  segment.x1=StringToDouble(token,&next_token);
2351  if (token == next_token)
2352  status=MagickFalse;
2353  GetNextToken(q,&q,extent,token);
2354  if (*token == ',')
2355  GetNextToken(q,&q,extent,token);
2356  segment.y1=StringToDouble(token,&next_token);
2357  if (token == next_token)
2358  status=MagickFalse;
2359  GetNextToken(q,&q,extent,token);
2360  if (*token == ',')
2361  GetNextToken(q,&q,extent,token);
2362  segment.x2=StringToDouble(token,&next_token);
2363  if (token == next_token)
2364  status=MagickFalse;
2365  GetNextToken(q,&q,extent,token);
2366  if (*token == ',')
2367  GetNextToken(q,&q,extent,token);
2368  segment.y2=StringToDouble(token,&next_token);
2369  if (token == next_token)
2370  status=MagickFalse;
2371  if (LocaleCompare(type,"radial") == 0)
2372  {
2373  GetNextToken(q,&q,extent,token);
2374  if (*token == ',')
2375  GetNextToken(q,&q,extent,token);
2376  }
2377  for (p=q; *q != '\0'; )
2378  {
2379  GetNextToken(q,&q,extent,token);
2380  if (LocaleCompare(token,"pop") != 0)
2381  continue;
2382  GetNextToken(q,(const char **) NULL,extent,token);
2383  if (LocaleCompare(token,"gradient") != 0)
2384  continue;
2385  break;
2386  }
2387  if ((size_t) (q-p-4+1) > 0)
2388  {
2389  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2390  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2391  graphic_context[n]->affine.ry*segment.y1+
2392  graphic_context[n]->affine.tx;
2393  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2394  graphic_context[n]->affine.sy*segment.y1+
2395  graphic_context[n]->affine.ty;
2396  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2397  graphic_context[n]->affine.ry*segment.y2+
2398  graphic_context[n]->affine.tx;
2399  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2400  graphic_context[n]->affine.sy*segment.y2+
2401  graphic_context[n]->affine.ty;
2402  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2403  (void) SetImageArtifact(image,key,token);
2404  (void) FormatLocaleString(key,MagickPathExtent,"%s-type",
2405  name);
2406  (void) SetImageArtifact(image,key,type);
2408  "%s-geometry",name);
2409  (void) FormatLocaleString(geometry,MagickPathExtent,
2410  "%gx%g%+.15g%+.15g",
2411  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2412  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2413  bounds.x1,bounds.y1);
2414  (void) SetImageArtifact(image,key,geometry);
2415  }
2416  GetNextToken(q,&q,extent,token);
2417  break;
2418  }
2419  if (LocaleCompare("pattern",token) == 0)
2420  {
2421  char
2422  key[2*MagickPathExtent],
2424 
2426  pattern_bounds;
2427 
2428  GetNextToken(q,&q,extent,token);
2429  (void) CopyMagickString(name,token,MagickPathExtent);
2430  GetNextToken(q,&q,extent,token);
2431  pattern_bounds.x=(ssize_t) ceil(StringToDouble(token,
2432  &next_token)-0.5);
2433  if (token == next_token)
2434  status=MagickFalse;
2435  GetNextToken(q,&q,extent,token);
2436  if (*token == ',')
2437  GetNextToken(q,&q,extent,token);
2438  pattern_bounds.y=(ssize_t) ceil(StringToDouble(token,
2439  &next_token)-0.5);
2440  if (token == next_token)
2441  status=MagickFalse;
2442  GetNextToken(q,&q,extent,token);
2443  if (*token == ',')
2444  GetNextToken(q,&q,extent,token);
2445  pattern_bounds.width=(size_t) floor(StringToDouble(token,
2446  &next_token)+0.5);
2447  if (token == next_token)
2448  status=MagickFalse;
2449  GetNextToken(q,&q,extent,token);
2450  if (*token == ',')
2451  GetNextToken(q,&q,extent,token);
2452  pattern_bounds.height=(size_t) floor(StringToDouble(token,
2453  &next_token)+0.5);
2454  if (token == next_token)
2455  status=MagickFalse;
2456  for (p=q; *q != '\0'; )
2457  {
2458  GetNextToken(q,&q,extent,token);
2459  if (LocaleCompare(token,"pop") != 0)
2460  continue;
2461  GetNextToken(q,(const char **) NULL,extent,token);
2462  if (LocaleCompare(token,"pattern") != 0)
2463  continue;
2464  break;
2465  }
2466  if ((size_t) (q-p-4+1) > 0)
2467  {
2468  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2469  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
2470  (void) SetImageArtifact(image,key,token);
2472  "%s-geometry",name);
2473  (void) FormatLocaleString(geometry,MagickPathExtent,
2474  "%.20gx%.20g%+.20g%+.20g",(double)pattern_bounds.width,
2475  (double)pattern_bounds.height,(double)pattern_bounds.x,
2476  (double)pattern_bounds.y);
2477  (void) SetImageArtifact(image,key,geometry);
2478  }
2479  GetNextToken(q,&q,extent,token);
2480  break;
2481  }
2482  if (LocaleCompare("graphic-context",token) == 0)
2483  {
2484  n++;
2485  graphic_context=(DrawInfo **) ResizeQuantumMemory(
2486  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2487  if (graphic_context == (DrawInfo **) NULL)
2488  {
2489  (void) ThrowMagickException(exception,GetMagickModule(),
2490  ResourceLimitError,"MemoryAllocationFailed","`%s'",
2491  image->filename);
2492  break;
2493  }
2494  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2495  graphic_context[n-1]);
2496  break;
2497  }
2498  if (LocaleCompare("defs",token) == 0)
2499  break;
2500  status=MagickFalse;
2501  break;
2502  }
2503  status=MagickFalse;
2504  break;
2505  }
2506  case 'r':
2507  case 'R':
2508  {
2509  if (LocaleCompare("rectangle",keyword) == 0)
2510  {
2511  primitive_type=RectanglePrimitive;
2512  break;
2513  }
2514  if (LocaleCompare("rotate",keyword) == 0)
2515  {
2516  GetNextToken(q,&q,extent,token);
2517  angle=StringToDouble(token,&next_token);
2518  if (token == next_token)
2519  status=MagickFalse;
2520  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2521  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2522  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2523  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2524  break;
2525  }
2526  if (LocaleCompare("roundRectangle",keyword) == 0)
2527  {
2528  primitive_type=RoundRectanglePrimitive;
2529  break;
2530  }
2531  status=MagickFalse;
2532  break;
2533  }
2534  case 's':
2535  case 'S':
2536  {
2537  if (LocaleCompare("scale",keyword) == 0)
2538  {
2539  GetNextToken(q,&q,extent,token);
2540  affine.sx=StringToDouble(token,&next_token);
2541  if (token == next_token)
2542  status=MagickFalse;
2543  GetNextToken(q,&q,extent,token);
2544  if (*token == ',')
2545  GetNextToken(q,&q,extent,token);
2546  affine.sy=StringToDouble(token,&next_token);
2547  if (token == next_token)
2548  status=MagickFalse;
2549  break;
2550  }
2551  if (LocaleCompare("skewX",keyword) == 0)
2552  {
2553  GetNextToken(q,&q,extent,token);
2554  angle=StringToDouble(token,&next_token);
2555  if (token == next_token)
2556  status=MagickFalse;
2557  affine.ry=sin(DegreesToRadians(angle));
2558  break;
2559  }
2560  if (LocaleCompare("skewY",keyword) == 0)
2561  {
2562  GetNextToken(q,&q,extent,token);
2563  angle=StringToDouble(token,&next_token);
2564  if (token == next_token)
2565  status=MagickFalse;
2566  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2567  break;
2568  }
2569  if (LocaleCompare("stop-color",keyword) == 0)
2570  {
2571  PixelInfo
2572  stop_color;
2573 
2574  number_stops++;
2575  if (number_stops == 1)
2576  stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
2577  else if (number_stops > 2)
2578  stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
2579  sizeof(*stops));
2580  if (stops == (StopInfo *) NULL)
2581  {
2582  (void) ThrowMagickException(exception,GetMagickModule(),
2583  ResourceLimitError,"MemoryAllocationFailed","`%s'",
2584  image->filename);
2585  break;
2586  }
2587  GetNextToken(q,&q,extent,token);
2588  (void) QueryColorCompliance(token,AllCompliance,&stop_color,
2589  exception);
2590  stops[number_stops-1].color=stop_color;
2591  GetNextToken(q,&q,extent,token);
2592  stops[number_stops-1].offset=StringToDouble(token,&next_token);
2593  if (token == next_token)
2594  status=MagickFalse;
2595  break;
2596  }
2597  if (LocaleCompare("stroke",keyword) == 0)
2598  {
2599  GetNextToken(q,&q,extent,token);
2600  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2601  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2602  (void) DrawPatternPath(image,draw_info,token,
2603  &graphic_context[n]->stroke_pattern,exception);
2604  else
2605  {
2606  status&=QueryColorCompliance(token,AllCompliance,
2607  &graphic_context[n]->stroke,exception);
2608  if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
2609  graphic_context[n]->stroke.alpha=
2610  graphic_context[n]->stroke_alpha;
2611  if (status == MagickFalse)
2612  {
2613  ImageInfo
2614  *pattern_info;
2615 
2616  pattern_info=AcquireImageInfo();
2617  (void) CopyMagickString(pattern_info->filename,token,
2619  graphic_context[n]->stroke_pattern=ReadImage(pattern_info,
2620  exception);
2621  CatchException(exception);
2622  pattern_info=DestroyImageInfo(pattern_info);
2623  }
2624  }
2625  break;
2626  }
2627  if (LocaleCompare("stroke-antialias",keyword) == 0)
2628  {
2629  GetNextToken(q,&q,extent,token);
2630  graphic_context[n]->stroke_antialias=
2631  StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2632  break;
2633  }
2634  if (LocaleCompare("stroke-dasharray",keyword) == 0)
2635  {
2636  if (graphic_context[n]->dash_pattern != (double *) NULL)
2637  graphic_context[n]->dash_pattern=(double *)
2638  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2639  if (IsPoint(q) != MagickFalse)
2640  {
2641  const char
2642  *r;
2643 
2644  r=q;
2645  GetNextToken(r,&r,extent,token);
2646  if (*token == ',')
2647  GetNextToken(r,&r,extent,token);
2648  for (x=0; IsPoint(token) != MagickFalse; x++)
2649  {
2650  GetNextToken(r,&r,extent,token);
2651  if (*token == ',')
2652  GetNextToken(r,&r,extent,token);
2653  }
2654  graphic_context[n]->dash_pattern=(double *)
2655  AcquireQuantumMemory((size_t) (2UL*x+2UL),
2656  sizeof(*graphic_context[n]->dash_pattern));
2657  if (graphic_context[n]->dash_pattern == (double *) NULL)
2658  {
2659  (void) ThrowMagickException(exception,GetMagickModule(),
2660  ResourceLimitError,"MemoryAllocationFailed","`%s'",
2661  image->filename);
2662  status=MagickFalse;
2663  break;
2664  }
2665  for (j=0; j < x; j++)
2666  {
2667  GetNextToken(q,&q,extent,token);
2668  if (*token == ',')
2669  GetNextToken(q,&q,extent,token);
2670  graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2671  &next_token);
2672  if (token == next_token)
2673  status=MagickFalse;
2674  if (graphic_context[n]->dash_pattern[j] < 0.0)
2675  status=MagickFalse;
2676  }
2677  if ((x & 0x01) != 0)
2678  for ( ; j < (2*x); j++)
2679  graphic_context[n]->dash_pattern[j]=
2680  graphic_context[n]->dash_pattern[j-x];
2681  graphic_context[n]->dash_pattern[j]=0.0;
2682  break;
2683  }
2684  GetNextToken(q,&q,extent,token);
2685  break;
2686  }
2687  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2688  {
2689  GetNextToken(q,&q,extent,token);
2690  graphic_context[n]->dash_offset=StringToDouble(token,
2691  &next_token);
2692  if (token == next_token)
2693  status=MagickFalse;
2694  break;
2695  }
2696  if (LocaleCompare("stroke-linecap",keyword) == 0)
2697  {
2698  ssize_t
2699  linecap;
2700 
2701  GetNextToken(q,&q,extent,token);
2703  if (linecap == -1)
2704  status=MagickFalse;
2705  else
2706  graphic_context[n]->linecap=(LineCap) linecap;
2707  break;
2708  }
2709  if (LocaleCompare("stroke-linejoin",keyword) == 0)
2710  {
2711  ssize_t
2712  linejoin;
2713 
2714  GetNextToken(q,&q,extent,token);
2716  token);
2717  if (linejoin == -1)
2718  status=MagickFalse;
2719  else
2720  graphic_context[n]->linejoin=(LineJoin) linejoin;
2721  break;
2722  }
2723  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2724  {
2725  GetNextToken(q,&q,extent,token);
2726  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2727  break;
2728  }
2729  if (LocaleCompare("stroke-opacity",keyword) == 0)
2730  {
2731  GetNextToken(q,&q,extent,token);
2732  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2733  graphic_context[n]->stroke.alpha=(MagickRealType) (QuantumRange-
2735  StringToDouble(token,&next_token))));
2736  if (token == next_token)
2737  status=MagickFalse;
2738  break;
2739  }
2740  if (LocaleCompare("stroke-width",keyword) == 0)
2741  {
2742  GetNextToken(q,&q,extent,token);
2743  graphic_context[n]->stroke_width=StringToDouble(token,&next_token);
2744  if (token == next_token)
2745  status=MagickFalse;
2746  break;
2747  }
2748  status=MagickFalse;
2749  break;
2750  }
2751  case 't':
2752  case 'T':
2753  {
2754  if (LocaleCompare("text",keyword) == 0)
2755  {
2756  primitive_type=TextPrimitive;
2757  break;
2758  }
2759  if (LocaleCompare("text-align",keyword) == 0)
2760  {
2761  ssize_t
2762  align;
2763 
2764  GetNextToken(q,&q,extent,token);
2766  if (align == -1)
2767  status=MagickFalse;
2768  else
2769  graphic_context[n]->align=(AlignType) align;
2770  break;
2771  }
2772  if (LocaleCompare("text-anchor",keyword) == 0)
2773  {
2774  ssize_t
2775  align;
2776 
2777  GetNextToken(q,&q,extent,token);
2779  if (align == -1)
2780  status=MagickFalse;
2781  else
2782  graphic_context[n]->align=(AlignType) align;
2783  break;
2784  }
2785  if (LocaleCompare("text-antialias",keyword) == 0)
2786  {
2787  GetNextToken(q,&q,extent,token);
2788  graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
2790  break;
2791  }
2792  if (LocaleCompare("text-undercolor",keyword) == 0)
2793  {
2794  GetNextToken(q,&q,extent,token);
2795  (void) QueryColorCompliance(token,AllCompliance,
2796  &graphic_context[n]->undercolor,exception);
2797  break;
2798  }
2799  if (LocaleCompare("translate",keyword) == 0)
2800  {
2801  GetNextToken(q,&q,extent,token);
2802  affine.tx=StringToDouble(token,&next_token);
2803  if (token == next_token)
2804  status=MagickFalse;
2805  GetNextToken(q,&q,extent,token);
2806  if (*token == ',')
2807  GetNextToken(q,&q,extent,token);
2808  affine.ty=StringToDouble(token,&next_token);
2809  if (token == next_token)
2810  status=MagickFalse;
2811  break;
2812  }
2813  status=MagickFalse;
2814  break;
2815  }
2816  case 'v':
2817  case 'V':
2818  {
2819  if (LocaleCompare("viewbox",keyword) == 0)
2820  {
2821  GetNextToken(q,&q,extent,token);
2822  graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2823  &next_token)-0.5);
2824  if (token == next_token)
2825  status=MagickFalse;
2826  GetNextToken(q,&q,extent,token);
2827  if (*token == ',')
2828  GetNextToken(q,&q,extent,token);
2829  graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2830  &next_token)-0.5);
2831  if (token == next_token)
2832  status=MagickFalse;
2833  GetNextToken(q,&q,extent,token);
2834  if (*token == ',')
2835  GetNextToken(q,&q,extent,token);
2836  graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2837  token,&next_token)+0.5);
2838  if (token == next_token)
2839  status=MagickFalse;
2840  GetNextToken(q,&q,extent,token);
2841  if (*token == ',')
2842  GetNextToken(q,&q,extent,token);
2843  graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2844  token,&next_token)+0.5);
2845  if (token == next_token)
2846  status=MagickFalse;
2847  break;
2848  }
2849  status=MagickFalse;
2850  break;
2851  }
2852  default:
2853  {
2854  status=MagickFalse;
2855  break;
2856  }
2857  }
2858  if (status == MagickFalse)
2859  break;
2860  if ((fabs(affine.sx-1.0) >= DrawEpsilon) ||
2861  (fabs(affine.rx) >= DrawEpsilon) || (fabs(affine.ry) >= DrawEpsilon) ||
2862  (fabs(affine.sy-1.0) >= DrawEpsilon) ||
2863  (fabs(affine.tx) >= DrawEpsilon) || (fabs(affine.ty) >= DrawEpsilon))
2864  {
2865  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2866  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2867  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2868  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2869  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2870  current.tx;
2871  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2872  current.ty;
2873  }
2874  if (primitive_type == UndefinedPrimitive)
2875  {
2876  if (*q == '\0')
2877  {
2878  if (number_stops > 1)
2879  {
2880  GradientType
2881  type;
2882 
2883  type=LinearGradient;
2884  if (draw_info->gradient.type == RadialGradient)
2885  type=RadialGradient;
2886  (void) GradientImage(image,type,PadSpread,stops,number_stops,
2887  exception);
2888  }
2889  if (number_stops > 0)
2890  stops=(StopInfo *) RelinquishMagickMemory(stops);
2891  }
2892  if (image->debug != MagickFalse)
2893  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
2894  (q-p),p);
2895  continue;
2896  }
2897  /*
2898  Parse the primitive attributes.
2899  */
2900  i=0;
2901  j=0;
2902  primitive_info[0].point.x=0.0;
2903  primitive_info[0].point.y=0.0;
2904  for (x=0; *q != '\0'; x++)
2905  {
2906  /*
2907  Define points.
2908  */
2909  if (IsPoint(q) == MagickFalse)
2910  break;
2911  GetNextToken(q,&q,extent,token);
2912  point.x=StringToDouble(token,&next_token);
2913  if (token == next_token)
2914  status=MagickFalse;
2915  GetNextToken(q,&q,extent,token);
2916  if (*token == ',')
2917  GetNextToken(q,&q,extent,token);
2918  point.y=StringToDouble(token,&next_token);
2919  if (token == next_token)
2920  status=MagickFalse;
2921  GetNextToken(q,(const char **) NULL,extent,token);
2922  if (*token == ',')
2923  GetNextToken(q,&q,extent,token);
2924  primitive_info[i].primitive=primitive_type;
2925  primitive_info[i].point=point;
2926  primitive_info[i].coordinates=0;
2927  primitive_info[i].method=FloodfillMethod;
2928  i++;
2929  if (i < (ssize_t) number_points)
2930  continue;
2931  number_points<<=1;
2932  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2933  (size_t) number_points,sizeof(*primitive_info));
2934  if ((primitive_info == (PrimitiveInfo *) NULL) ||
2935  (number_points != (MagickSizeType) ((size_t) number_points)))
2936  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2937  image->filename);
2938  }
2939  primitive_info[j].primitive=primitive_type;
2940  primitive_info[j].coordinates=(size_t) x;
2941  primitive_info[j].method=FloodfillMethod;
2942  primitive_info[j].text=(char *) NULL;
2943  /*
2944  Circumscribe primitive within a circle.
2945  */
2946  bounds.x1=primitive_info[j].point.x;
2947  bounds.y1=primitive_info[j].point.y;
2948  bounds.x2=primitive_info[j].point.x;
2949  bounds.y2=primitive_info[j].point.y;
2950  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2951  {
2952  point=primitive_info[j+k].point;
2953  if (point.x < bounds.x1)
2954  bounds.x1=point.x;
2955  if (point.y < bounds.y1)
2956  bounds.y1=point.y;
2957  if (point.x > bounds.x2)
2958  bounds.x2=point.x;
2959  if (point.y > bounds.y2)
2960  bounds.y2=point.y;
2961  }
2962  /*
2963  Speculate how many points our primitive might consume.
2964  */
2965  points_extent=(double) primitive_info[j].coordinates;
2966  switch (primitive_type)
2967  {
2968  case RectanglePrimitive:
2969  {
2970  points_extent*=5;
2971  break;
2972  }
2974  {
2975  double
2976  alpha,
2977  beta,
2978  radius;
2979 
2980  alpha=bounds.x2-bounds.x1;
2981  beta=bounds.y2-bounds.y1;
2982  radius=hypot((double) alpha,(double) beta);
2983  points_extent*=5;
2984  points_extent+=2*ceil((double) MagickPI*radius)+6*BezierQuantum+360;
2985  break;
2986  }
2987  case BezierPrimitive:
2988  {
2989  if (primitive_info[j].coordinates > 107)
2990  (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
2991  "TooManyBezierCoordinates","`%s'",token);
2992  points_extent=(double) (BezierQuantum*primitive_info[j].coordinates);
2993  break;
2994  }
2995  case PathPrimitive:
2996  {
2997  char
2998  *s,
2999  *t;
3000 
3001  GetNextToken(q,&q,extent,token);
3002  points_extent=1;
3003  t=token;
3004  for (s=token; *s != '\0'; s=t)
3005  {
3006  double
3007  value;
3008 
3009  value=StringToDouble(s,&t);
3010  (void) value;
3011  if (s == t)
3012  {
3013  t++;
3014  continue;
3015  }
3016  points_extent++;
3017  }
3018  points_extent=points_extent*BezierQuantum;
3019  break;
3020  }
3021  case CirclePrimitive:
3022  case ArcPrimitive:
3023  case EllipsePrimitive:
3024  {
3025  double
3026  alpha,
3027  beta,
3028  radius;
3029 
3030  alpha=bounds.x2-bounds.x1;
3031  beta=bounds.y2-bounds.y1;
3032  radius=hypot((double) alpha,(double) beta);
3033  points_extent=2*ceil((double) MagickPI*radius)+6*BezierQuantum+360;
3034  break;
3035  }
3036  default:
3037  break;
3038  }
3039  if (((double) ((size_t) points_extent)) < points_extent)
3040  {
3041  (void) ThrowMagickException(exception,GetMagickModule(),
3042  ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
3043  break;
3044  }
3045  if (((MagickSizeType) (i+points_extent)) >= number_points)
3046  {
3047  /*
3048  Resize based on speculative points required by primitive.
3049  */
3050  number_points+=points_extent+1;
3051  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
3052  (size_t) number_points,sizeof(*primitive_info));
3053  if ((primitive_info == (PrimitiveInfo *) NULL) ||
3054  (number_points != (MagickSizeType) ((size_t) number_points)))
3055  {
3056  (void) ThrowMagickException(exception,GetMagickModule(),
3057  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3058  image->filename);
3059  break;
3060  }
3061  }
3062  switch (primitive_type)
3063  {
3064  case PointPrimitive:
3065  default:
3066  {
3067  if (primitive_info[j].coordinates != 1)
3068  {
3069  status=MagickFalse;
3070  break;
3071  }
3072  TracePoint(primitive_info+j,primitive_info[j].point);
3073  i=(ssize_t) (j+primitive_info[j].coordinates);
3074  break;
3075  }
3076  case LinePrimitive:
3077  {
3078  if (primitive_info[j].coordinates != 2)
3079  {
3080  status=MagickFalse;
3081  break;
3082  }
3083  TraceLine(primitive_info+j,primitive_info[j].point,
3084  primitive_info[j+1].point);
3085  i=(ssize_t) (j+primitive_info[j].coordinates);
3086  break;
3087  }
3088  case RectanglePrimitive:
3089  {
3090  if (primitive_info[j].coordinates != 2)
3091  {
3092  status=MagickFalse;
3093  break;
3094  }
3095  TraceRectangle(primitive_info+j,primitive_info[j].point,
3096  primitive_info[j+1].point);
3097  i=(ssize_t) (j+primitive_info[j].coordinates);
3098  break;
3099  }
3101  {
3102  if (primitive_info[j].coordinates != 3)
3103  {
3104  status=MagickFalse;
3105  break;
3106  }
3107  TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
3108  primitive_info[j+1].point,primitive_info[j+2].point);
3109  i=(ssize_t) (j+primitive_info[j].coordinates);
3110  break;
3111  }
3112  case ArcPrimitive:
3113  {
3114  if (primitive_info[j].coordinates != 3)
3115  {
3116  primitive_type=UndefinedPrimitive;
3117  break;
3118  }
3119  TraceArc(primitive_info+j,primitive_info[j].point,
3120  primitive_info[j+1].point,primitive_info[j+2].point);
3121  i=(ssize_t) (j+primitive_info[j].coordinates);
3122  break;
3123  }
3124  case EllipsePrimitive:
3125  {
3126  if (primitive_info[j].coordinates != 3)
3127  {
3128  status=MagickFalse;
3129  break;
3130  }
3131  TraceEllipse(primitive_info+j,primitive_info[j].point,
3132  primitive_info[j+1].point,primitive_info[j+2].point);
3133  i=(ssize_t) (j+primitive_info[j].coordinates);
3134  break;
3135  }
3136  case CirclePrimitive:
3137  {
3138  if (primitive_info[j].coordinates != 2)
3139  {
3140  status=MagickFalse;
3141  break;
3142  }
3143  TraceCircle(primitive_info+j,primitive_info[j].point,
3144  primitive_info[j+1].point);
3145  i=(ssize_t) (j+primitive_info[j].coordinates);
3146  break;
3147  }
3148  case PolylinePrimitive:
3149  break;
3150  case PolygonPrimitive:
3151  {
3152  primitive_info[i]=primitive_info[j];
3153  primitive_info[i].coordinates=0;
3154  primitive_info[j].coordinates++;
3155  i++;
3156  break;
3157  }
3158  case BezierPrimitive:
3159  {
3160  if (primitive_info[j].coordinates < 3)
3161  {
3162  status=MagickFalse;
3163  break;
3164  }
3165  TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3166  i=(ssize_t) (j+primitive_info[j].coordinates);
3167  break;
3168  }
3169  case PathPrimitive:
3170  {
3171  i=(ssize_t) (j+TracePath(primitive_info+j,token));
3172  break;
3173  }
3174  case AlphaPrimitive:
3175  case ColorPrimitive:
3176  {
3177  ssize_t
3178  method;
3179 
3180  if (primitive_info[j].coordinates != 1)
3181  {
3182  status=MagickFalse;
3183  break;
3184  }
3185  GetNextToken(q,&q,extent,token);
3187  if (method == -1)
3188  status=MagickFalse;
3189  else
3190  primitive_info[j].method=(PaintMethod) method;
3191  break;
3192  }
3193  case TextPrimitive:
3194  {
3195  if (primitive_info[j].coordinates != 1)
3196  {
3197  status=MagickFalse;
3198  break;
3199  }
3200  if (*token != ',')
3201  GetNextToken(q,&q,extent,token);
3202  primitive_info[j].text=AcquireString(token);
3203  break;
3204  }
3205  case ImagePrimitive:
3206  {
3207  if (primitive_info[j].coordinates != 2)
3208  {
3209  status=MagickFalse;
3210  break;
3211  }
3212  GetNextToken(q,&q,extent,token);
3213  primitive_info[j].text=AcquireString(token);
3214  break;
3215  }
3216  }
3217  if (primitive_info == (PrimitiveInfo *) NULL)
3218  break;
3219  if (image->debug != MagickFalse)
3220  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3221  if (status == MagickFalse)
3222  break;
3223  primitive_info[i].primitive=UndefinedPrimitive;
3224  if (i == 0)
3225  continue;
3226  /*
3227  Transform points.
3228  */
3229  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3230  {
3231  point=primitive_info[i].point;
3232  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3233  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3234  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3235  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3236  point=primitive_info[i].point;
3237  if (point.x < graphic_context[n]->bounds.x1)
3238  graphic_context[n]->bounds.x1=point.x;
3239  if (point.y < graphic_context[n]->bounds.y1)
3240  graphic_context[n]->bounds.y1=point.y;
3241  if (point.x > graphic_context[n]->bounds.x2)
3242  graphic_context[n]->bounds.x2=point.x;
3243  if (point.y > graphic_context[n]->bounds.y2)
3244  graphic_context[n]->bounds.y2=point.y;
3245  if (primitive_info[i].primitive == ImagePrimitive)
3246  break;
3247  if (i >= (ssize_t) number_points)
3248  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3249  }
3250  if (graphic_context[n]->render != MagickFalse)
3251  {
3252  if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3253  (LocaleCompare(graphic_context[n]->clip_mask,
3254  graphic_context[n-1]->clip_mask) != 0))
3255  status&=DrawClipPath(image,graphic_context[n],
3256  graphic_context[n]->clip_mask,exception);
3257  status&=DrawPrimitive(image,graphic_context[n],primitive_info,
3258  exception);
3259  }
3260  if (primitive_info->text != (char *) NULL)
3261  primitive_info->text=(char *) RelinquishMagickMemory(
3262  primitive_info->text);
3263  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3264  primitive_extent);
3265  if (proceed == MagickFalse)
3266  break;
3267  if (status == 0)
3268  break;
3269  }
3270  if (image->debug != MagickFalse)
3271  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3272  /*
3273  Relinquish resources.
3274  */
3275  token=DestroyString(token);
3276  if (primitive_info != (PrimitiveInfo *) NULL)
3277  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3278  primitive=DestroyString(primitive);
3279  for ( ; n >= 0; n--)
3280  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3281  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3282  if (status == MagickFalse)
3283  ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3284  keyword);
3285  return(status != 0 ? MagickTrue : MagickFalse);
3286 }
3287 
3288 /*
3289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3290 % %
3291 % %
3292 % %
3293 % D r a w G r a d i e n t I m a g e %
3294 % %
3295 % %
3296 % %
3297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3298 %
3299 % DrawGradientImage() draws a linear gradient on the image.
3300 %
3301 % The format of the DrawGradientImage method is:
3302 %
3303 % MagickBooleanType DrawGradientImage(Image *image,
3304 % const DrawInfo *draw_info,ExceptionInfo *exception)
3305 %
3306 % A description of each parameter follows:
3307 %
3308 % o image: the image.
3309 %
3310 % o draw_info: the draw info.
3311 %
3312 % o exception: return any errors or warnings in this structure.
3313 %
3314 */
3315 
3316 static inline double GetStopColorOffset(const GradientInfo *gradient,
3317  const ssize_t x,const ssize_t y)
3318 {
3319  switch (gradient->type)
3320  {
3321  case UndefinedGradient:
3322  case LinearGradient:
3323  {
3324  double
3325  gamma,
3326  length,
3327  offset,
3328  scale;
3329 
3330  PointInfo
3331  p,
3332  q;
3333 
3334  const SegmentInfo
3335  *gradient_vector;
3336 
3337  gradient_vector=(&gradient->gradient_vector);
3338  p.x=gradient_vector->x2-gradient_vector->x1;
3339  p.y=gradient_vector->y2-gradient_vector->y1;
3340  q.x=(double) x-gradient_vector->x1;
3341  q.y=(double) y-gradient_vector->y1;
3342  length=sqrt(q.x*q.x+q.y*q.y);
3343  gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3344  gamma=PerceptibleReciprocal(gamma);
3345  scale=p.x*q.x+p.y*q.y;
3346  offset=gamma*scale*length;
3347  return(offset);
3348  }
3349  case RadialGradient:
3350  {
3351  PointInfo
3352  v;
3353 
3354  if (gradient->spread == RepeatSpread)
3355  {
3356  v.x=(double) x-gradient->center.x;
3357  v.y=(double) y-gradient->center.y;
3358  return(sqrt(v.x*v.x+v.y*v.y));
3359  }
3360  v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
3361  gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
3362  gradient->angle))))/gradient->radii.x;
3363  v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
3364  gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
3365  gradient->angle))))/gradient->radii.y;
3366  return(sqrt(v.x*v.x+v.y*v.y));
3367  }
3368  }
3369  return(0.0);
3370 }
3371 
3372 static int StopInfoCompare(const void *x,const void *y)
3373 {
3374  StopInfo
3375  *stop_1,
3376  *stop_2;
3377 
3378  stop_1=(StopInfo *) x;
3379  stop_2=(StopInfo *) y;
3380  if (stop_1->offset > stop_2->offset)
3381  return(1);
3382  if (fabs(stop_1->offset-stop_2->offset) <= DrawEpsilon)
3383  return(0);
3384  return(-1);
3385 }
3386 
3388  const DrawInfo *draw_info,ExceptionInfo *exception)
3389 {
3390  CacheView
3391  *image_view;
3392 
3393  const GradientInfo
3394  *gradient;
3395 
3396  const SegmentInfo
3397  *gradient_vector;
3398 
3399  double
3400  length;
3401 
3403  status;
3404 
3405  PixelInfo
3406  zero;
3407 
3408  PointInfo
3409  point;
3410 
3412  bounding_box;
3413 
3414  ssize_t
3415  y;
3416 
3417  /*
3418  Draw linear or radial gradient on image.
3419  */
3420  assert(image != (Image *) NULL);
3421  assert(image->signature == MagickCoreSignature);
3422  if (image->debug != MagickFalse)
3423  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3424  assert(draw_info != (const DrawInfo *) NULL);
3425  gradient=(&draw_info->gradient);
3426  qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
3427  StopInfoCompare);
3428  gradient_vector=(&gradient->gradient_vector);
3429  point.x=gradient_vector->x2-gradient_vector->x1;
3430  point.y=gradient_vector->y2-gradient_vector->y1;
3431  length=sqrt(point.x*point.x+point.y*point.y);
3432  bounding_box=gradient->bounding_box;
3433  status=MagickTrue;
3434  GetPixelInfo(image,&zero);
3435  image_view=AcquireAuthenticCacheView(image,exception);
3436 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3437  #pragma omp parallel for schedule(static,4) shared(status) \
3438  magick_number_threads(image,image,bounding_box.height-bounding_box.y,1)
3439 #endif
3440  for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3441  {
3442  PixelInfo
3443  composite,
3444  pixel;
3445 
3446  double
3447  alpha,
3448  offset;
3449 
3450  register Quantum
3451  *magick_restrict q;
3452 
3453  register ssize_t
3454  i,
3455  x;
3456 
3457  ssize_t
3458  j;
3459 
3460  if (status == MagickFalse)
3461  continue;
3462  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3463  if (q == (Quantum *) NULL)
3464  {
3465  status=MagickFalse;
3466  continue;
3467  }
3468  pixel=zero;
3469  composite=zero;
3470  offset=GetStopColorOffset(gradient,0,y);
3471  if (gradient->type != RadialGradient)
3472  offset/=length;
3473  for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3474  {
3475  GetPixelInfoPixel(image,q,&pixel);
3476  switch (gradient->spread)
3477  {
3478  case UndefinedSpread:
3479  case PadSpread:
3480  {
3481  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3482  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3483  {
3484  offset=GetStopColorOffset(gradient,x,y);
3485  if (gradient->type != RadialGradient)
3486  offset/=length;
3487  }
3488  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3489  if (offset < gradient->stops[i].offset)
3490  break;
3491  if ((offset < 0.0) || (i == 0))
3492  composite=gradient->stops[0].color;
3493  else
3494  if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3495  composite=gradient->stops[gradient->number_stops-1].color;
3496  else
3497  {
3498  j=i;
3499  i--;
3500  alpha=(offset-gradient->stops[i].offset)/
3501  (gradient->stops[j].offset-gradient->stops[i].offset);
3502  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3503  &gradient->stops[j].color,alpha,&composite);
3504  }
3505  break;
3506  }
3507  case ReflectSpread:
3508  {
3509  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3510  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3511  {
3512  offset=GetStopColorOffset(gradient,x,y);
3513  if (gradient->type != RadialGradient)
3514  offset/=length;
3515  }
3516  if (offset < 0.0)
3517  offset=(-offset);
3518  if ((ssize_t) fmod(offset,2.0) == 0)
3519  offset=fmod(offset,1.0);
3520  else
3521  offset=1.0-fmod(offset,1.0);
3522  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3523  if (offset < gradient->stops[i].offset)
3524  break;
3525  if (i == 0)
3526  composite=gradient->stops[0].color;
3527  else
3528  if (i == (ssize_t) gradient->number_stops)
3529  composite=gradient->stops[gradient->number_stops-1].color;
3530  else
3531  {
3532  j=i;
3533  i--;
3534  alpha=(offset-gradient->stops[i].offset)/
3535  (gradient->stops[j].offset-gradient->stops[i].offset);
3536  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3537  &gradient->stops[j].color,alpha,&composite);
3538  }
3539  break;
3540  }
3541  case RepeatSpread:
3542  {
3544  antialias;
3545 
3546  double
3547  repeat;
3548 
3549  antialias=MagickFalse;
3550  repeat=0.0;
3551  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3552  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3553  {
3554  offset=GetStopColorOffset(gradient,x,y);
3555  if (gradient->type == LinearGradient)
3556  {
3557  repeat=fmod(offset,length);
3558  if (repeat < 0.0)
3559  repeat=length-fmod(-repeat,length);
3560  else
3561  repeat=fmod(offset,length);
3562  antialias=(repeat < length) && ((repeat+1.0) > length) ?
3564  offset=repeat/length;
3565  }
3566  else
3567  {
3568  repeat=fmod(offset,gradient->radius);
3569  if (repeat < 0.0)
3570  repeat=gradient->radius-fmod(-repeat,gradient->radius);
3571  else
3572  repeat=fmod(offset,gradient->radius);
3573  antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3574  MagickFalse;
3575  offset=repeat/gradient->radius;
3576  }
3577  }
3578  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3579  if (offset < gradient->stops[i].offset)
3580  break;
3581  if (i == 0)
3582  composite=gradient->stops[0].color;
3583  else
3584  if (i == (ssize_t) gradient->number_stops)
3585  composite=gradient->stops[gradient->number_stops-1].color;
3586  else
3587  {
3588  j=i;
3589  i--;
3590  alpha=(offset-gradient->stops[i].offset)/
3591  (gradient->stops[j].offset-gradient->stops[i].offset);
3592  if (antialias != MagickFalse)
3593  {
3594  if (gradient->type == LinearGradient)
3595  alpha=length-repeat;
3596  else
3597  alpha=gradient->radius-repeat;
3598  i=0;
3599  j=(ssize_t) gradient->number_stops-1L;
3600  }
3601  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
3602  &gradient->stops[j].color,alpha,&composite);
3603  }
3604  break;
3605  }
3606  }
3607  CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
3608  &pixel);
3609  SetPixelViaPixelInfo(image,&pixel,q);
3610  q+=GetPixelChannels(image);
3611  }
3612  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3613  status=MagickFalse;
3614  }
3615  image_view=DestroyCacheView(image_view);
3616  return(status);
3617 }
3618 
3619 /*
3620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3621 % %
3622 % %
3623 % %
3624 % D r a w P a t t e r n P a t h %
3625 % %
3626 % %
3627 % %
3628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3629 %
3630 % DrawPatternPath() draws a pattern.
3631 %
3632 % The format of the DrawPatternPath method is:
3633 %
3634 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3635 % const char *name,Image **pattern,ExceptionInfo *exception)
3636 %
3637 % A description of each parameter follows:
3638 %
3639 % o image: the image.
3640 %
3641 % o draw_info: the draw info.
3642 %
3643 % o name: the pattern name.
3644 %
3645 % o image: the image.
3646 %
3647 % o exception: return any errors or warnings in this structure.
3648 %
3649 */
3651  const DrawInfo *draw_info,const char *name,Image **pattern,
3652  ExceptionInfo *exception)
3653 {
3654  char
3655  property[MagickPathExtent];
3656 
3657  const char
3658  *geometry,
3659  *path,
3660  *type;
3661 
3662  DrawInfo
3663  *clone_info;
3664 
3665  ImageInfo
3666  *image_info;
3667 
3669  status;
3670 
3671  assert(image != (Image *) NULL);
3672  assert(image->signature == MagickCoreSignature);
3673  if (image->debug != MagickFalse)
3674  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3675  assert(draw_info != (const DrawInfo *) NULL);
3676  assert(name != (const char *) NULL);
3677  (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
3678  path=GetImageArtifact(image,property);
3679  if (path == (const char *) NULL)
3680  return(MagickFalse);
3681  (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
3682  geometry=GetImageArtifact(image,property);
3683  if (geometry == (const char *) NULL)
3684  return(MagickFalse);
3685  if ((*pattern) != (Image *) NULL)
3686  *pattern=DestroyImage(*pattern);
3687  image_info=AcquireImageInfo();
3688  image_info->size=AcquireString(geometry);
3689  *pattern=AcquireImage(image_info,exception);
3690  image_info=DestroyImageInfo(image_info);
3691  (void) QueryColorCompliance("#000000ff",AllCompliance,
3692  &(*pattern)->background_color,exception);
3693  (void) SetImageBackgroundColor(*pattern,exception);
3694  if (image->debug != MagickFalse)
3696  "begin pattern-path %s %s",name,geometry);
3697  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3698  clone_info->fill_pattern=NewImageList();
3699  clone_info->stroke_pattern=NewImageList();
3700  (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
3701  type=GetImageArtifact(image,property);
3702  if (type != (const char *) NULL)
3705  (void) CloneString(&clone_info->primitive,path);
3706  status=DrawImage(*pattern,clone_info,exception);
3707  clone_info=DestroyDrawInfo(clone_info);
3708  if (image->debug != MagickFalse)
3709  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3710  return(status);
3711 }
3712 
3713 /*
3714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3715 % %
3716 % %
3717 % %
3718 + D r a w P o l y g o n P r i m i t i v e %
3719 % %
3720 % %
3721 % %
3722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3723 %
3724 % DrawPolygonPrimitive() draws a polygon on the image.
3725 %
3726 % The format of the DrawPolygonPrimitive method is:
3727 %
3728 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3729 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3730 % ExceptionInfo *exception)
3731 %
3732 % A description of each parameter follows:
3733 %
3734 % o image: the image.
3735 %
3736 % o draw_info: the draw info.
3737 %
3738 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3739 %
3740 % o exception: return any errors or warnings in this structure.
3741 %
3742 */
3743 
3745 {
3746  register ssize_t
3747  i;
3748 
3749  assert(polygon_info != (PolygonInfo **) NULL);
3750  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3751  if (polygon_info[i] != (PolygonInfo *) NULL)
3752  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3753  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3754  return(polygon_info);
3755 }
3756 
3758  const PrimitiveInfo *primitive_info)
3759 {
3760  PathInfo
3761  *magick_restrict path_info;
3762 
3763  PolygonInfo
3764  **polygon_info;
3765 
3766  register ssize_t
3767  i;
3768 
3769  size_t
3770  number_threads;
3771 
3772  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3773  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3774  sizeof(*polygon_info));
3775  if (polygon_info == (PolygonInfo **) NULL)
3776  return((PolygonInfo **) NULL);
3777  (void) ResetMagickMemory(polygon_info,0,number_threads*sizeof(*polygon_info));
3778  path_info=ConvertPrimitiveToPath(primitive_info);
3779  if (path_info == (PathInfo *) NULL)
3780  return(DestroyPolygonThreadSet(polygon_info));
3781  for (i=0; i < (ssize_t) number_threads; i++)
3782  {
3783  polygon_info[i]=ConvertPathToPolygon(path_info);
3784  if (polygon_info[i] == (PolygonInfo *) NULL)
3785  return(DestroyPolygonThreadSet(polygon_info));
3786  }
3787  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3788  return(polygon_info);
3789 }
3790 
3791 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
3792  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3793  const ssize_t y,double *stroke_alpha)
3794 {
3795  double
3796  alpha,
3797  beta,
3798  distance,
3799  subpath_alpha;
3800 
3801  PointInfo
3802  delta;
3803 
3804  register const PointInfo
3805  *q;
3806 
3807  register EdgeInfo
3808  *p;
3809 
3810  register ssize_t
3811  i;
3812 
3813  ssize_t
3814  j,
3815  winding_number;
3816 
3817  /*
3818  Compute fill & stroke opacity for this (x,y) point.
3819  */
3820  *stroke_alpha=0.0;
3821  subpath_alpha=0.0;
3822  p=polygon_info->edges;
3823  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3824  {
3825  if ((double) y <= (p->bounds.y1-mid-0.5))
3826  break;
3827  if ((double) y > (p->bounds.y2+mid+0.5))
3828  {
3829  (void) DestroyEdge(polygon_info,(size_t) j);
3830  continue;
3831  }
3832  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3833  ((double) x > (p->bounds.x2+mid+0.5)))
3834  continue;
3835  i=(ssize_t) MagickMax((double) p->highwater,1.0);
3836  for ( ; i < (ssize_t) p->number_points; i++)
3837  {
3838  if ((double) y <= (p->points[i-1].y-mid-0.5))
3839  break;
3840  if ((double) y > (p->points[i].y+mid+0.5))
3841  continue;
3842  if (p->scanline != (double) y)
3843  {
3844  p->scanline=(double) y;
3845  p->highwater=(size_t) i;
3846  }
3847  /*
3848  Compute distance between a point and an edge.
3849  */
3850  q=p->points+i-1;
3851  delta.x=(q+1)->x-q->x;
3852  delta.y=(q+1)->y-q->y;
3853  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3854  if (beta < 0.0)
3855  {
3856  delta.x=(double) x-q->x;
3857  delta.y=(double) y-q->y;
3858  distance=delta.x*delta.x+delta.y*delta.y;
3859  }
3860  else
3861  {
3862  alpha=delta.x*delta.x+delta.y*delta.y;
3863  if (beta > alpha)
3864  {
3865  delta.x=(double) x-(q+1)->x;
3866  delta.y=(double) y-(q+1)->y;
3867  distance=delta.x*delta.x+delta.y*delta.y;
3868  }
3869  else
3870  {
3871  alpha=1.0/alpha;
3872  beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3873  distance=alpha*beta*beta;
3874  }
3875  }
3876  /*
3877  Compute stroke & subpath opacity.
3878  */
3879  beta=0.0;
3880  if (p->ghostline == MagickFalse)
3881  {
3882  alpha=mid+0.5;
3883  if ((*stroke_alpha < 1.0) &&
3884  (distance <= ((alpha+0.25)*(alpha+0.25))))
3885  {
3886  alpha=mid-0.5;
3887  if (distance <= ((alpha+0.25)*(alpha+0.25)))
3888  *stroke_alpha=1.0;
3889  else
3890  {
3891  beta=1.0;
3892  if (fabs(distance-1.0) >= DrawEpsilon)
3893  beta=sqrt((double) distance);
3894  alpha=beta-mid-0.5;
3895  if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
3896  *stroke_alpha=(alpha-0.25)*(alpha-0.25);
3897  }
3898  }
3899  }
3900  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
3901  continue;
3902  if (distance <= 0.0)
3903  {
3904  subpath_alpha=1.0;
3905  continue;
3906  }
3907  if (distance > 1.0)
3908  continue;
3909  if (fabs(beta) < DrawEpsilon)
3910  {
3911  beta=1.0;
3912  if (fabs(distance-1.0) >= DrawEpsilon)
3913  beta=sqrt(distance);
3914  }
3915  alpha=beta-1.0;
3916  if (subpath_alpha < (alpha*alpha))
3917  subpath_alpha=alpha*alpha;
3918  }
3919  }
3920  /*
3921  Compute fill opacity.
3922  */
3923  if (fill == MagickFalse)
3924  return(0.0);
3925  if (subpath_alpha >= 1.0)
3926  return(1.0);
3927  /*
3928  Determine winding number.
3929  */
3930  winding_number=0;
3931  p=polygon_info->edges;
3932  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3933  {
3934  if ((double) y <= p->bounds.y1)
3935  break;
3936  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3937  continue;
3938  if ((double) x > p->bounds.x2)
3939  {
3940  winding_number+=p->direction ? 1 : -1;
3941  continue;
3942  }
3943  i=(ssize_t) MagickMax((double) p->highwater,1.0);
3944  for ( ; i < (ssize_t) p->number_points; i++)
3945  if ((double) y <= p->points[i].y)
3946  break;
3947  q=p->points+i-1;
3948  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3949  winding_number+=p->direction ? 1 : -1;
3950  }
3951  if (fill_rule != NonZeroRule)
3952  {
3953  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3954  return(1.0);
3955  }
3956  else
3957  if (MagickAbsoluteValue(winding_number) != 0)
3958  return(1.0);
3959  return(subpath_alpha);
3960 }
3961 
3963  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
3964  ExceptionInfo *exception)
3965 {
3966  CacheView
3967  *image_view;
3968 
3970  fill,
3971  status;
3972 
3973  double
3974  mid;
3975 
3976  PolygonInfo
3977  **magick_restrict polygon_info;
3978 
3979  register EdgeInfo
3980  *p;
3981 
3982  register ssize_t
3983  i;
3984 
3985  SegmentInfo
3986  bounds;
3987 
3988  ssize_t
3989  start_y,
3990  stop_y,
3991  y;
3992 
3993  /*
3994  Compute bounding box.
3995  */
3996  assert(image != (Image *) NULL);
3997  assert(image->signature == MagickCoreSignature);
3998  if (image->debug != MagickFalse)
3999  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4000  assert(draw_info != (DrawInfo *) NULL);
4001  assert(draw_info->signature == MagickCoreSignature);
4002  assert(primitive_info != (PrimitiveInfo *) NULL);
4003  if (primitive_info->coordinates == 0)
4004  return(MagickTrue);
4005  polygon_info=AcquirePolygonThreadSet(primitive_info);
4006  if (polygon_info == (PolygonInfo **) NULL)
4007  return(MagickFalse);
4008 DisableMSCWarning(4127)
4009  if (0)
4010  DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
4012  if (image->debug != MagickFalse)
4013  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4014  fill=(primitive_info->method == FillToBorderMethod) ||
4015  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4016  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4017  bounds=polygon_info[0]->edges[0].bounds;
4018  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4019  {
4020  p=polygon_info[0]->edges+i;
4021  if (p->bounds.x1 < bounds.x1)
4022  bounds.x1=p->bounds.x1;
4023  if (p->bounds.y1 < bounds.y1)
4024  bounds.y1=p->bounds.y1;
4025  if (p->bounds.x2 > bounds.x2)
4026  bounds.x2=p->bounds.x2;
4027  if (p->bounds.y2 > bounds.y2)
4028  bounds.y2=p->bounds.y2;
4029  }
4030  bounds.x1-=(mid+1.0);
4031  bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
4032  image->columns ? (double) image->columns-1 : bounds.x1;
4033  bounds.y1-=(mid+1.0);
4034  bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
4035  image->rows ? (double) image->rows-1 : bounds.y1;
4036  bounds.x2+=(mid+1.0);
4037  bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
4038  image->columns ? (double) image->columns-1 : bounds.x2;
4039  bounds.y2+=(mid+1.0);
4040  bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
4041  image->rows ? (double) image->rows-1 : bounds.y2;
4042  status=MagickTrue;
4043  image_view=AcquireAuthenticCacheView(image,exception);
4044  if ((primitive_info->coordinates == 1) ||
4045  (polygon_info[0]->number_edges == 0))
4046  {
4047  /*
4048  Draw point.
4049  */
4050  start_y=(ssize_t) ceil(bounds.y1-0.5);
4051  stop_y=(ssize_t) floor(bounds.y2+0.5);
4052 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4053  #pragma omp parallel for schedule(static,4) shared(status) \
4054  magick_number_threads(image,image,stop_y-start_y+1,1)
4055 #endif
4056  for (y=start_y; y <= stop_y; y++)
4057  {
4059  sync;
4060 
4061  PixelInfo
4062  pixel;
4063 
4064  register ssize_t
4065  x;
4066 
4067  register Quantum
4068  *magick_restrict q;
4069 
4070  ssize_t
4071  start_x,
4072  stop_x;
4073 
4074  if (status == MagickFalse)
4075  continue;
4076  start_x=(ssize_t) ceil(bounds.x1-0.5);
4077  stop_x=(ssize_t) floor(bounds.x2+0.5);
4078  x=start_x;
4079  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4080  exception);
4081  if (q == (Quantum *) NULL)
4082  {
4083  status=MagickFalse;
4084  continue;
4085  }
4086  GetPixelInfo(image,&pixel);
4087  for ( ; x <= stop_x; x++)
4088  {
4089  if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4090  (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4091  {
4092  GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4093  SetPixelViaPixelInfo(image,&pixel,q);
4094  }
4095  q+=GetPixelChannels(image);
4096  }
4097  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4098  if (sync == MagickFalse)
4099  status=MagickFalse;
4100  }
4101  image_view=DestroyCacheView(image_view);
4102  polygon_info=DestroyPolygonThreadSet(polygon_info);
4103  if (image->debug != MagickFalse)
4105  " end draw-polygon");
4106  return(status);
4107  }
4108  /*
4109  Draw polygon or line.
4110  */
4111  if (image->alpha_trait == UndefinedPixelTrait)
4112  (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4113  start_y=(ssize_t) ceil(bounds.y1-0.5);
4114  stop_y=(ssize_t) floor(bounds.y2+0.5);
4115 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4116  #pragma omp parallel for schedule(static,4) shared(status) \
4117  magick_number_threads(image,image,stop_y-start_y+1,1)
4118 #endif
4119  for (y=start_y; y <= stop_y; y++)
4120  {
4121  const int
4122  id = GetOpenMPThreadId();
4123 
4124  double
4125  fill_alpha,
4126  stroke_alpha;
4127 
4128  PixelInfo
4129  fill_color,
4130  stroke_color;
4131 
4132  register Quantum
4133  *magick_restrict q;
4134 
4135  register ssize_t
4136  x;
4137 
4138  ssize_t
4139  start_x,
4140  stop_x;
4141 
4142  if (status == MagickFalse)
4143  continue;
4144  start_x=(ssize_t) ceil(bounds.x1-0.5);
4145  stop_x=(ssize_t) floor(bounds.x2+0.5);
4146  q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+ 1),1,exception);
4147  if (q == (Quantum *) NULL)
4148  {
4149  status=MagickFalse;
4150  continue;
4151  }
4152  for (x=start_x; x <= stop_x; x++)
4153  {
4154  /*
4155  Fill and/or stroke.
4156  */
4157  fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4158  x,y,&stroke_alpha);
4159  if (draw_info->stroke_antialias == MagickFalse)
4160  {
4161  fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4162  stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4163  }
4164  GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4165  fill_alpha=fill_alpha*fill_color.alpha;
4166  CompositePixelOver(image,&fill_color,fill_alpha,q,(double)
4167  GetPixelAlpha(image,q),q);
4168  GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4169  stroke_alpha=stroke_alpha*stroke_color.alpha;
4170  CompositePixelOver(image,&stroke_color,stroke_alpha,q,(double)
4171  GetPixelAlpha(image,q),q);
4172  q+=GetPixelChannels(image);
4173  }
4174  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4175  status=MagickFalse;
4176  }
4177  image_view=DestroyCacheView(image_view);
4178  polygon_info=DestroyPolygonThreadSet(polygon_info);
4179  if (image->debug != MagickFalse)
4180  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4181  return(status);
4182 }
4183 
4184 /*
4185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4186 % %
4187 % %
4188 % %
4189 % D r a w P r i m i t i v e %
4190 % %
4191 % %
4192 % %
4193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4194 %
4195 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4196 %
4197 % The format of the DrawPrimitive method is:
4198 %
4199 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4200 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4201 %
4202 % A description of each parameter follows:
4203 %
4204 % o image: the image.
4205 %
4206 % o draw_info: the draw info.
4207 %
4208 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4209 %
4210 % o exception: return any errors or warnings in this structure.
4211 %
4212 */
4213 
4214 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4215 {
4216  const char
4217  *methods[] =
4218  {
4219  "point",
4220  "replace",
4221  "floodfill",
4222  "filltoborder",
4223  "reset",
4224  "?"
4225  };
4226 
4227  PointInfo
4228  p,
4229  q,
4230  point;
4231 
4232  register ssize_t
4233  i,
4234  x;
4235 
4236  ssize_t
4237  coordinates,
4238  y;
4239 
4240  x=(ssize_t) ceil(primitive_info->point.x-0.5);
4241  y=(ssize_t) ceil(primitive_info->point.y-0.5);
4242  switch (primitive_info->primitive)
4243  {
4244  case AlphaPrimitive:
4245  {
4247  "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
4248  methods[primitive_info->method]);
4249  return;
4250  }
4251  case ColorPrimitive:
4252  {
4254  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4255  methods[primitive_info->method]);
4256  return;
4257  }
4258  case ImagePrimitive:
4259  {
4261  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4262  return;
4263  }
4264  case PointPrimitive:
4265  {
4267  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4268  methods[primitive_info->method]);
4269  return;
4270  }
4271  case TextPrimitive:
4272  {
4274  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4275  return;
4276  }
4277  default:
4278  break;
4279  }
4280  coordinates=0;
4281  p=primitive_info[0].point;
4282  q.x=(-1.0);
4283  q.y=(-1.0);
4284  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4285  {
4286  point=primitive_info[i].point;
4287  if (coordinates <= 0)
4288  {
4289  coordinates=(ssize_t) primitive_info[i].coordinates;
4291  " begin open (%.20g)",(double) coordinates);
4292  p=point;
4293  }
4294  point=primitive_info[i].point;
4295  if ((fabs(q.x-point.x) >= DrawEpsilon) ||
4296  (fabs(q.y-point.y) >= DrawEpsilon))
4298  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4299  else
4301  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4302  q=point;
4303  coordinates--;
4304  if (coordinates > 0)
4305  continue;
4306  if ((fabs(p.x-point.x) >= DrawEpsilon) ||
4307  (fabs(p.y-point.y) >= DrawEpsilon))
4308  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4309  (double) coordinates);
4310  else
4311  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4312  (double) coordinates);
4313  }
4314 }
4315 
4317  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4318  ExceptionInfo *exception)
4319 {
4320  CacheView
4321  *image_view;
4322 
4324  status;
4325 
4326  register ssize_t
4327  i,
4328  x;
4329 
4330  ssize_t
4331  y;
4332 
4333  if (image->debug != MagickFalse)
4334  {
4336  " begin draw-primitive");
4338  " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
4339  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4340  draw_info->affine.tx,draw_info->affine.ty);
4341  }
4342  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4343  ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
4344  (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
4345  (void) SetImageColorspace(image,sRGBColorspace,exception);
4346  status=MagickTrue;
4347  x=(ssize_t) ceil(primitive_info->point.x-0.5);
4348  y=(ssize_t) ceil(primitive_info->point.y-0.5);
4349  image_view=AcquireAuthenticCacheView(image,exception);
4350  switch (primitive_info->primitive)
4351  {
4352  case AlphaPrimitive:
4353  {
4354  if (image->alpha_trait == UndefinedPixelTrait)
4355  (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
4356  switch (primitive_info->method)
4357  {
4358  case PointMethod:
4359  default:
4360  {
4361  PixelInfo
4362  pixel;
4363 
4364  register Quantum
4365  *q;
4366 
4367  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4368  if (q == (Quantum *) NULL)
4369  break;
4370  GetFillColor(draw_info,x,y,&pixel,exception);
4371  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4372  (void) SyncCacheViewAuthenticPixels(image_view,exception);
4373  break;
4374  }
4375  case ReplaceMethod:
4376  {
4378  sync;
4379 
4380  PixelInfo
4381  pixel,
4382  target;
4383 
4384  (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4385  exception);
4386  GetPixelInfo(image,&pixel);
4387  for (y=0; y < (ssize_t) image->rows; y++)
4388  {
4389  register Quantum
4390  *magick_restrict q;
4391 
4392  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4393  exception);
4394  if (q == (Quantum *) NULL)
4395  break;
4396  for (x=0; x < (ssize_t) image->columns; x++)
4397  {
4398  GetPixelInfoPixel(image,q,&pixel);
4399  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4400  {
4401  q+=GetPixelChannels(image);
4402  continue;
4403  }
4404  GetFillColor(draw_info,x,y,&pixel,exception);
4405  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4406  q+=GetPixelChannels(image);
4407  }
4408  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4409  if (sync == MagickFalse)
4410  break;
4411  }
4412  break;
4413  }
4414  case FloodfillMethod:
4415  case FillToBorderMethod:
4416  {
4417  ChannelType
4418  channel_mask;
4419 
4420  PixelInfo
4421  target;
4422 
4424  &target,exception);
4425  if (primitive_info->method == FillToBorderMethod)
4426  {
4427  target.red=(double) draw_info->border_color.red;
4428  target.green=(double) draw_info->border_color.green;
4429  target.blue=(double) draw_info->border_color.blue;
4430  }
4431  channel_mask=SetImageChannelMask(image,AlphaChannel);
4432  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4433  primitive_info->method == FloodfillMethod ? MagickFalse :
4434  MagickTrue,exception);
4435  (void) SetImageChannelMask(image,channel_mask);
4436  break;
4437  }
4438  case ResetMethod:
4439  {
4441  sync;
4442 
4443  PixelInfo
4444  pixel;
4445 
4446  for (y=0; y < (ssize_t) image->rows; y++)
4447  {
4448  register Quantum
4449  *magick_restrict q;
4450 
4451  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4452  exception);
4453  if (q == (Quantum *) NULL)
4454  break;
4455  for (x=0; x < (ssize_t) image->columns; x++)
4456  {
4457  GetFillColor(draw_info,x,y,&pixel,exception);
4458  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
4459  q+=GetPixelChannels(image);
4460  }
4461  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4462  if (sync == MagickFalse)
4463  break;
4464  }
4465  break;
4466  }
4467  }
4468  break;
4469  }
4470  case ColorPrimitive:
4471  {
4472  switch (primitive_info->method)
4473  {
4474  case PointMethod:
4475  default:
4476  {
4477  PixelInfo
4478  pixel;
4479 
4480  register Quantum
4481  *q;
4482 
4483  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4484  if (q == (Quantum *) NULL)
4485  break;
4486  GetPixelInfo(image,&pixel);
4487  GetFillColor(draw_info,x,y,&pixel,exception);
4488  SetPixelViaPixelInfo(image,&pixel,q);
4489  (void) SyncCacheViewAuthenticPixels(image_view,exception);
4490  break;
4491  }
4492  case ReplaceMethod:
4493  {
4495  sync;
4496 
4497  PixelInfo
4498  pixel,
4499  target;
4500 
4501  (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
4502  exception);
4503  for (y=0; y < (ssize_t) image->rows; y++)
4504  {
4505  register Quantum
4506  *magick_restrict q;
4507 
4508  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4509  exception);
4510  if (q == (Quantum *) NULL)
4511  break;
4512  for (x=0; x < (ssize_t) image->columns; x++)
4513  {
4514  GetPixelInfoPixel(image,q,&pixel);
4515  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
4516  {
4517  q+=GetPixelChannels(image);
4518  continue;
4519  }
4520  GetFillColor(draw_info,x,y,&pixel,exception);
4521  SetPixelViaPixelInfo(image,&pixel,q);
4522  q+=GetPixelChannels(image);
4523  }
4524  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4525  if (sync == MagickFalse)
4526  break;
4527  }
4528  break;
4529  }
4530  case FloodfillMethod:
4531  case FillToBorderMethod:
4532  {
4533  PixelInfo
4534  target;
4535 
4537  &target,exception);
4538  if (primitive_info->method == FillToBorderMethod)
4539  {
4540  target.red=(double) draw_info->border_color.red;
4541  target.green=(double) draw_info->border_color.green;
4542  target.blue=(double) draw_info->border_color.blue;
4543  }
4544  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
4545  primitive_info->method == FloodfillMethod ? MagickFalse :
4546  MagickTrue,exception);
4547  break;
4548  }
4549  case ResetMethod:
4550  {
4552  sync;
4553 
4554  PixelInfo
4555  pixel;
4556 
4557  GetPixelInfo(image,&pixel);
4558  for (y=0; y < (ssize_t) image->rows; y++)
4559  {
4560  register Quantum
4561  *magick_restrict q;
4562 
4563  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4564  exception);
4565  if (q == (Quantum *) NULL)
4566  break;
4567  for (x=0; x < (ssize_t) image->columns; x++)
4568  {
4569  GetFillColor(draw_info,x,y,&pixel,exception);
4570  SetPixelViaPixelInfo(image,&pixel,q);
4571  q+=GetPixelChannels(image);
4572  }
4573  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4574  if (sync == MagickFalse)
4575  break;
4576  }
4577  break;
4578  }
4579  }
4580  break;
4581  }
4582  case ImagePrimitive:
4583  {
4584  AffineMatrix
4585  affine;
4586 
4587  char
4588  composite_geometry[MagickPathExtent];
4589 
4590  Image
4591  *composite_image;
4592 
4593  ImageInfo
4594  *clone_info;
4595 
4597  geometry;
4598 
4599  ssize_t
4600  x1,
4601  y1;
4602 
4603  if (primitive_info->text == (char *) NULL)
4604  break;
4605  clone_info=AcquireImageInfo();
4606  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4607  composite_image=ReadInlineImage(clone_info,primitive_info->text,
4608  exception);
4609  else
4610  {
4611  (void) CopyMagickString(clone_info->filename,primitive_info->text,
4613  composite_image=ReadImage(clone_info,exception);
4614  }
4615  clone_info=DestroyImageInfo(clone_info);
4616  if (composite_image == (Image *) NULL)
4617  break;
4618  (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4619  NULL,(void *) NULL);
4620  x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4621  y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4622  if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4623  ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4624  {
4625  /*
4626  Resize image.
4627  */
4628  (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4629  "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
4630  composite_image->filter=image->filter;
4631  (void) TransformImage(&composite_image,(char *) NULL,
4632  composite_geometry,exception);
4633  }
4634  if (composite_image->alpha_trait == UndefinedPixelTrait)
4635  (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
4636  exception);
4637  if (draw_info->alpha != OpaqueAlpha)
4638  (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
4639  SetGeometry(image,&geometry);
4640  image->gravity=draw_info->gravity;
4641  geometry.x=x;
4642  geometry.y=y;
4643  (void) FormatLocaleString(composite_geometry,MagickPathExtent,
4644  "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4645  composite_image->rows,(double) geometry.x,(double) geometry.y);
4646  (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
4647  affine=draw_info->affine;
4648  affine.tx=(double) geometry.x;
4649  affine.ty=(double) geometry.y;
4650  composite_image->interpolate=image->interpolate;
4651  status&=DrawAffineImage(image,composite_image,&affine,exception);
4652  composite_image=DestroyImage(composite_image);
4653  break;
4654  }
4655  case PointPrimitive:
4656  {
4657  PixelInfo
4658  fill_color;
4659 
4660  register Quantum
4661  *q;
4662 
4663  if ((y < 0) || (y >= (ssize_t) image->rows))
4664  break;
4665  if ((x < 0) || (x >= (ssize_t) image->columns))
4666  break;
4667  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4668  if (q == (Quantum *) NULL)
4669  break;
4670  GetFillColor(draw_info,x,y,&fill_color,exception);
4671  CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
4672  (double) GetPixelAlpha(image,q),q);
4673  (void) SyncCacheViewAuthenticPixels(image_view,exception);
4674  break;
4675  }
4676  case TextPrimitive:
4677  {
4678  char
4679  geometry[MagickPathExtent];
4680 
4681  DrawInfo
4682  *clone_info;
4683 
4684  if (primitive_info->text == (char *) NULL)
4685  break;
4686  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4687  (void) CloneString(&clone_info->text,primitive_info->text);
4688  (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4689  primitive_info->point.x,primitive_info->point.y);
4690  (void) CloneString(&clone_info->geometry,geometry);
4691  status&=AnnotateImage(image,clone_info,exception);
4692  clone_info=DestroyDrawInfo(clone_info);
4693  break;
4694  }
4695  default:
4696  {
4697  double
4698  mid,
4699  scale;
4700 
4701  DrawInfo
4702  *clone_info;
4703 
4704  if (IsEventLogging() != MagickFalse)
4705  LogPrimitiveInfo(primitive_info);
4706  scale=ExpandAffine(&draw_info->affine);
4707  if ((draw_info->dash_pattern != (double *) NULL) &&
4708  (fabs(draw_info->dash_pattern[0]) >= DrawEpsilon) &&
4709  (fabs(scale*draw_info->stroke_width) >= DrawEpsilon) &&
4710  (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
4711  {
4712  /*
4713  Draw dash polygon.
4714  */
4715  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4716  clone_info->stroke_width=0.0;
4717  clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4718  status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4719  exception);
4720  clone_info=DestroyDrawInfo(clone_info);
4721  (void) DrawDashPolygon(draw_info,primitive_info,image,exception);
4722  break;
4723  }
4724  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4725  if ((mid > 1.0) &&
4726  ((draw_info->stroke.alpha != (Quantum) TransparentAlpha) ||
4727  (draw_info->stroke_pattern != (Image *) NULL)))
4728  {
4730  closed_path;
4731 
4732  /*
4733  Draw strokes while respecting line cap/join attributes.
4734  */
4735  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4736  closed_path=
4737  (fabs(primitive_info[i-1].point.x-primitive_info[0].point.x) < DrawEpsilon) &&
4738  (fabs(primitive_info[i-1].point.y-primitive_info[0].point.y) < DrawEpsilon) ?
4740  i=(ssize_t) primitive_info[0].coordinates;
4741  if ((((draw_info->linecap == RoundCap) ||
4742  (closed_path != MagickFalse)) &&
4743  (draw_info->linejoin == RoundJoin)) ||
4744  (primitive_info[i].primitive != UndefinedPrimitive))
4745  {
4746  (void) DrawPolygonPrimitive(image,draw_info,primitive_info,
4747  exception);
4748  break;
4749  }
4750  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4751  clone_info->stroke_width=0.0;
4752  clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
4753  status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
4754  exception);
4755  clone_info=DestroyDrawInfo(clone_info);
4756  status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
4757  break;
4758  }
4759  status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
4760  break;
4761  }
4762  }
4763  image_view=DestroyCacheView(image_view);
4764  if (image->debug != MagickFalse)
4765  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4766  return(status != 0 ? MagickTrue : MagickFalse);
4767 }
4768 
4769 /*
4770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4771 % %
4772 % %
4773 % %
4774 + D r a w S t r o k e P o l y g o n %
4775 % %
4776 % %
4777 % %
4778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4779 %
4780 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4781 % the image while respecting the line cap and join attributes.
4782 %
4783 % The format of the DrawStrokePolygon method is:
4784 %
4785 % MagickBooleanType DrawStrokePolygon(Image *image,
4786 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4787 %
4788 % A description of each parameter follows:
4789 %
4790 % o image: the image.
4791 %
4792 % o draw_info: the draw info.
4793 %
4794 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4795 %
4796 %
4797 */
4798 
4799 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4800  const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4801 {
4803  linecap[5];
4804 
4805  register ssize_t
4806  i;
4807 
4808  for (i=0; i < 4; i++)
4809  linecap[i]=(*primitive_info);
4810  linecap[0].coordinates=4;
4811  linecap[1].point.x+=2.0*DrawEpsilon;
4812  linecap[2].point.x+=2.0*DrawEpsilon;
4813  linecap[2].point.y+=2.0*DrawEpsilon;
4814  linecap[3].point.y+=2.0*DrawEpsilon;
4815  linecap[4].primitive=UndefinedPrimitive;
4816  (void) DrawPolygonPrimitive(image,draw_info,linecap,exception);
4817 }
4818 
4820  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4821  ExceptionInfo *exception)
4822 {
4823  DrawInfo
4824  *clone_info;
4825 
4827  closed_path;
4828 
4830  status;
4831 
4833  *stroke_polygon;
4834 
4835  register const PrimitiveInfo
4836  *p,
4837  *q;
4838 
4839  /*
4840  Draw stroked polygon.
4841  */
4842  if (image->debug != MagickFalse)
4844  " begin draw-stroke-polygon");
4845  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4846  clone_info->fill=draw_info->stroke;
4847  if (clone_info->fill_pattern != (Image *) NULL)
4848  clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4849  if (clone_info->stroke_pattern != (Image *) NULL)
4850  clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4851  MagickTrue,exception);
4853  clone_info->stroke_width=0.0;
4854  clone_info->fill_rule=NonZeroRule;
4855  status=MagickTrue;
4856  for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4857  {
4858  stroke_polygon=TraceStrokePolygon(draw_info,p);
4859  if (stroke_polygon == (PrimitiveInfo *) NULL)
4860  {
4861  status=0;
4862  break;
4863  }
4864  status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
4865  if (status == 0)
4866  break;
4867  stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4868  q=p+p->coordinates-1;
4869  closed_path=(fabs(q->point.x-p->point.x) < DrawEpsilon) &&
4870  (fabs(q->point.y-p->point.y) < DrawEpsilon) ? MagickTrue : MagickFalse;
4871  if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4872  {
4873  DrawRoundLinecap(image,draw_info,p,exception);
4874  DrawRoundLinecap(image,draw_info,q,exception);
4875  }
4876  }
4877  clone_info=DestroyDrawInfo(clone_info);
4878  if (image->debug != MagickFalse)
4880  " end draw-stroke-polygon");
4881  return(status != 0 ? MagickTrue : MagickFalse);
4882 }
4883 
4884 /*
4885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4886 % %
4887 % %
4888 % %
4889 % G e t A f f i n e M a t r i x %
4890 % %
4891 % %
4892 % %
4893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4894 %
4895 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4896 % matrix.
4897 %
4898 % The format of the GetAffineMatrix method is:
4899 %
4900 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4901 %
4902 % A description of each parameter follows:
4903 %
4904 % o affine_matrix: the affine matrix.
4905 %
4906 */
4908 {
4909  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4910  assert(affine_matrix != (AffineMatrix *) NULL);
4911  (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4912  affine_matrix->sx=1.0;
4913  affine_matrix->sy=1.0;
4914 }
4915 
4916 /*
4917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4918 % %
4919 % %
4920 % %
4921 + G e t D r a w I n f o %
4922 % %
4923 % %
4924 % %
4925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4926 %
4927 % GetDrawInfo() initializes draw_info to default values from image_info.
4928 %
4929 % The format of the GetDrawInfo method is:
4930 %
4931 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4932 %
4933 % A description of each parameter follows:
4934 %
4935 % o image_info: the image info..
4936 %
4937 % o draw_info: the draw info.
4938 %
4939 */
4940 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4941 {
4942  char
4943  *next_token;
4944 
4945  const char
4946  *option;
4947 
4949  *exception;
4950 
4951  ImageInfo
4952  *clone_info;
4953 
4954  /*
4955  Initialize draw attributes.
4956  */
4957  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4958  assert(draw_info != (DrawInfo *) NULL);
4959  (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4960  clone_info=CloneImageInfo(image_info);
4961  GetAffineMatrix(&draw_info->affine);
4962  exception=AcquireExceptionInfo();
4963  (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
4964  exception);
4965  (void) QueryColorCompliance("#0000",AllCompliance,&draw_info->stroke,
4966  exception);
4967  draw_info->stroke_width=1.0;
4968  draw_info->fill_rule=EvenOddRule;
4969  draw_info->alpha=OpaqueAlpha;
4970  draw_info->fill_alpha=OpaqueAlpha;
4971  draw_info->stroke_alpha=OpaqueAlpha;
4972  draw_info->linecap=ButtCap;
4973  draw_info->linejoin=MiterJoin;
4974  draw_info->miterlimit=10;
4975  draw_info->decorate=NoDecoration;
4976  draw_info->pointsize=12.0;
4978  draw_info->compose=OverCompositeOp;
4979  draw_info->render=MagickTrue;
4980  draw_info->debug=IsEventLogging();
4981  draw_info->stroke_antialias=clone_info->antialias;
4982  if (clone_info->font != (char *) NULL)
4983  draw_info->font=AcquireString(clone_info->font);
4984  if (clone_info->density != (char *) NULL)
4985  draw_info->density=AcquireString(clone_info->density);
4986  draw_info->text_antialias=clone_info->antialias;
4987  if (fabs(clone_info->pointsize) >= DrawEpsilon)
4988  draw_info->pointsize=clone_info->pointsize;
4989  draw_info->border_color=clone_info->border_color;
4990  if (clone_info->server_name != (char *) NULL)
4991  draw_info->server_name=AcquireString(clone_info->server_name);
4992  option=GetImageOption(clone_info,"direction");
4993  if (option != (const char *) NULL)
4996  else
4997  draw_info->direction=UndefinedDirection;
4998  option=GetImageOption(clone_info,"encoding");
4999  if (option != (const char *) NULL)
5000  (void) CloneString(&draw_info->encoding,option);
5001  option=GetImageOption(clone_info,"family");
5002  if (option != (const char *) NULL)
5003  (void) CloneString(&draw_info->family,option);
5004  option=GetImageOption(clone_info,"fill");
5005  if (option != (const char *) NULL)
5006  (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
5007  exception);
5008  option=GetImageOption(clone_info,"gravity");
5009  if (option != (const char *) NULL)
5011  MagickFalse,option);
5012  option=GetImageOption(clone_info,"interline-spacing");
5013  if (option != (const char *) NULL)
5014  draw_info->interline_spacing=StringToDouble(option,&next_token);
5015  option=GetImageOption(clone_info,"interword-spacing");
5016  if (option != (const char *) NULL)
5017  draw_info->interword_spacing=StringToDouble(option,&next_token);
5018  option=GetImageOption(clone_info,"kerning");
5019  if (option != (const char *) NULL)
5020  draw_info->kerning=StringToDouble(option,&next_token);
5021  option=GetImageOption(clone_info,"stroke");
5022  if (option != (const char *) NULL)
5023  (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
5024  exception);
5025  option=GetImageOption(clone_info,"strokewidth");
5026  if (option != (const char *) NULL)
5027  draw_info->stroke_width=StringToDouble(option,&next_token);
5028  option=GetImageOption(clone_info,"style");
5029  if (option != (const char *) NULL)
5031  MagickFalse,option);
5032  option=GetImageOption(clone_info,"undercolor");
5033  if (option != (const char *) NULL)
5034  (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
5035  exception);
5036  option=GetImageOption(clone_info,"weight");
5037  if (option != (const char *) NULL)
5038  {
5039  ssize_t
5040  weight;
5041 
5043  if (weight == -1)
5044  weight=(ssize_t) StringToUnsignedLong(option);
5045  draw_info->weight=(size_t) weight;
5046  }
5047  exception=DestroyExceptionInfo(exception);
5048  draw_info->signature=MagickCoreSignature;
5049  clone_info=DestroyImageInfo(clone_info);
5050 }
5051 
5052 /*
5053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5054 % %
5055 % %
5056 % %
5057 + P e r m u t a t e %
5058 % %
5059 % %
5060 % %
5061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5062 %
5063 % Permutate() returns the permuation of the (n,k).
5064 %
5065 % The format of the Permutate method is:
5066 %
5067 % void Permutate(ssize_t n,ssize_t k)
5068 %
5069 % A description of each parameter follows:
5070 %
5071 % o n:
5072 %
5073 % o k:
5074 %
5075 %
5076 */
5077 static inline double Permutate(const ssize_t n,const ssize_t k)
5078 {
5079  double
5080  r;
5081 
5082  register ssize_t
5083  i;
5084 
5085  r=1.0;
5086  for (i=k+1; i <= n; i++)
5087  r*=i;
5088  for (i=1; i <= (n-k); i++)
5089  r/=i;
5090  return(r);
5091 }
5092 
5093 /*
5094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5095 % %
5096 % %
5097 % %
5098 + T r a c e P r i m i t i v e %
5099 % %
5100 % %
5101 % %
5102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5103 %
5104 % TracePrimitive is a collection of methods for generating graphic
5105 % primitives such as arcs, ellipses, paths, etc.
5106 %
5107 */
5108 
5109 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
5110  const PointInfo end,const PointInfo degrees)
5111 {
5112  PointInfo
5113  center,
5114  radii;
5115 
5116  center.x=0.5*(end.x+start.x);
5117  center.y=0.5*(end.y+start.y);
5118  radii.x=fabs(center.x-start.x);
5119  radii.y=fabs(center.y-start.y);
5120  TraceEllipse(primitive_info,center,radii,degrees);
5121 }
5122 
5123 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
5124  const PointInfo end,const PointInfo arc,const double angle,
5125  const MagickBooleanType large_arc,const MagickBooleanType sweep)
5126 {
5127  double
5128  alpha,
5129  beta,
5130  delta,
5131  factor,
5132  gamma,
5133  theta;
5134 
5135  PointInfo
5136  center,
5137  points[3],
5138  radii;
5139 
5140  register double
5141  cosine,
5142  sine;
5143 
5144  register PrimitiveInfo
5145  *p;
5146 
5147  register ssize_t
5148  i;
5149 
5150  size_t
5151  arc_segments;
5152 
5153  if ((fabs(start.x-end.x) < DrawEpsilon) &&
5154  (fabs(start.y-end.y) < DrawEpsilon))
5155  {
5156  TracePoint(primitive_info,end);
5157  return;
5158  }
5159  radii.x=fabs(arc.x);
5160  radii.y=fabs(arc.y);
5161  if ((fabs(radii.x) < DrawEpsilon) || (fabs(radii.y) < DrawEpsilon))
5162  {
5163  TraceLine(primitive_info,start,end);
5164  return;
5165  }
5166  cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5167  sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5168  center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5169  center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5170  delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5171  (radii.y*radii.y);
5172  if (delta < DrawEpsilon)
5173  {
5174  TraceLine(primitive_info,start,end);
5175  return;
5176  }
5177  if (delta > 1.0)
5178  {
5179  radii.x*=sqrt((double) delta);
5180  radii.y*=sqrt((double) delta);
5181  }
5182  points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5183  points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5184  points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5185  points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5186  alpha=points[1].x-points[0].x;
5187  beta=points[1].y-points[0].y;
5188  factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5189  if (factor <= 0.0)
5190  factor=0.0;
5191  else
5192  {
5193  factor=sqrt((double) factor);
5194  if (sweep == large_arc)
5195  factor=(-factor);
5196  }
5197  center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5198  center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5199  alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5200  theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5201  if ((theta < 0.0) && (sweep != MagickFalse))
5202  theta+=2.0*MagickPI;
5203  else
5204  if ((theta > 0.0) && (sweep == MagickFalse))
5205  theta-=2.0*MagickPI;
5206  arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+DrawEpsilon))));
5207  p=primitive_info;
5208  for (i=0; i < (ssize_t) arc_segments; i++)
5209  {
5210  beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5211  gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5212  sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5213  sin(fmod((double) beta,DegreesToRadians(360.0)));
5214  points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5215  arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5216  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5217  points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5218  arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5219  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5220  points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5221  theta/arc_segments),DegreesToRadians(360.0))));
5222  points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5223  theta/arc_segments),DegreesToRadians(360.0))));
5224  points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5225  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5226  points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5227  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5228  p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5229  p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5230  (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5231  points[0].y);
5232  (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5233  points[0].y);
5234  (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5235  points[1].y);
5236  (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5237  points[1].y);
5238  (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5239  points[2].y);
5240  (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5241  points[2].y);
5242  if (i == (ssize_t) (arc_segments-1))
5243  (p+3)->point=end;
5244  TraceBezier(p,4);
5245  p+=p->coordinates;
5246  }
5247  primitive_info->coordinates=(size_t) (p-primitive_info);
5248  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5249  {
5250  p->primitive=primitive_info->primitive;
5251  p--;
5252  }
5253 }
5254 
5255 static void TraceBezier(PrimitiveInfo *primitive_info,
5256  const size_t number_coordinates)
5257 {
5258  double
5259  alpha,
5260  *coefficients,
5261  weight;
5262 
5263  PointInfo
5264  end,
5265  point,
5266  *points;
5267 
5268  register PrimitiveInfo
5269  *p;
5270 
5271  register ssize_t
5272  i,
5273  j;
5274 
5275  size_t
5276  control_points,
5277  quantum;
5278 
5279  /*
5280  Allocate coeficients.
5281  */
5282  quantum=number_coordinates;
5283  for (i=0; i < (ssize_t) number_coordinates; i++)
5284  {
5285  for (j=i+1; j < (ssize_t) number_coordinates; j++)
5286  {
5287  alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5288  if (alpha > (double) quantum)
5289  quantum=(size_t) alpha;
5290  alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5291  if (alpha > (double) quantum)
5292  quantum=(size_t) alpha;
5293  }
5294  }
5295  quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5296  (double) BezierQuantum);
5297  control_points=quantum*number_coordinates;
5298  coefficients=(double *) AcquireQuantumMemory((size_t)
5299  number_coordinates,sizeof(*coefficients));
5300  points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5301  sizeof(*points));
5302  if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
5303  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5304  /*
5305  Compute bezier points.
5306  */
5307  end=primitive_info[number_coordinates-1].point;
5308  for (i=0; i < (ssize_t) number_coordinates; i++)
5309  coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5310  weight=0.0;
5311  for (i=0; i < (ssize_t) control_points; i++)
5312  {
5313  p=primitive_info;
5314  point.x=0.0;
5315  point.y=0.0;
5316  alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5317  for (j=0; j < (ssize_t) number_coordinates; j++)
5318  {
5319  point.x+=alpha*coefficients[j]*p->point.x;
5320  point.y+=alpha*coefficients[j]*p->point.y;
5321  alpha*=weight/(1.0-weight);
5322  p++;
5323  }
5324  points[i]=point;
5325  weight+=1.0/control_points;
5326  }
5327  /*
5328  Bezier curves are just short segmented polys.
5329  */
5330  p=primitive_info;
5331  for (i=0; i < (ssize_t) control_points; i++)
5332  {
5333  TracePoint(p,points[i]);
5334  p+=p->coordinates;
5335  }
5336  TracePoint(p,end);
5337  p+=p->coordinates;
5338  primitive_info->coordinates=(size_t) (p-primitive_info);
5339  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5340  {
5341  p->primitive=primitive_info->primitive;
5342  p--;
5343  }
5344  points=(PointInfo *) RelinquishMagickMemory(points);
5345  coefficients=(double *) RelinquishMagickMemory(coefficients);
5346 }
5347 
5348 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5349  const PointInfo end)
5350 {
5351  double
5352  alpha,
5353  beta,
5354  radius;
5355 
5356  PointInfo
5357  offset,
5358  degrees;
5359 
5360  alpha=end.x-start.x;
5361  beta=end.y-start.y;
5362  radius=hypot((double) alpha,(double) beta);
5363  offset.x=(double) radius;
5364  offset.y=(double) radius;
5365  degrees.x=0.0;
5366  degrees.y=360.0;
5367  TraceEllipse(primitive_info,start,offset,degrees);
5368 }
5369 
5370 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5371  const PointInfo stop,const PointInfo degrees)
5372 {
5373  double
5374  delta,
5375  step,
5376