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-2009 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/token.h"
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 % G e t G e o m e t r y %
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 % GetGeometry() parses a geometry specification and returns the width,
64 % height, x, and y values. It also returns flags that indicates which
65 % of the four values (width, height, x, y) were located in the string, and
66 % whether the x or y values are negative. In addition, there are flags to
67 % report any meta characters (%, !, <, or >).
69 % The format of the GetGeometry method is:
71 % MagickStatusType GetGeometry(const char *geometry,long *x,long *y,
72 % unsigned long *width,unsigned long *height)
74 % A description of each parameter follows:
76 % o geometry: The geometry.
78 % o x,y: The x and y offset as determined by the geometry specification.
80 % o width,height: The width and height as determined by the geometry
84 MagickExport MagickStatusType GetGeometry(const char *geometry,long *x,long *y,
85 unsigned long *width,unsigned long *height)
89 pedantic_geometry[MaxTextExtent],
99 Remove whitespace and meta characters from geometry specification.
102 if ((geometry == (char *) NULL) || (*geometry == '\0'))
104 if (strlen(geometry) >= MaxTextExtent)
106 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
107 for (p=pedantic_geometry; *p != '\0'; )
109 if (isspace((int) ((unsigned char) *p)) != 0)
111 (void) CopyMagickString(p,p+1,MaxTextExtent);
119 (void) CopyMagickString(p,p+1,MaxTextExtent);
125 (void) CopyMagickString(p,p+1,MaxTextExtent);
131 (void) CopyMagickString(p,p+1,MaxTextExtent);
137 (void) CopyMagickString(p,p+1,MaxTextExtent);
143 (void) CopyMagickString(p,p+1,MaxTextExtent);
149 (void) CopyMagickString(p,p+1,MaxTextExtent);
155 (void) CopyMagickString(p,p+1,MaxTextExtent);
184 Parse width, height, x, and y.
191 if (LocaleNCompare(p,"0x",2) == 0)
192 value=(double) strtol(p,&q,10);
193 if (((int) *q == -41) || (*q == 'x') || (*q == 'X') || (*q == '\0'))
199 if (LocaleNCompare(p,"0x",2) == 0)
200 *width=(unsigned long) strtol(p,&p,10);
202 *width=(unsigned long) floor(strtod(p,&p)+0.5);
206 if (((int) *p == -41) || (*p == 'x') || (*p == 'X'))
209 if ((*p != '+') && (*p != '-'))
215 *height=(unsigned long) floor(strtod(p,&p)+0.5);
220 if ((*p == '+') || (*p == '-'))
228 *x=(long) ceil(strtod(p,&p)-0.5);
231 if ((*p == '+') || (*p == '-'))
239 *y=(long) ceil(strtod(p,&p)-0.5);
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % G e t P a g e G e o m e t r y %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % GetPageGeometry() replaces any page mneumonic with the equivalent size in
261 % The format of the GetPageGeometry method is:
263 % char *GetPageGeometry(const char *page_geometry)
265 % A description of each parameter follows.
267 % o page_geometry: Specifies a pointer to an array of characters.
268 % The string is either a Postscript page name (e.g. A4) or a postscript
269 % page geometry (e.g. 612x792+36+36).
272 MagickExport char *GetPageGeometry(const char *page_geometry)
277 { "4x6", "288x432" },
278 { "5x7", "360x504" },
279 { "7x9", "504x648" },
280 { "8x10", "576x720" },
281 { "9x11", "648x792" },
282 { "9x12", "648x864" },
283 { "10x13", "720x936" },
284 { "10x14", "720x1008" },
285 { "11x17", "792x1224" },
286 { "a0", "2384x3370" },
287 { "a1", "1684x2384" },
289 { "a2", "1191x1684" },
290 { "a3", "842x1191" },
292 { "a4smaLL", "595x842" },
298 { "archa", "648x864" },
299 { "archb", "864x1296" },
300 { "archC", "1296x1728" },
301 { "archd", "1728x2592" },
302 { "arche", "2592x3456" },
303 { "b0", "2920x4127" },
304 { "b1", "2064x2920" },
306 { "b2", "1460x2064" },
307 { "b3", "1032x1460" },
308 { "b4", "729x1032" },
314 { "c0", "2599x3676" },
315 { "c1", "1837x2599" },
316 { "c2", "1298x1837" },
317 { "c3", "918x1296" },
322 { "executive", "540x720" },
323 { "flsa", "612x936" },
324 { "flse", "612x936" },
325 { "folio", "612x936" },
326 { "halfletter", "396x612" },
327 { "isob0", "2835x4008" },
328 { "isob1", "2004x2835" },
329 { "isob10", "88x125" },
330 { "isob2", "1417x2004" },
331 { "isob3", "1001x1417" },
332 { "isob4", "709x1001" },
333 { "isob5", "499x709" },
334 { "isob6", "354x499" },
335 { "isob7", "249x354" },
336 { "isob8", "176x249" },
337 { "isob9", "125x176" },
338 { "jisb0", "1030x1456" },
339 { "jisb1", "728x1030" },
340 { "jisb2", "515x728" },
341 { "jisb3", "364x515" },
342 { "jisb4", "257x364" },
343 { "jisb5", "182x257" },
344 { "jisb6", "128x182" },
345 { "ledger", "1224x792" },
346 { "legal", "612x1008" },
347 { "letter", "612x792" },
348 { "lettersmaLL", "612x792" },
349 { "quarto", "610x780" },
350 { "statement", "396x612" },
351 { "tabloid", "792x1224" },
352 { (char *) NULL, (char *) NULL }
361 assert(page_geometry != (char *) NULL);
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",page_geometry);
363 page=AcquireString(page_geometry);
364 for (i=0; *PageSizes[i] != (char *) NULL; i++)
365 if (LocaleNCompare(PageSizes[i][0],page,strlen(PageSizes[i][0])) == 0)
374 Replace mneumonic with the equivalent size in dots-per-inch.
376 (void) CopyMagickString(page,PageSizes[i][1],MaxTextExtent);
377 (void) ConcatenateMagickString(page,page_geometry+
378 strlen(PageSizes[i][0]),MaxTextExtent);
379 flags=GetGeometry(page,&geometry.x,&geometry.y,&geometry.width,
381 if ((flags & GreaterValue) == 0)
382 (void) ConcatenateMagickString(page,">",MaxTextExtent);
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 % G r a v i t y A d j u s t G e o m e t r y %
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % GravityAdjustGeometry() adjusts the offset of a region with regard to the
400 % given: width, height and gravity; against which it is positioned.
402 % The region should also have an appropriate width and height to correctly
403 % set the right offset of the top left corner of the region.
405 % The format of the GravityAdjustGeometry method is:
407 % void GravityAdjustGeometry(const unsigned long width,
408 % const unsigned long height,const GravityType gravity,
409 % 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 unsigned long width,
421 const unsigned long 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=(long) (width-region->width-region->x);
441 region->x+=(long) (width/2-region->width/2);
445 case NorthWestGravity:
447 case SouthWestGravity:
453 case SouthWestGravity:
455 case SouthEastGravity:
457 region->y=(long) (height-region->height-region->y);
465 region->y+=(long) (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=strtod(geometry,&p);
559 if (strspn(geometry,"0123456789-, ") != strlen(geometry))
561 if ((pedantic != MagickFalse) && (strchr(geometry,',') != (char *) NULL))
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % P a r s e A b s o l u t e G e o m e t r y %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % ParseAbsoluteGeometry() returns a region as defined by the geometry string.
579 % The format of the ParseAbsoluteGeometry method is:
581 % MagickStatusType ParseAbsoluteGeometry(const char *geometry,
582 % RectangleInfo *region_info)
584 % A description of each parameter follows:
586 % o geometry: The geometry (e.g. 100x100+10+10).
588 % o region_info: the region as defined by the geometry string.
591 MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry,
592 RectangleInfo *region_info)
597 flags=GetGeometry(geometry,®ion_info->x,®ion_info->y,
598 ®ion_info->width,®ion_info->height);
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % P a r s e A f f i n e G e o m e t r y %
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % ParseAffineGeometry() returns an affine matrix as defined by the geometry
616 % The format of the ParseAffineGeometry method is:
618 % MagickStatusType ParseAffineGeometry(const char *geometry,
619 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
621 % A description of each parameter follows:
623 % o geometry: The geometry (e.g. 1.0,0.0,0.0,1.0,3.2,1.2).
625 % o affine_matrix: the affine matrix as defined by the geometry string.
627 % o exception: return any errors or warnings in this structure.
630 MagickExport MagickStatusType ParseAffineGeometry(const char *geometry,
631 AffineMatrix *affine_matrix,ExceptionInfo *exception)
634 token[MaxTextExtent];
648 GetAffineMatrix(affine_matrix);
651 for (i=0; (*p != '\0') && (i < 6); i++)
653 GetMagickToken(p,&p,token);
655 GetMagickToken(p,&p,token);
658 case 0: affine_matrix->sx=atof(token); break;
659 case 1: affine_matrix->rx=atof(token); break;
660 case 2: affine_matrix->ry=atof(token); break;
661 case 3: affine_matrix->sy=atof(token); break;
662 case 4: affine_matrix->tx=atof(token); flags|=XValue; break;
663 case 5: affine_matrix->ty=atof(token); flags|=YValue; break;
666 determinant=(affine_matrix->sx*affine_matrix->sy-affine_matrix->rx*
668 if (fabs(determinant) < MagickEpsilon)
669 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
670 "InvalidGeometry","`%s'",geometry);
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % P a r s e G e o m e t r y %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % ParseGeometry() parses a geometry specification and returns the sigma,
686 % rho, xi, and psi values. It also returns flags that indicates which
687 % of the four values (sigma, rho, xi, psi) were located in the string, and
688 % whether the xi or pi values are negative. In addition, there are flags to
689 % report any meta characters (%, !, <, or >).
691 % The format of the ParseGeometry method is:
693 % MagickStatusType ParseGeometry(const char *geometry,
694 % GeometryInfo *geometry_info)
696 % A description of each parameter follows:
698 % o geometry: The geometry.
700 % o geometry_info: returns the parsed width/height/x/y in this structure.
703 MagickExport MagickStatusType ParseGeometry(const char *geometry,
704 GeometryInfo *geometry_info)
708 pedantic_geometry[MaxTextExtent],
718 Remove whitespaces meta characters from geometry specification.
720 assert(geometry_info != (GeometryInfo *) NULL);
722 if ((geometry == (char *) NULL) || (*geometry == '\0'))
724 if (strlen(geometry) >= MaxTextExtent)
726 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
727 for (p=pedantic_geometry; *p != '\0'; )
729 if (isspace((int) ((unsigned char) *p)) != 0)
731 (void) CopyMagickString(p,p+1,MaxTextExtent);
739 (void) CopyMagickString(p,p+1,MaxTextExtent);
745 (void) CopyMagickString(p,p+1,MaxTextExtent);
751 (void) CopyMagickString(p,p+1,MaxTextExtent);
757 (void) CopyMagickString(p,p+1,MaxTextExtent);
763 (void) CopyMagickString(p,p+1,MaxTextExtent);
769 (void) CopyMagickString(p,p+1,MaxTextExtent);
775 (void) CopyMagickString(p,p+1,MaxTextExtent);
811 Parse rho, sigma, xi, psi, and optionally chi.
818 if (LocaleNCompare(p,"0x",2) == 0)
819 value=(double) strtol(p,&q,10);
820 if (((int) *q == -41) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
821 (*q == '/') || (*q == ':') || (*q =='\0'))
827 if (LocaleNCompare(p,"0x",2) == 0)
828 value=(double) strtol(p,&p,10);
834 geometry_info->rho=value;
838 if (((int) *p == -41) || (*p == 'x') || (*p == 'X') || (*p == ',') ||
839 (*p == '/') || (*p == ':'))
845 while (isspace((int) ((unsigned char) *p)) != 0)
847 if ((((int) *q != -41) && (*q != 'x') && (*q != 'X')) ||
848 ((*p != '+') && (*p != '-')))
855 geometry_info->sigma=value;
859 while (isspace((int) ((unsigned char) *p)) != 0)
861 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
866 if ((*p == ',') || (*p == '/') || (*p == ':'))
875 geometry_info->xi=value;
877 while (isspace((int) ((unsigned char) *p)) != 0)
879 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
885 if ((*p == ',') || (*p == '/') || (*p == ':'))
894 geometry_info->psi=value;
897 while (isspace((int) ((unsigned char) *p)) != 0)
899 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
905 if ((*p == ',') || (*p == '/') || (*p == ':'))
914 geometry_info->chi=value;
918 if (strchr(pedantic_geometry,':') != (char *) NULL)
921 Normalize sampling factor (e.g. 4:2:2 => 2x1).
923 geometry_info->rho/=geometry_info->sigma;
924 geometry_info->sigma=1.0;
925 if (geometry_info->xi == 0.0)
926 geometry_info->sigma=2.0;
928 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
929 ((flags & PsiValue) == 0))
932 Support negative height values (e.g. 30x-20).
934 geometry_info->sigma=geometry_info->xi;
935 geometry_info->xi=0.0;
943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947 % P a r s e G r a v i t y G e o m e t r y %
951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 % ParseGravityGeometry() returns a region as defined by the geometry string
954 % with respect to the image dimensions and its gravity.
956 % The format of the ParseGravityGeometry method is:
958 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
959 % RectangeInfo *region_info,ExceptionInfo *exception)
961 % A description of each parameter follows:
963 % o geometry: The geometry (e.g. 100x100+10+10).
965 % o region_info: the region as defined by the geometry string with
966 % respect to the image dimensions and its gravity.
968 % o exception: return any errors or warnings in this structure.
971 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
972 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
981 SetGeometry(image,region_info);
982 if (image->page.width != 0)
983 region_info->width=image->page.width;
984 if (image->page.height != 0)
985 region_info->height=image->page.height;
986 flags=ParseAbsoluteGeometry(geometry,region_info);
987 if (flags == NoValue)
989 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
990 "InvalidGeometry","`%s'",geometry);
993 if ((flags & PercentValue) != 0)
1005 Geometry is a percentage of the image size.
1007 if (image->gravity != UndefinedGravity)
1008 flags|=XValue | YValue;
1009 status=ParseGeometry(geometry,&geometry_info);
1010 scale.x=geometry_info.rho;
1011 if ((status & RhoValue) == 0)
1013 scale.y=geometry_info.sigma;
1014 if ((status & SigmaValue) == 0)
1016 region_info->width=(unsigned long) ((scale.x*image->columns/100.0)+0.5);
1017 region_info->height=(unsigned long) ((scale.y*image->rows/100.0)+0.5);
1020 Adjust offset according to gravity setting.
1022 width=region_info->width;
1023 height=region_info->height;
1025 region_info->width=image->page.width | image->columns;
1027 region_info->height=image->page.height | image->rows;
1028 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1029 region_info->width=width;
1030 region_info->height=height;
1035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1039 + P a r s e M e t a G e o m e t r y %
1043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1045 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1046 % geometry is modified as determined by the meta characters: %, !, <, >,
1049 % The format of the ParseMetaGeometry method is:
1051 % MagickStatusType ParseMetaGeometry(const char *geometry,long *x,long *y,
1052 % unsigned long *width,unsigned long *height)
1054 % A description of each parameter follows:
1056 % o geometry: The geometry.
1058 % o x,y: The x and y offset as determined by the geometry specification.
1060 % o width,height: The width and height as determined by the geometry
1065 static inline unsigned long MagickMax(const unsigned long x,
1066 const unsigned long y)
1073 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,long *x,
1074 long *y,unsigned long *width,unsigned long *height)
1087 Ensure the image geometry is valid.
1089 assert(x != (long *) NULL);
1090 assert(y != (long *) NULL);
1091 assert(width != (unsigned long *) NULL);
1092 assert(height != (unsigned long *) NULL);
1093 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1095 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1097 Parse geometry using GetGeometry.
1099 SetGeometryInfo(&geometry_info);
1100 former_width=(*width);
1101 former_height=(*height);
1102 flags=GetGeometry(geometry,x,y,width,height);
1103 if ((flags & PercentValue) != 0)
1112 Geometry is a percentage of the image size.
1114 flags=ParseGeometry(geometry,&geometry_info);
1115 scale.x=geometry_info.rho;
1116 if ((flags & RhoValue) == 0)
1118 scale.y=geometry_info.sigma;
1119 if ((flags & SigmaValue) == 0)
1121 *width=(unsigned long) (scale.x*former_width/100.0+0.5);
1124 *height=(unsigned long) (scale.y*former_height/100.0+0.5);
1127 former_width=(*width);
1128 former_height=(*height);
1130 if (((flags & AspectValue) == 0) && ((*width != former_width) ||
1131 (*height != former_height)))
1137 Respect aspect ratio of the image.
1139 if ((former_width == 0) || (former_height == 0))
1142 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1144 scale_factor=(MagickRealType) *width/(MagickRealType) former_width;
1145 if ((flags & MinimumValue) == 0)
1147 if (scale_factor > ((MagickRealType) *height/
1148 (MagickRealType) former_height))
1149 scale_factor=(MagickRealType) *height/(MagickRealType)
1153 if (scale_factor < ((MagickRealType) *height/
1154 (MagickRealType) former_height))
1155 scale_factor=(MagickRealType) *height/(MagickRealType)
1159 if ((flags & RhoValue) != 0)
1161 scale_factor=(MagickRealType) *width/(MagickRealType)
1163 if (((flags & MinimumValue) != 0) &&
1164 (scale_factor < ((MagickRealType) *width/
1165 (MagickRealType) former_height)))
1166 scale_factor=(MagickRealType) *width/(MagickRealType)
1171 scale_factor=(MagickRealType) *height/(MagickRealType)
1173 if (((flags & MinimumValue) != 0) &&
1174 (scale_factor < ((MagickRealType) *height/
1175 (MagickRealType) former_width)))
1176 scale_factor=(MagickRealType) *height/(MagickRealType)
1179 *width=MagickMax((unsigned long) (scale_factor*former_width+0.5),1UL);
1180 *height=MagickMax((unsigned long) (scale_factor*former_height+0.5),1UL);
1182 if ((flags & GreaterValue) != 0)
1184 if (former_width < *width)
1185 *width=former_width;
1186 if (former_height < *height)
1187 *height=former_height;
1189 if ((flags & LessValue) != 0)
1191 if (former_width > *width)
1192 *width=former_width;
1193 if (former_height > *height)
1194 *height=former_height;
1196 if ((flags & AreaValue) != 0)
1206 Geometry is a maximum area in pixels.
1208 (void) ParseGeometry(geometry,&geometry_info);
1209 area=geometry_info.rho;
1210 distance=sqrt((double) former_width*former_height);
1211 scale.x=(double) former_width/(double) (distance/sqrt((double) area));
1212 scale.y=(double) former_height/(double) (distance/sqrt((double) area));
1213 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1215 *width=(unsigned long) (former_width/(distance/sqrt((double) area)));
1216 *height=(unsigned long) (former_height/(distance/
1217 sqrt((double) area)));
1219 former_width=(*width);
1220 former_height=(*height);
1226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1230 % P a r s e P a g e G e o m e t r y %
1234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1236 % ParsePageGeometry() returns a region as defined by the geometry string with
1237 % respect to the image dimensions.
1239 % The format of the ParsePageGeometry method is:
1241 % MagickStatusType ParsePageGeometry(const Image *image,
1242 % const char *geometry,RectangeInfo *region_info,
1243 % ExceptionInfo *exception)
1245 % A description of each parameter follows:
1247 % o geometry: The geometry (e.g. 100x100+10+10).
1249 % o region_info: the region as defined by the geometry string with
1250 % respect to the image and its gravity.
1252 % o exception: return any errors or warnings in this structure.
1255 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1256 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1261 SetGeometry(image,region_info);
1262 if (image->page.width != 0)
1263 region_info->width=image->page.width;
1264 if (image->page.height != 0)
1265 region_info->height=image->page.height;
1266 flags=ParseAbsoluteGeometry(geometry,region_info);
1267 if (flags == NoValue)
1269 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1270 "InvalidGeometry","`%s'",geometry);
1273 if ((flags & PercentValue) != 0)
1275 region_info->width=image->columns;
1276 region_info->height=image->rows;
1278 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1279 ®ion_info->width,®ion_info->height);
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1288 % P a r s e R e g i o n G e o m e t r y %
1292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1294 % ParseRegionGeometry() returns a region as defined by the geometry string
1295 % with respect to the image dimensions and aspect ratio.
1297 % The format of the ParseRegionGeometry method is:
1299 % MagickStatusType ParseRegionGeometry(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 (e.g. 100x100+10+10).
1307 % o region_info: the region as defined by the geometry string.
1309 % o exception: return any errors or warnings in this structure.
1312 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1313 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1318 SetGeometry(image,region_info);
1319 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1320 ®ion_info->width,®ion_info->height);
1321 if (flags == NoValue)
1322 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1323 "InvalidGeometry","`%s'",geometry);
1328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1332 % S e t G e o m e t r y %
1336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1338 % SetGeometry() sets the geometry to its default values.
1340 % The format of the SetGeometry method is:
1342 % SetGeometry(const Image *image,RectangleInfo *geometry)
1344 % A description of each parameter follows:
1346 % o image: the image.
1348 % o geometry: the geometry.
1351 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1353 assert(image != (Image *) NULL);
1354 assert(image->signature == MagickSignature);
1355 if (image->debug != MagickFalse)
1356 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1357 assert(geometry != (RectangleInfo *) NULL);
1358 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1359 geometry->width=image->columns;
1360 geometry->height=image->rows;
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 % S e t G e o m e t r y I n f o %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1376 % The format of the SetGeometryInfo method is:
1378 % SetGeometryInfo(GeometryInfo *geometry_info)
1380 % A description of each parameter follows:
1382 % o geometry_info: the geometry info structure.
1385 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1387 assert(geometry_info != (GeometryInfo *) NULL);
1388 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1389 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));