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