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