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-2011 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 format of the GetGeometry method is:
72 % MagickStatusType GetGeometry(const char *geometry,ssize_t *x,ssize_t *y,
73 % size_t *width,size_t *height)
75 % A description of each parameter follows:
77 % o geometry: The geometry.
79 % o x,y: The x and y offset as determined by the geometry specification.
81 % o width,height: The width and height as determined by the geometry
85 MagickExport MagickStatusType GetGeometry(const char *geometry,ssize_t *x,
86 ssize_t *y,size_t *width,size_t *height)
90 pedantic_geometry[MaxTextExtent],
100 Remove whitespace and meta characters from geometry specification.
103 if ((geometry == (char *) NULL) || (*geometry == '\0'))
105 if (strlen(geometry) >= (MaxTextExtent-1))
107 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
108 for (p=pedantic_geometry; *p != '\0'; )
110 if (isspace((int) ((unsigned char) *p)) != 0)
112 (void) CopyMagickString(p,p+1,MaxTextExtent);
120 (void) CopyMagickString(p,p+1,MaxTextExtent);
126 (void) CopyMagickString(p,p+1,MaxTextExtent);
132 (void) CopyMagickString(p,p+1,MaxTextExtent);
138 (void) CopyMagickString(p,p+1,MaxTextExtent);
144 (void) CopyMagickString(p,p+1,MaxTextExtent);
150 (void) CopyMagickString(p,p+1,MaxTextExtent);
156 (void) CopyMagickString(p,p+1,MaxTextExtent);
185 Parse width, height, x, and y.
191 value=InterpretLocaleValue(p,&q);
193 if (LocaleNCompare(p,"0x",2) == 0)
194 value=(double) strtol(p,&q,10);
195 if ((((int) *q) == -41) || (*q == 'x') || (*q == 'X') || (*q == '\0'))
201 if (LocaleNCompare(p,"0x",2) == 0)
202 *width=(size_t) strtol(p,&p,10);
204 *width=(size_t) floor(InterpretLocaleValue(p,&p)+0.5);
208 if ((((int) *p) == -41) || (*p == 'x') || (*p == 'X'))
211 if ((*p != '+') && (*p != '-'))
217 *height=(size_t) floor(InterpretLocaleValue(p,&p)+0.5);
222 if ((*p == '+') || (*p == '-'))
230 *x=(ssize_t) ceil(InterpretLocaleValue(p,&p)-0.5);
233 if ((*p == '+') || (*p == '-'))
241 *y=(ssize_t) ceil(InterpretLocaleValue(p,&p)-0.5);
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % G e t P a g e G e o m e t r y %
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % GetPageGeometry() replaces any page mneumonic with the equivalent size in
263 % The format of the GetPageGeometry method is:
265 % char *GetPageGeometry(const char *page_geometry)
267 % A description of each parameter follows.
269 % o page_geometry: Specifies a pointer to an array of characters. The
270 % string is either a Postscript page name (e.g. A4) or a postscript page
271 % geometry (e.g. 612x792+36+36).
274 MagickExport char *GetPageGeometry(const char *page_geometry)
279 { "4x6", "288x432" },
280 { "5x7", "360x504" },
281 { "7x9", "504x648" },
282 { "8x10", "576x720" },
283 { "9x11", "648x792" },
284 { "9x12", "648x864" },
285 { "10x13", "720x936" },
286 { "10x14", "720x1008" },
287 { "11x17", "792x1224" },
288 { "a0", "2384x3370" },
289 { "a1", "1684x2384" },
291 { "a2", "1191x1684" },
292 { "a3", "842x1191" },
294 { "a4smaLL", "595x842" },
300 { "archa", "648x864" },
301 { "archb", "864x1296" },
302 { "archC", "1296x1728" },
303 { "archd", "1728x2592" },
304 { "arche", "2592x3456" },
305 { "b0", "2920x4127" },
306 { "b1", "2064x2920" },
308 { "b2", "1460x2064" },
309 { "b3", "1032x1460" },
310 { "b4", "729x1032" },
316 { "c0", "2599x3676" },
317 { "c1", "1837x2599" },
318 { "c2", "1298x1837" },
319 { "c3", "918x1296" },
324 { "executive", "540x720" },
325 { "flsa", "612x936" },
326 { "flse", "612x936" },
327 { "folio", "612x936" },
328 { "halfletter", "396x612" },
329 { "isob0", "2835x4008" },
330 { "isob1", "2004x2835" },
331 { "isob10", "88x125" },
332 { "isob2", "1417x2004" },
333 { "isob3", "1001x1417" },
334 { "isob4", "709x1001" },
335 { "isob5", "499x709" },
336 { "isob6", "354x499" },
337 { "isob7", "249x354" },
338 { "isob8", "176x249" },
339 { "isob9", "125x176" },
340 { "jisb0", "1030x1456" },
341 { "jisb1", "728x1030" },
342 { "jisb2", "515x728" },
343 { "jisb3", "364x515" },
344 { "jisb4", "257x364" },
345 { "jisb5", "182x257" },
346 { "jisb6", "128x182" },
347 { "ledger", "1224x792" },
348 { "legal", "612x1008" },
349 { "letter", "612x792" },
350 { "lettersmaLL", "612x792" },
351 { "quarto", "610x780" },
352 { "statement", "396x612" },
353 { "tabloid", "792x1224" },
354 { (char *) NULL, (char *) NULL }
363 assert(page_geometry != (char *) NULL);
364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",page_geometry);
365 page=AcquireString(page_geometry);
366 for (i=0; *PageSizes[i] != (char *) NULL; i++)
367 if (LocaleNCompare(PageSizes[i][0],page,strlen(PageSizes[i][0])) == 0)
376 Replace mneumonic with the equivalent size in dots-per-inch.
378 (void) CopyMagickString(page,PageSizes[i][1],MaxTextExtent);
379 (void) ConcatenateMagickString(page,page_geometry+
380 strlen(PageSizes[i][0]),MaxTextExtent);
381 flags=GetGeometry(page,&geometry.x,&geometry.y,&geometry.width,
383 if ((flags & GreaterValue) == 0)
384 (void) ConcatenateMagickString(page,">",MaxTextExtent);
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 % G r a v i t y A d j u s t G e o m e t r y %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % GravityAdjustGeometry() adjusts the offset of a region with regard to the
401 % given: width, height and gravity; against which it is positioned.
403 % The region should also have an appropriate width and height to correctly
404 % set the right offset of the top left corner of the region.
406 % The format of the GravityAdjustGeometry method is:
408 % void GravityAdjustGeometry(const size_t width, const size_t height,
409 % const GravityType gravity,RectangleInfo *region);
411 % A description of each parameter follows:
413 % o width, height: the larger area the region is relative to
415 % o gravity: the edge/corner the current offset is relative to
417 % o region: The region requiring a offset adjustment relative to gravity
420 MagickExport void GravityAdjustGeometry(const size_t width,
421 const size_t height,const GravityType gravity,RectangleInfo *region)
423 if (region->height == 0)
424 region->height=height;
425 if (region->width == 0)
429 case NorthEastGravity:
431 case SouthEastGravity:
433 region->x=(ssize_t) (width-region->width-region->x);
441 region->x+=(ssize_t) (width/2-region->width/2);
445 case NorthWestGravity:
447 case SouthWestGravity:
453 case SouthWestGravity:
455 case SouthEastGravity:
457 region->y=(ssize_t) (height-region->height-region->y);
465 region->y+=(ssize_t) (height/2-region->height/2);
469 case NorthWestGravity:
471 case NorthEastGravity:
479 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483 + I s G e o m e t r y %
487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
489 % IsGeometry() returns MagickTrue if the geometry specification is valid.
490 % Examples are 100, 100x200, x200, 100x200+10+20, +10+20, 200%, 200x200!, etc.
492 % The format of the IsGeometry method is:
494 % MagickBooleanType IsGeometry(const char *geometry)
496 % A description of each parameter follows:
498 % o geometry: This string is the geometry specification.
501 MagickExport MagickBooleanType IsGeometry(const char *geometry)
509 if (geometry == (const char *) NULL)
511 flags=ParseGeometry(geometry,&geometry_info);
512 if (flags == NoValue)
513 flags=ParseGeometry(geometry+1,&geometry_info); /* i.e. +-4+-4 */
514 return(flags != NoValue ? MagickTrue : MagickFalse);
518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522 + I s S c e n e G e o m e t r y %
526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % IsSceneGeometry() returns MagickTrue if the geometry is a valid scene
529 % specification (e.g. [1], [1-9], [1,7,4]).
531 % The format of the IsSceneGeometry method is:
533 % MagickBooleanType IsSceneGeometry(const char *geometry,
534 % const MagickBooleanType pedantic)
536 % A description of each parameter follows:
538 % o geometry: This string is the geometry specification.
540 % o pedantic: A value other than 0 invokes a more restrictive set of
541 % conditions for a valid specification (e.g. [1], [1-4], [4-1]).
544 MagickExport MagickBooleanType IsSceneGeometry(const char *geometry,
545 const MagickBooleanType pedantic)
553 if (geometry == (const char *) NULL)
556 value=InterpretLocaleValue(geometry,&p);
560 if (strspn(geometry,"0123456789-, ") != strlen(geometry))
562 if ((pedantic != MagickFalse) && (strchr(geometry,',') != (char *) NULL))
568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % P a r s e A b s o l u t e G e o m e t r y %
576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 % ParseAbsoluteGeometry() returns a region as defined by the geometry string,
579 % without any modification by percentages or gravity.
581 % It currently just a wrapper around GetGeometry(), but may be expanded in
582 % the future to handle other positioning information.
584 % The format of the ParseAbsoluteGeometry method is:
586 % MagickStatusType ParseAbsoluteGeometry(const char *geometry,
587 % RectangleInfo *region_info)
589 % A description of each parameter follows:
591 % o geometry: The geometry string (e.g. "100x100+10+10").
593 % o region_info: the region as defined by the geometry string.
596 MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry,
597 RectangleInfo *region_info)
602 flags=GetGeometry(geometry,®ion_info->x,®ion_info->y,
603 ®ion_info->width,®ion_info->height);
608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
612 % P a r s e A f f i n e G e o m e t r y %
616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618 % ParseAffineGeometry() returns an affine matrix as defined by a string of 4
619 % to 6 comma/space separated floating point values.
621 % The affine matrix determinant is checked for validity of the values.
623 % The format of the ParseAffineGeometry method is:
625 % MagickStatusType ParseAffineGeometry(const char *geometry,
626 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
628 % A description of each parameter follows:
630 % o geometry: The geometry string (e.g. "1.0,0.0,0.0,1.0,3.2,1.2").
632 % o affine_matrix: the affine matrix as defined by the geometry string.
634 % o exception: return any errors or warnings in this structure.
637 MagickExport MagickStatusType ParseAffineGeometry(const char *geometry,
638 AffineMatrix *affine_matrix,ExceptionInfo *exception)
641 token[MaxTextExtent];
655 GetAffineMatrix(affine_matrix);
658 for (i=0; (*p != '\0') && (i < 6); i++)
660 GetMagickToken(p,&p,token);
662 GetMagickToken(p,&p,token);
667 affine_matrix->sx=InterpretLocaleValue(token,(char **) NULL);
672 affine_matrix->rx=InterpretLocaleValue(token,(char **) NULL);
677 affine_matrix->ry=InterpretLocaleValue(token,(char **) NULL);
682 affine_matrix->sy=InterpretLocaleValue(token,(char **) NULL);
687 affine_matrix->tx=InterpretLocaleValue(token,(char **) NULL);
693 affine_matrix->ty=InterpretLocaleValue(token,(char **) NULL);
699 determinant=(affine_matrix->sx*affine_matrix->sy-affine_matrix->rx*
701 if (fabs(determinant) < MagickEpsilon)
702 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
703 "InvalidGeometry","`%s'",geometry);
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712 % P a r s e G e o m e t r y %
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718 % ParseGeometry() parses a geometry specification and returns the sigma,
719 % rho, xi, and psi values. It also returns flags that indicates which
720 % of the four values (sigma, rho, xi, psi) were located in the string, and
721 % whether the xi or pi values are negative.
723 % In addition, it reports if there are any of meta characters (%, !, <, >, @,
724 % and ^) flags present. It does not report the location of the percentage
725 % relative to the values.
727 % The format of the ParseGeometry method is:
729 % MagickStatusType ParseGeometry(const char *geometry,
730 % GeometryInfo *geometry_info)
732 % A description of each parameter follows:
734 % o geometry: The geometry string (e.g. "100x100+10+10").
736 % o geometry_info: returns the parsed width/height/x/y in this structure.
739 MagickExport MagickStatusType ParseGeometry(const char *geometry,
740 GeometryInfo *geometry_info)
744 pedantic_geometry[MaxTextExtent],
754 Remove whitespaces meta characters from geometry specification.
756 assert(geometry_info != (GeometryInfo *) NULL);
758 if ((geometry == (char *) NULL) || (*geometry == '\0'))
760 if (strlen(geometry) >= (MaxTextExtent-1))
762 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
763 for (p=pedantic_geometry; *p != '\0'; )
765 if (isspace((int) ((unsigned char) *p)) != 0)
767 (void) CopyMagickString(p,p+1,MaxTextExtent);
775 (void) CopyMagickString(p,p+1,MaxTextExtent);
781 (void) CopyMagickString(p,p+1,MaxTextExtent);
787 (void) CopyMagickString(p,p+1,MaxTextExtent);
793 (void) CopyMagickString(p,p+1,MaxTextExtent);
799 (void) CopyMagickString(p,p+1,MaxTextExtent);
805 (void) CopyMagickString(p,p+1,MaxTextExtent);
811 (void) CopyMagickString(p,p+1,MaxTextExtent);
847 Parse rho, sigma, xi, psi, and optionally chi.
853 value=InterpretLocaleValue(p,&q);
854 if (LocaleNCompare(p,"0x",2) == 0)
855 value=(double) strtol(p,&q,10);
856 if ((((int) *q) == -41) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
857 (*q == '/') || (*q == ':') || (*q =='\0'))
863 if (LocaleNCompare(p,"0x",2) == 0)
864 value=(double) strtol(p,&p,10);
866 value=InterpretLocaleValue(p,&p);
870 geometry_info->rho=value;
874 if ((((int) *p) == -41) || (*p == 'x') || (*p == 'X') || (*p == ',') ||
875 (*p == '/') || (*p == ':'))
881 while (isspace((int) ((unsigned char) *p)) != 0)
883 if (((((int) *q) != -41) && (*q != 'x') && (*q != 'X')) ||
884 ((*p != '+') && (*p != '-')))
887 value=InterpretLocaleValue(p,&p);
891 geometry_info->sigma=value;
895 while (isspace((int) ((unsigned char) *p)) != 0)
897 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
902 if ((*p == ',') || (*p == '/') || (*p == ':'))
905 value=InterpretLocaleValue(p,&p);
911 geometry_info->xi=value;
913 while (isspace((int) ((unsigned char) *p)) != 0)
915 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
921 if ((*p == ',') || (*p == '/') || (*p == ':'))
924 value=InterpretLocaleValue(p,&p);
930 geometry_info->psi=value;
933 while (isspace((int) ((unsigned char) *p)) != 0)
935 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
941 if ((*p == ',') || (*p == '/') || (*p == ':'))
944 value=InterpretLocaleValue(p,&p);
950 geometry_info->chi=value;
954 if (strchr(pedantic_geometry,':') != (char *) NULL)
957 Normalize sampling factor (e.g. 4:2:2 => 2x1).
959 geometry_info->rho/=geometry_info->sigma;
960 geometry_info->sigma=1.0;
961 if (geometry_info->xi == 0.0)
962 geometry_info->sigma=2.0;
964 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
965 ((flags & PsiValue) == 0))
968 Support negative height values (e.g. 30x-20).
970 geometry_info->sigma=geometry_info->xi;
971 geometry_info->xi=0.0;
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983 % P a r s e G r a v i t y G e o m e t r y %
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 % ParseGravityGeometry() returns a region as defined by the geometry string
990 % with respect to the given image page (canvas) dimensions and the images
993 % This is typically used for specifing a area within a given image for
994 % cropping images to a smaller size, chopping out rows and or columns, or
995 % resizing and positioning overlay images.
997 % Percentages are relative to image size and not page size, and are set to
998 % nearest integer (pixel) size.
1000 % The format of the ParseGravityGeometry method is:
1002 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
1003 % RectangeInfo *region_info,ExceptionInfo *exception)
1005 % A description of each parameter follows:
1007 % o geometry: The geometry string (e.g. "100x100+10+10").
1009 % o region_info: the region as defined by the geometry string with respect
1010 % to the image dimensions and its gravity.
1012 % o exception: return any errors or warnings in this structure.
1015 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
1016 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1025 SetGeometry(image,region_info);
1026 if (image->page.width != 0)
1027 region_info->width=image->page.width;
1028 if (image->page.height != 0)
1029 region_info->height=image->page.height;
1030 flags=ParseAbsoluteGeometry(geometry,region_info);
1031 if (flags == NoValue)
1033 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1034 "InvalidGeometry","`%s'",geometry);
1037 if ((flags & PercentValue) != 0)
1049 Geometry is a percentage of the image size, not canvas size
1051 if (image->gravity != UndefinedGravity)
1052 flags|=XValue | YValue;
1053 status=ParseGeometry(geometry,&geometry_info);
1054 scale.x=geometry_info.rho;
1055 if ((status & RhoValue) == 0)
1057 scale.y=geometry_info.sigma;
1058 if ((status & SigmaValue) == 0)
1060 region_info->width=(size_t) floor((scale.x*image->columns/100.0)+0.5);
1061 region_info->height=(size_t) floor((scale.y*image->rows/100.0)+0.5);
1064 Adjust offset according to gravity setting.
1066 width=region_info->width;
1067 height=region_info->height;
1069 region_info->width=image->page.width | image->columns;
1071 region_info->height=image->page.height | image->rows;
1072 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1073 region_info->width=width;
1074 region_info->height=height;
1079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1083 + P a r s e M e t a G e o m e t r y %
1087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1090 % geometry is modified as determined by the meta characters: %, !, <, >, @,
1091 % and ^ in relation to image resizing.
1093 % Final image dimensions are adjusted so as to preserve the aspect ratio as
1094 % much as possible, while generating a integer (pixel) size, and fitting the
1095 % image within the specified geometry width and height.
1097 % Flags are interpreted...
1098 % % geometry size is given percentage of original image size
1099 % ! do not try to preserve aspect ratio
1100 % < only enlarge images smaller that geometry
1101 % > only shrink images larger than geometry
1102 % @ Fit image to contain at most this many pixels
1103 % ^ Contain the given geometry given, (minimal dimensions given)
1105 % The format of the ParseMetaGeometry method is:
1107 % MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1108 % ssize_t *y, size_t *width,size_t *height)
1110 % A description of each parameter follows:
1112 % o geometry: The geometry string (e.g. "100x100+10+10").
1114 % o x,y: The x and y offset, set according to the geometry specification.
1116 % o width,height: The width and height of original image, modified by
1117 % the given geometry specification.
1121 static inline size_t MagickMax(const size_t x,
1129 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1130 ssize_t *y,size_t *width,size_t *height)
1143 Ensure the image geometry is valid.
1145 assert(x != (ssize_t *) NULL);
1146 assert(y != (ssize_t *) NULL);
1147 assert(width != (size_t *) NULL);
1148 assert(height != (size_t *) NULL);
1149 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1151 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1153 Parse geometry using GetGeometry.
1155 SetGeometryInfo(&geometry_info);
1156 former_width=(*width);
1157 former_height=(*height);
1158 flags=GetGeometry(geometry,x,y,width,height);
1159 if ((flags & PercentValue) != 0)
1168 Geometry is a percentage of the image size.
1170 flags=ParseGeometry(geometry,&geometry_info);
1171 scale.x=geometry_info.rho;
1172 if ((flags & RhoValue) == 0)
1174 scale.y=geometry_info.sigma;
1175 if ((flags & SigmaValue) == 0)
1177 *width=(size_t) floor(scale.x*former_width/100.0+0.5);
1180 *height=(size_t) floor(scale.y*former_height/100.0+0.5);
1183 former_width=(*width);
1184 former_height=(*height);
1186 if (((flags & AspectValue) == 0) && ((*width != former_width) ||
1187 (*height != former_height)))
1193 Respect aspect ratio of the image.
1195 if ((former_width == 0) || (former_height == 0))
1198 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1200 scale_factor=(MagickRealType) *width/(MagickRealType) former_width;
1201 if ((flags & MinimumValue) == 0)
1203 if (scale_factor > ((MagickRealType) *height/(MagickRealType)
1205 scale_factor=(MagickRealType) *height/(MagickRealType)
1209 if (scale_factor < ((MagickRealType) *height/(MagickRealType)
1211 scale_factor=(MagickRealType) *height/(MagickRealType)
1215 if ((flags & RhoValue) != 0)
1217 scale_factor=(MagickRealType) *width/(MagickRealType)
1219 if (((flags & MinimumValue) != 0) &&
1220 (scale_factor < ((MagickRealType) *width/(MagickRealType)
1222 scale_factor=(MagickRealType) *width/(MagickRealType)
1227 scale_factor=(MagickRealType) *height/(MagickRealType)
1229 if (((flags & MinimumValue) != 0) &&
1230 (scale_factor < ((MagickRealType) *height/(MagickRealType)
1232 scale_factor=(MagickRealType) *height/(MagickRealType)
1235 *width=MagickMax((size_t) floor(scale_factor*former_width+0.5),1UL);
1236 *height=MagickMax((size_t) floor(scale_factor*former_height+0.5),1UL);
1238 if ((flags & GreaterValue) != 0)
1240 if (former_width < *width)
1241 *width=former_width;
1242 if (former_height < *height)
1243 *height=former_height;
1245 if ((flags & LessValue) != 0)
1247 if (former_width > *width)
1248 *width=former_width;
1249 if (former_height > *height)
1250 *height=former_height;
1252 if ((flags & AreaValue) != 0)
1262 Geometry is a maximum area in pixels.
1264 (void) ParseGeometry(geometry,&geometry_info);
1265 area=geometry_info.rho;
1266 distance=sqrt((double) former_width*former_height);
1267 scale.x=(double) former_width/(double) (distance/sqrt((double) area));
1268 scale.y=(double) former_height/(double) (distance/sqrt((double) area));
1269 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1271 *width=(size_t) (former_width/(distance/sqrt((double) area)));
1272 *height=(size_t) (former_height/(distance/sqrt((double) area)));
1274 former_width=(*width);
1275 former_height=(*height);
1281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1285 % P a r s e P a g e G e o m e t r y %
1289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1291 % ParsePageGeometry() returns a region as defined by the geometry string with
1292 % respect to the image page (canvas) dimensions.
1294 % WARNING: Percentage dimensions remain relative to the actual image
1295 % dimensions, and not canvas dimensions.
1297 % The format of the ParsePageGeometry method is:
1299 % MagickStatusType ParsePageGeometry(const Image *image,
1300 % const char *geometry,RectangeInfo *region_info,
1301 % ExceptionInfo *exception)
1303 % A description of each parameter follows:
1305 % o geometry: The geometry string (e.g. "100x100+10+10").
1307 % o region_info: the region as defined by the geometry string with
1308 % respect to the image and its gravity.
1310 % o exception: return any errors or warnings in this structure.
1313 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1314 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1319 SetGeometry(image,region_info);
1320 if (image->page.width != 0)
1321 region_info->width=image->page.width;
1322 if (image->page.height != 0)
1323 region_info->height=image->page.height;
1324 flags=ParseAbsoluteGeometry(geometry,region_info);
1325 if (flags == NoValue)
1327 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1328 "InvalidGeometry","`%s'",geometry);
1331 if ((flags & PercentValue) != 0)
1333 region_info->width=image->columns;
1334 region_info->height=image->rows;
1336 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1337 ®ion_info->width,®ion_info->height);
1342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1346 % P a r s e R e g i o n G e o m e t r y %
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1352 % ParseRegionGeometry() returns a region as defined by the geometry string
1353 % with respect to the image dimensions and aspect ratio.
1355 % This is basically a wrapper around ParseMetaGeometry. This is typically
1356 % used to parse a geometry string to work out the final integer dimensions
1357 % for image resizing.
1359 % The format of the ParseRegionGeometry method is:
1361 % MagickStatusType ParseRegionGeometry(const Image *image,
1362 % const char *geometry,RectangeInfo *region_info,
1363 % ExceptionInfo *exception)
1365 % A description of each parameter follows:
1367 % o geometry: The geometry string (e.g. "100x100+10+10").
1369 % o region_info: the region as defined by the geometry string.
1371 % o exception: return any errors or warnings in this structure.
1374 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1375 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1380 SetGeometry(image,region_info);
1381 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1382 ®ion_info->width,®ion_info->height);
1383 if (flags == NoValue)
1384 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1385 "InvalidGeometry","`%s'",geometry);
1390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1394 % S e t G e o m e t r y %
1398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1400 % SetGeometry() sets the geometry to its default values.
1402 % The format of the SetGeometry method is:
1404 % SetGeometry(const Image *image,RectangleInfo *geometry)
1406 % A description of each parameter follows:
1408 % o image: the image.
1410 % o geometry: the geometry.
1413 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1415 assert(image != (Image *) NULL);
1416 assert(image->signature == MagickSignature);
1417 if (image->debug != MagickFalse)
1418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1419 assert(geometry != (RectangleInfo *) NULL);
1420 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1421 geometry->width=image->columns;
1422 geometry->height=image->rows;
1426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430 % S e t G e o m e t r y I n f o %
1434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1438 % The format of the SetGeometryInfo method is:
1440 % SetGeometryInfo(GeometryInfo *geometry_info)
1442 % A description of each parameter follows:
1444 % o geometry_info: the geometry info structure.
1447 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1449 assert(geometry_info != (GeometryInfo *) NULL);
1450 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1451 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));