MagickCore  6.9.2
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  ssize_t
2102  weight;
2103 
2104  GetMagickToken(q,&q,token);
2106  token);
2107  if (weight == -1)
2108  weight=StringToUnsignedLong(token);
2109  graphic_context[n]->weight=(size_t) weight;
2110  break;
2111  }
2112  status=MagickFalse;
2113  break;
2114  }
2115  case 'g':
2116  case 'G':
2117  {
2118  if (LocaleCompare("gradient-units",keyword) == 0)
2119  {
2120  GetMagickToken(q,&q,token);
2121  break;
2122  }
2123  if (LocaleCompare("gravity",keyword) == 0)
2124  {
2125  ssize_t
2126  gravity;
2127 
2128  GetMagickToken(q,&q,token);
2130  if (gravity == -1)
2131  {
2132  status=MagickFalse;
2133  break;
2134  }
2135  graphic_context[n]->gravity=(GravityType) gravity;
2136  break;
2137  }
2138  status=MagickFalse;
2139  break;
2140  }
2141  case 'i':
2142  case 'I':
2143  {
2144  if (LocaleCompare("image",keyword) == 0)
2145  {
2146  ssize_t
2147  compose;
2148 
2149  primitive_type=ImagePrimitive;
2150  GetMagickToken(q,&q,token);
2152  if (compose == -1)
2153  {
2154  status=MagickFalse;
2155  break;
2156  }
2157  graphic_context[n]->compose=(CompositeOperator) compose;
2158  break;
2159  }
2160  if (LocaleCompare("interline-spacing",keyword) == 0)
2161  {
2162  GetMagickToken(q,&q,token);
2163  graphic_context[n]->interline_spacing=StringToDouble(token,
2164  (char **) NULL);
2165  break;
2166  }
2167  if (LocaleCompare("interword-spacing",keyword) == 0)
2168  {
2169  GetMagickToken(q,&q,token);
2170  graphic_context[n]->interword_spacing=StringToDouble(token,
2171  (char **) NULL);
2172  break;
2173  }
2174  status=MagickFalse;
2175  break;
2176  }
2177  case 'k':
2178  case 'K':
2179  {
2180  if (LocaleCompare("kerning",keyword) == 0)
2181  {
2182  GetMagickToken(q,&q,token);
2183  graphic_context[n]->kerning=StringToDouble(token,(char **) NULL);
2184  break;
2185  }
2186  status=MagickFalse;
2187  break;
2188  }
2189  case 'l':
2190  case 'L':
2191  {
2192  if (LocaleCompare("line",keyword) == 0)
2193  {
2194  primitive_type=LinePrimitive;
2195  break;
2196  }
2197  status=MagickFalse;
2198  break;
2199  }
2200  case 'm':
2201  case 'M':
2202  {
2203  if (LocaleCompare("matte",keyword) == 0)
2204  {
2205  primitive_type=MattePrimitive;
2206  break;
2207  }
2208  status=MagickFalse;
2209  break;
2210  }
2211  case 'o':
2212  case 'O':
2213  {
2214  if (LocaleCompare("offset",keyword) == 0)
2215  {
2216  GetMagickToken(q,&q,token);
2217  break;
2218  }
2219  if (LocaleCompare("opacity",keyword) == 0)
2220  {
2221  GetMagickToken(q,&q,token);
2222  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2223  graphic_context[n]->opacity=ClampToQuantum((MagickRealType)
2224  QuantumRange*(1.0-((1.0-QuantumScale*graphic_context[n]->opacity)*
2225  factor*StringToDouble(token,(char **) NULL))));
2226  graphic_context[n]->fill.opacity=graphic_context[n]->opacity;
2227  graphic_context[n]->stroke.opacity=graphic_context[n]->opacity;
2228  break;
2229  }
2230  status=MagickFalse;
2231  break;
2232  }
2233  case 'p':
2234  case 'P':
2235  {
2236  if (LocaleCompare("path",keyword) == 0)
2237  {
2238  primitive_type=PathPrimitive;
2239  break;
2240  }
2241  if (LocaleCompare("point",keyword) == 0)
2242  {
2243  primitive_type=PointPrimitive;
2244  break;
2245  }
2246  if (LocaleCompare("polyline",keyword) == 0)
2247  {
2248  primitive_type=PolylinePrimitive;
2249  break;
2250  }
2251  if (LocaleCompare("polygon",keyword) == 0)
2252  {
2253  primitive_type=PolygonPrimitive;
2254  break;
2255  }
2256  if (LocaleCompare("pop",keyword) == 0)
2257  {
2258  GetMagickToken(q,&q,token);
2259  if (LocaleCompare("clip-path",token) == 0)
2260  break;
2261  if (LocaleCompare("defs",token) == 0)
2262  break;
2263  if (LocaleCompare("gradient",token) == 0)
2264  break;
2265  if (LocaleCompare("graphic-context",token) == 0)
2266  {
2267  if (n <= 0)
2268  {
2269  (void) ThrowMagickException(&image->exception,
2271  "UnbalancedGraphicContextPushPop","`%s'",token);
2272  n=0;
2273  break;
2274  }
2275  if (graphic_context[n]->clip_mask != (char *) NULL)
2276  if (LocaleCompare(graphic_context[n]->clip_mask,
2277  graphic_context[n-1]->clip_mask) != 0)
2278  (void) SetImageClipMask(image,(Image *) NULL);
2279  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2280  n--;
2281  break;
2282  }
2283  if (LocaleCompare("pattern",token) == 0)
2284  break;
2285  status=MagickFalse;
2286  break;
2287  }
2288  if (LocaleCompare("push",keyword) == 0)
2289  {
2290  GetMagickToken(q,&q,token);
2291  if (LocaleCompare("clip-path",token) == 0)
2292  {
2293  char
2294  name[MaxTextExtent];
2295 
2296  GetMagickToken(q,&q,token);
2297  (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
2298  for (p=q; *q != '\0'; )
2299  {
2300  GetMagickToken(q,&q,token);
2301  if (LocaleCompare(token,"pop") != 0)
2302  continue;
2303  GetMagickToken(q,(const char **) NULL,token);
2304  if (LocaleCompare(token,"clip-path") != 0)
2305  continue;
2306  break;
2307  }
2308  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2309  (void) SetImageArtifact(image,name,token);
2310  GetMagickToken(q,&q,token);
2311  break;
2312  }
2313  if (LocaleCompare("gradient",token) == 0)
2314  {
2315  char
2316  key[2*MaxTextExtent],
2317  name[MaxTextExtent],
2318  type[MaxTextExtent];
2319 
2320  SegmentInfo
2321  segment;
2322 
2323  GetMagickToken(q,&q,token);
2324  (void) CopyMagickString(name,token,MaxTextExtent);
2325  GetMagickToken(q,&q,token);
2326  (void) CopyMagickString(type,token,MaxTextExtent);
2327  GetMagickToken(q,&q,token);
2328  segment.x1=StringToDouble(token,(char **) NULL);
2329  GetMagickToken(q,&q,token);
2330  if (*token == ',')
2331  GetMagickToken(q,&q,token);
2332  segment.y1=StringToDouble(token,(char **) NULL);
2333  GetMagickToken(q,&q,token);
2334  if (*token == ',')
2335  GetMagickToken(q,&q,token);
2336  segment.x2=StringToDouble(token,(char **) NULL);
2337  GetMagickToken(q,&q,token);
2338  if (*token == ',')
2339  GetMagickToken(q,&q,token);
2340  segment.y2=StringToDouble(token,(char **) NULL);
2341  if (LocaleCompare(type,"radial") == 0)
2342  {
2343  GetMagickToken(q,&q,token);
2344  if (*token == ',')
2345  GetMagickToken(q,&q,token);
2346  }
2347  for (p=q; *q != '\0'; )
2348  {
2349  GetMagickToken(q,&q,token);
2350  if (LocaleCompare(token,"pop") != 0)
2351  continue;
2352  GetMagickToken(q,(const char **) NULL,token);
2353  if (LocaleCompare(token,"gradient") != 0)
2354  continue;
2355  break;
2356  }
2357  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2358  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
2359  graphic_context[n]->affine.ry*segment.y1+
2360  graphic_context[n]->affine.tx;
2361  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
2362  graphic_context[n]->affine.sy*segment.y1+
2363  graphic_context[n]->affine.ty;
2364  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
2365  graphic_context[n]->affine.ry*segment.y2+
2366  graphic_context[n]->affine.tx;
2367  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
2368  graphic_context[n]->affine.sy*segment.y2+
2369  graphic_context[n]->affine.ty;
2370  (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2371  (void) SetImageArtifact(image,key,token);
2372  (void) FormatLocaleString(key,MaxTextExtent,"%s-type",name);
2373  (void) SetImageArtifact(image,key,type);
2374  (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2375  (void) FormatLocaleString(geometry,MaxTextExtent,
2376  "%gx%g%+.15g%+.15g",
2377  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
2378  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
2379  bounds.x1,bounds.y1);
2380  (void) SetImageArtifact(image,key,geometry);
2381  GetMagickToken(q,&q,token);
2382  break;
2383  }
2384  if (LocaleCompare("pattern",token) == 0)
2385  {
2387  bounds;
2388 
2389  GetMagickToken(q,&q,token);
2390  (void) CopyMagickString(name,token,MaxTextExtent);
2391  GetMagickToken(q,&q,token);
2392  bounds.x=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2393  0.5);
2394  GetMagickToken(q,&q,token);
2395  if (*token == ',')
2396  GetMagickToken(q,&q,token);
2397  bounds.y=(ssize_t) ceil(StringToDouble(token,(char **) NULL)-
2398  0.5);
2399  GetMagickToken(q,&q,token);
2400  if (*token == ',')
2401  GetMagickToken(q,&q,token);
2402  bounds.width=(size_t) floor(StringToDouble(token,
2403  (char **) NULL)+0.5);
2404  GetMagickToken(q,&q,token);
2405  if (*token == ',')
2406  GetMagickToken(q,&q,token);
2407  bounds.height=(size_t) floor(StringToDouble(token,
2408  (char **) NULL)+0.5);
2409  for (p=q; *q != '\0'; )
2410  {
2411  GetMagickToken(q,&q,token);
2412  if (LocaleCompare(token,"pop") != 0)
2413  continue;
2414  GetMagickToken(q,(const char **) NULL,token);
2415  if (LocaleCompare(token,"pattern") != 0)
2416  continue;
2417  break;
2418  }
2419  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
2420  (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
2421  (void) SetImageArtifact(image,key,token);
2422  (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
2423  (void) FormatLocaleString(geometry,MaxTextExtent,
2424  "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
2425  bounds.height,(double) bounds.x,(double) bounds.y);
2426  (void) SetImageArtifact(image,key,geometry);
2427  GetMagickToken(q,&q,token);
2428  break;
2429  }
2430  if (LocaleCompare("graphic-context",token) == 0)
2431  {
2432  n++;
2433  graphic_context=(DrawInfo **) ResizeQuantumMemory(
2434  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
2435  if (graphic_context == (DrawInfo **) NULL)
2436  {
2437  (void) ThrowMagickException(&image->exception,
2439  "MemoryAllocationFailed","`%s'",image->filename);
2440  break;
2441  }
2442  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
2443  graphic_context[n-1]);
2444  break;
2445  }
2446  if (LocaleCompare("defs",token) == 0)
2447  break;
2448  status=MagickFalse;
2449  break;
2450  }
2451  status=MagickFalse;
2452  break;
2453  }
2454  case 'r':
2455  case 'R':
2456  {
2457  if (LocaleCompare("rectangle",keyword) == 0)
2458  {
2459  primitive_type=RectanglePrimitive;
2460  break;
2461  }
2462  if (LocaleCompare("rotate",keyword) == 0)
2463  {
2464  GetMagickToken(q,&q,token);
2465  angle=StringToDouble(token,(char **) NULL);
2466  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
2467  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
2468  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
2469  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
2470  break;
2471  }
2472  if (LocaleCompare("roundRectangle",keyword) == 0)
2473  {
2474  primitive_type=RoundRectanglePrimitive;
2475  break;
2476  }
2477  status=MagickFalse;
2478  break;
2479  }
2480  case 's':
2481  case 'S':
2482  {
2483  if (LocaleCompare("scale",keyword) == 0)
2484  {
2485  GetMagickToken(q,&q,token);
2486  affine.sx=StringToDouble(token,(char **) NULL);
2487  GetMagickToken(q,&q,token);
2488  if (*token == ',')
2489  GetMagickToken(q,&q,token);
2490  affine.sy=StringToDouble(token,(char **) NULL);
2491  break;
2492  }
2493  if (LocaleCompare("skewX",keyword) == 0)
2494  {
2495  GetMagickToken(q,&q,token);
2496  angle=StringToDouble(token,(char **) NULL);
2497  affine.ry=sin(DegreesToRadians(angle));
2498  break;
2499  }
2500  if (LocaleCompare("skewY",keyword) == 0)
2501  {
2502  GetMagickToken(q,&q,token);
2503  angle=StringToDouble(token,(char **) NULL);
2504  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
2505  break;
2506  }
2507  if (LocaleCompare("stop-color",keyword) == 0)
2508  {
2509  GradientType
2510  type;
2511 
2512  PixelPacket
2513  stop_color;
2514 
2515  GetMagickToken(q,&q,token);
2516  (void) QueryColorDatabase(token,&stop_color,&image->exception);
2517  type=LinearGradient;
2518  if (draw_info->gradient.type == RadialGradient)
2519  type=RadialGradient;
2520  (void) GradientImage(image,type,PadSpread,&start_color,
2521  &stop_color);
2522  start_color=stop_color;
2523  GetMagickToken(q,&q,token);
2524  break;
2525  }
2526  if (LocaleCompare("stroke",keyword) == 0)
2527  {
2528  GetMagickToken(q,&q,token);
2529  (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2530  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2531  (void) DrawPatternPath(image,draw_info,token,
2532  &graphic_context[n]->stroke_pattern);
2533  else
2534  {
2535  status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
2536  &image->exception);
2537  if (status == MagickFalse)
2538  {
2539  ImageInfo
2540  *pattern_info;
2541 
2542  pattern_info=AcquireImageInfo();
2543  (void) CopyMagickString(pattern_info->filename,token,
2544  MaxTextExtent);
2545  graphic_context[n]->stroke_pattern=
2546  ReadImage(pattern_info,&image->exception);
2547  CatchException(&image->exception);
2548  pattern_info=DestroyImageInfo(pattern_info);
2549  }
2550  }
2551  break;
2552  }
2553  if (LocaleCompare("stroke-antialias",keyword) == 0)
2554  {
2555  GetMagickToken(q,&q,token);
2556  graphic_context[n]->stroke_antialias=
2557  StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2558  break;
2559  }
2560  if (LocaleCompare("stroke-dasharray",keyword) == 0)
2561  {
2562  if (graphic_context[n]->dash_pattern != (double *) NULL)
2563  graphic_context[n]->dash_pattern=(double *)
2564  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
2565  if (IsPoint(q) != MagickFalse)
2566  {
2567  const char
2568  *p;
2569 
2570  p=q;
2571  GetMagickToken(p,&p,token);
2572  if (*token == ',')
2573  GetMagickToken(p,&p,token);
2574  for (x=0; IsPoint(token) != MagickFalse; x++)
2575  {
2576  GetMagickToken(p,&p,token);
2577  if (*token == ',')
2578  GetMagickToken(p,&p,token);
2579  }
2580  graphic_context[n]->dash_pattern=(double *)
2581  AcquireQuantumMemory((size_t) (2UL*x+1UL),
2582  sizeof(*graphic_context[n]->dash_pattern));
2583  if (graphic_context[n]->dash_pattern == (double *) NULL)
2584  {
2585  (void) ThrowMagickException(&image->exception,
2587  "MemoryAllocationFailed","`%s'",image->filename);
2588  break;
2589  }
2590  for (j=0; j < x; j++)
2591  {
2592  GetMagickToken(q,&q,token);
2593  if (*token == ',')
2594  GetMagickToken(q,&q,token);
2595  graphic_context[n]->dash_pattern[j]=StringToDouble(token,
2596  (char **) NULL);
2597  }
2598  if ((x & 0x01) != 0)
2599  for ( ; j < (2*x); j++)
2600  graphic_context[n]->dash_pattern[j]=
2601  graphic_context[n]->dash_pattern[j-x];
2602  graphic_context[n]->dash_pattern[j]=0.0;
2603  break;
2604  }
2605  GetMagickToken(q,&q,token);
2606  break;
2607  }
2608  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
2609  {
2610  GetMagickToken(q,&q,token);
2611  graphic_context[n]->dash_offset=StringToDouble(token,
2612  (char **) NULL);
2613  break;
2614  }
2615  if (LocaleCompare("stroke-linecap",keyword) == 0)
2616  {
2617  ssize_t
2618  linecap;
2619 
2620  GetMagickToken(q,&q,token);
2622  if (linecap == -1)
2623  {
2624  status=MagickFalse;
2625  break;
2626  }
2627  graphic_context[n]->linecap=(LineCap) linecap;
2628  break;
2629  }
2630  if (LocaleCompare("stroke-linejoin",keyword) == 0)
2631  {
2632  ssize_t
2633  linejoin;
2634 
2635  GetMagickToken(q,&q,token);
2637  if (linejoin == -1)
2638  {
2639  status=MagickFalse;
2640  break;
2641  }
2642  graphic_context[n]->linejoin=(LineJoin) linejoin;
2643  break;
2644  }
2645  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
2646  {
2647  GetMagickToken(q,&q,token);
2648  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
2649  break;
2650  }
2651  if (LocaleCompare("stroke-opacity",keyword) == 0)
2652  {
2653  GetMagickToken(q,&q,token);
2654  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2655  graphic_context[n]->stroke.opacity=ClampToQuantum((MagickRealType)
2656  QuantumRange*(1.0-factor*StringToDouble(token,(char **) NULL)));
2657  break;
2658  }
2659  if (LocaleCompare("stroke-width",keyword) == 0)
2660  {
2661  GetMagickToken(q,&q,token);
2662  graphic_context[n]->stroke_width=StringToDouble(token,
2663  (char **) NULL);
2664  break;
2665  }
2666  status=MagickFalse;
2667  break;
2668  }
2669  case 't':
2670  case 'T':
2671  {
2672  if (LocaleCompare("text",keyword) == 0)
2673  {
2674  primitive_type=TextPrimitive;
2675  break;
2676  }
2677  if (LocaleCompare("text-align",keyword) == 0)
2678  {
2679  ssize_t
2680  align;
2681 
2682  GetMagickToken(q,&q,token);
2684  if (align == -1)
2685  {
2686  status=MagickFalse;
2687  break;
2688  }
2689  graphic_context[n]->align=(AlignType) align;
2690  break;
2691  }
2692  if (LocaleCompare("text-anchor",keyword) == 0)
2693  {
2694  ssize_t
2695  align;
2696 
2697  GetMagickToken(q,&q,token);
2699  if (align == -1)
2700  {
2701  status=MagickFalse;
2702  break;
2703  }
2704  graphic_context[n]->align=(AlignType) align;
2705  break;
2706  }
2707  if (LocaleCompare("text-antialias",keyword) == 0)
2708  {
2709  GetMagickToken(q,&q,token);
2710  graphic_context[n]->text_antialias=
2711  StringToLong(token) != 0 ? MagickTrue : MagickFalse;
2712  break;
2713  }
2714  if (LocaleCompare("text-undercolor",keyword) == 0)
2715  {
2716  GetMagickToken(q,&q,token);
2717  (void) QueryColorDatabase(token,&graphic_context[n]->undercolor,
2718  &image->exception);
2719  break;
2720  }
2721  if (LocaleCompare("translate",keyword) == 0)
2722  {
2723  GetMagickToken(q,&q,token);
2724  affine.tx=StringToDouble(token,(char **) NULL);
2725  GetMagickToken(q,&q,token);
2726  if (*token == ',')
2727  GetMagickToken(q,&q,token);
2728  affine.ty=StringToDouble(token,(char **) NULL);
2729  break;
2730  }
2731  status=MagickFalse;
2732  break;
2733  }
2734  case 'v':
2735  case 'V':
2736  {
2737  if (LocaleCompare("viewbox",keyword) == 0)
2738  {
2739  GetMagickToken(q,&q,token);
2740  graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
2741  (char **) NULL)-0.5);
2742  GetMagickToken(q,&q,token);
2743  if (*token == ',')
2744  GetMagickToken(q,&q,token);
2745  graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
2746  (char **) NULL)-0.5);
2747  GetMagickToken(q,&q,token);
2748  if (*token == ',')
2749  GetMagickToken(q,&q,token);
2750  graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
2751  token,(char **) NULL)+0.5);
2752  GetMagickToken(q,&q,token);
2753  if (*token == ',')
2754  GetMagickToken(q,&q,token);
2755  graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
2756  token,(char **) NULL)+0.5);
2757  break;
2758  }
2759  status=MagickFalse;
2760  break;
2761  }
2762  default:
2763  {
2764  status=MagickFalse;
2765  break;
2766  }
2767  }
2768  if (status == MagickFalse)
2769  break;
2770  if ((affine.sx != 1.0) || (affine.rx != 0.0) || (affine.ry != 0.0) ||
2771  (affine.sy != 1.0) || (affine.tx != 0.0) || (affine.ty != 0.0))
2772  {
2773  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
2774  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
2775  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
2776  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
2777  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
2778  current.tx;
2779  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
2780  current.ty;
2781  }
2782  if (primitive_type == UndefinedPrimitive)
2783  {
2784  if (image->debug != MagickFalse)
2785  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",
2786  (int) (q-p),p);
2787  continue;
2788  }
2789  /*
2790  Parse the primitive attributes.
2791  */
2792  i=0;
2793  j=0;
2794  primitive_info[0].point.x=0.0;
2795  primitive_info[0].point.y=0.0;
2796  for (x=0; *q != '\0'; x++)
2797  {
2798  /*
2799  Define points.
2800  */
2801  if (IsPoint(q) == MagickFalse)
2802  break;
2803  GetMagickToken(q,&q,token);
2804  point.x=StringToDouble(token,(char **) NULL);
2805  GetMagickToken(q,&q,token);
2806  if (*token == ',')
2807  GetMagickToken(q,&q,token);
2808  point.y=StringToDouble(token,(char **) NULL);
2809  GetMagickToken(q,(const char **) NULL,token);
2810  if (*token == ',')
2811  GetMagickToken(q,&q,token);
2812  primitive_info[i].primitive=primitive_type;
2813  primitive_info[i].point=point;
2814  primitive_info[i].coordinates=0;
2815  primitive_info[i].method=FloodfillMethod;
2816  i++;
2817  if (i < (ssize_t) number_points)
2818  continue;
2819  number_points<<=1;
2820  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2821  (size_t) number_points,sizeof(*primitive_info));
2822  if (primitive_info == (PrimitiveInfo *) NULL)
2823  {
2825  ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
2826  break;
2827  }
2828  }
2829  primitive_info[j].primitive=primitive_type;
2830  primitive_info[j].coordinates=(size_t) x;
2831  primitive_info[j].method=FloodfillMethod;
2832  primitive_info[j].text=(char *) NULL;
2833  /*
2834  Circumscribe primitive within a circle.
2835  */
2836  bounds.x1=primitive_info[j].point.x;
2837  bounds.y1=primitive_info[j].point.y;
2838  bounds.x2=primitive_info[j].point.x;
2839  bounds.y2=primitive_info[j].point.y;
2840  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
2841  {
2842  point=primitive_info[j+k].point;
2843  if (point.x < bounds.x1)
2844  bounds.x1=point.x;
2845  if (point.y < bounds.y1)
2846  bounds.y1=point.y;
2847  if (point.x > bounds.x2)
2848  bounds.x2=point.x;
2849  if (point.y > bounds.y2)
2850  bounds.y2=point.y;
2851  }
2852  /*
2853  Speculate how many points our primitive might consume.
2854  */
2855  length=primitive_info[j].coordinates;
2856  switch (primitive_type)
2857  {
2858  case RectanglePrimitive:
2859  {
2860  length*=5;
2861  break;
2862  }
2864  {
2865  double
2866  alpha,
2867  beta,
2868  radius;
2869 
2870  alpha=bounds.x2-bounds.x1;
2871  beta=bounds.y2-bounds.y1;
2872  radius=hypot((double) alpha,(double) beta);
2873  length*=5;
2874  length+=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2875  break;
2876  }
2877  case BezierPrimitive:
2878  {
2879  if (primitive_info[j].coordinates > 107)
2881  DrawError,"TooManyBezierCoordinates","`%s'",token);
2882  length=BezierQuantum*primitive_info[j].coordinates;
2883  break;
2884  }
2885  case PathPrimitive:
2886  {
2887  char
2888  *s,
2889  *t;
2890 
2891  GetMagickToken(q,&q,token);
2892  length=1;
2893  t=token;
2894  for (s=token; *s != '\0'; s=t)
2895  {
2896  double
2897  value;
2898 
2899  value=StringToDouble(s,&t);
2900  (void) value;
2901  if (s == t)
2902  {
2903  t++;
2904  continue;
2905  }
2906  length++;
2907  }
2908  length=length*BezierQuantum/2;
2909  break;
2910  }
2911  case CirclePrimitive:
2912  case ArcPrimitive:
2913  case EllipsePrimitive:
2914  {
2915  double
2916  alpha,
2917  beta,
2918  radius;
2919 
2920  alpha=bounds.x2-bounds.x1;
2921  beta=bounds.y2-bounds.y1;
2922  radius=hypot((double) alpha,(double) beta);
2923  length=2*((size_t) ceil((double) MagickPI*radius))+6*BezierQuantum+360;
2924  break;
2925  }
2926  default:
2927  break;
2928  }
2929  if ((size_t) (i+length) >= number_points)
2930  {
2931  /*
2932  Resize based on speculative points required by primitive.
2933  */
2934  number_points+=length+1;
2935  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(primitive_info,
2936  (size_t) number_points,sizeof(*primitive_info));
2937  if (primitive_info == (PrimitiveInfo *) NULL)
2938  {
2940  ResourceLimitError,"MemoryAllocationFailed","`%s'",
2941  image->filename);
2942  break;
2943  }
2944  }
2945  switch (primitive_type)
2946  {
2947  case PointPrimitive:
2948  default:
2949  {
2950  if (primitive_info[j].coordinates != 1)
2951  {
2952  status=MagickFalse;
2953  break;
2954  }
2955  TracePoint(primitive_info+j,primitive_info[j].point);
2956  i=(ssize_t) (j+primitive_info[j].coordinates);
2957  break;
2958  }
2959  case LinePrimitive:
2960  {
2961  if (primitive_info[j].coordinates != 2)
2962  {
2963  status=MagickFalse;
2964  break;
2965  }
2966  TraceLine(primitive_info+j,primitive_info[j].point,
2967  primitive_info[j+1].point);
2968  i=(ssize_t) (j+primitive_info[j].coordinates);
2969  break;
2970  }
2971  case RectanglePrimitive:
2972  {
2973  if (primitive_info[j].coordinates != 2)
2974  {
2975  status=MagickFalse;
2976  break;
2977  }
2978  TraceRectangle(primitive_info+j,primitive_info[j].point,
2979  primitive_info[j+1].point);
2980  i=(ssize_t) (j+primitive_info[j].coordinates);
2981  break;
2982  }
2984  {
2985  if (primitive_info[j].coordinates != 3)
2986  {
2987  status=MagickFalse;
2988  break;
2989  }
2990  TraceRoundRectangle(primitive_info+j,primitive_info[j].point,
2991  primitive_info[j+1].point,primitive_info[j+2].point);
2992  i=(ssize_t) (j+primitive_info[j].coordinates);
2993  break;
2994  }
2995  case ArcPrimitive:
2996  {
2997  if (primitive_info[j].coordinates != 3)
2998  {
2999  primitive_type=UndefinedPrimitive;
3000  break;
3001  }
3002  TraceArc(primitive_info+j,primitive_info[j].point,
3003  primitive_info[j+1].point,primitive_info[j+2].point);
3004  i=(ssize_t) (j+primitive_info[j].coordinates);
3005  break;
3006  }
3007  case EllipsePrimitive:
3008  {
3009  if (primitive_info[j].coordinates != 3)
3010  {
3011  status=MagickFalse;
3012  break;
3013  }
3014  TraceEllipse(primitive_info+j,primitive_info[j].point,
3015  primitive_info[j+1].point,primitive_info[j+2].point);
3016  i=(ssize_t) (j+primitive_info[j].coordinates);
3017  break;
3018  }
3019  case CirclePrimitive:
3020  {
3021  if (primitive_info[j].coordinates != 2)
3022  {
3023  status=MagickFalse;
3024  break;
3025  }
3026  TraceCircle(primitive_info+j,primitive_info[j].point,
3027  primitive_info[j+1].point);
3028  i=(ssize_t) (j+primitive_info[j].coordinates);
3029  break;
3030  }
3031  case PolylinePrimitive:
3032  break;
3033  case PolygonPrimitive:
3034  {
3035  primitive_info[i]=primitive_info[j];
3036  primitive_info[i].coordinates=0;
3037  primitive_info[j].coordinates++;
3038  i++;
3039  break;
3040  }
3041  case BezierPrimitive:
3042  {
3043  if (primitive_info[j].coordinates < 3)
3044  {
3045  status=MagickFalse;
3046  break;
3047  }
3048  TraceBezier(primitive_info+j,primitive_info[j].coordinates);
3049  i=(ssize_t) (j+primitive_info[j].coordinates);
3050  break;
3051  }
3052  case PathPrimitive:
3053  {
3054  i=(ssize_t) (j+TracePath(primitive_info+j,token));
3055  break;
3056  }
3057  case ColorPrimitive:
3058  case MattePrimitive:
3059  {
3060  ssize_t
3061  method;
3062 
3063  if (primitive_info[j].coordinates != 1)
3064  {
3065  status=MagickFalse;
3066  break;
3067  }
3068  GetMagickToken(q,&q,token);
3070  if (method == -1)
3071  {
3072  status=MagickFalse;
3073  break;
3074  }
3075  primitive_info[j].method=(PaintMethod) method;
3076  break;
3077  }
3078  case TextPrimitive:
3079  {
3080  if (primitive_info[j].coordinates != 1)
3081  {
3082  status=MagickFalse;
3083  break;
3084  }
3085  if (*token != ',')
3086  GetMagickToken(q,&q,token);
3087  primitive_info[j].text=AcquireString(token);
3088  break;
3089  }
3090  case ImagePrimitive:
3091  {
3092  if (primitive_info[j].coordinates != 2)
3093  {
3094  status=MagickFalse;
3095  break;
3096  }
3097  GetMagickToken(q,&q,token);
3098  primitive_info[j].text=AcquireString(token);
3099  break;
3100  }
3101  }
3102  if (primitive_info == (PrimitiveInfo *) NULL)
3103  break;
3104  if (image->debug != MagickFalse)
3105  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
3106  if (status == MagickFalse)
3107  break;
3108  primitive_info[i].primitive=UndefinedPrimitive;
3109  if (i == 0)
3110  continue;
3111  /*
3112  Transform points.
3113  */
3114  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3115  {
3116  point=primitive_info[i].point;
3117  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
3118  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
3119  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
3120  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
3121  point=primitive_info[i].point;
3122  if (point.x < graphic_context[n]->bounds.x1)
3123  graphic_context[n]->bounds.x1=point.x;
3124  if (point.y < graphic_context[n]->bounds.y1)
3125  graphic_context[n]->bounds.y1=point.y;
3126  if (point.x > graphic_context[n]->bounds.x2)
3127  graphic_context[n]->bounds.x2=point.x;
3128  if (point.y > graphic_context[n]->bounds.y2)
3129  graphic_context[n]->bounds.y2=point.y;
3130  if (primitive_info[i].primitive == ImagePrimitive)
3131  break;
3132  if (i >= (ssize_t) number_points)
3133  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
3134  }
3135  if (graphic_context[n]->render != MagickFalse)
3136  {
3137  if ((n != 0) && (graphic_context[n]->clip_mask != (char *) NULL) &&
3138  (LocaleCompare(graphic_context[n]->clip_mask,
3139  graphic_context[n-1]->clip_mask) != 0))
3140  status&=DrawClipPath(image,graphic_context[n],
3141  graphic_context[n]->clip_mask);
3142  status&=DrawPrimitive(image,graphic_context[n],primitive_info);
3143  }
3144  if (primitive_info->text != (char *) NULL)
3145  primitive_info->text=(char *) RelinquishMagickMemory(
3146  primitive_info->text);
3147  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
3148  primitive_extent);
3149  if (proceed == MagickFalse)
3150  break;
3151  if (status == 0)
3152  break;
3153  }
3154  if (image->debug != MagickFalse)
3155  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
3156  /*
3157  Relinquish resources.
3158  */
3159  token=DestroyString(token);
3160  if (primitive_info != (PrimitiveInfo *) NULL)
3161  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
3162  primitive=DestroyString(primitive);
3163  for ( ; n >= 0; n--)
3164  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3165  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
3166  if (status == MagickFalse)
3167  ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
3168  keyword);
3169  return(status != 0 ? MagickTrue : MagickFalse);
3170 }
3171 
3172 /*
3173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3174 % %
3175 % %
3176 % %
3177 % D r a w G r a d i e n t I m a g e %
3178 % %
3179 % %
3180 % %
3181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3182 %
3183 % DrawGradientImage() draws a linear gradient on the image.
3184 %
3185 % The format of the DrawGradientImage method is:
3186 %
3187 % MagickBooleanType DrawGradientImage(Image *image,
3188 % const DrawInfo *draw_info)
3189 %
3190 % A description of each parameter follows:
3191 %
3192 % o image: the image.
3193 %
3194 % o _info: the draw info.
3195 %
3196 */
3197 
3198 static inline double GetStopColorOffset(const GradientInfo *gradient,
3199  const ssize_t x,const ssize_t y)
3200 {
3201  switch (gradient->type)
3202  {
3203  case UndefinedGradient:
3204  case LinearGradient:
3205  {
3206  double
3207  gamma,
3208  length,
3209  offset,
3210  scale;
3211 
3212  PointInfo
3213  p,
3214  q;
3215 
3216  const SegmentInfo
3217  *gradient_vector;
3218 
3219  gradient_vector=(&gradient->gradient_vector);
3220  p.x=gradient_vector->x2-gradient_vector->x1;
3221  p.y=gradient_vector->y2-gradient_vector->y1;
3222  q.x=(double) x-gradient_vector->x1;
3223  q.y=(double) y-gradient_vector->y1;
3224  length=sqrt(q.x*q.x+q.y*q.y);
3225  gamma=sqrt(p.x*p.x+p.y*p.y)*length;
3226  gamma=PerceptibleReciprocal(gamma);
3227  scale=p.x*q.x+p.y*q.y;
3228  offset=gamma*scale*length;
3229  return(offset);
3230  }
3231  case RadialGradient:
3232  {
3233  double
3234  length,
3235  offset;
3236 
3237  PointInfo
3238  v;
3239 
3240  v.x=(double) x-gradient->center.x;
3241  v.y=(double) y-gradient->center.y;
3242  length=sqrt(v.x*v.x+v.y*v.y);
3243  if (gradient->spread == RepeatSpread)
3244  return(length);
3245  offset=length/gradient->radius;
3246  return(offset);
3247  }
3248  }
3249  return(0.0);
3250 }
3251 
3253  const DrawInfo *draw_info)
3254 {
3255  CacheView
3256  *image_view;
3257 
3258  const GradientInfo
3259  *gradient;
3260 
3261  const SegmentInfo
3262  *gradient_vector;
3263 
3264  double
3265  length;
3266 
3268  *exception;
3269 
3271  status;
3272 
3274  zero;
3275 
3276  PointInfo
3277  point;
3278 
3280  bounding_box;
3281 
3282  ssize_t
3283  y;
3284 
3285  /*
3286  Draw linear or radial gradient on image.
3287  */
3288  assert(image != (Image *) NULL);
3289  assert(image->signature == MagickSignature);
3290  if (image->debug != MagickFalse)
3291  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3292  assert(draw_info != (const DrawInfo *) NULL);
3293  gradient=(&draw_info->gradient);
3294  gradient_vector=(&gradient->gradient_vector);
3295  point.x=gradient_vector->x2-gradient_vector->x1;
3296  point.y=gradient_vector->y2-gradient_vector->y1;
3297  length=sqrt(point.x*point.x+point.y*point.y);
3298  bounding_box=gradient->bounding_box;
3299  status=MagickTrue;
3300  exception=(&image->exception);
3301  GetMagickPixelPacket(image,&zero);
3302  image_view=AcquireAuthenticCacheView(image,exception);
3303 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3304  #pragma omp parallel for schedule(static,4) shared(status) \
3305  magick_threads(image,image,1,1)
3306 #endif
3307  for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
3308  {
3309  double
3310  alpha,
3311  offset;
3312 
3314  composite,
3315  pixel;
3316 
3317  register IndexPacket
3318  *restrict indexes;
3319 
3320  register ssize_t
3321  i,
3322  x;
3323 
3324  register PixelPacket
3325  *restrict q;
3326 
3327  ssize_t
3328  j;
3329 
3330  if (status == MagickFalse)
3331  continue;
3332  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3333  if (q == (PixelPacket *) NULL)
3334  {
3335  status=MagickFalse;
3336  continue;
3337  }
3338  indexes=GetCacheViewAuthenticIndexQueue(image_view);
3339  pixel=zero;
3340  composite=zero;
3341  offset=GetStopColorOffset(gradient,0,y);
3342  if (gradient->type != RadialGradient)
3343  offset/=length;
3344  for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
3345  {
3346  SetMagickPixelPacket(image,q,indexes+x,&pixel);
3347  switch (gradient->spread)
3348  {
3349  case UndefinedSpread:
3350  case PadSpread:
3351  {
3352  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3353  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3354  {
3355  offset=GetStopColorOffset(gradient,x,y);
3356  if (gradient->type != RadialGradient)
3357  offset/=length;
3358  }
3359  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3360  if (offset < gradient->stops[i].offset)
3361  break;
3362  if ((offset < 0.0) || (i == 0))
3363  composite=gradient->stops[0].color;
3364  else
3365  if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
3366  composite=gradient->stops[gradient->number_stops-1].color;
3367  else
3368  {
3369  j=i;
3370  i--;
3371  alpha=(offset-gradient->stops[i].offset)/
3372  (gradient->stops[j].offset-gradient->stops[i].offset);
3373  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3374  &gradient->stops[j].color,alpha,&composite);
3375  }
3376  break;
3377  }
3378  case ReflectSpread:
3379  {
3380  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3381  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3382  {
3383  offset=GetStopColorOffset(gradient,x,y);
3384  if (gradient->type != RadialGradient)
3385  offset/=length;
3386  }
3387  if (offset < 0.0)
3388  offset=(-offset);
3389  if ((ssize_t) fmod(offset,2.0) == 0)
3390  offset=fmod(offset,1.0);
3391  else
3392  offset=1.0-fmod(offset,1.0);
3393  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3394  if (offset < gradient->stops[i].offset)
3395  break;
3396  if (i == 0)
3397  composite=gradient->stops[0].color;
3398  else
3399  if (i == (ssize_t) gradient->number_stops)
3400  composite=gradient->stops[gradient->number_stops-1].color;
3401  else
3402  {
3403  j=i;
3404  i--;
3405  alpha=(offset-gradient->stops[i].offset)/
3406  (gradient->stops[j].offset-gradient->stops[i].offset);
3407  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3408  &gradient->stops[j].color,alpha,&composite);
3409  }
3410  break;
3411  }
3412  case RepeatSpread:
3413  {
3414  double
3415  repeat;
3416 
3418  antialias;
3419 
3420  antialias=MagickFalse;
3421  repeat=0.0;
3422  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
3423  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
3424  {
3425  offset=GetStopColorOffset(gradient,x,y);
3426  if (gradient->type == LinearGradient)
3427  {
3428  repeat=fmod(offset,length);
3429  if (repeat < 0.0)
3430  repeat=length-fmod(-repeat,length);
3431  else
3432  repeat=fmod(offset,length);
3433  antialias=(repeat < length) && ((repeat+1.0) > length) ?
3435  offset=repeat/length;
3436  }
3437  else
3438  {
3439  repeat=fmod(offset,(double) gradient->radius);
3440  if (repeat < 0.0)
3441  repeat=gradient->radius-fmod(-repeat,
3442  (double) gradient->radius);
3443  else
3444  repeat=fmod(offset,(double) gradient->radius);
3445  antialias=repeat+1.0 > gradient->radius ? MagickTrue :
3446  MagickFalse;
3447  offset=repeat/gradient->radius;
3448  }
3449  }
3450  for (i=0; i < (ssize_t) gradient->number_stops; i++)
3451  if (offset < gradient->stops[i].offset)
3452  break;
3453  if (i == 0)
3454  composite=gradient->stops[0].color;
3455  else
3456  if (i == (ssize_t) gradient->number_stops)
3457  composite=gradient->stops[gradient->number_stops-1].color;
3458  else
3459  {
3460  j=i;
3461  i--;
3462  alpha=(offset-gradient->stops[i].offset)/
3463  (gradient->stops[j].offset-gradient->stops[i].offset);
3464  if (antialias != MagickFalse)
3465  {
3466  if (gradient->type == LinearGradient)
3467  alpha=length-repeat;
3468  else
3469  alpha=gradient->radius-repeat;
3470  i=0;
3471  j=(ssize_t) gradient->number_stops-1L;
3472  }
3473  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
3474  &gradient->stops[j].color,alpha,&composite);
3475  }
3476  break;
3477  }
3478  }
3479  MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
3480  pixel.opacity,&pixel);
3481  SetPixelPacket(image,&pixel,q,indexes+x);
3482  q++;
3483  }
3484  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3485  status=MagickFalse;
3486  }
3487  image_view=DestroyCacheView(image_view);
3488  return(status);
3489 }
3490 
3491 /*
3492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3493 % %
3494 % %
3495 % %
3496 % D r a w P a t t e r n P a t h %
3497 % %
3498 % %
3499 % %
3500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3501 %
3502 % DrawPatternPath() draws a pattern.
3503 %
3504 % The format of the DrawPatternPath method is:
3505 %
3506 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
3507 % const char *name,Image **pattern)
3508 %
3509 % A description of each parameter follows:
3510 %
3511 % o image: the image.
3512 %
3513 % o draw_info: the draw info.
3514 %
3515 % o name: the pattern name.
3516 %
3517 % o image: the image.
3518 %
3519 */
3521  const DrawInfo *draw_info,const char *name,Image **pattern)
3522 {
3523  char
3524  property[MaxTextExtent];
3525 
3526  const char
3527  *geometry,
3528  *path,
3529  *type;
3530 
3531  DrawInfo
3532  *clone_info;
3533 
3534  ImageInfo
3535  *image_info;
3536 
3538  status;
3539 
3540  assert(image != (Image *) NULL);
3541  assert(image->signature == MagickSignature);
3542  if (image->debug != MagickFalse)
3543  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3544  assert(draw_info != (const DrawInfo *) NULL);
3545  assert(name != (const char *) NULL);
3546  (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
3547  path=GetImageArtifact(image,property);
3548  if (path == (const char *) NULL)
3549  return(MagickFalse);
3550  (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
3551  geometry=GetImageArtifact(image,property);
3552  if (geometry == (const char *) NULL)
3553  return(MagickFalse);
3554  if ((*pattern) != (Image *) NULL)
3555  *pattern=DestroyImage(*pattern);
3556  image_info=AcquireImageInfo();
3557  image_info->size=AcquireString(geometry);
3558  *pattern=AcquireImage(image_info);
3559  image_info=DestroyImageInfo(image_info);
3560  (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
3561  &image->exception);
3562  (void) SetImageBackgroundColor(*pattern);
3563  if (image->debug != MagickFalse)
3565  "begin pattern-path %s %s",name,geometry);
3566  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
3567  clone_info->fill_pattern=NewImageList();
3568  clone_info->stroke_pattern=NewImageList();
3569  (void) FormatLocaleString(property,MaxTextExtent,"%s-type",name);
3570  type=GetImageArtifact(image,property);
3571  if (type != (const char *) NULL)
3574  (void) CloneString(&clone_info->primitive,path);
3575  status=DrawImage(*pattern,clone_info);
3576  clone_info=DestroyDrawInfo(clone_info);
3577  if (image->debug != MagickFalse)
3578  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
3579  return(status);
3580 }
3581 
3582 /*
3583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3584 % %
3585 % %
3586 % %
3587 + D r a w P o l y g o n P r i m i t i v e %
3588 % %
3589 % %
3590 % %
3591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3592 %
3593 % DrawPolygonPrimitive() draws a polygon on the image.
3594 %
3595 % The format of the DrawPolygonPrimitive method is:
3596 %
3597 % MagickBooleanType DrawPolygonPrimitive(Image *image,
3598 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3599 %
3600 % A description of each parameter follows:
3601 %
3602 % o image: the image.
3603 %
3604 % o draw_info: the draw info.
3605 %
3606 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
3607 %
3608 */
3609 
3611 {
3612  register ssize_t
3613  i;
3614 
3615  assert(polygon_info != (PolygonInfo **) NULL);
3616  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
3617  if (polygon_info[i] != (PolygonInfo *) NULL)
3618  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
3619  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
3620  return(polygon_info);
3621 }
3622 
3624  const PrimitiveInfo *primitive_info)
3625 {
3626  PathInfo
3627  *restrict path_info;
3628 
3629  PolygonInfo
3630  **polygon_info;
3631 
3632  register ssize_t
3633  i;
3634 
3635  size_t
3636  number_threads;
3637 
3638  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
3639  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
3640  sizeof(*polygon_info));
3641  if (polygon_info == (PolygonInfo **) NULL)
3642  return((PolygonInfo **) NULL);
3643  (void) ResetMagickMemory(polygon_info,0,(size_t)
3644  GetMagickResourceLimit(ThreadResource)*sizeof(*polygon_info));
3645  path_info=ConvertPrimitiveToPath(draw_info,primitive_info);
3646  if (path_info == (PathInfo *) NULL)
3647  return(DestroyPolygonThreadSet(polygon_info));
3648  for (i=0; i < (ssize_t) number_threads; i++)
3649  {
3650  polygon_info[i]=ConvertPathToPolygon(draw_info,path_info);
3651  if (polygon_info[i] == (PolygonInfo *) NULL)
3652  return(DestroyPolygonThreadSet(polygon_info));
3653  }
3654  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
3655  return(polygon_info);
3656 }
3657 
3658 static double GetOpacityPixel(PolygonInfo *polygon_info,const double mid,
3659  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
3660  const ssize_t y,double *stroke_opacity)
3661 {
3662  double
3663  alpha,
3664  beta,
3665  distance,
3666  subpath_opacity;
3667 
3668  PointInfo
3669  delta;
3670 
3671  register EdgeInfo
3672  *p;
3673 
3674  register const PointInfo
3675  *q;
3676 
3677  register ssize_t
3678  i;
3679 
3680  ssize_t
3681  j,
3682  winding_number;
3683 
3684  /*
3685  Compute fill & stroke opacity for this (x,y) point.
3686  */
3687  *stroke_opacity=0.0;
3688  subpath_opacity=0.0;
3689  p=polygon_info->edges;
3690  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3691  {
3692  if ((double) y <= (p->bounds.y1-mid-0.5))
3693  break;
3694  if ((double) y > (p->bounds.y2+mid+0.5))
3695  {
3696  (void) DestroyEdge(polygon_info,(size_t) j);
3697  continue;
3698  }
3699  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
3700  ((double) x > (p->bounds.x2+mid+0.5)))
3701  continue;
3702  i=(ssize_t) MagickMax((double) p->highwater,1.0);
3703  for ( ; i < (ssize_t) p->number_points; i++)
3704  {
3705  if ((double) y <= (p->points[i-1].y-mid-0.5))
3706  break;
3707  if ((double) y > (p->points[i].y+mid+0.5))
3708  continue;
3709  if (p->scanline != (double) y)
3710  {
3711  p->scanline=(double) y;
3712  p->highwater=(size_t) i;
3713  }
3714  /*
3715  Compute distance between a point and an edge.
3716  */
3717  q=p->points+i-1;
3718  delta.x=(q+1)->x-q->x;
3719  delta.y=(q+1)->y-q->y;
3720  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
3721  if (beta < 0.0)
3722  {
3723  delta.x=(double) x-q->x;
3724  delta.y=(double) y-q->y;
3725  distance=delta.x*delta.x+delta.y*delta.y;
3726  }
3727  else
3728  {
3729  alpha=delta.x*delta.x+delta.y*delta.y;
3730  if (beta > alpha)
3731  {
3732  delta.x=(double) x-(q+1)->x;
3733  delta.y=(double) y-(q+1)->y;
3734  distance=delta.x*delta.x+delta.y*delta.y;
3735  }
3736  else
3737  {
3738  alpha=1.0/alpha;
3739  beta=delta.x*(y-q->y)-delta.y*(x-q->x);
3740  distance=alpha*beta*beta;
3741  }
3742  }
3743  /*
3744  Compute stroke & subpath opacity.
3745  */
3746  beta=0.0;
3747  if (p->ghostline == MagickFalse)
3748  {
3749  alpha=mid+0.5;
3750  if ((*stroke_opacity < 1.0) &&
3751  (distance <= ((alpha+0.25)*(alpha+0.25))))
3752  {
3753  alpha=mid-0.5;
3754  if (distance <= ((alpha+0.25)*(alpha+0.25)))
3755  *stroke_opacity=1.0;
3756  else
3757  {
3758  beta=1.0;
3759  if (distance != 1.0)
3760  beta=sqrt((double) distance);
3761  alpha=beta-mid-0.5;
3762  if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
3763  *stroke_opacity=(alpha-0.25)*(alpha-0.25);
3764  }
3765  }
3766  }
3767  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
3768  continue;
3769  if (distance <= 0.0)
3770  {
3771  subpath_opacity=1.0;
3772  continue;
3773  }
3774  if (distance > 1.0)
3775  continue;
3776  if (beta == 0.0)
3777  {
3778  beta=1.0;
3779  if (distance != 1.0)
3780  beta=sqrt(distance);
3781  }
3782  alpha=beta-1.0;
3783  if (subpath_opacity < (alpha*alpha))
3784  subpath_opacity=alpha*alpha;
3785  }
3786  }
3787  /*
3788  Compute fill opacity.
3789  */
3790  if (fill == MagickFalse)
3791  return(0.0);
3792  if (subpath_opacity >= 1.0)
3793  return(1.0);
3794  /*
3795  Determine winding number.
3796  */
3797  winding_number=0;
3798  p=polygon_info->edges;
3799  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
3800  {
3801  if ((double) y <= p->bounds.y1)
3802  break;
3803  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
3804  continue;
3805  if ((double) x > p->bounds.x2)
3806  {
3807  winding_number+=p->direction ? 1 : -1;
3808  continue;
3809  }
3810  i=(ssize_t) MagickMax((double) p->highwater,1.0);
3811  for ( ; i < (ssize_t) p->number_points; i++)
3812  if ((double) y <= p->points[i].y)
3813  break;
3814  q=p->points+i-1;
3815  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
3816  winding_number+=p->direction ? 1 : -1;
3817  }
3818  if (fill_rule != NonZeroRule)
3819  {
3820  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
3821  return(1.0);
3822  }
3823  else
3824  if (MagickAbsoluteValue(winding_number) != 0)
3825  return(1.0);
3826  return(subpath_opacity);
3827 }
3828 
3830  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
3831 {
3832  CacheView
3833  *image_view;
3834 
3835  double
3836  mid;
3837 
3839  *exception;
3840 
3842  fill,
3843  status;
3844 
3845  PolygonInfo
3846  **restrict polygon_info;
3847 
3848  register EdgeInfo
3849  *p;
3850 
3851  register ssize_t
3852  i;
3853 
3854  SegmentInfo
3855  bounds;
3856 
3857  ssize_t
3858  start_y,
3859  stop_y,
3860  y;
3861 
3862  /*
3863  Compute bounding box.
3864  */
3865  assert(image != (Image *) NULL);
3866  assert(image->signature == MagickSignature);
3867  if (image->debug != MagickFalse)
3868  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3869  assert(draw_info != (DrawInfo *) NULL);
3870  assert(draw_info->signature == MagickSignature);
3871  assert(primitive_info != (PrimitiveInfo *) NULL);
3872  if (primitive_info->coordinates == 0)
3873  return(MagickTrue);
3874  polygon_info=AcquirePolygonThreadSet(draw_info,primitive_info);
3875  if (polygon_info == (PolygonInfo **) NULL)
3876  return(MagickFalse);
3877 DisableMSCWarning(4127)
3878  if (0)
3879  DrawBoundingRectangles(image,draw_info,polygon_info[0]);
3881  if (image->debug != MagickFalse)
3882  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
3883  fill=(primitive_info->method == FillToBorderMethod) ||
3884  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
3885  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
3886  bounds=polygon_info[0]->edges[0].bounds;
3887  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
3888  {
3889  p=polygon_info[0]->edges+i;
3890  if (p->bounds.x1 < bounds.x1)
3891  bounds.x1=p->bounds.x1;
3892  if (p->bounds.y1 < bounds.y1)
3893  bounds.y1=p->bounds.y1;
3894  if (p->bounds.x2 > bounds.x2)
3895  bounds.x2=p->bounds.x2;
3896  if (p->bounds.y2 > bounds.y2)
3897  bounds.y2=p->bounds.y2;
3898  }
3899  bounds.x1-=(mid+1.0);
3900  bounds.x1=bounds.x1 < 0.0 ? 0.0 : (size_t) ceil(bounds.x1-0.5) >=
3901  image->columns ? (double) image->columns-1 : bounds.x1;
3902  bounds.y1-=(mid+1.0);
3903  bounds.y1=bounds.y1 < 0.0 ? 0.0 : (size_t) ceil(bounds.y1-0.5) >=
3904  image->rows ? (double) image->rows-1 : bounds.y1;
3905  bounds.x2+=(mid+1.0);
3906  bounds.x2=bounds.x2 < 0.0 ? 0.0 : (size_t) floor(bounds.x2+0.5) >=
3907  image->columns ? (double) image->columns-1 : bounds.x2;
3908  bounds.y2+=(mid+1.0);
3909  bounds.y2=bounds.y2 < 0.0 ? 0.0 : (size_t) floor(bounds.y2+0.5) >=
3910  image->rows ? (double) image->rows-1 : bounds.y2;
3911  status=MagickTrue;
3912  exception=(&image->exception);
3913  image_view=AcquireAuthenticCacheView(image,exception);
3914  if ((primitive_info->coordinates == 1) ||
3915  (polygon_info[0]->number_edges == 0))
3916  {
3917  /*
3918  Draw point.
3919  */
3920  start_y=(ssize_t) ceil(bounds.y1-0.5);
3921  stop_y=(ssize_t) floor(bounds.y2+0.5);
3922 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3923  #pragma omp parallel for schedule(static,4) shared(status) \
3924  magick_threads(image,image,1,1)
3925 #endif
3926  for (y=start_y; y <= stop_y; y++)
3927  {
3929  sync;
3930 
3931  register PixelPacket
3932  *restrict q;
3933 
3934  register ssize_t
3935  x;
3936 
3937  ssize_t
3938  start_x,
3939  stop_x;
3940 
3941  if (status == MagickFalse)
3942  continue;
3943  start_x=(ssize_t) ceil(bounds.x1-0.5);
3944  stop_x=(ssize_t) floor(bounds.x2+0.5);
3945  x=start_x;
3946  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
3947  exception);
3948  if (q == (PixelPacket *) NULL)
3949  {
3950  status=MagickFalse;
3951  continue;
3952  }
3953  for ( ; x <= stop_x; x++)
3954  {
3955  if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
3956  (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
3957  (void) GetFillColor(draw_info,x-start_x,y-start_y,q);
3958  q++;
3959  }
3960  sync=SyncCacheViewAuthenticPixels(image_view,exception);
3961  if (sync == MagickFalse)
3962  status=MagickFalse;
3963  }
3964  image_view=DestroyCacheView(image_view);
3965  polygon_info=DestroyPolygonThreadSet(polygon_info);
3966  if (image->debug != MagickFalse)
3968  " end draw-polygon");
3969  return(status);
3970  }
3971  /*
3972  Draw polygon or line.
3973  */
3974  if (image->matte == MagickFalse)
3976  start_y=(ssize_t) ceil(bounds.y1-0.5);
3977  stop_y=(ssize_t) floor(bounds.y2+0.5);
3978 #if defined(MAGICKCORE_OPENMP_SUPPORT)
3979  #pragma omp parallel for schedule(static,4) shared(status) \
3980  magick_threads(image,image,1,1)
3981 #endif
3982  for (y=start_y; y <= stop_y; y++)
3983  {
3984  const int
3985  id = GetOpenMPThreadId();
3986 
3987  double
3988  fill_opacity,
3989  stroke_opacity;
3990 
3991  PixelPacket
3992  fill_color,
3993  stroke_color;
3994 
3995  register PixelPacket
3996  *restrict q;
3997 
3998  register ssize_t
3999  x;
4000 
4001  ssize_t
4002  start_x,
4003  stop_x;
4004 
4005  if (status == MagickFalse)
4006  continue;
4007  start_x=(ssize_t) ceil(bounds.x1-0.5);
4008  stop_x=(ssize_t) floor(bounds.x2+0.5);
4009  q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+1),1,
4010  exception);
4011  if (q == (PixelPacket *) NULL)
4012  {
4013  status=MagickFalse;
4014  continue;
4015  }
4016  for (x=start_x; x <= stop_x; x++)
4017  {
4018  /*
4019  Fill and/or stroke.
4020  */
4021  fill_opacity=GetOpacityPixel(polygon_info[id],mid,fill,
4022  draw_info->fill_rule,x,y,&stroke_opacity);
4023  if (draw_info->stroke_antialias == MagickFalse)
4024  {
4025  fill_opacity=fill_opacity > 0.25 ? 1.0 : 0.0;
4026  stroke_opacity=stroke_opacity > 0.25 ? 1.0 : 0.0;
4027  }
4028  (void) GetFillColor(draw_info,x-start_x,y-start_y,&fill_color);
4029  fill_opacity=(double) (QuantumRange-fill_opacity*(QuantumRange-
4030  fill_color.opacity));
4031  MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
4032  (MagickRealType) q->opacity,q);
4033  (void) GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color);
4034  stroke_opacity=(double) (QuantumRange-stroke_opacity*(QuantumRange-
4035  stroke_color.opacity));
4036  MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
4037  (MagickRealType) q->opacity,q);
4038  q++;
4039  }
4040  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4041  status=MagickFalse;
4042  }
4043  image_view=DestroyCacheView(image_view);
4044  polygon_info=DestroyPolygonThreadSet(polygon_info);
4045  if (image->debug != MagickFalse)
4046  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4047  return(status);
4048 }
4049 
4050 /*
4051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4052 % %
4053 % %
4054 % %
4055 % D r a w P r i m i t i v e %
4056 % %
4057 % %
4058 % %
4059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4060 %
4061 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4062 %
4063 % The format of the DrawPrimitive method is:
4064 %
4065 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4066 % PrimitiveInfo *primitive_info)
4067 %
4068 % A description of each parameter follows:
4069 %
4070 % o image: the image.
4071 %
4072 % o draw_info: the draw info.
4073 %
4074 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4075 %
4076 */
4077 
4078 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4079 {
4080  const char
4081  *methods[] =
4082  {
4083  "point",
4084  "replace",
4085  "floodfill",
4086  "filltoborder",
4087  "reset",
4088  "?"
4089  };
4090 
4091  PointInfo
4092  p,
4093  q,
4094  point;
4095 
4096  register ssize_t
4097  i,
4098  x;
4099 
4100  ssize_t
4101  coordinates,
4102  y;
4103 
4104  x=(ssize_t) ceil(primitive_info->point.x-0.5);
4105  y=(ssize_t) ceil(primitive_info->point.y-0.5);
4106  switch (primitive_info->primitive)
4107  {
4108  case PointPrimitive:
4109  {
4111  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
4112  methods[primitive_info->method]);
4113  return;
4114  }
4115  case ColorPrimitive:
4116  {
4118  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
4119  methods[primitive_info->method]);
4120  return;
4121  }
4122  case MattePrimitive:
4123  {
4125  "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
4126  methods[primitive_info->method]);
4127  return;
4128  }
4129  case TextPrimitive:
4130  {
4132  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
4133  return;
4134  }
4135  case ImagePrimitive:
4136  {
4138  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
4139  return;
4140  }
4141  default:
4142  break;
4143  }
4144  coordinates=0;
4145  p=primitive_info[0].point;
4146  q.x=(-1.0);
4147  q.y=(-1.0);
4148  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4149  {
4150  point=primitive_info[i].point;
4151  if (coordinates <= 0)
4152  {
4153  coordinates=(ssize_t) primitive_info[i].coordinates;
4155  " begin open (%.20g)",(double) coordinates);
4156  p=point;
4157  }
4158  point=primitive_info[i].point;
4159  if ((fabs(q.x-point.x) >= MagickEpsilon) ||
4160  (fabs(q.y-point.y) >= MagickEpsilon))
4162  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
4163  else
4165  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
4166  q=point;
4167  coordinates--;
4168  if (coordinates > 0)
4169  continue;
4170  if ((fabs(p.x-point.x) >= MagickEpsilon) ||
4171  (fabs(p.y-point.y) >= MagickEpsilon))
4172  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
4173  (double) coordinates);
4174  else
4175  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
4176  (double) coordinates);
4177  }
4178 }
4179 
4181  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4182 {
4183  CacheView
4184  *image_view;
4185 
4187  *exception;
4188 
4190  status;
4191 
4192  register ssize_t
4193  i,
4194  x;
4195 
4196  ssize_t
4197  y;
4198 
4199  if (image->debug != MagickFalse)
4200  {
4202  " begin draw-primitive");
4204  " affine: %g %g %g %g %g %g",draw_info->affine.sx,
4205  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
4206  draw_info->affine.tx,draw_info->affine.ty);
4207  }
4208  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
4209  ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
4210  (IsPixelGray(&draw_info->stroke) == MagickFalse)))
4211  (void) SetImageColorspace(image,sRGBColorspace);
4212  status=MagickTrue;
4213  exception=(&image->exception);
4214  x=(ssize_t) ceil(primitive_info->point.x-0.5);
4215  y=(ssize_t) ceil(primitive_info->point.y-0.5);
4216  image_view=AcquireAuthenticCacheView(image,exception);
4217  switch (primitive_info->primitive)
4218  {
4219  case PointPrimitive:
4220  {
4221  PixelPacket
4222  fill_color;
4223 
4224  PixelPacket
4225  *q;
4226 
4227  if ((y < 0) || (y >= (ssize_t) image->rows))
4228  break;
4229  if ((x < 0) || (x >= (ssize_t) image->columns))
4230  break;
4231  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4232  if (q == (PixelPacket *) NULL)
4233  break;
4234  (void) GetFillColor(draw_info,x,y,&fill_color);
4235  MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
4236  (MagickRealType) q->opacity,q);
4237  status&=SyncCacheViewAuthenticPixels(image_view,exception);
4238  break;
4239  }
4240  case ColorPrimitive:
4241  {
4242  switch (primitive_info->method)
4243  {
4244  case PointMethod:
4245  default:
4246  {
4247  PixelPacket
4248  *q;
4249 
4250  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4251  if (q == (PixelPacket *) NULL)
4252  break;
4253  (void) GetFillColor(draw_info,x,y,q);
4254  status&=SyncCacheViewAuthenticPixels(image_view,exception);
4255  break;
4256  }
4257  case ReplaceMethod:
4258  {
4260  sync;
4261 
4262  PixelPacket
4263  target;
4264 
4265  status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4266  for (y=0; y < (ssize_t) image->rows; y++)
4267  {
4268  register PixelPacket
4269  *restrict q;
4270 
4271  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4272  exception);
4273  if (q == (PixelPacket *) NULL)
4274  break;
4275  for (x=0; x < (ssize_t) image->columns; x++)
4276  {
4277  if (IsColorSimilar(image,q,&target) == MagickFalse)
4278  {
4279  q++;
4280  continue;
4281  }
4282  (void) GetFillColor(draw_info,x,y,q);
4283  q++;
4284  }
4285  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4286  if (sync == MagickFalse)
4287  break;
4288  }
4289  break;
4290  }
4291  case FloodfillMethod:
4292  case FillToBorderMethod:
4293  {
4295  target;
4296 
4297  (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4298  if (primitive_info->method == FillToBorderMethod)
4299  {
4300  target.red=(MagickRealType) draw_info->border_color.red;
4301  target.green=(MagickRealType) draw_info->border_color.green;
4302  target.blue=(MagickRealType) draw_info->border_color.blue;
4303  }
4304  status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
4305  y,primitive_info->method == FloodfillMethod ? MagickFalse :
4306  MagickTrue);
4307  break;
4308  }
4309  case ResetMethod:
4310  {
4312  sync;
4313 
4314  for (y=0; y < (ssize_t) image->rows; y++)
4315  {
4316  register PixelPacket
4317  *restrict q;
4318 
4319  register ssize_t
4320  x;
4321 
4322  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4323  exception);
4324  if (q == (PixelPacket *) NULL)
4325  break;
4326  for (x=0; x < (ssize_t) image->columns; x++)
4327  {
4328  (void) GetFillColor(draw_info,x,y,q);
4329  q++;
4330  }
4331  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4332  if (sync == MagickFalse)
4333  break;
4334  }
4335  break;
4336  }
4337  }
4338  break;
4339  }
4340  case MattePrimitive:
4341  {
4342  if (image->matte == MagickFalse)
4344  switch (primitive_info->method)
4345  {
4346  case PointMethod:
4347  default:
4348  {
4349  PixelPacket
4350  pixel;
4351 
4352  PixelPacket
4353  *q;
4354 
4355  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
4356  if (q == (PixelPacket *) NULL)
4357  break;
4358  (void) GetFillColor(draw_info,x,y,&pixel);
4359  SetPixelOpacity(q,pixel.opacity);
4360  status&=SyncCacheViewAuthenticPixels(image_view,exception);
4361  break;
4362  }
4363  case ReplaceMethod:
4364  {
4366  sync;
4367 
4368  PixelPacket
4369  pixel,
4370  target;
4371 
4372  status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
4373  for (y=0; y < (ssize_t) image->rows; y++)
4374  {
4375  register PixelPacket
4376  *restrict q;
4377 
4378  register ssize_t
4379  x;
4380 
4381  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4382  exception);
4383  if (q == (PixelPacket *) NULL)
4384  break;
4385  for (x=0; x < (ssize_t) image->columns; x++)
4386  {
4387  if (IsColorSimilar(image,q,&target) == MagickFalse)
4388  {
4389  q++;
4390  continue;
4391  }
4392  (void) GetFillColor(draw_info,x,y,&pixel);
4393  SetPixelOpacity(q,pixel.opacity);
4394  q++;
4395  }
4396  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4397  if (sync == MagickFalse)
4398  break;
4399  }
4400  break;
4401  }
4402  case FloodfillMethod:
4403  case FillToBorderMethod:
4404  {
4406  target;
4407 
4408  (void) GetOneVirtualMagickPixel(image,x,y,&target,exception);
4409  if (primitive_info->method == FillToBorderMethod)
4410  {
4411  target.red=(MagickRealType) draw_info->border_color.red;
4412  target.green=(MagickRealType) draw_info->border_color.green;
4413  target.blue=(MagickRealType) draw_info->border_color.blue;
4414  }
4415  status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
4416  y,primitive_info->method == FloodfillMethod ? MagickFalse :
4417  MagickTrue);
4418  break;
4419  }
4420  case ResetMethod:
4421  {
4423  sync;
4424 
4425  PixelPacket
4426  pixel;
4427 
4428  for (y=0; y < (ssize_t) image->rows; y++)
4429  {
4430  register PixelPacket
4431  *restrict q;
4432 
4433  register ssize_t
4434  x;
4435 
4436  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
4437  exception);
4438  if (q == (PixelPacket *) NULL)
4439  break;
4440  for (x=0; x < (ssize_t) image->columns; x++)
4441  {
4442  (void) GetFillColor(draw_info,x,y,&pixel);
4443  SetPixelOpacity(q,pixel.opacity);
4444  q++;
4445  }
4446  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4447  if (sync == MagickFalse)
4448  break;
4449  }
4450  break;
4451  }
4452  }
4453  break;
4454  }
4455  case TextPrimitive:
4456  {
4457  char
4458  geometry[MaxTextExtent];
4459 
4460  DrawInfo
4461  *clone_info;
4462 
4463  if (primitive_info->text == (char *) NULL)
4464  break;
4465  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4466  (void) CloneString(&clone_info->text,primitive_info->text);
4467  (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
4468  primitive_info->point.x,primitive_info->point.y);
4469  (void) CloneString(&clone_info->geometry,geometry);
4470  status&=AnnotateImage(image,clone_info);
4471  clone_info=DestroyDrawInfo(clone_info);
4472  break;
4473  }
4474  case ImagePrimitive:
4475  {
4476  AffineMatrix
4477  affine;
4478 
4479  char
4480  composite_geometry[MaxTextExtent];
4481 
4482  Image
4483  *composite_image;
4484 
4485  ImageInfo
4486  *clone_info;
4487 
4489  geometry;
4490 
4491  ssize_t
4492  x1,
4493  y1;
4494 
4495  if (primitive_info->text == (char *) NULL)
4496  break;
4497  clone_info=AcquireImageInfo();
4498  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
4499  composite_image=ReadInlineImage(clone_info,primitive_info->text,
4500  &image->exception);
4501  else
4502  {
4503  (void) CopyMagickString(clone_info->filename,primitive_info->text,
4504  MaxTextExtent);
4505  composite_image=ReadImage(clone_info,&image->exception);
4506  }
4507  clone_info=DestroyImageInfo(clone_info);
4508  if (composite_image == (Image *) NULL)
4509  break;
4510  (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
4511  NULL,(void *) NULL);
4512  x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
4513  y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
4514  if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
4515  ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
4516  {
4517  char
4518  geometry[MaxTextExtent];
4519 
4520  /*
4521  Resize image.
4522  */
4523  (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
4524  primitive_info[1].point.x,primitive_info[1].point.y);
4525  composite_image->filter=image->filter;
4526  (void) TransformImage(&composite_image,(char *) NULL,geometry);
4527  }
4528  if (composite_image->matte == MagickFalse)
4529  (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
4530  if (draw_info->opacity != OpaqueOpacity)
4531  (void) SetImageOpacity(composite_image,draw_info->opacity);
4532  SetGeometry(image,&geometry);
4533  image->gravity=draw_info->gravity;
4534  geometry.x=x;
4535  geometry.y=y;
4536  (void) FormatLocaleString(composite_geometry,MaxTextExtent,
4537  "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
4538  composite_image->rows,(double) geometry.x,(double) geometry.y);
4539  (void) ParseGravityGeometry(image,composite_geometry,&geometry,
4540  &image->exception);
4541  affine=draw_info->affine;
4542  affine.tx=(double) geometry.x;
4543  affine.ty=(double) geometry.y;
4544  composite_image->interpolate=image->interpolate;
4545  if (draw_info->compose == OverCompositeOp)
4546  (void) DrawAffineImage(image,composite_image,&affine);
4547  else
4548  (void) CompositeImage(image,draw_info->compose,composite_image,
4549  geometry.x,geometry.y);
4550  composite_image=DestroyImage(composite_image);
4551  break;
4552  }
4553  default:
4554  {
4555  double
4556  mid,
4557  scale;
4558 
4559  DrawInfo
4560  *clone_info;
4561 
4562  if (IsEventLogging() != MagickFalse)
4563  LogPrimitiveInfo(primitive_info);
4564  scale=ExpandAffine(&draw_info->affine);
4565  if ((draw_info->dash_pattern != (double *) NULL) &&
4566  (draw_info->dash_pattern[0] != 0.0) &&
4567  ((scale*draw_info->stroke_width) >= MagickEpsilon) &&
4568  (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
4569  {
4570  /*
4571  Draw dash polygon.
4572  */
4573  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4574  clone_info->stroke_width=0.0;
4575  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4576  status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
4577  clone_info=DestroyDrawInfo(clone_info);
4578  (void) DrawDashPolygon(draw_info,primitive_info,image);
4579  break;
4580  }
4581  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4582  if ((mid > 1.0) &&
4583  (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
4584  {
4586  closed_path;
4587 
4588  /*
4589  Draw strokes while respecting line cap/join attributes.
4590  */
4591  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
4592  closed_path=
4593  (primitive_info[i-1].point.x == primitive_info[0].point.x) &&
4594  (primitive_info[i-1].point.y == primitive_info[0].point.y) ?
4596  i=(ssize_t) primitive_info[0].coordinates;
4597  if ((((draw_info->linecap == RoundCap) ||
4598  (closed_path != MagickFalse)) &&
4599  (draw_info->linejoin == RoundJoin)) ||
4600  (primitive_info[i].primitive != UndefinedPrimitive))
4601  {
4602  (void) DrawPolygonPrimitive(image,draw_info,primitive_info);
4603  break;
4604  }
4605  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4606  clone_info->stroke_width=0.0;
4607  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4608  status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
4609  clone_info=DestroyDrawInfo(clone_info);
4610  status&=DrawStrokePolygon(image,draw_info,primitive_info);
4611  break;
4612  }
4613  status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
4614  break;
4615  }
4616  }
4617  image_view=DestroyCacheView(image_view);
4618  if (image->debug != MagickFalse)
4619  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
4620  return(status != 0 ? MagickTrue : MagickFalse);
4621 }
4622 
4623 /*
4624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4625 % %
4626 % %
4627 % %
4628 + D r a w S t r o k e P o l y g o n %
4629 % %
4630 % %
4631 % %
4632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4633 %
4634 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
4635 % the image while respecting the line cap and join attributes.
4636 %
4637 % The format of the DrawStrokePolygon method is:
4638 %
4639 % MagickBooleanType DrawStrokePolygon(Image *image,
4640 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4641 %
4642 % A description of each parameter follows:
4643 %
4644 % o image: the image.
4645 %
4646 % o draw_info: the draw info.
4647 %
4648 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4649 %
4650 %
4651 */
4652 
4653 static void DrawRoundLinecap(Image *image,const DrawInfo *draw_info,
4654  const PrimitiveInfo *primitive_info)
4655 {
4657  linecap[5];
4658 
4659  register ssize_t
4660  i;
4661 
4662  for (i=0; i < 4; i++)
4663  linecap[i]=(*primitive_info);
4664  linecap[0].coordinates=4;
4665  linecap[1].point.x+=(double) (10.0*MagickEpsilon);
4666  linecap[2].point.x+=(double) (10.0*MagickEpsilon);
4667  linecap[2].point.y+=(double) (10.0*MagickEpsilon);
4668  linecap[3].point.y+=(double) (10.0*MagickEpsilon);
4669  linecap[4].primitive=UndefinedPrimitive;
4670  (void) DrawPolygonPrimitive(image,draw_info,linecap);
4671 }
4672 
4674  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4675 {
4676  DrawInfo
4677  *clone_info;
4678 
4680  closed_path;
4681 
4683  status;
4684 
4686  *stroke_polygon;
4687 
4688  register const PrimitiveInfo
4689  *p,
4690  *q;
4691 
4692  /*
4693  Draw stroked polygon.
4694  */
4695  if (image->debug != MagickFalse)
4697  " begin draw-stroke-polygon");
4698  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4699  clone_info->fill=draw_info->stroke;
4700  if (clone_info->fill_pattern != (Image *) NULL)
4701  clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4702  if (clone_info->stroke_pattern != (Image *) NULL)
4703  clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
4704  MagickTrue,&clone_info->stroke_pattern->exception);
4705  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
4706  clone_info->stroke_width=0.0;
4707  clone_info->fill_rule=NonZeroRule;
4708  status=MagickTrue;
4709  for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
4710  {
4711  stroke_polygon=TraceStrokePolygon(draw_info,p);
4712  status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
4713  if (status == 0)
4714  break;
4715  stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
4716  q=p+p->coordinates-1;
4717  closed_path=(q->point.x == p->point.x) && (q->point.y == p->point.y) ?
4719  if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
4720  {
4721  DrawRoundLinecap(image,draw_info,p);
4722  DrawRoundLinecap(image,draw_info,q);
4723  }
4724  }
4725  clone_info=DestroyDrawInfo(clone_info);
4726  if (image->debug != MagickFalse)
4728  " end draw-stroke-polygon");
4729  return(status != 0 ? MagickTrue : MagickFalse);
4730 }
4731 
4732 /*
4733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4734 % %
4735 % %
4736 % %
4737 % G e t A f f i n e M a t r i x %
4738 % %
4739 % %
4740 % %
4741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4742 %
4743 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
4744 % matrix.
4745 %
4746 % The format of the GetAffineMatrix method is:
4747 %
4748 % void GetAffineMatrix(AffineMatrix *affine_matrix)
4749 %
4750 % A description of each parameter follows:
4751 %
4752 % o affine_matrix: the affine matrix.
4753 %
4754 */
4756 {
4757  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4758  assert(affine_matrix != (AffineMatrix *) NULL);
4759  (void) ResetMagickMemory(affine_matrix,0,sizeof(*affine_matrix));
4760  affine_matrix->sx=1.0;
4761  affine_matrix->sy=1.0;
4762 }
4763 
4764 /*
4765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4766 % %
4767 % %
4768 % %
4769 + G e t D r a w I n f o %
4770 % %
4771 % %
4772 % %
4773 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4774 %
4775 % GetDrawInfo() initializes draw_info to default values from image_info.
4776 %
4777 % The format of the GetDrawInfo method is:
4778 %
4779 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4780 %
4781 % A description of each parameter follows:
4782 %
4783 % o image_info: the image info..
4784 %
4785 % o draw_info: the draw info.
4786 %
4787 */
4788 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
4789 {
4790  const char
4791  *option;
4792 
4794  *exception;
4795 
4796  ImageInfo
4797  *clone_info;
4798 
4799  /*
4800  Initialize draw attributes.
4801  */
4802  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
4803  assert(draw_info != (DrawInfo *) NULL);
4804  (void) ResetMagickMemory(draw_info,0,sizeof(*draw_info));
4805  clone_info=CloneImageInfo(image_info);
4806  GetAffineMatrix(&draw_info->affine);
4807  exception=AcquireExceptionInfo();
4808  (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
4809  (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
4810  draw_info->stroke_antialias=clone_info->antialias;
4811  draw_info->stroke_width=1.0;
4812  draw_info->opacity=OpaqueOpacity;
4813  draw_info->fill_rule=EvenOddRule;
4814  draw_info->linecap=ButtCap;
4815  draw_info->linejoin=MiterJoin;
4816  draw_info->miterlimit=10;
4817  draw_info->decorate=NoDecoration;
4818  if (clone_info->font != (char *) NULL)
4819  draw_info->font=AcquireString(clone_info->font);
4820  if (clone_info->density != (char *) NULL)
4821  draw_info->density=AcquireString(clone_info->density);
4822  draw_info->text_antialias=clone_info->antialias;
4823  draw_info->pointsize=12.0;
4824  if (clone_info->pointsize != 0.0)
4825  draw_info->pointsize=clone_info->pointsize;
4827  draw_info->border_color=clone_info->border_color;
4828  draw_info->compose=OverCompositeOp;
4829  if (clone_info->server_name != (char *) NULL)
4830  draw_info->server_name=AcquireString(clone_info->server_name);
4831  draw_info->render=MagickTrue;
4832  draw_info->debug=IsEventLogging();
4833  option=GetImageOption(clone_info,"direction");
4834  if (option != (const char *) NULL)
4837  else
4838  draw_info->direction=UndefinedDirection;
4839  option=GetImageOption(clone_info,"encoding");
4840  if (option != (const char *) NULL)
4841  (void) CloneString(&draw_info->encoding,option);
4842  option=GetImageOption(clone_info,"family");
4843  if (option != (const char *) NULL)
4844  (void) CloneString(&draw_info->family,option);
4845  option=GetImageOption(clone_info,"fill");
4846  if (option != (const char *) NULL)
4847  (void) QueryColorDatabase(option,&draw_info->fill,exception);
4848  option=GetImageOption(clone_info,"gravity");
4849  if (option != (const char *) NULL)
4851  MagickFalse,option);
4852  option=GetImageOption(clone_info,"interline-spacing");
4853  if (option != (const char *) NULL)
4854  draw_info->interline_spacing=StringToDouble(option,(char **) NULL);
4855  option=GetImageOption(clone_info,"interword-spacing");
4856  if (option != (const char *) NULL)
4857  draw_info->interword_spacing=StringToDouble(option,(char **) NULL);
4858  option=GetImageOption(clone_info,"kerning");
4859  if (option != (const char *) NULL)
4860  draw_info->kerning=StringToDouble(option,(char **) NULL);
4861  option=GetImageOption(clone_info,"stroke");
4862  if (option != (const char *) NULL)
4863  (void) QueryColorDatabase(option,&draw_info->stroke,exception);
4864  option=GetImageOption(clone_info,"strokewidth");
4865  if (option != (const char *) NULL)
4866  draw_info->stroke_width=StringToDouble(option,(char **) NULL);
4867  option=GetImageOption(clone_info,"style");
4868  if (option != (const char *) NULL)
4870  MagickFalse,option);
4871  option=GetImageOption(clone_info,"undercolor");
4872  if (option != (const char *) NULL)
4873  (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
4874  option=GetImageOption(clone_info,"weight");
4875  if (option != (const char *) NULL)
4876  {
4877  ssize_t
4878  weight;
4879 
4881  if (weight == -1)
4882  weight=StringToUnsignedLong(option);
4883  draw_info->weight=(size_t) weight;
4884  }
4885  exception=DestroyExceptionInfo(exception);
4886  draw_info->signature=MagickSignature;
4887  clone_info=DestroyImageInfo(clone_info);
4888 }
4889 
4890 /*
4891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4892 % %
4893 % %
4894 % %
4895 + P e r m u t a t e %
4896 % %
4897 % %
4898 % %
4899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4900 %
4901 % Permutate() returns the permuation of the (n,k).
4902 %
4903 % The format of the Permutate method is:
4904 %
4905 % void Permutate(ssize_t n,ssize_t k)
4906 %
4907 % A description of each parameter follows:
4908 %
4909 % o n:
4910 %
4911 % o k:
4912 %
4913 %
4914 */
4915 static inline double Permutate(const ssize_t n,const ssize_t k)
4916 {
4917  double
4918  r;
4919 
4920  register ssize_t
4921  i;
4922 
4923  r=1.0;
4924  for (i=k+1; i <= n; i++)
4925  r*=i;
4926  for (i=1; i <= (n-k); i++)
4927  r/=i;
4928  return(r);
4929 }
4930 
4931 /*
4932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4933 % %
4934 % %
4935 % %
4936 + T r a c e P r i m i t i v e %
4937 % %
4938 % %
4939 % %
4940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4941 %
4942 % TracePrimitive is a collection of methods for generating graphic
4943 % primitives such as arcs, ellipses, paths, etc.
4944 %
4945 */
4946 
4947 static void TraceArc(PrimitiveInfo *primitive_info,const PointInfo start,
4948  const PointInfo end,const PointInfo degrees)
4949 {
4950  PointInfo
4951  center,
4952  radii;
4953 
4954  center.x=0.5*(end.x+start.x);
4955  center.y=0.5*(end.y+start.y);
4956  radii.x=fabs(center.x-start.x);
4957  radii.y=fabs(center.y-start.y);
4958  TraceEllipse(primitive_info,center,radii,degrees);
4959 }
4960 
4961 static void TraceArcPath(PrimitiveInfo *primitive_info,const PointInfo start,
4962  const PointInfo end,const PointInfo arc,const double angle,
4963  const MagickBooleanType large_arc,const MagickBooleanType sweep)
4964 {
4965  double
4966  alpha,
4967  beta,
4968  delta,
4969  factor,
4970  gamma,
4971  theta;
4972 
4973  PointInfo
4974  center,
4975  points[3],
4976  radii;
4977 
4978  register double
4979  cosine,
4980  sine;
4981 
4982  register PrimitiveInfo
4983  *p;
4984 
4985  register ssize_t
4986  i;
4987 
4988  size_t
4989  arc_segments;
4990 
4991  if ((start.x == end.x) && (start.y == end.y))
4992  {
4993  TracePoint(primitive_info,end);
4994  return;
4995  }
4996  radii.x=fabs(arc.x);
4997  radii.y=fabs(arc.y);
4998  if ((radii.x == 0.0) || (radii.y == 0.0))
4999  {
5000  TraceLine(primitive_info,start,end);
5001  return;
5002  }
5003  cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5004  sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5005  center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5006  center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5007  delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5008  (radii.y*radii.y);
5009  if (delta < MagickEpsilon)
5010  {
5011  TraceLine(primitive_info,start,end);
5012  return;
5013  }
5014  if (delta > 1.0)
5015  {
5016  radii.x*=sqrt((double) delta);
5017  radii.y*=sqrt((double) delta);
5018  }
5019  points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5020  points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5021  points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5022  points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5023  alpha=points[1].x-points[0].x;
5024  beta=points[1].y-points[0].y;
5025  factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5026  if (factor <= 0.0)
5027  factor=0.0;
5028  else
5029  {
5030  factor=sqrt((double) factor);
5031  if (sweep == large_arc)
5032  factor=(-factor);
5033  }
5034  center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5035  center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5036  alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5037  theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5038  if ((theta < 0.0) && (sweep != MagickFalse))
5039  theta+=2.0*MagickPI;
5040  else
5041  if ((theta > 0.0) && (sweep == MagickFalse))
5042  theta-=2.0*MagickPI;
5043  arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5044  MagickEpsilon))));
5045  p=primitive_info;
5046  for (i=0; i < (ssize_t) arc_segments; i++)
5047  {
5048  beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5049  gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5050  sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5051  sin(fmod((double) beta,DegreesToRadians(360.0)));
5052  points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
5053  arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
5054  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5055  points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
5056  arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
5057  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
5058  points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
5059  theta/arc_segments),DegreesToRadians(360.0))));
5060  points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
5061  theta/arc_segments),DegreesToRadians(360.0))));
5062  points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
5063  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5064  points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
5065  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
5066  p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
5067  p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
5068  (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
5069  points[0].y);
5070  (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
5071  points[0].y);
5072  (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
5073  points[1].y);
5074  (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
5075  points[1].y);
5076  (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
5077  points[2].y);
5078  (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
5079  points[2].y);
5080  if (i == (ssize_t) (arc_segments-1))
5081  (p+3)->point=end;
5082  TraceBezier(p,4);
5083  p+=p->coordinates;
5084  }
5085  primitive_info->coordinates=(size_t) (p-primitive_info);
5086  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5087  {
5088  p->primitive=primitive_info->primitive;
5089  p--;
5090  }
5091 }
5092 
5093 static void TraceBezier(PrimitiveInfo *primitive_info,
5094  const size_t number_coordinates)
5095 {
5096  double
5097  alpha,
5098  *coefficients,
5099  weight;
5100 
5101  PointInfo
5102  end,
5103  point,
5104  *points;
5105 
5106  register PrimitiveInfo
5107  *p;
5108 
5109  register ssize_t
5110  i,
5111  j;
5112 
5113  size_t
5114  control_points,
5115  quantum;
5116 
5117  /*
5118  Allocate coeficients.
5119  */
5120  quantum=number_coordinates;
5121  for (i=0; i < (ssize_t) number_coordinates; i++)
5122  {
5123  for (j=i+1; j < (ssize_t) number_coordinates; j++)
5124  {
5125  alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
5126  if (alpha > (double) quantum)
5127  quantum=(size_t) alpha;
5128  alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
5129  if (alpha > (double) quantum)
5130  quantum=(size_t) alpha;
5131  }
5132  }
5133  quantum=(size_t) MagickMin((double) quantum/number_coordinates,
5134  (double) BezierQuantum);
5135  control_points=quantum*number_coordinates;
5136  coefficients=(double *) AcquireQuantumMemory((size_t)
5137  number_coordinates,sizeof(*coefficients));
5138  points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
5139  sizeof(*points));
5140  if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
5141  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
5142  /*
5143  Compute bezier points.
5144  */
5145  end=primitive_info[number_coordinates-1].point;
5146  for (i=0; i < (ssize_t) number_coordinates; i++)
5147  coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
5148  weight=0.0;
5149  for (i=0; i < (ssize_t) control_points; i++)
5150  {
5151  p=primitive_info;
5152  point.x=0.0;
5153  point.y=0.0;
5154  alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
5155  for (j=0; j < (ssize_t) number_coordinates; j++)
5156  {
5157  point.x+=alpha*coefficients[j]*p->point.x;
5158  point.y+=alpha*coefficients[j]*p->point.y;
5159  alpha*=weight/(1.0-weight);
5160  p++;
5161  }
5162  points[i]=point;
5163  weight+=1.0/control_points;
5164  }
5165  /*
5166  Bezier curves are just short segmented polys.
5167  */
5168  p=primitive_info;
5169  for (i=0; i < (ssize_t) control_points; i++)
5170  {
5171  TracePoint(p,points[i]);
5172  p+=p->coordinates;
5173  }
5174  TracePoint(p,end);
5175  p+=p->coordinates;
5176  primitive_info->coordinates=(size_t) (p-primitive_info);
5177  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5178  {
5179  p->primitive=primitive_info->primitive;
5180  p--;
5181  }
5182  points=(PointInfo *) RelinquishMagickMemory(points);
5183  coefficients=(double *) RelinquishMagickMemory(coefficients);
5184 }
5185 
5186 static void TraceCircle(PrimitiveInfo *primitive_info,const PointInfo start,
5187  const PointInfo end)
5188 {
5189  double
5190  alpha,
5191  beta,
5192  radius;
5193 
5194  PointInfo
5195  offset,
5196  degrees;
5197 
5198  alpha=end.x-start.x;
5199  beta=end.y-start.y;
5200  radius=hypot((double) alpha,(double) beta);
5201  offset.x=(double) radius;
5202  offset.y=(double) radius;
5203  degrees.x=0.0;
5204  degrees.y=360.0;
5205  TraceEllipse(primitive_info,start,offset,degrees);
5206 }
5207 
5208 static void TraceEllipse(PrimitiveInfo *primitive_info,const PointInfo start,
5209  const PointInfo stop,const PointInfo degrees)
5210 {
5211  double
5212  delta,
5213  step,
5214  y;
5215 
5216  PointInfo
5217  angle,
5218  point;
5219 
5220  register PrimitiveInfo
5221  *p;
5222 
5223  register ssize_t
5224  i;
5225 
5226  /*
5227  Ellipses are just short segmented polys.
5228  */
5229  if ((stop.x == 0.0) && (stop.y == 0.0))
5230  {
5231  TracePoint(primitive_info,start);
5232  return;
5233  }
5234  delta=2.0/MagickMax(stop.x,stop.y);
5235  step=MagickPI/8.0;
5236  if ((delta >= 0.0) && (delta < (MagickPI/8.0)))
5237  step=MagickPI/(4*(MagickPI/delta/2+0.5));
5238  angle.x=DegreesToRadians(degrees.x);
5239  y=degrees.y;
5240  while (y < degrees.x)
5241  y+=360.0;
5242  angle.y=(double) (DegreesToRadians(y)-MagickEpsilon);
5243  for (p=primitive_info; angle.x < angle.y; angle.x+=step)
5244  {
5245  point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*stop.x+start.x;
5246  point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*stop.y+start.y;
5247  TracePoint(p,point);
5248  p+=p->coordinates;
5249  }
5250  point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*stop.x+start.x;
5251  point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*stop.y+start.y;
5252  TracePoint(p,point);
5253  p+=p->coordinates;
5254  primitive_info->coordinates=(size_t) (p-primitive_info);
5255  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
5256  {
5257  p->primitive=primitive_info->primitive;
5258  p--;
5259  }
5260 }
5261 
5262 static void TraceLine(PrimitiveInfo *primitive_info,const PointInfo start,
5263  const PointInfo end)
5264 {
5265  TracePoint(primitive_info,start);
5266  if ((fabs(start.x-end.x) < MagickEpsilon) &&
5267  (fabs(start.y-end.y) < MagickEpsilon))
5268  {
5269  primitive_info->primitive=PointPrimitive;
5270  primitive_info->coordinates=1;
5271  return;
5272  }
5273  TracePoint(primitive_info+1,end);
5274  (primitive_info+1)->primitive=primitive_info->primitive;
5275  primitive_info->coordinates=2;
5276 }
5277 
5278 static size_t TracePath(PrimitiveInfo *primitive_info,const char *path)
5279 {
5280  char
5281  token[MaxTextExtent];
5282 
5283  const char
5284  *p;
5285 
5286  double
5287  x,
5288  y;
5289 
5290  int
5291  attribute,
5292  last_attribute;
5293 
5294  PointInfo
5295  end = {0.0, 0.0},
5296  points[4] = { {0.0,0.0}, {0.0,0.0}, {0.0,0.0}, {0.0,0.0} },
5297  point = {0.0, 0.0},
5298  start = {0.0, 0.0};
5299 
5301  primitive_type;
5302 
5303  register PrimitiveInfo
5304  *q;
5305 
5306  register ssize_t
5307  i;
5308 
5309  size_t
5310  number_coordinates,
5311  z_count;
5312 
5313  attribute=0;
5314  number_coordinates=0;
5315  z_count=0;
5316  primitive_type=primitive_info->primitive;
5317  q=primitive_info;
5318  for (p=path; *p != '\0'; )
5319  {
5320  while (isspace((int) ((unsigned char) *p)) != 0)
5321  p++;
5322  if (*p == '\0')
5323  break;
5324  last_attribute=attribute;
5325  attribute=(int) (*p++);
5326  switch (attribute)
5327  {
5328  case 'a':
5329  case 'A':
5330  {
5331  double
5332  angle;
5333 
5335  large_arc,
5336  sweep;
5337 
5338  PointInfo
5339  arc;
5340 
5341  /*
5342  Compute arc points.
5343  */
5344  do
5345  {
5346  GetMagickToken(p,&p,token);
5347  if (*token == ',')
5348  GetMagickToken(p,&p,token);
5349  arc.x=StringToDouble(token,(char **) NULL);
5350  GetMagickToken(p,&p,token);
5351  if (*token == ',')
5352  GetMagickToken(p,&p,token);
5353  arc.y=StringToDouble(token,(char **) NULL);
5354  GetMagickToken(p,&p,token);
5355  if (*token == ',')
5356  GetMagickToken(p,&p,token);
5357  angle=StringToDouble(token,(char **) NULL);
5358  GetMagickToken(p,&p,token);
5359  if (*token == ',')
5360  GetMagickToken(p,&p,token);
5361  large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5362  GetMagickToken(p,&p,token);
5363  if (*token == ',')
5364  GetMagickToken(p,&p,token);
5365  sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
5366  GetMagickToken(p,&p,token);
5367  if (*token == ',')
5368  GetMagickToken(p,&p,token);
5369  x=StringToDouble(token,(char **) NULL);
5370  GetMagickToken(p,&p,token);
5371  if (*token == ',')
5372  GetMagickToken(p,&p,token);
5373  y=StringToDouble(token,(char **) NULL);
5374  end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
5375  end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
5376  TraceArcPath(q,point,end,arc,angle,large_arc,sweep);
5377  q+=q->coordinates;
5378  point=end;
5379  while (isspace((int) ((unsigned char) *p)) != 0)
5380  p++;
5381  if (*p == ',')
5382  p++;
5383  } while (IsPoint(p) != MagickFalse);
5384  break;
5385  }
5386  case 'c':
5387  case 'C':
5388  {
5389  /*
5390  Compute bezier points.
5391  */
5392  do
5393  {
5394  points[0]=point;
5395  for (i=1; i < 4; i++)
5396  {
5397  GetMagickToken(p,&p,token);
5398  if (*token == ',')
5399  GetMagickToken(p,&p,token);