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