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 "magick/studio.h"
43 #include "magick/constitute.h"
44 #include "magick/draw.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/geometry.h"
48 #include "magick/memory_.h"
49 #include "magick/string_.h"
50 #include "magick/string-private.h"
51 #include "magick/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,ssize_t *y,
86 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)
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.
193 if (LocaleNCompare(p,"0x",2) == 0)
194 value=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(strtod(p,&p)+0.5);
208 if ((((int) *p) == -41) || (*p == 'x') || (*p == 'X'))
211 if ((*p != '+') && (*p != '-'))
217 *height=(size_t) floor(strtod(p,&p)+0.5);
222 if ((*p == '+') || (*p == '-'))
230 *x=(ssize_t) ceil(strtod(p,&p)-0.5);
233 if ((*p == '+') || (*p == '-'))
241 *y=(ssize_t) ceil(strtod(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.
270 % The string is either a Postscript page name (e.g. A4) or a postscript
271 % page 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 %
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % GravityAdjustGeometry() adjusts the offset of a region with regard to the
402 % given: width, height and gravity; against which it is positioned.
404 % The region should also have an appropriate width and height to correctly
405 % set the right offset of the top left corner of the region.
407 % The format of the GravityAdjustGeometry method is:
409 % void GravityAdjustGeometry(const size_t width,
410 % const size_t height,const GravityType gravity,
411 % RectangleInfo *region);
413 % A description of each parameter follows:
415 % o width, height: the larger area the region is relative to
417 % o gravity: the edge/corner the current offset is relative to
419 % o region: The region requiring a offset adjustment relative to gravity
422 MagickExport void GravityAdjustGeometry(const size_t width,
423 const size_t height,const GravityType gravity,RectangleInfo *region)
425 if (region->height == 0)
426 region->height=height;
427 if (region->width == 0)
431 case NorthEastGravity:
433 case SouthEastGravity:
435 region->x=(ssize_t) (width-region->width-region->x);
443 region->x+=(ssize_t) (width/2-region->width/2);
447 case NorthWestGravity:
449 case SouthWestGravity:
455 case SouthWestGravity:
457 case SouthEastGravity:
459 region->y=(ssize_t) (height-region->height-region->y);
467 region->y+=(ssize_t) (height/2-region->height/2);
471 case NorthWestGravity:
473 case NorthEastGravity:
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 + I s G e o m e t r y %
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 % IsGeometry() returns MagickTrue if the geometry specification is valid.
492 % Examples are 100, 100x200, x200, 100x200+10+20, +10+20, 200%, 200x200!, etc.
494 % The format of the IsGeometry method is:
496 % MagickBooleanType IsGeometry(const char *geometry)
498 % A description of each parameter follows:
500 % o geometry: This string is the geometry specification.
503 MagickExport MagickBooleanType IsGeometry(const char *geometry)
511 if (geometry == (const char *) NULL)
513 flags=ParseGeometry(geometry,&geometry_info);
514 if (flags == NoValue)
515 flags=ParseGeometry(geometry+1,&geometry_info); /* i.e. +-4+-4 */
516 return(flags != NoValue ? MagickTrue : MagickFalse);
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 + I s S c e n e G e o m e t r y %
528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530 % IsSceneGeometry() returns MagickTrue if the geometry is a valid scene
531 % specification (e.g. [1], [1-9], [1,7,4]).
533 % The format of the IsSceneGeometry method is:
535 % MagickBooleanType IsSceneGeometry(const char *geometry,
536 % const MagickBooleanType pedantic)
538 % A description of each parameter follows:
540 % o geometry: This string is the geometry specification.
542 % o pedantic: A value other than 0 invokes a more restrictive set of
543 % conditions for a valid specification (e.g. [1], [1-4], [4-1]).
546 MagickExport MagickBooleanType IsSceneGeometry(const char *geometry,
547 const MagickBooleanType pedantic)
555 if (geometry == (const char *) NULL)
558 value=strtod(geometry,&p);
562 if (strspn(geometry,"0123456789-, ") != strlen(geometry))
564 if ((pedantic != MagickFalse) && (strchr(geometry,',') != (char *) NULL))
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 % P a r s e A b s o l u t e G e o m e t r y %
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580 % ParseAbsoluteGeometry() returns a region as defined by the geometry string.
582 % The format of the ParseAbsoluteGeometry method is:
584 % MagickStatusType ParseAbsoluteGeometry(const char *geometry,
585 % RectangleInfo *region_info)
587 % A description of each parameter follows:
589 % o geometry: The geometry (e.g. 100x100+10+10).
591 % o region_info: the region as defined by the geometry string.
594 MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry,
595 RectangleInfo *region_info)
600 flags=GetGeometry(geometry,®ion_info->x,®ion_info->y,
601 ®ion_info->width,®ion_info->height);
606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610 % P a r s e A f f i n e G e o m e t r y %
614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
616 % ParseAffineGeometry() returns an affine matrix as defined by the geometry
619 % The format of the ParseAffineGeometry method is:
621 % MagickStatusType ParseAffineGeometry(const char *geometry,
622 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
624 % A description of each parameter follows:
626 % o geometry: The geometry (e.g. 1.0,0.0,0.0,1.0,3.2,1.2).
628 % o affine_matrix: the affine matrix as defined by the geometry string.
630 % o exception: return any errors or warnings in this structure.
633 MagickExport MagickStatusType ParseAffineGeometry(const char *geometry,
634 AffineMatrix *affine_matrix,ExceptionInfo *exception)
637 token[MaxTextExtent];
651 GetAffineMatrix(affine_matrix);
654 for (i=0; (*p != '\0') && (i < 6); i++)
656 GetMagickToken(p,&p,token);
658 GetMagickToken(p,&p,token);
661 case 0: affine_matrix->sx=StringToDouble(token); break;
662 case 1: affine_matrix->rx=StringToDouble(token); break;
663 case 2: affine_matrix->ry=StringToDouble(token); break;
664 case 3: affine_matrix->sy=StringToDouble(token); break;
665 case 4: affine_matrix->tx=StringToDouble(token); flags|=XValue; break;
666 case 5: affine_matrix->ty=StringToDouble(token); flags|=YValue; break;
669 determinant=(affine_matrix->sx*affine_matrix->sy-affine_matrix->rx*
671 if (fabs(determinant) < MagickEpsilon)
672 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
673 "InvalidGeometry","`%s'",geometry);
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % P a r s e G e o m e t r y %
686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 % ParseGeometry() parses a geometry specification and returns the sigma,
689 % rho, xi, and psi values. It also returns flags that indicates which
690 % of the four values (sigma, rho, xi, psi) were located in the string, and
691 % whether the xi or pi values are negative. In addition, there are flags to
692 % report any meta characters (%, !, <, or >).
694 % The format of the ParseGeometry method is:
696 % MagickStatusType ParseGeometry(const char *geometry,
697 % GeometryInfo *geometry_info)
699 % A description of each parameter follows:
701 % o geometry: The geometry.
703 % o geometry_info: returns the parsed width/height/x/y in this structure.
706 MagickExport MagickStatusType ParseGeometry(const char *geometry,
707 GeometryInfo *geometry_info)
711 pedantic_geometry[MaxTextExtent],
721 Remove whitespaces meta characters from geometry specification.
723 assert(geometry_info != (GeometryInfo *) NULL);
725 if ((geometry == (char *) NULL) || (*geometry == '\0'))
727 if (strlen(geometry) >= MaxTextExtent)
729 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
730 for (p=pedantic_geometry; *p != '\0'; )
732 if (isspace((int) ((unsigned char) *p)) != 0)
734 (void) CopyMagickString(p,p+1,MaxTextExtent);
742 (void) CopyMagickString(p,p+1,MaxTextExtent);
748 (void) CopyMagickString(p,p+1,MaxTextExtent);
754 (void) CopyMagickString(p,p+1,MaxTextExtent);
760 (void) CopyMagickString(p,p+1,MaxTextExtent);
766 (void) CopyMagickString(p,p+1,MaxTextExtent);
772 (void) CopyMagickString(p,p+1,MaxTextExtent);
778 (void) CopyMagickString(p,p+1,MaxTextExtent);
814 Parse rho, sigma, xi, psi, and optionally chi.
821 if (LocaleNCompare(p,"0x",2) == 0)
822 value=(double) strtol(p,&q,10);
823 if ((((int) *q) == -41) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
824 (*q == '/') || (*q == ':') || (*q =='\0'))
830 if (LocaleNCompare(p,"0x",2) == 0)
831 value=(double) strtol(p,&p,10);
837 geometry_info->rho=value;
841 if ((((int) *p) == -41) || (*p == 'x') || (*p == 'X') || (*p == ',') ||
842 (*p == '/') || (*p == ':'))
848 while (isspace((int) ((unsigned char) *p)) != 0)
850 if (((((int) *q) != -41) && (*q != 'x') && (*q != 'X')) ||
851 ((*p != '+') && (*p != '-')))
858 geometry_info->sigma=value;
862 while (isspace((int) ((unsigned char) *p)) != 0)
864 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
869 if ((*p == ',') || (*p == '/') || (*p == ':'))
878 geometry_info->xi=value;
880 while (isspace((int) ((unsigned char) *p)) != 0)
882 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
888 if ((*p == ',') || (*p == '/') || (*p == ':'))
897 geometry_info->psi=value;
900 while (isspace((int) ((unsigned char) *p)) != 0)
902 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
908 if ((*p == ',') || (*p == '/') || (*p == ':'))
917 geometry_info->chi=value;
921 if (strchr(pedantic_geometry,':') != (char *) NULL)
924 Normalize sampling factor (e.g. 4:2:2 => 2x1).
926 geometry_info->rho/=geometry_info->sigma;
927 geometry_info->sigma=1.0;
928 if (geometry_info->xi == 0.0)
929 geometry_info->sigma=2.0;
931 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
932 ((flags & PsiValue) == 0))
935 Support negative height values (e.g. 30x-20).
937 geometry_info->sigma=geometry_info->xi;
938 geometry_info->xi=0.0;
946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 % P a r s e G r a v i t y G e o m e t r y %
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 % ParseGravityGeometry() returns a region as defined by the geometry string
957 % with respect to the image dimensions and its gravity.
959 % The format of the ParseGravityGeometry method is:
961 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
962 % RectangeInfo *region_info,ExceptionInfo *exception)
964 % A description of each parameter follows:
966 % o geometry: The geometry (e.g. 100x100+10+10).
968 % o region_info: the region as defined by the geometry string with
969 % respect to the image dimensions and its gravity.
971 % o exception: return any errors or warnings in this structure.
974 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
975 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
984 SetGeometry(image,region_info);
985 if (image->page.width != 0)
986 region_info->width=image->page.width;
987 if (image->page.height != 0)
988 region_info->height=image->page.height;
989 flags=ParseAbsoluteGeometry(geometry,region_info);
990 if (flags == NoValue)
992 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
993 "InvalidGeometry","`%s'",geometry);
996 if ((flags & PercentValue) != 0)
1008 Geometry is a percentage of the image size.
1010 if (image->gravity != UndefinedGravity)
1011 flags|=XValue | YValue;
1012 status=ParseGeometry(geometry,&geometry_info);
1013 scale.x=geometry_info.rho;
1014 if ((status & RhoValue) == 0)
1016 scale.y=geometry_info.sigma;
1017 if ((status & SigmaValue) == 0)
1019 region_info->width=(size_t) floor((scale.x*image->columns/100.0)+
1021 region_info->height=(size_t) floor((scale.y*image->rows/100.0)+
1025 Adjust offset according to gravity setting.
1027 width=region_info->width;
1028 height=region_info->height;
1030 region_info->width=image->page.width | image->columns;
1032 region_info->height=image->page.height | image->rows;
1033 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1034 region_info->width=width;
1035 region_info->height=height;
1040 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1044 + P a r s e M e t a G e o m e t r y %
1048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1051 % geometry is modified as determined by the meta characters: %, !, <, >,
1054 % The format of the ParseMetaGeometry method is:
1056 % MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,ssize_t *y,
1057 % size_t *width,size_t *height)
1059 % A description of each parameter follows:
1061 % o geometry: The geometry.
1063 % o x,y: The x and y offset as determined by the geometry specification.
1065 % o width,height: The width and height as determined by the geometry
1070 static inline size_t MagickMax(const size_t x,
1078 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1079 ssize_t *y,size_t *width,size_t *height)
1092 Ensure the image geometry is valid.
1094 assert(x != (ssize_t *) NULL);
1095 assert(y != (ssize_t *) NULL);
1096 assert(width != (size_t *) NULL);
1097 assert(height != (size_t *) NULL);
1098 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1100 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1102 Parse geometry using GetGeometry.
1104 SetGeometryInfo(&geometry_info);
1105 former_width=(*width);
1106 former_height=(*height);
1107 flags=GetGeometry(geometry,x,y,width,height);
1108 if ((flags & PercentValue) != 0)
1117 Geometry is a percentage of the image size.
1119 flags=ParseGeometry(geometry,&geometry_info);
1120 scale.x=geometry_info.rho;
1121 if ((flags & RhoValue) == 0)
1123 scale.y=geometry_info.sigma;
1124 if ((flags & SigmaValue) == 0)
1126 *width=(size_t) floor(scale.x*former_width/100.0+0.5);
1129 *height=(size_t) floor(scale.y*former_height/100.0+0.5);
1132 former_width=(*width);
1133 former_height=(*height);
1135 if (((flags & AspectValue) == 0) && ((*width != former_width) ||
1136 (*height != former_height)))
1142 Respect aspect ratio of the image.
1144 if ((former_width == 0) || (former_height == 0))
1147 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1149 scale_factor=(MagickRealType) *width/(MagickRealType) former_width;
1150 if ((flags & MinimumValue) == 0)
1152 if (scale_factor > ((MagickRealType) *height/
1153 (MagickRealType) former_height))
1154 scale_factor=(MagickRealType) *height/(MagickRealType)
1158 if (scale_factor < ((MagickRealType) *height/
1159 (MagickRealType) former_height))
1160 scale_factor=(MagickRealType) *height/(MagickRealType)
1164 if ((flags & RhoValue) != 0)
1166 scale_factor=(MagickRealType) *width/(MagickRealType)
1168 if (((flags & MinimumValue) != 0) &&
1169 (scale_factor < ((MagickRealType) *width/
1170 (MagickRealType) former_height)))
1171 scale_factor=(MagickRealType) *width/(MagickRealType)
1176 scale_factor=(MagickRealType) *height/(MagickRealType)
1178 if (((flags & MinimumValue) != 0) &&
1179 (scale_factor < ((MagickRealType) *height/
1180 (MagickRealType) former_width)))
1181 scale_factor=(MagickRealType) *height/(MagickRealType)
1184 *width=MagickMax((size_t) floor(scale_factor*former_width+0.5),
1186 *height=MagickMax((size_t) floor(scale_factor*former_height+0.5),
1189 if ((flags & GreaterValue) != 0)
1191 if (former_width < *width)
1192 *width=former_width;
1193 if (former_height < *height)
1194 *height=former_height;
1196 if ((flags & LessValue) != 0)
1198 if (former_width > *width)
1199 *width=former_width;
1200 if (former_height > *height)
1201 *height=former_height;
1203 if ((flags & AreaValue) != 0)
1213 Geometry is a maximum area in pixels.
1215 (void) ParseGeometry(geometry,&geometry_info);
1216 area=geometry_info.rho;
1217 distance=sqrt((double) former_width*former_height);
1218 scale.x=(double) former_width/(double) (distance/sqrt((double) area));
1219 scale.y=(double) former_height/(double) (distance/sqrt((double) area));
1220 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1222 *width=(size_t) (former_width/(distance/sqrt((double) area)));
1223 *height=(size_t) (former_height/(distance/
1224 sqrt((double) area)));
1226 former_width=(*width);
1227 former_height=(*height);
1233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237 % P a r s e P a g e G e o m e t r y %
1241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1243 % ParsePageGeometry() returns a region as defined by the geometry string with
1244 % respect to the image dimensions.
1246 % The format of the ParsePageGeometry method is:
1248 % MagickStatusType ParsePageGeometry(const Image *image,
1249 % const char *geometry,RectangeInfo *region_info,
1250 % ExceptionInfo *exception)
1252 % A description of each parameter follows:
1254 % o geometry: The geometry (e.g. 100x100+10+10).
1256 % o region_info: the region as defined by the geometry string with
1257 % respect to the image and its gravity.
1259 % o exception: return any errors or warnings in this structure.
1262 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1263 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1268 SetGeometry(image,region_info);
1269 if (image->page.width != 0)
1270 region_info->width=image->page.width;
1271 if (image->page.height != 0)
1272 region_info->height=image->page.height;
1273 flags=ParseAbsoluteGeometry(geometry,region_info);
1274 if (flags == NoValue)
1276 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1277 "InvalidGeometry","`%s'",geometry);
1280 if ((flags & PercentValue) != 0)
1282 region_info->width=image->columns;
1283 region_info->height=image->rows;
1285 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1286 ®ion_info->width,®ion_info->height);
1291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1295 % P a r s e R e g i o n G e o m e t r y %
1299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % ParseRegionGeometry() returns a region as defined by the geometry string
1302 % with respect to the image dimensions and aspect ratio.
1304 % The format of the ParseRegionGeometry method is:
1306 % MagickStatusType ParseRegionGeometry(const Image *image,
1307 % const char *geometry,RectangeInfo *region_info,
1308 % ExceptionInfo *exception)
1310 % A description of each parameter follows:
1312 % o geometry: The geometry (e.g. 100x100+10+10).
1314 % o region_info: the region as defined by the geometry string.
1316 % o exception: return any errors or warnings in this structure.
1319 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1320 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1325 SetGeometry(image,region_info);
1326 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1327 ®ion_info->width,®ion_info->height);
1328 if (flags == NoValue)
1329 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1330 "InvalidGeometry","`%s'",geometry);
1335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1339 % S e t G e o m e t r y %
1343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345 % SetGeometry() sets the geometry to its default values.
1347 % The format of the SetGeometry method is:
1349 % SetGeometry(const Image *image,RectangleInfo *geometry)
1351 % A description of each parameter follows:
1353 % o image: the image.
1355 % o geometry: the geometry.
1358 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1360 assert(image != (Image *) NULL);
1361 assert(image->signature == MagickSignature);
1362 if (image->debug != MagickFalse)
1363 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1364 assert(geometry != (RectangleInfo *) NULL);
1365 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1366 geometry->width=image->columns;
1367 geometry->height=image->rows;
1371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375 % S e t G e o m e t r y I n f o %
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1383 % The format of the SetGeometryInfo method is:
1385 % SetGeometryInfo(GeometryInfo *geometry_info)
1387 % A description of each parameter follows:
1389 % o geometry_info: the geometry info structure.
1392 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1394 assert(geometry_info != (GeometryInfo *) NULL);
1395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1396 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));