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,
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)
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. 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=strtod(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);
665 case 0: affine_matrix->sx=StringToDouble(token); break;
666 case 1: affine_matrix->rx=StringToDouble(token); break;
667 case 2: affine_matrix->ry=StringToDouble(token); break;
668 case 3: affine_matrix->sy=StringToDouble(token); break;
669 case 4: affine_matrix->tx=StringToDouble(token); flags|=XValue; break;
670 case 5: affine_matrix->ty=StringToDouble(token); flags|=YValue; break;
673 determinant=(affine_matrix->sx*affine_matrix->sy-affine_matrix->rx*
675 if (fabs(determinant) < MagickEpsilon)
676 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
677 "InvalidGeometry","`%s'",geometry);
682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 % P a r s e G e o m e t r y %
690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 % ParseGeometry() parses a geometry specification and returns the sigma,
693 % rho, xi, and psi values. It also returns flags that indicates which
694 % of the four values (sigma, rho, xi, psi) were located in the string, and
695 % whether the xi or pi values are negative.
697 % In addition, it reports if there are any of meta characters (%, !, <, >, @,
698 % and ^) flags present. It does not report the location of the percentage
699 % relative to the values.
701 % The format of the ParseGeometry method is:
703 % MagickStatusType ParseGeometry(const char *geometry,
704 % GeometryInfo *geometry_info)
706 % A description of each parameter follows:
708 % o geometry: The geometry string (e.g. "100x100+10+10").
710 % o geometry_info: returns the parsed width/height/x/y in this structure.
713 MagickExport MagickStatusType ParseGeometry(const char *geometry,
714 GeometryInfo *geometry_info)
718 pedantic_geometry[MaxTextExtent],
728 Remove whitespaces meta characters from geometry specification.
730 assert(geometry_info != (GeometryInfo *) NULL);
732 if ((geometry == (char *) NULL) || (*geometry == '\0'))
734 if (strlen(geometry) >= MaxTextExtent)
736 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
737 for (p=pedantic_geometry; *p != '\0'; )
739 if (isspace((int) ((unsigned char) *p)) != 0)
741 (void) CopyMagickString(p,p+1,MaxTextExtent);
749 (void) CopyMagickString(p,p+1,MaxTextExtent);
755 (void) CopyMagickString(p,p+1,MaxTextExtent);
761 (void) CopyMagickString(p,p+1,MaxTextExtent);
767 (void) CopyMagickString(p,p+1,MaxTextExtent);
773 (void) CopyMagickString(p,p+1,MaxTextExtent);
779 (void) CopyMagickString(p,p+1,MaxTextExtent);
785 (void) CopyMagickString(p,p+1,MaxTextExtent);
821 Parse rho, sigma, xi, psi, and optionally chi.
828 if (LocaleNCompare(p,"0x",2) == 0)
829 value=(double) strtol(p,&q,10);
830 if ((((int) *q) == -41) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
831 (*q == '/') || (*q == ':') || (*q =='\0'))
837 if (LocaleNCompare(p,"0x",2) == 0)
838 value=(double) strtol(p,&p,10);
844 geometry_info->rho=value;
848 if ((((int) *p) == -41) || (*p == 'x') || (*p == 'X') || (*p == ',') ||
849 (*p == '/') || (*p == ':'))
855 while (isspace((int) ((unsigned char) *p)) != 0)
857 if (((((int) *q) != -41) && (*q != 'x') && (*q != 'X')) ||
858 ((*p != '+') && (*p != '-')))
865 geometry_info->sigma=value;
869 while (isspace((int) ((unsigned char) *p)) != 0)
871 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
876 if ((*p == ',') || (*p == '/') || (*p == ':'))
885 geometry_info->xi=value;
887 while (isspace((int) ((unsigned char) *p)) != 0)
889 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
895 if ((*p == ',') || (*p == '/') || (*p == ':'))
904 geometry_info->psi=value;
907 while (isspace((int) ((unsigned char) *p)) != 0)
909 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
915 if ((*p == ',') || (*p == '/') || (*p == ':'))
924 geometry_info->chi=value;
928 if (strchr(pedantic_geometry,':') != (char *) NULL)
931 Normalize sampling factor (e.g. 4:2:2 => 2x1).
933 geometry_info->rho/=geometry_info->sigma;
934 geometry_info->sigma=1.0;
935 if (geometry_info->xi == 0.0)
936 geometry_info->sigma=2.0;
938 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
939 ((flags & PsiValue) == 0))
942 Support negative height values (e.g. 30x-20).
944 geometry_info->sigma=geometry_info->xi;
945 geometry_info->xi=0.0;
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 % P a r s e G r a v i t y G e o m e t r y %
961 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 % ParseGravityGeometry() returns a region as defined by the geometry string
964 % with respect to the given image page (canvas) dimensions and the images
967 % This is typically used for specifing a area within a given image for
968 % cropping images to a smaller size, chopping out rows and or columns, or
969 % resizing and positioning overlay images.
971 % Percentages are relative to image size and not page size, and are set to
972 % nearest integer (pixel) size.
974 % The format of the ParseGravityGeometry method is:
976 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
977 % RectangeInfo *region_info,ExceptionInfo *exception)
979 % A description of each parameter follows:
981 % o geometry: The geometry string (e.g. "100x100+10+10").
983 % o region_info: the region as defined by the geometry string with respect
984 % to the image dimensions and its gravity.
986 % o exception: return any errors or warnings in this structure.
989 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
990 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
999 SetGeometry(image,region_info);
1000 if (image->page.width != 0)
1001 region_info->width=image->page.width;
1002 if (image->page.height != 0)
1003 region_info->height=image->page.height;
1004 flags=ParseAbsoluteGeometry(geometry,region_info);
1005 if (flags == NoValue)
1007 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1008 "InvalidGeometry","`%s'",geometry);
1011 if ((flags & PercentValue) != 0)
1023 Geometry is a percentage of the image size, not canvas size
1025 if (image->gravity != UndefinedGravity)
1026 flags|=XValue | YValue;
1027 status=ParseGeometry(geometry,&geometry_info);
1028 scale.x=geometry_info.rho;
1029 if ((status & RhoValue) == 0)
1031 scale.y=geometry_info.sigma;
1032 if ((status & SigmaValue) == 0)
1034 region_info->width=(size_t) floor((scale.x*image->columns/100.0)+0.5);
1035 region_info->height=(size_t) floor((scale.y*image->rows/100.0)+0.5);
1038 Adjust offset according to gravity setting.
1040 width=region_info->width;
1041 height=region_info->height;
1043 region_info->width=image->page.width | image->columns;
1045 region_info->height=image->page.height | image->rows;
1046 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1047 region_info->width=width;
1048 region_info->height=height;
1053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1057 + P a r s e M e t a G e o m e t r y %
1061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1063 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1064 % geometry is modified as determined by the meta characters: %, !, <, >, @,
1065 % and ^ in relation to image resizing.
1067 % Final image dimensions are adjusted so as to preserve the aspect ratio as
1068 % much as posible, while generating a integer (pixel) size, and fitting the
1069 % image within the specified geometry width and height.
1071 % Flags are interpreted...
1072 % % geometry size is given percentage of original image size
1073 % ! do not try to preserve aspect ratio
1074 % < only enlarge images smaller that geometry
1075 % > only shrink images larger than geometry
1076 % @ Fit image to contain at most this many pixels
1077 % ^ Contain the given geometry given, (minimal dimensions given)
1079 % The format of the ParseMetaGeometry method is:
1081 % MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1082 % ssize_t *y, size_t *width,size_t *height)
1084 % A description of each parameter follows:
1086 % o geometry: The geometry string (e.g. "100x100+10+10").
1088 % o x,y: The x and y offset, set according to the geometry specification.
1090 % o width,height: The width and height of original image, modified by
1091 % the given geometry specification.
1095 static inline size_t MagickMax(const size_t x,
1103 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1104 ssize_t *y,size_t *width,size_t *height)
1117 Ensure the image geometry is valid.
1119 assert(x != (ssize_t *) NULL);
1120 assert(y != (ssize_t *) NULL);
1121 assert(width != (size_t *) NULL);
1122 assert(height != (size_t *) NULL);
1123 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1127 Parse geometry using GetGeometry.
1129 SetGeometryInfo(&geometry_info);
1130 former_width=(*width);
1131 former_height=(*height);
1132 flags=GetGeometry(geometry,x,y,width,height);
1133 if ((flags & PercentValue) != 0)
1142 Geometry is a percentage of the image size.
1144 flags=ParseGeometry(geometry,&geometry_info);
1145 scale.x=geometry_info.rho;
1146 if ((flags & RhoValue) == 0)
1148 scale.y=geometry_info.sigma;
1149 if ((flags & SigmaValue) == 0)
1151 *width=(size_t) floor(scale.x*former_width/100.0+0.5);
1154 *height=(size_t) floor(scale.y*former_height/100.0+0.5);
1157 former_width=(*width);
1158 former_height=(*height);
1160 if (((flags & AspectValue) == 0) && ((*width != former_width) ||
1161 (*height != former_height)))
1167 Respect aspect ratio of the image.
1169 if ((former_width == 0) || (former_height == 0))
1172 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1174 scale_factor=(MagickRealType) *width/(MagickRealType) former_width;
1175 if ((flags & MinimumValue) == 0)
1177 if (scale_factor > ((MagickRealType) *height/(MagickRealType)
1179 scale_factor=(MagickRealType) *height/(MagickRealType)
1183 if (scale_factor < ((MagickRealType) *height/(MagickRealType)
1185 scale_factor=(MagickRealType) *height/(MagickRealType)
1189 if ((flags & RhoValue) != 0)
1191 scale_factor=(MagickRealType) *width/(MagickRealType)
1193 if (((flags & MinimumValue) != 0) &&
1194 (scale_factor < ((MagickRealType) *width/(MagickRealType)
1196 scale_factor=(MagickRealType) *width/(MagickRealType)
1201 scale_factor=(MagickRealType) *height/(MagickRealType)
1203 if (((flags & MinimumValue) != 0) &&
1204 (scale_factor < ((MagickRealType) *height/(MagickRealType)
1206 scale_factor=(MagickRealType) *height/(MagickRealType)
1209 *width=MagickMax((size_t) floor(scale_factor*former_width+0.5),1UL);
1210 *height=MagickMax((size_t) floor(scale_factor*former_height+0.5),1UL);
1212 if ((flags & GreaterValue) != 0)
1214 if (former_width < *width)
1215 *width=former_width;
1216 if (former_height < *height)
1217 *height=former_height;
1219 if ((flags & LessValue) != 0)
1221 if (former_width > *width)
1222 *width=former_width;
1223 if (former_height > *height)
1224 *height=former_height;
1226 if ((flags & AreaValue) != 0)
1236 Geometry is a maximum area in pixels.
1238 (void) ParseGeometry(geometry,&geometry_info);
1239 area=geometry_info.rho;
1240 distance=sqrt((double) former_width*former_height);
1241 scale.x=(double) former_width/(double) (distance/sqrt((double) area));
1242 scale.y=(double) former_height/(double) (distance/sqrt((double) area));
1243 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1245 *width=(size_t) (former_width/(distance/sqrt((double) area)));
1246 *height=(size_t) (former_height/(distance/sqrt((double) area)));
1248 former_width=(*width);
1249 former_height=(*height);
1255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 % P a r s e P a g e G e o m e t r y %
1263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1265 % ParsePageGeometry() returns a region as defined by the geometry string with
1266 % respect to the image page (canvas) dimensions.
1268 % WARNING: Percentage dimensions remain relative to the actual image
1269 % dimensions, and not canvas dimensions.
1271 % The format of the ParsePageGeometry method is:
1273 % MagickStatusType ParsePageGeometry(const Image *image,
1274 % const char *geometry,RectangeInfo *region_info,
1275 % ExceptionInfo *exception)
1277 % A description of each parameter follows:
1279 % o geometry: The geometry string (e.g. "100x100+10+10").
1281 % o region_info: the region as defined by the geometry string with
1282 % respect to the image and its gravity.
1284 % o exception: return any errors or warnings in this structure.
1287 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1288 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1293 SetGeometry(image,region_info);
1294 if (image->page.width != 0)
1295 region_info->width=image->page.width;
1296 if (image->page.height != 0)
1297 region_info->height=image->page.height;
1298 flags=ParseAbsoluteGeometry(geometry,region_info);
1299 if (flags == NoValue)
1301 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1302 "InvalidGeometry","`%s'",geometry);
1305 if ((flags & PercentValue) != 0)
1307 region_info->width=image->columns;
1308 region_info->height=image->rows;
1310 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1311 ®ion_info->width,®ion_info->height);
1316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320 % P a r s e R e g i o n G e o m e t r y %
1324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1326 % ParseRegionGeometry() returns a region as defined by the geometry string
1327 % with respect to the image dimensions and aspect ratio.
1329 % This is basically a wrapper around ParseMetaGeometry. This is typically
1330 % used to parse a geometry string to work out the final integer dimensions
1331 % for image resizing.
1333 % The format of the ParseRegionGeometry method is:
1335 % MagickStatusType ParseRegionGeometry(const Image *image,
1336 % const char *geometry,RectangeInfo *region_info,
1337 % ExceptionInfo *exception)
1339 % A description of each parameter follows:
1341 % o geometry: The geometry string (e.g. "100x100+10+10").
1343 % o region_info: the region as defined by the geometry string.
1345 % o exception: return any errors or warnings in this structure.
1348 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1349 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1354 SetGeometry(image,region_info);
1355 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1356 ®ion_info->width,®ion_info->height);
1357 if (flags == NoValue)
1358 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1359 "InvalidGeometry","`%s'",geometry);
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 % S e t G e o m e t r y %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % SetGeometry() sets the geometry to its default values.
1376 % The format of the SetGeometry method is:
1378 % SetGeometry(const Image *image,RectangleInfo *geometry)
1380 % A description of each parameter follows:
1382 % o image: the image.
1384 % o geometry: the geometry.
1387 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1389 assert(image != (Image *) NULL);
1390 assert(image->signature == MagickSignature);
1391 if (image->debug != MagickFalse)
1392 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1393 assert(geometry != (RectangleInfo *) NULL);
1394 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1395 geometry->width=image->columns;
1396 geometry->height=image->rows;
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % S e t G e o m e t r y I n f o %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1412 % The format of the SetGeometryInfo method is:
1414 % SetGeometryInfo(GeometryInfo *geometry_info)
1416 % A description of each parameter follows:
1418 % o geometry_info: the geometry info structure.
1421 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1423 assert(geometry_info != (GeometryInfo *) NULL);
1424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1425 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));