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