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 c=(int) ((unsigned char) *q);
210 if ((c == 215) || (*q == 'x') || (*q == 'X') || (*q == '\0'))
216 if (LocaleNCompare(p,"0x",2) == 0)
217 *width=(size_t) strtol(p,&p,10);
219 *width=(size_t) floor(StringToDouble(p,&p)+0.5);
223 c=(int) ((unsigned char) *p);
224 if ((c == 215) || (*p == 'x') || (*p == 'X'))
227 if ((*p != '+') && (*p != '-'))
233 *height=(size_t) floor(StringToDouble(p,&p)+0.5);
238 if ((*p == '+') || (*p == '-'))
243 while ((*p == '+') || (*p == '-'))
246 flags^=XNegative; /* negate sign */
250 *x=(ssize_t) ceil(StringToDouble(p,&p)-0.5);
254 if ( (flags&XNegative) != 0 )
258 if ((*p == '+') || (*p == '-'))
263 while ((*p == '+') || (*p == '-'))
266 flags^=YNegative; /* negate sign */
270 *y=(ssize_t) ceil(StringToDouble(p,&p)-0.5);
274 if ( (flags&YNegative) != 0 )
279 /* Debugging Geometry */
280 fprintf(stderr,"GetGeometry...\n");
281 fprintf(stderr,"Input: %s\n",geometry);
282 fprintf(stderr,"Flags: %c %c %s %s\n",
283 (flags&WidthValue)?'W':' ',(flags&HeightValue)?'H':' ',
284 (flags&XValue)?((flags&XNegative)?"-X":"+X"):" ",
285 (flags&YValue)?((flags&YNegative)?"-Y":"+Y"):" ");
286 fprintf(stderr,"Geometry: %ldx%ld%+ld%+ld\n",
287 (long)*width,(long)*height,(long)*x,(long)*y);
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % G e t P a g e G e o m e t r y %
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % GetPageGeometry() replaces any page mneumonic with the equivalent size in
306 % The format of the GetPageGeometry method is:
308 % char *GetPageGeometry(const char *page_geometry)
310 % A description of each parameter follows.
312 % o page_geometry: Specifies a pointer to an array of characters. The
313 % string is either a Postscript page name (e.g. A4) or a postscript page
314 % geometry (e.g. 612x792+36+36).
317 MagickExport char *GetPageGeometry(const char *page_geometry)
322 { "4x6", "288x432" },
323 { "5x7", "360x504" },
324 { "7x9", "504x648" },
325 { "8x10", "576x720" },
326 { "9x11", "648x792" },
327 { "9x12", "648x864" },
328 { "10x13", "720x936" },
329 { "10x14", "720x1008" },
330 { "11x17", "792x1224" },
331 { "a0", "2384x3370" },
332 { "a1", "1684x2384" },
334 { "a2", "1191x1684" },
335 { "a3", "842x1191" },
337 { "a4smaLL", "595x842" },
343 { "archa", "648x864" },
344 { "archb", "864x1296" },
345 { "archC", "1296x1728" },
346 { "archd", "1728x2592" },
347 { "arche", "2592x3456" },
348 { "b0", "2920x4127" },
349 { "b1", "2064x2920" },
351 { "b2", "1460x2064" },
352 { "b3", "1032x1460" },
353 { "b4", "729x1032" },
359 { "c0", "2599x3676" },
360 { "c1", "1837x2599" },
361 { "c2", "1298x1837" },
362 { "c3", "918x1296" },
367 { "executive", "540x720" },
368 { "flsa", "612x936" },
369 { "flse", "612x936" },
370 { "folio", "612x936" },
371 { "halfletter", "396x612" },
372 { "isob0", "2835x4008" },
373 { "isob1", "2004x2835" },
374 { "isob10", "88x125" },
375 { "isob2", "1417x2004" },
376 { "isob3", "1001x1417" },
377 { "isob4", "709x1001" },
378 { "isob5", "499x709" },
379 { "isob6", "354x499" },
380 { "isob7", "249x354" },
381 { "isob8", "176x249" },
382 { "isob9", "125x176" },
383 { "jisb0", "1030x1456" },
384 { "jisb1", "728x1030" },
385 { "jisb2", "515x728" },
386 { "jisb3", "364x515" },
387 { "jisb4", "257x364" },
388 { "jisb5", "182x257" },
389 { "jisb6", "128x182" },
390 { "ledger", "1224x792" },
391 { "legal", "612x1008" },
392 { "letter", "612x792" },
393 { "lettersmaLL", "612x792" },
394 { "quarto", "610x780" },
395 { "statement", "396x612" },
396 { "tabloid", "792x1224" },
397 { (char *) NULL, (char *) NULL }
406 assert(page_geometry != (char *) NULL);
407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",page_geometry);
408 page=AcquireString(page_geometry);
409 for (i=0; *PageSizes[i] != (char *) NULL; i++)
410 if (LocaleNCompare(PageSizes[i][0],page,strlen(PageSizes[i][0])) == 0)
419 Replace mneumonic with the equivalent size in dots-per-inch.
421 (void) CopyMagickString(page,PageSizes[i][1],MaxTextExtent);
422 (void) ConcatenateMagickString(page,page_geometry+
423 strlen(PageSizes[i][0]),MaxTextExtent);
424 flags=GetGeometry(page,&geometry.x,&geometry.y,&geometry.width,
426 if ((flags & GreaterValue) == 0)
427 (void) ConcatenateMagickString(page,">",MaxTextExtent);
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 % G r a v i t y A d j u s t G e o m e t r y %
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443 % GravityAdjustGeometry() adjusts the offset of a region with regard to the
444 % given: width, height and gravity; against which it is positioned.
446 % The region should also have an appropriate width and height to correctly
447 % set the right offset of the top left corner of the region.
449 % The format of the GravityAdjustGeometry method is:
451 % void GravityAdjustGeometry(const size_t width, const size_t height,
452 % const GravityType gravity,RectangleInfo *region);
454 % A description of each parameter follows:
456 % o width, height: the larger area the region is relative to
458 % o gravity: the edge/corner the current offset is relative to
460 % o region: The region requiring a offset adjustment relative to gravity
463 MagickExport void GravityAdjustGeometry(const size_t width,
464 const size_t height,const GravityType gravity,RectangleInfo *region)
466 if (region->height == 0)
467 region->height=height;
468 if (region->width == 0)
472 case NorthEastGravity:
474 case SouthEastGravity:
476 region->x=(ssize_t) (width-region->width-region->x);
483 region->x+=(ssize_t) (width/2-region->width/2);
487 case NorthWestGravity:
489 case SouthWestGravity:
495 case SouthWestGravity:
497 case SouthEastGravity:
499 region->y=(ssize_t) (height-region->height-region->y);
506 region->y+=(ssize_t) (height/2-region->height/2);
510 case NorthWestGravity:
512 case NorthEastGravity:
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 + I s G e o m e t r y %
528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530 % IsGeometry() returns MagickTrue if the geometry specification is valid.
531 % Examples are 100, 100x200, x200, 100x200+10+20, +10+20, 200%, 200x200!, etc.
533 % The format of the IsGeometry method is:
535 % MagickBooleanType IsGeometry(const char *geometry)
537 % A description of each parameter follows:
539 % o geometry: This string is the geometry specification.
542 MagickExport MagickBooleanType IsGeometry(const char *geometry)
550 if (geometry == (const char *) NULL)
552 flags=ParseGeometry(geometry,&geometry_info);
553 return(flags != NoValue ? MagickTrue : MagickFalse);
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561 + I s S c e n e G e o m e t r y %
565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % IsSceneGeometry() returns MagickTrue if the geometry is a valid scene
568 % specification (e.g. [1], [1-9], [1,7,4]).
570 % The format of the IsSceneGeometry method is:
572 % MagickBooleanType IsSceneGeometry(const char *geometry,
573 % const MagickBooleanType pedantic)
575 % A description of each parameter follows:
577 % o geometry: This string is the geometry specification.
579 % o pedantic: A value other than 0 invokes a more restrictive set of
580 % conditions for a valid specification (e.g. [1], [1-4], [4-1]).
583 MagickExport MagickBooleanType IsSceneGeometry(const char *geometry,
584 const MagickBooleanType pedantic)
592 if (geometry == (const char *) NULL)
595 value=StringToDouble(geometry,&p);
599 if (strspn(geometry,"0123456789-, ") != strlen(geometry))
601 if ((pedantic != MagickFalse) && (strchr(geometry,',') != (char *) NULL))
607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611 % P a r s e A b s o l u t e G e o m e t r y %
615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 % ParseAbsoluteGeometry() returns a region as defined by the geometry string,
618 % without any modification by percentages or gravity.
620 % It currently just a wrapper around GetGeometry(), but may be expanded in
621 % the future to handle other positioning information.
623 % The format of the ParseAbsoluteGeometry method is:
625 % MagickStatusType ParseAbsoluteGeometry(const char *geometry,
626 % RectangleInfo *region_info)
628 % A description of each parameter follows:
630 % o geometry: The geometry string (e.g. "100x100+10+10").
632 % o region_info: the region as defined by the geometry string.
635 MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry,
636 RectangleInfo *region_info)
641 flags=GetGeometry(geometry,®ion_info->x,®ion_info->y,
642 ®ion_info->width,®ion_info->height);
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % P a r s e A f f i n e G e o m e t r y %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % ParseAffineGeometry() returns an affine matrix as defined by a string of 4
658 % to 6 comma/space separated floating point values.
660 % The affine matrix determinant is checked for validity of the values.
662 % The format of the ParseAffineGeometry method is:
664 % MagickStatusType ParseAffineGeometry(const char *geometry,
665 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
667 % A description of each parameter follows:
669 % o geometry: The geometry string (e.g. "1.0,0.0,0.0,1.0,3.2,1.2").
671 % o affine_matrix: the affine matrix as defined by the geometry string.
673 % o exception: return any errors or warnings in this structure.
676 MagickExport MagickStatusType ParseAffineGeometry(const char *geometry,
677 AffineMatrix *affine_matrix,ExceptionInfo *exception)
680 token[MaxTextExtent];
694 GetAffineMatrix(affine_matrix);
697 for (i=0; (*p != '\0') && (i < 6); i++)
699 GetMagickToken(p,&p,token);
701 GetMagickToken(p,&p,token);
706 affine_matrix->sx=StringToDouble(token,(char **) NULL);
711 affine_matrix->rx=StringToDouble(token,(char **) NULL);
716 affine_matrix->ry=StringToDouble(token,(char **) NULL);
721 affine_matrix->sy=StringToDouble(token,(char **) NULL);
726 affine_matrix->tx=StringToDouble(token,(char **) NULL);
732 affine_matrix->ty=StringToDouble(token,(char **) NULL);
738 determinant=(affine_matrix->sx*affine_matrix->sy
739 - affine_matrix->rx*affine_matrix->ry);
740 if (fabs(determinant) < MagickEpsilon)
741 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
742 "InvalidArgument","'%s' : 'Indeterminate Matrix'",geometry);
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 % P a r s e G e o m e t r y %
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % ParseGeometry() parses a geometry specification and returns the sigma,
758 % rho, xi, and psi values. It also returns flags that indicates which
759 % of the four values (sigma, rho, xi, psi) were located in the string, and
760 % whether the xi or pi values are negative.
762 % In addition, it reports if there are any of meta characters (%, !, <, >, @,
763 % and ^) flags present. It does not report the location of the percentage
764 % relative to the values.
766 % Values may also be seperated by commas, colons, or slashes, and offsets.
767 % Offsets may be prefixed by multiple signs to make offset string
768 % substitutions easier to handle from shell scripts.
769 % For example: "-10-10", "-+10-+10", or "+-10+-10" will generate negtive
770 % offsets, while "+10+10", "++10++10", or "--10--10" will generate positive
773 % The format of the ParseGeometry method is:
775 % MagickStatusType ParseGeometry(const char *geometry,
776 % GeometryInfo *geometry_info)
778 % A description of each parameter follows:
780 % o geometry: The geometry string (e.g. "100x100+10+10").
782 % o geometry_info: returns the parsed width/height/x/y in this structure.
785 MagickExport MagickStatusType ParseGeometry(const char *geometry,
786 GeometryInfo *geometry_info)
790 pedantic_geometry[MaxTextExtent],
803 Remove whitespaces meta characters from geometry specification.
805 assert(geometry_info != (GeometryInfo *) NULL);
807 if ((geometry == (char *) NULL) || (*geometry == '\0'))
809 if (strlen(geometry) >= (MaxTextExtent-1))
811 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
812 for (p=pedantic_geometry; *p != '\0'; )
814 if (isspace((int) ((unsigned char) *p)) != 0)
816 (void) CopyMagickString(p,p+1,MaxTextExtent);
819 c=(int) ((unsigned char) *p);
825 (void) CopyMagickString(p,p+1,MaxTextExtent);
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);
897 Parse rho, sigma, xi, psi, and optionally chi.
903 value=StringToDouble(p,&q);
904 if (LocaleNCompare(p,"0x",2) == 0)
905 value=(double) strtol(p,&q,10);
906 c=(int) ((unsigned char) *q);
907 if ((c == 215) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
908 (*q == '/') || (*q == ':') || (*q =='\0'))
914 if (LocaleNCompare(p,"0x",2) == 0)
915 value=(double) strtol(p,&p,10);
917 value=StringToDouble(p,&p);
921 geometry_info->rho=value;
925 c=(int) ((unsigned char) *p);
926 if ((c == 215) || (*p == 'x') || (*p == 'X') || (*p == ',') || (*p == '/') ||
933 while (isspace((int) ((unsigned char) *p)) != 0)
935 c=(int) ((unsigned char) *q);
936 if (((c != 215) && (*q != 'x') && (*q != 'X')) || ((*p != '+') &&
940 value=StringToDouble(p,&p);
944 geometry_info->sigma=value;
948 while (isspace((int) ((unsigned char) *p)) != 0)
950 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
955 if ((*p == ',') || (*p == '/') || (*p == ':') )
957 while ((*p == '+') || (*p == '-'))
960 flags^=XiNegative; /* negate sign */
964 value=StringToDouble(p,&p);
968 if ( (flags&XiNegative) != 0 )
970 geometry_info->xi=value;
972 while (isspace((int) ((unsigned char) *p)) != 0)
974 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
980 if ((*p == ',') || (*p == '/') || (*p == ':'))
982 while ((*p == '+') || (*p == '-'))
985 flags^=PsiNegative; /* negate sign */
989 value=StringToDouble(p,&p);
992 if ( (flags&PsiNegative) != 0 )
994 geometry_info->psi=value;
997 while (isspace((int) ((unsigned char) *p)) != 0)
999 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
1005 if ((*p == ',') || (*p == '/') || (*p == ':'))
1007 while ((*p == '+') || (*p == '-'))
1010 flags^=ChiNegative; /* negate sign */
1014 value=StringToDouble(p,&p);
1017 if ( (flags&ChiNegative) != 0 )
1019 geometry_info->chi=value;
1023 if (strchr(pedantic_geometry,':') != (char *) NULL)
1026 Normalize sampling factor (e.g. 4:2:2 => 2x1).
1028 geometry_info->rho/=geometry_info->sigma;
1029 geometry_info->sigma=1.0;
1030 if (geometry_info->xi == 0.0)
1031 geometry_info->sigma=2.0;
1033 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
1034 ((flags & PsiValue) == 0))
1037 Support negative height values (e.g. 30x-20).
1039 geometry_info->sigma=geometry_info->xi;
1040 geometry_info->xi=0.0;
1045 /* Debugging Geometry */
1046 fprintf(stderr,"ParseGeometry...\n");
1047 fprintf(stderr,"Flags: %c %c %s %s %s\n",
1048 (flags&RhoValue)?'W':' ',(flags&SigmaValue)?'H':' ',
1049 (flags&XiValue)?((flags&XiNegative)?"-X":"+X"):" ",
1050 (flags&PsiValue)?((flags&PsiNegative)?"-Y":"+Y"):" ",
1051 (flags&ChiValue)?((flags&ChiNegative)?"-Z":"+Z"):" ");
1052 fprintf(stderr,"Geometry: %lg,%lg,%lg,%lg,%lg\n",
1053 geometry_info->rho,geometry_info->sigma,
1054 geometry_info->xi,geometry_info->psi,geometry_info->chi);
1060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064 % P a r s e G r a v i t y G e o m e t r y %
1068 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 % ParseGravityGeometry() returns a region as defined by the geometry string
1071 % with respect to the given image page (canvas) dimensions and the images
1074 % This is typically used for specifing a area within a given image for
1075 % cropping images to a smaller size, chopping out rows and or columns, or
1076 % resizing and positioning overlay images.
1078 % Percentages are relative to image size and not page size, and are set to
1079 % nearest integer (pixel) size.
1081 % The format of the ParseGravityGeometry method is:
1083 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
1084 % RectangeInfo *region_info,ExceptionInfo *exception)
1086 % A description of each parameter follows:
1088 % o geometry: The geometry string (e.g. "100x100+10+10").
1090 % o region_info: the region as defined by the geometry string with respect
1091 % to the image dimensions and its gravity.
1093 % o exception: return any errors or warnings in this structure.
1096 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
1097 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1106 SetGeometry(image,region_info);
1107 if (image->page.width != 0)
1108 region_info->width=image->page.width;
1109 if (image->page.height != 0)
1110 region_info->height=image->page.height;
1111 flags=ParseAbsoluteGeometry(geometry,region_info);
1112 if (flags == NoValue)
1114 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1115 "InvalidGeometry","'%s'",geometry);
1118 if ((flags & PercentValue) != 0)
1130 Geometry is a percentage of the image size, not canvas size
1132 if (image->gravity != UndefinedGravity)
1133 flags|=XValue | YValue;
1134 status=ParseGeometry(geometry,&geometry_info);
1135 scale.x=geometry_info.rho;
1136 if ((status & RhoValue) == 0)
1138 scale.y=geometry_info.sigma;
1139 if ((status & SigmaValue) == 0)
1141 region_info->width=(size_t) floor((scale.x*image->columns/100.0)+0.5);
1142 region_info->height=(size_t) floor((scale.y*image->rows/100.0)+0.5);
1145 Adjust offset according to gravity setting.
1147 width=region_info->width;
1148 height=region_info->height;
1150 region_info->width=image->page.width | image->columns;
1152 region_info->height=image->page.height | image->rows;
1153 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1154 region_info->width=width;
1155 region_info->height=height;
1160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1164 + P a r s e M e t a G e o m e t r y %
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1170 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1171 % geometry is modified as determined by the meta characters: %, !, <, >, @,
1172 % and ^ in relation to image resizing.
1174 % Final image dimensions are adjusted so as to preserve the aspect ratio as
1175 % much as possible, while generating a integer (pixel) size, and fitting the
1176 % image within the specified geometry width and height.
1178 % Flags are interpreted...
1179 % % geometry size is given percentage of original width and height given
1180 % ! do not try to preserve aspect ratio
1181 % < only enlarge images smaller that geometry
1182 % > only shrink images larger than geometry
1183 % @ Fit image to contain at most this many pixels
1184 % ^ Contain the given geometry given, (minimal dimensions given)
1186 % The format of the ParseMetaGeometry method is:
1188 % MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1189 % ssize_t *y, size_t *width,size_t *height)
1191 % A description of each parameter follows:
1193 % o geometry: The geometry string (e.g. "100x100+10+10").
1195 % o x,y: The x and y offset, set according to the geometry specification.
1197 % o width,height: The width and height of original image, modified by
1198 % the given geometry specification.
1202 static inline size_t MagickMax(const size_t x,
1210 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1211 ssize_t *y,size_t *width,size_t *height)
1224 Ensure the image geometry is valid.
1226 assert(x != (ssize_t *) NULL);
1227 assert(y != (ssize_t *) NULL);
1228 assert(width != (size_t *) NULL);
1229 assert(height != (size_t *) NULL);
1230 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1234 Parse geometry using GetGeometry.
1236 SetGeometryInfo(&geometry_info);
1237 former_width=(*width);
1238 former_height=(*height);
1239 flags=GetGeometry(geometry,x,y,width,height);
1240 if ((flags & PercentValue) != 0)
1249 Geometry is a percentage of the image size.
1251 flags=ParseGeometry(geometry,&geometry_info);
1252 scale.x=geometry_info.rho;
1253 if ((flags & RhoValue) == 0)
1255 scale.y=geometry_info.sigma;
1256 if ((flags & SigmaValue) == 0)
1258 *width=(size_t) floor(scale.x*former_width/100.0+0.5);
1261 *height=(size_t) floor(scale.y*former_height/100.0+0.5);
1264 former_width=(*width);
1265 former_height=(*height);
1267 if (((flags & AspectValue) != 0) || ((*width == former_width) &&
1268 (*height == former_height)))
1270 if ((flags & RhoValue) == 0)
1271 *width=former_width;
1272 if ((flags & SigmaValue) == 0)
1273 *height=former_height;
1281 Respect aspect ratio of the image.
1283 if ((former_width == 0) || (former_height == 0))
1286 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1288 scale_factor=(double) *width/(double) former_width;
1289 if ((flags & MinimumValue) == 0)
1291 if (scale_factor > ((double) *height/(double) former_height))
1292 scale_factor=(double) *height/(double) former_height;
1295 if (scale_factor < ((double) *height/(double) former_height))
1296 scale_factor=(double) *height/(double) former_height;
1299 if ((flags & RhoValue) != 0)
1301 scale_factor=(double) *width/(double) former_width;
1302 if (((flags & MinimumValue) != 0) &&
1303 (scale_factor < ((double) *width/(double) former_height)))
1304 scale_factor=(double) *width/(double) former_height;
1308 scale_factor=(double) *height/(double) former_height;
1309 if (((flags & MinimumValue) != 0) &&
1310 (scale_factor < ((double) *height/(double) former_width)))
1311 scale_factor=(double) *height/(double) former_width;
1313 *width=MagickMax((size_t) floor(scale_factor*former_width+0.5),1UL);
1314 *height=MagickMax((size_t) floor(scale_factor*former_height+0.5),1UL);
1316 if ((flags & GreaterValue) != 0)
1318 if (former_width < *width)
1319 *width=former_width;
1320 if (former_height < *height)
1321 *height=former_height;
1323 if ((flags & LessValue) != 0)
1325 if (former_width > *width)
1326 *width=former_width;
1327 if (former_height > *height)
1328 *height=former_height;
1330 if ((flags & AreaValue) != 0)
1340 Geometry is a maximum area in pixels.
1342 (void) ParseGeometry(geometry,&geometry_info);
1343 area=geometry_info.rho;
1344 distance=sqrt((double) former_width*former_height);
1345 scale.x=(double) former_width/(distance/sqrt((double) area));
1346 scale.y=(double) former_height/(distance/sqrt((double) area));
1347 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1349 *width=(size_t) (former_width/(distance/sqrt(area))+0.5);
1350 *height=(size_t) (former_height/(distance/sqrt(area))+0.5);
1352 former_width=(*width);
1353 former_height=(*height);
1359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363 % P a r s e P a g e G e o m e t r y %
1367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 % ParsePageGeometry() returns a region as defined by the geometry string with
1370 % respect to the image page (canvas) dimensions.
1372 % WARNING: Percentage dimensions remain relative to the actual image
1373 % dimensions, and not canvas dimensions.
1375 % The format of the ParsePageGeometry method is:
1377 % MagickStatusType ParsePageGeometry(const Image *image,
1378 % const char *geometry,RectangeInfo *region_info,
1379 % ExceptionInfo *exception)
1381 % A description of each parameter follows:
1383 % o geometry: The geometry string (e.g. "100x100+10+10").
1385 % o region_info: the region as defined by the geometry string with
1386 % respect to the image and its gravity.
1388 % o exception: return any errors or warnings in this structure.
1391 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1392 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1397 SetGeometry(image,region_info);
1398 if (image->page.width != 0)
1399 region_info->width=image->page.width;
1400 if (image->page.height != 0)
1401 region_info->height=image->page.height;
1402 flags=ParseAbsoluteGeometry(geometry,region_info);
1403 if (flags == NoValue)
1405 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1406 "InvalidGeometry","'%s'",geometry);
1409 if ((flags & PercentValue) != 0)
1411 region_info->width=image->columns;
1412 region_info->height=image->rows;
1414 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1415 ®ion_info->width,®ion_info->height);
1420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424 % P a r s e R e g i o n G e o m e t r y %
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430 % ParseRegionGeometry() returns a region as defined by the geometry string
1431 % with respect to the image dimensions and aspect ratio.
1433 % This is basically a wrapper around ParseMetaGeometry. This is typically
1434 % used to parse a geometry string to work out the final integer dimensions
1435 % for image resizing.
1437 % The format of the ParseRegionGeometry method is:
1439 % MagickStatusType ParseRegionGeometry(const Image *image,
1440 % const char *geometry,RectangeInfo *region_info,
1441 % ExceptionInfo *exception)
1443 % A description of each parameter follows:
1445 % o geometry: The geometry string (e.g. "100x100+10+10").
1447 % o region_info: the region as defined by the geometry string.
1449 % o exception: return any errors or warnings in this structure.
1452 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1453 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1458 SetGeometry(image,region_info);
1459 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1460 ®ion_info->width,®ion_info->height);
1461 if (flags == NoValue)
1462 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1463 "InvalidGeometry","'%s'",geometry);
1468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1472 % S e t G e o m e t r y %
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 % SetGeometry() sets the geometry to its default values.
1480 % The format of the SetGeometry method is:
1482 % SetGeometry(const Image *image,RectangleInfo *geometry)
1484 % A description of each parameter follows:
1486 % o image: the image.
1488 % o geometry: the geometry.
1491 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1493 assert(image != (Image *) NULL);
1494 assert(image->signature == MagickSignature);
1495 if (image->debug != MagickFalse)
1496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1497 assert(geometry != (RectangleInfo *) NULL);
1498 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1499 geometry->width=image->columns;
1500 geometry->height=image->rows;
1504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1508 % S e t G e o m e t r y I n f o %
1512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1514 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1516 % The format of the SetGeometryInfo method is:
1518 % SetGeometryInfo(GeometryInfo *geometry_info)
1520 % A description of each parameter follows:
1522 % o geometry_info: the geometry info structure.
1525 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1527 assert(geometry_info != (GeometryInfo *) NULL);
1528 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1529 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));