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