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