2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % GGGG EEEEE OOO M M EEEEE TTTTT RRRR Y Y %
7 % G E O O MM MM E T R R Y Y %
8 % G GG EEE O O M M M EEE T RRRR Y %
9 % G G E O O M M E T R R Y %
10 % GGGG EEEEE OOO M M EEEEE T R R Y %
13 % MagickCore Geometry Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/constitute.h"
44 #include "MagickCore/draw.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/geometry.h"
48 #include "MagickCore/memory_.h"
49 #include "MagickCore/string_.h"
50 #include "MagickCore/string-private.h"
51 #include "MagickCore/token.h"
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 % G e t G e o m e t r y %
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 % GetGeometry() parses a geometry specification and returns the width,
65 % height, x, and y values. It also returns flags that indicates which
66 % of the four values (width, height, x, y) were located in the string, and
67 % whether the x or y values are negative. In addition, there are flags to
68 % report any meta characters (%, !, <, or >).
70 % The value must form a proper geometry style specification of WxH+X+Y
71 % of integers only, and values can not be separated by comma, colon, or
72 % slash charcaters. See ParseGeometry() below.
74 % Offsets may be prefixed by multiple signs to make offset string
75 % substitutions easier to handle from shell scripts.
76 % For example: "-10-10", "-+10-+10", or "+-10+-10" will generate negtive
77 % offsets, while "+10+10", "++10++10", or "--10--10" will generate positive
80 % The format of the GetGeometry method is:
82 % MagickStatusType GetGeometry(const char *geometry,ssize_t *x,ssize_t *y,
83 % size_t *width,size_t *height)
85 % A description of each parameter follows:
87 % o geometry: The geometry.
89 % o x,y: The x and y offset as determined by the geometry specification.
91 % o width,height: The width and height as determined by the geometry
95 MagickExport MagickStatusType GetGeometry(const char *geometry,ssize_t *x,
96 ssize_t *y,size_t *width,size_t *height)
100 pedantic_geometry[MaxTextExtent],
113 Remove whitespace and meta characters from geometry specification.
116 if ((geometry == (char *) NULL) || (*geometry == '\0'))
118 if (strlen(geometry) >= (MaxTextExtent-1))
120 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
121 for (p=pedantic_geometry; *p != '\0'; )
123 if (isspace((int) ((unsigned char) *p)) != 0)
125 (void) CopyMagickString(p,p+1,MaxTextExtent);
134 (void) CopyMagickString(p,p+1,MaxTextExtent);
140 (void) CopyMagickString(p,p+1,MaxTextExtent);
146 (void) CopyMagickString(p,p+1,MaxTextExtent);
152 (void) CopyMagickString(p,p+1,MaxTextExtent);
158 (void) CopyMagickString(p,p+1,MaxTextExtent);
164 (void) CopyMagickString(p,p+1,MaxTextExtent);
170 (void) CopyMagickString(p,p+1,MaxTextExtent);
199 Parse width, height, x, and y.
205 value=StringToDouble(p,&q);
207 if (LocaleNCompare(p,"0x",2) == 0)
208 value=(double) strtol(p,&q,10);
209 if ((*p != '+') && (*p != '-'))
211 c=(int) ((unsigned char) *q);
212 if ((c == 215) || (*q == 'x') || (*q == 'X') || (*q == '\0'))
218 if (LocaleNCompare(p,"0x",2) == 0)
219 *width=(size_t) strtol(p,&p,10);
221 *width=(size_t) floor(StringToDouble(p,&p)+0.5);
226 if ((*p != '+') && (*p != '-'))
228 c=(int) ((unsigned char) *p);
229 if ((c == 215) || (*p == 'x') || (*p == 'X'))
232 if ((*p != '+') && (*p != '-'))
238 *height=(size_t) floor(StringToDouble(p,&p)+0.5);
244 if ((*p == '+') || (*p == '-'))
249 while ((*p == '+') || (*p == '-'))
252 flags^=XNegative; /* negate sign */
256 *x=(ssize_t) ceil(StringToDouble(p,&p)-0.5);
260 if ((flags & XNegative) != 0)
264 if ((*p == '+') || (*p == '-'))
269 while ((*p == '+') || (*p == '-'))
272 flags^=YNegative; /* negate sign */
276 *y=(ssize_t) ceil(StringToDouble(p,&p)-0.5);
280 if ((flags & YNegative) != 0)
285 /* Debugging Geometry */
286 (void) fprintf(stderr,"GetGeometry...\n");
287 (void) fprintf(stderr,"Input: %s\n",geometry);
288 (void) fprintf(stderr,"Flags: %c %c %s %s\n",
289 (flags & WidthValue) ? 'W' : ' ',(flags & HeightValue) ? 'H' : ' ',
290 (flags & XValue) ? ((flags & XNegative) ? "-X" : "+X") : " ",
291 (flags & YValue) ? ((flags & YNegative) ? "-Y" : "+Y") : " ");
292 (void) fprintf(stderr,"Geometry: %ldx%ld%+ld%+ld\n",(long) *width,(long)
293 *height,(long) *x,(long) *y);
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % G e t P a g e G e o m e t r y %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % GetPageGeometry() replaces any page mneumonic with the equivalent size in
312 % The format of the GetPageGeometry method is:
314 % char *GetPageGeometry(const char *page_geometry)
316 % A description of each parameter follows.
318 % o page_geometry: Specifies a pointer to an array of characters. The
319 % string is either a Postscript page name (e.g. A4) or a postscript page
320 % geometry (e.g. 612x792+36+36).
323 MagickExport char *GetPageGeometry(const char *page_geometry)
328 { "4x6", "288x432" },
329 { "5x7", "360x504" },
330 { "7x9", "504x648" },
331 { "8x10", "576x720" },
332 { "9x11", "648x792" },
333 { "9x12", "648x864" },
334 { "10x13", "720x936" },
335 { "10x14", "720x1008" },
336 { "11x17", "792x1224" },
337 { "a0", "2384x3370" },
338 { "a1", "1684x2384" },
340 { "a2", "1191x1684" },
341 { "a3", "842x1191" },
343 { "a4smaLL", "595x842" },
349 { "archa", "648x864" },
350 { "archb", "864x1296" },
351 { "archC", "1296x1728" },
352 { "archd", "1728x2592" },
353 { "arche", "2592x3456" },
354 { "b0", "2920x4127" },
355 { "b1", "2064x2920" },
357 { "b2", "1460x2064" },
358 { "b3", "1032x1460" },
359 { "b4", "729x1032" },
365 { "c0", "2599x3676" },
366 { "c1", "1837x2599" },
367 { "c2", "1298x1837" },
368 { "c3", "918x1296" },
373 { "executive", "540x720" },
374 { "flsa", "612x936" },
375 { "flse", "612x936" },
376 { "folio", "612x936" },
377 { "halfletter", "396x612" },
378 { "isob0", "2835x4008" },
379 { "isob1", "2004x2835" },
380 { "isob10", "88x125" },
381 { "isob2", "1417x2004" },
382 { "isob3", "1001x1417" },
383 { "isob4", "709x1001" },
384 { "isob5", "499x709" },
385 { "isob6", "354x499" },
386 { "isob7", "249x354" },
387 { "isob8", "176x249" },
388 { "isob9", "125x176" },
389 { "jisb0", "1030x1456" },
390 { "jisb1", "728x1030" },
391 { "jisb2", "515x728" },
392 { "jisb3", "364x515" },
393 { "jisb4", "257x364" },
394 { "jisb5", "182x257" },
395 { "jisb6", "128x182" },
396 { "ledger", "1224x792" },
397 { "legal", "612x1008" },
398 { "letter", "612x792" },
399 { "lettersmaLL", "612x792" },
400 { "quarto", "610x780" },
401 { "statement", "396x612" },
402 { "tabloid", "792x1224" },
403 { (char *) NULL, (char *) NULL }
412 assert(page_geometry != (char *) NULL);
413 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",page_geometry);
414 page=AcquireString(page_geometry);
415 for (i=0; *PageSizes[i] != (char *) NULL; i++)
416 if (LocaleNCompare(PageSizes[i][0],page,strlen(PageSizes[i][0])) == 0)
425 Replace mneumonic with the equivalent size in dots-per-inch.
427 (void) CopyMagickString(page,PageSizes[i][1],MaxTextExtent);
428 (void) ConcatenateMagickString(page,page_geometry+
429 strlen(PageSizes[i][0]),MaxTextExtent);
430 flags=GetGeometry(page,&geometry.x,&geometry.y,&geometry.width,
432 if ((flags & GreaterValue) == 0)
433 (void) ConcatenateMagickString(page,">",MaxTextExtent);
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % G r a v i t y A d j u s t G e o m e t r y %
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 % GravityAdjustGeometry() adjusts the offset of a region with regard to the
450 % given: width, height and gravity; against which it is positioned.
452 % The region should also have an appropriate width and height to correctly
453 % set the right offset of the top left corner of the region.
455 % The format of the GravityAdjustGeometry method is:
457 % void GravityAdjustGeometry(const size_t width, const size_t height,
458 % const GravityType gravity,RectangleInfo *region);
460 % A description of each parameter follows:
462 % o width, height: the larger area the region is relative to
464 % o gravity: the edge/corner the current offset is relative to
466 % o region: The region requiring a offset adjustment relative to gravity
469 MagickExport void GravityAdjustGeometry(const size_t width,
470 const size_t height,const GravityType gravity,RectangleInfo *region)
472 if (region->height == 0)
473 region->height=height;
474 if (region->width == 0)
478 case NorthEastGravity:
480 case SouthEastGravity:
482 region->x=(ssize_t) (width-region->width-region->x);
489 region->x+=(ssize_t) (width/2-region->width/2);
493 case NorthWestGravity:
495 case SouthWestGravity:
501 case SouthWestGravity:
503 case SouthEastGravity:
505 region->y=(ssize_t) (height-region->height-region->y);
512 region->y+=(ssize_t) (height/2-region->height/2);
516 case NorthWestGravity:
518 case NorthEastGravity:
526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530 + I s G e o m e t r y %
534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 % IsGeometry() returns MagickTrue if the geometry specification is valid.
537 % Examples are 100, 100x200, x200, 100x200+10+20, +10+20, 200%, 200x200!, etc.
539 % The format of the IsGeometry method is:
541 % MagickBooleanType IsGeometry(const char *geometry)
543 % A description of each parameter follows:
545 % o geometry: This string is the geometry specification.
548 MagickExport MagickBooleanType IsGeometry(const char *geometry)
556 if (geometry == (const char *) NULL)
558 flags=ParseGeometry(geometry,&geometry_info);
559 return(flags != NoValue ? MagickTrue : MagickFalse);
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 + I s S c e n e G e o m e t r y %
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 % IsSceneGeometry() returns MagickTrue if the geometry is a valid scene
574 % specification (e.g. [1], [1-9], [1,7,4]).
576 % The format of the IsSceneGeometry method is:
578 % MagickBooleanType IsSceneGeometry(const char *geometry,
579 % const MagickBooleanType pedantic)
581 % A description of each parameter follows:
583 % o geometry: This string is the geometry specification.
585 % o pedantic: A value other than 0 invokes a more restrictive set of
586 % conditions for a valid specification (e.g. [1], [1-4], [4-1]).
589 MagickExport MagickBooleanType IsSceneGeometry(const char *geometry,
590 const MagickBooleanType pedantic)
598 if (geometry == (const char *) NULL)
601 value=StringToDouble(geometry,&p);
605 if (strspn(geometry,"0123456789-, ") != strlen(geometry))
607 if ((pedantic != MagickFalse) && (strchr(geometry,',') != (char *) NULL))
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 % P a r s e A b s o l u t e G e o m e t r y %
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % ParseAbsoluteGeometry() returns a region as defined by the geometry string,
624 % without any modification by percentages or gravity.
626 % It currently just a wrapper around GetGeometry(), but may be expanded in
627 % the future to handle other positioning information.
629 % The format of the ParseAbsoluteGeometry method is:
631 % MagickStatusType ParseAbsoluteGeometry(const char *geometry,
632 % RectangleInfo *region_info)
634 % A description of each parameter follows:
636 % o geometry: The geometry string (e.g. "100x100+10+10").
638 % o region_info: the region as defined by the geometry string.
641 MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry,
642 RectangleInfo *region_info)
647 flags=GetGeometry(geometry,®ion_info->x,®ion_info->y,
648 ®ion_info->width,®ion_info->height);
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % P a r s e A f f i n e G e o m e t r y %
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 % ParseAffineGeometry() returns an affine matrix as defined by a string of 4
664 % to 6 comma/space separated floating point values.
666 % The affine matrix determinant is checked for validity of the values.
668 % The format of the ParseAffineGeometry method is:
670 % MagickStatusType ParseAffineGeometry(const char *geometry,
671 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
673 % A description of each parameter follows:
675 % o geometry: The geometry string (e.g. "1.0,0.0,0.0,1.0,3.2,1.2").
677 % o affine_matrix: the affine matrix as defined by the geometry string.
679 % o exception: return any errors or warnings in this structure.
682 MagickExport MagickStatusType ParseAffineGeometry(const char *geometry,
683 AffineMatrix *affine_matrix,ExceptionInfo *exception)
686 token[MaxTextExtent];
700 GetAffineMatrix(affine_matrix);
703 for (i=0; (*p != '\0') && (i < 6); i++)
705 GetMagickToken(p,&p,token);
707 GetMagickToken(p,&p,token);
712 affine_matrix->sx=StringToDouble(token,(char **) NULL);
717 affine_matrix->rx=StringToDouble(token,(char **) NULL);
722 affine_matrix->ry=StringToDouble(token,(char **) NULL);
727 affine_matrix->sy=StringToDouble(token,(char **) NULL);
732 affine_matrix->tx=StringToDouble(token,(char **) NULL);
738 affine_matrix->ty=StringToDouble(token,(char **) NULL);
744 determinant=(affine_matrix->sx*affine_matrix->sy-affine_matrix->rx*
746 if (fabs(determinant) < MagickEpsilon)
747 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
748 "InvalidArgument","'%s' : 'Indeterminate Matrix'",geometry);
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % P a r s e G e o m e t r y %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % ParseGeometry() parses a geometry specification and returns the sigma,
764 % rho, xi, and psi values. It also returns flags that indicates which
765 % of the four values (sigma, rho, xi, psi) were located in the string, and
766 % whether the xi or pi values are negative.
768 % In addition, it reports if there are any of meta characters (%, !, <, >, @,
769 % and ^) flags present. It does not report the location of the percentage
770 % relative to the values.
772 % Values may also be seperated by commas, colons, or slashes, and offsets.
773 % Offsets may be prefixed by multiple signs to make offset string
774 % substitutions easier to handle from shell scripts.
775 % For example: "-10-10", "-+10-+10", or "+-10+-10" will generate negtive
776 % offsets, while "+10+10", "++10++10", or "--10--10" will generate positive
779 % The format of the ParseGeometry method is:
781 % MagickStatusType ParseGeometry(const char *geometry,
782 % GeometryInfo *geometry_info)
784 % A description of each parameter follows:
786 % o geometry: The geometry string (e.g. "100x100+10+10").
788 % o geometry_info: returns the parsed width/height/x/y in this structure.
791 MagickExport MagickStatusType ParseGeometry(const char *geometry,
792 GeometryInfo *geometry_info)
796 pedantic_geometry[MaxTextExtent],
809 Remove whitespaces meta characters from geometry specification.
811 assert(geometry_info != (GeometryInfo *) NULL);
813 if ((geometry == (char *) NULL) || (*geometry == '\0'))
815 if (strlen(geometry) >= (MaxTextExtent-1))
817 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
818 for (p=pedantic_geometry; *p != '\0'; )
820 if (isspace((int) ((unsigned char) *p)) != 0)
822 (void) CopyMagickString(p,p+1,MaxTextExtent);
825 c=(int) ((unsigned char) *p);
831 (void) CopyMagickString(p,p+1,MaxTextExtent);
837 (void) CopyMagickString(p,p+1,MaxTextExtent);
843 (void) CopyMagickString(p,p+1,MaxTextExtent);
849 (void) CopyMagickString(p,p+1,MaxTextExtent);
855 (void) CopyMagickString(p,p+1,MaxTextExtent);
861 (void) CopyMagickString(p,p+1,MaxTextExtent);
867 (void) CopyMagickString(p,p+1,MaxTextExtent);
903 Parse rho, sigma, xi, psi, and optionally chi.
909 value=StringToDouble(p,&q);
910 if (LocaleNCompare(p,"0x",2) == 0)
911 value=(double) strtol(p,&q,10);
912 c=(int) ((unsigned char) *q);
913 if ((c == 215) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
914 (*q == '/') || (*q == ':') || (*q =='\0'))
920 if (LocaleNCompare(p,"0x",2) == 0)
921 value=(double) strtol(p,&p,10);
923 value=StringToDouble(p,&p);
927 geometry_info->rho=value;
931 c=(int) ((unsigned char) *p);
932 if ((c == 215) || (*p == 'x') || (*p == 'X') || (*p == ',') || (*p == '/') ||
939 while (isspace((int) ((unsigned char) *p)) != 0)
941 c=(int) ((unsigned char) *q);
942 if (((c != 215) && (*q != 'x') && (*q != 'X')) || ((*p != '+') &&
946 value=StringToDouble(p,&p);
950 geometry_info->sigma=value;
954 while (isspace((int) ((unsigned char) *p)) != 0)
956 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
961 if ((*p == ',') || (*p == '/') || (*p == ':') )
963 while ((*p == '+') || (*p == '-'))
966 flags^=XiNegative; /* negate sign */
970 value=StringToDouble(p,&p);
974 if ((flags & XiNegative) != 0)
976 geometry_info->xi=value;
978 while (isspace((int) ((unsigned char) *p)) != 0)
980 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
986 if ((*p == ',') || (*p == '/') || (*p == ':'))
988 while ((*p == '+') || (*p == '-'))
991 flags^=PsiNegative; /* negate sign */
995 value=StringToDouble(p,&p);
999 if ((flags & PsiNegative) != 0)
1001 geometry_info->psi=value;
1004 while (isspace((int) ((unsigned char) *p)) != 0)
1006 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
1012 if ((*p == ',') || (*p == '/') || (*p == ':'))
1014 while ((*p == '+') || (*p == '-'))
1017 flags^=ChiNegative; /* negate sign */
1021 value=StringToDouble(p,&p);
1025 if ((flags & ChiNegative) != 0)
1027 geometry_info->chi=value;
1031 if (strchr(pedantic_geometry,':') != (char *) NULL)
1034 Normalize sampling factor (e.g. 4:2:2 => 2x1).
1036 geometry_info->rho/=geometry_info->sigma;
1037 geometry_info->sigma=1.0;
1038 if (geometry_info->xi == 0.0)
1039 geometry_info->sigma=2.0;
1041 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
1042 ((flags & PsiValue) == 0))
1045 Support negative height values (e.g. 30x-20).
1047 geometry_info->sigma=geometry_info->xi;
1048 geometry_info->xi=0.0;
1053 /* Debugging Geometry */
1054 (void) fprintf(stderr,"ParseGeometry...\n");
1055 (void) fprintf(stderr,"Flags: %c %c %s %s %s\n",
1056 (flags & RhoValue) ? 'W' : ' ',(flags & SigmaValue) ? 'H' : ' ',
1057 (flags & XiValue) ? ((flags & XiNegative) ? "-X" : "+X") : " ",
1058 (flags & PsiValue) ? ((flags & PsiNegative) ? "-Y" : "+Y") : " ",
1059 (flags & ChiValue) ? ((flags & ChiNegative) ? "-Z" : "+Z") : " ");
1060 (void) fprintf(stderr,"Geometry: %lg,%lg,%lg,%lg,%lg\n",geometry_info->rho,
1061 geometry_info->sigma,geometry_info->xi,geometry_info->psi,
1062 geometry_info->chi);
1068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1072 % P a r s e G r a v i t y G e o m e t r y %
1076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078 % ParseGravityGeometry() returns a region as defined by the geometry string
1079 % with respect to the given image page (canvas) dimensions and the images
1082 % This is typically used for specifing a area within a given image for
1083 % cropping images to a smaller size, chopping out rows and or columns, or
1084 % resizing and positioning overlay images.
1086 % Percentages are relative to image size and not page size, and are set to
1087 % nearest integer (pixel) size.
1089 % The format of the ParseGravityGeometry method is:
1091 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
1092 % RectangeInfo *region_info,ExceptionInfo *exception)
1094 % A description of each parameter follows:
1096 % o geometry: The geometry string (e.g. "100x100+10+10").
1098 % o region_info: the region as defined by the geometry string with respect
1099 % to the image dimensions and its gravity.
1101 % o exception: return any errors or warnings in this structure.
1104 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
1105 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1114 SetGeometry(image,region_info);
1115 if (image->page.width != 0)
1116 region_info->width=image->page.width;
1117 if (image->page.height != 0)
1118 region_info->height=image->page.height;
1119 flags=ParseAbsoluteGeometry(geometry,region_info);
1120 if (flags == NoValue)
1122 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1123 "InvalidGeometry","'%s'",geometry);
1126 if ((flags & PercentValue) != 0)
1138 Geometry is a percentage of the image size, not canvas size
1140 if (image->gravity != UndefinedGravity)
1141 flags|=XValue | YValue;
1142 status=ParseGeometry(geometry,&geometry_info);
1143 scale.x=geometry_info.rho;
1144 if ((status & RhoValue) == 0)
1146 scale.y=geometry_info.sigma;
1147 if ((status & SigmaValue) == 0)
1149 region_info->width=(size_t) floor((scale.x*image->columns/100.0)+0.5);
1150 region_info->height=(size_t) floor((scale.y*image->rows/100.0)+0.5);
1153 Adjust offset according to gravity setting.
1155 width=region_info->width;
1156 height=region_info->height;
1158 region_info->width=image->page.width | image->columns;
1160 region_info->height=image->page.height | image->rows;
1161 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1162 region_info->width=width;
1163 region_info->height=height;
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 + P a r s e M e t a G e o m e t r y %
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1178 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1179 % geometry is modified as determined by the meta characters: %, !, <, >, @,
1180 % and ^ in relation to image resizing.
1182 % Final image dimensions are adjusted so as to preserve the aspect ratio as
1183 % much as possible, while generating a integer (pixel) size, and fitting the
1184 % image within the specified geometry width and height.
1186 % Flags are interpreted...
1187 % % geometry size is given percentage of original width and height given
1188 % ! do not try to preserve aspect ratio
1189 % < only enlarge images smaller that geometry
1190 % > only shrink images larger than geometry
1191 % @ Fit image to contain at most this many pixels
1192 % ^ Contain the given geometry given, (minimal dimensions given)
1194 % The format of the ParseMetaGeometry method is:
1196 % MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1197 % ssize_t *y, size_t *width,size_t *height)
1199 % A description of each parameter follows:
1201 % o geometry: The geometry string (e.g. "100x100+10+10").
1203 % o x,y: The x and y offset, set according to the geometry specification.
1205 % o width,height: The width and height of original image, modified by
1206 % the given geometry specification.
1210 static inline size_t MagickMax(const size_t x,
1218 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1219 ssize_t *y,size_t *width,size_t *height)
1232 Ensure the image geometry is valid.
1234 assert(x != (ssize_t *) NULL);
1235 assert(y != (ssize_t *) NULL);
1236 assert(width != (size_t *) NULL);
1237 assert(height != (size_t *) NULL);
1238 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1242 Parse geometry using GetGeometry.
1244 SetGeometryInfo(&geometry_info);
1245 former_width=(*width);
1246 former_height=(*height);
1247 flags=GetGeometry(geometry,x,y,width,height);
1248 if ((flags & PercentValue) != 0)
1257 Geometry is a percentage of the image size.
1259 flags=ParseGeometry(geometry,&geometry_info);
1260 scale.x=geometry_info.rho;
1261 if ((flags & RhoValue) == 0)
1263 scale.y=geometry_info.sigma;
1264 if ((flags & SigmaValue) == 0)
1266 *width=(size_t) floor(scale.x*former_width/100.0+0.5);
1269 *height=(size_t) floor(scale.y*former_height/100.0+0.5);
1272 former_width=(*width);
1273 former_height=(*height);
1275 if (((flags & AspectValue) != 0) || ((*width == former_width) &&
1276 (*height == former_height)))
1278 if ((flags & RhoValue) == 0)
1279 *width=former_width;
1280 if ((flags & SigmaValue) == 0)
1281 *height=former_height;
1289 Respect aspect ratio of the image.
1291 if ((former_width == 0) || (former_height == 0))
1294 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1296 scale_factor=(double) *width/(double) former_width;
1297 if ((flags & MinimumValue) == 0)
1299 if (scale_factor > ((double) *height/(double) former_height))
1300 scale_factor=(double) *height/(double) former_height;
1303 if (scale_factor < ((double) *height/(double) former_height))
1304 scale_factor=(double) *height/(double) former_height;
1307 if ((flags & RhoValue) != 0)
1309 scale_factor=(double) *width/(double) former_width;
1310 if (((flags & MinimumValue) != 0) &&
1311 (scale_factor < ((double) *width/(double) former_height)))
1312 scale_factor=(double) *width/(double) former_height;
1316 scale_factor=(double) *height/(double) former_height;
1317 if (((flags & MinimumValue) != 0) &&
1318 (scale_factor < ((double) *height/(double) former_width)))
1319 scale_factor=(double) *height/(double) former_width;
1321 *width=MagickMax((size_t) floor(scale_factor*former_width+0.5),1UL);
1322 *height=MagickMax((size_t) floor(scale_factor*former_height+0.5),1UL);
1324 if ((flags & GreaterValue) != 0)
1326 if (former_width < *width)
1327 *width=former_width;
1328 if (former_height < *height)
1329 *height=former_height;
1331 if ((flags & LessValue) != 0)
1333 if (former_width > *width)
1334 *width=former_width;
1335 if (former_height > *height)
1336 *height=former_height;
1338 if ((flags & AreaValue) != 0)
1348 Geometry is a maximum area in pixels.
1350 (void) ParseGeometry(geometry,&geometry_info);
1351 area=geometry_info.rho;
1352 distance=sqrt((double) former_width*former_height);
1353 scale.x=(double) former_width/(distance/sqrt((double) area));
1354 scale.y=(double) former_height/(distance/sqrt((double) area));
1355 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1357 *width=(size_t) (former_width/(distance/sqrt(area))+0.5);
1358 *height=(size_t) (former_height/(distance/sqrt(area))+0.5);
1360 former_width=(*width);
1361 former_height=(*height);
1367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 % P a r s e P a g e G e o m e t r y %
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377 % ParsePageGeometry() returns a region as defined by the geometry string with
1378 % respect to the image page (canvas) dimensions.
1380 % WARNING: Percentage dimensions remain relative to the actual image
1381 % dimensions, and not canvas dimensions.
1383 % The format of the ParsePageGeometry method is:
1385 % MagickStatusType ParsePageGeometry(const Image *image,
1386 % const char *geometry,RectangeInfo *region_info,
1387 % ExceptionInfo *exception)
1389 % A description of each parameter follows:
1391 % o geometry: The geometry string (e.g. "100x100+10+10").
1393 % o region_info: the region as defined by the geometry string with
1394 % respect to the image and its gravity.
1396 % o exception: return any errors or warnings in this structure.
1399 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1400 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1405 SetGeometry(image,region_info);
1406 if (image->page.width != 0)
1407 region_info->width=image->page.width;
1408 if (image->page.height != 0)
1409 region_info->height=image->page.height;
1410 flags=ParseAbsoluteGeometry(geometry,region_info);
1411 if (flags == NoValue)
1413 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1414 "InvalidGeometry","'%s'",geometry);
1417 if ((flags & PercentValue) != 0)
1419 region_info->width=image->columns;
1420 region_info->height=image->rows;
1422 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1423 ®ion_info->width,®ion_info->height);
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % P a r s e R e g i o n G e o m e t r y %
1436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438 % ParseRegionGeometry() returns a region as defined by the geometry string
1439 % with respect to the image dimensions and aspect ratio.
1441 % This is basically a wrapper around ParseMetaGeometry. This is typically
1442 % used to parse a geometry string to work out the final integer dimensions
1443 % for image resizing.
1445 % The format of the ParseRegionGeometry method is:
1447 % MagickStatusType ParseRegionGeometry(const Image *image,
1448 % const char *geometry,RectangeInfo *region_info,
1449 % ExceptionInfo *exception)
1451 % A description of each parameter follows:
1453 % o geometry: The geometry string (e.g. "100x100+10+10").
1455 % o region_info: the region as defined by the geometry string.
1457 % o exception: return any errors or warnings in this structure.
1460 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1461 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1466 SetGeometry(image,region_info);
1467 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1468 ®ion_info->width,®ion_info->height);
1469 if (flags == NoValue)
1470 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1471 "InvalidGeometry","'%s'",geometry);
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480 % S e t G e o m e t r y %
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1486 % SetGeometry() sets the geometry to its default values.
1488 % The format of the SetGeometry method is:
1490 % SetGeometry(const Image *image,RectangleInfo *geometry)
1492 % A description of each parameter follows:
1494 % o image: the image.
1496 % o geometry: the geometry.
1499 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1501 assert(image != (Image *) NULL);
1502 assert(image->signature == MagickSignature);
1503 if (image->debug != MagickFalse)
1504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1505 assert(geometry != (RectangleInfo *) NULL);
1506 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1507 geometry->width=image->columns;
1508 geometry->height=image->rows;
1512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1516 % S e t G e o m e t r y I n f o %
1520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1522 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1524 % The format of the SetGeometryInfo method is:
1526 % SetGeometryInfo(GeometryInfo *geometry_info)
1528 % A description of each parameter follows:
1530 % o geometry_info: the geometry info structure.
1533 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1535 assert(geometry_info != (GeometryInfo *) NULL);
1536 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1537 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));