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-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/constitute.h"
44 #include "MagickCore/draw.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/geometry.h"
48 #include "MagickCore/memory_.h"
49 #include "MagickCore/string_.h"
50 #include "MagickCore/string-private.h"
51 #include "MagickCore/token.h"
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 % G e t G e o m e t r y %
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 % GetGeometry() parses a geometry specification and returns the width,
65 % height, x, and y values. It also returns flags that indicates which
66 % of the four values (width, height, x, y) were located in the string, and
67 % whether the x or y values are negative. In addition, there are flags to
68 % report any meta characters (%, !, <, or >).
70 % The value must form a proper geometry style specification of WxH+X+Y
71 % of integers only, and values can not be separated by comma, colon, or
72 % slash charcaters. See ParseGeometry() below.
74 % Offsets may be prefixed by multiple signs to make offset string
75 % substitutions easier to handle from shell scripts.
76 % For example: "-10-10", "-+10-+10", or "+-10+-10" will generate negtive
77 % offsets, while "+10+10", "++10++10", or "--10--10" will generate positive
80 % The format of the GetGeometry method is:
82 % MagickStatusType GetGeometry(const char *geometry,ssize_t *x,ssize_t *y,
83 % size_t *width,size_t *height)
85 % A description of each parameter follows:
87 % o geometry: The geometry.
89 % o x,y: The x and y offset as determined by the geometry specification.
91 % o width,height: The width and height as determined by the geometry
95 MagickExport MagickStatusType GetGeometry(const char *geometry,ssize_t *x,
96 ssize_t *y,size_t *width,size_t *height)
100 pedantic_geometry[MaxTextExtent],
113 Remove whitespace and meta characters from geometry specification.
116 if ((geometry == (char *) NULL) || (*geometry == '\0'))
118 if (strlen(geometry) >= (MaxTextExtent-1))
120 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
121 for (p=pedantic_geometry; *p != '\0'; )
123 if (isspace((int) ((unsigned char) *p)) != 0)
125 (void) CopyMagickString(p,p+1,MaxTextExtent);
134 (void) CopyMagickString(p,p+1,MaxTextExtent);
140 (void) CopyMagickString(p,p+1,MaxTextExtent);
146 (void) CopyMagickString(p,p+1,MaxTextExtent);
152 (void) CopyMagickString(p,p+1,MaxTextExtent);
158 (void) CopyMagickString(p,p+1,MaxTextExtent);
164 (void) CopyMagickString(p,p+1,MaxTextExtent);
170 (void) CopyMagickString(p,p+1,MaxTextExtent);
176 flags|=SeparatorValue;
206 Parse width, height, x, and y.
212 value=StringToDouble(p,&q);
214 if (LocaleNCompare(p,"0x",2) == 0)
215 value=(double) strtol(p,&q,10);
216 if ((*p != '+') && (*p != '-'))
218 c=(int) ((unsigned char) *q);
219 if ((c == 215) || (*q == 'x') || (*q == 'X') || (*q == '\0'))
225 if (LocaleNCompare(p,"0x",2) == 0)
226 *width=(size_t) strtol(p,&p,10);
228 *width=(size_t) floor(StringToDouble(p,&p)+0.5);
233 if ((*p != '+') && (*p != '-'))
235 c=(int) ((unsigned char) *p);
236 if ((c == 215) || (*p == 'x') || (*p == 'X'))
239 if ((*p != '+') && (*p != '-'))
245 *height=(size_t) floor(StringToDouble(p,&p)+0.5);
251 if ((*p == '+') || (*p == '-'))
256 while ((*p == '+') || (*p == '-'))
259 flags^=XNegative; /* negate sign */
263 *x=(ssize_t) ceil(StringToDouble(p,&p)-0.5);
267 if ((flags & XNegative) != 0)
271 if ((*p == '+') || (*p == '-'))
276 while ((*p == '+') || (*p == '-'))
279 flags^=YNegative; /* negate sign */
283 *y=(ssize_t) ceil(StringToDouble(p,&p)-0.5);
287 if ((flags & YNegative) != 0)
291 if ((flags & PercentValue) != 0)
293 if (((flags & SeparatorValue) == 0) && ((flags & HeightValue) == 0))
298 if (((flags & SeparatorValue) != 0) && ((flags & WidthValue) == 0))
302 /* Debugging Geometry */
303 (void) fprintf(stderr,"GetGeometry...\n");
304 (void) fprintf(stderr,"Input: %s\n",geometry);
305 (void) fprintf(stderr,"Flags: %c %c %s %s\n",
306 (flags & WidthValue) ? 'W' : ' ',(flags & HeightValue) ? 'H' : ' ',
307 (flags & XValue) ? ((flags & XNegative) ? "-X" : "+X") : " ",
308 (flags & YValue) ? ((flags & YNegative) ? "-Y" : "+Y") : " ");
309 (void) fprintf(stderr,"Geometry: %ldx%ld%+ld%+ld\n",(long) *width,(long)
310 *height,(long) *x,(long) *y);
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 % G e t P a g e G e o m e t r y %
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 % GetPageGeometry() replaces any page mneumonic with the equivalent size in
329 % The format of the GetPageGeometry method is:
331 % char *GetPageGeometry(const char *page_geometry)
333 % A description of each parameter follows.
335 % o page_geometry: Specifies a pointer to an array of characters. The
336 % string is either a Postscript page name (e.g. A4) or a postscript page
337 % geometry (e.g. 612x792+36+36).
340 MagickExport char *GetPageGeometry(const char *page_geometry)
345 { "4x6", "288x432" },
346 { "5x7", "360x504" },
347 { "7x9", "504x648" },
348 { "8x10", "576x720" },
349 { "9x11", "648x792" },
350 { "9x12", "648x864" },
351 { "10x13", "720x936" },
352 { "10x14", "720x1008" },
353 { "11x17", "792x1224" },
354 { "a0", "2384x3370" },
355 { "a1", "1684x2384" },
357 { "a2", "1191x1684" },
358 { "a3", "842x1191" },
360 { "a4small", "595x842" },
366 { "archa", "648x864" },
367 { "archb", "864x1296" },
368 { "archC", "1296x1728" },
369 { "archd", "1728x2592" },
370 { "arche", "2592x3456" },
371 { "b0", "2920x4127" },
372 { "b1", "2064x2920" },
374 { "b2", "1460x2064" },
375 { "b3", "1032x1460" },
376 { "b4", "729x1032" },
382 { "c0", "2599x3676" },
383 { "c1", "1837x2599" },
384 { "c2", "1298x1837" },
385 { "c3", "918x1296" },
390 { "executive", "540x720" },
391 { "flsa", "612x936" },
392 { "flse", "612x936" },
393 { "folio", "612x936" },
394 { "halfletter", "396x612" },
395 { "isob0", "2835x4008" },
396 { "isob1", "2004x2835" },
397 { "isob10", "88x125" },
398 { "isob2", "1417x2004" },
399 { "isob3", "1001x1417" },
400 { "isob4", "709x1001" },
401 { "isob5", "499x709" },
402 { "isob6", "354x499" },
403 { "isob7", "249x354" },
404 { "isob8", "176x249" },
405 { "isob9", "125x176" },
406 { "jisb0", "1030x1456" },
407 { "jisb1", "728x1030" },
408 { "jisb2", "515x728" },
409 { "jisb3", "364x515" },
410 { "jisb4", "257x364" },
411 { "jisb5", "182x257" },
412 { "jisb6", "128x182" },
413 { "ledger", "1224x792" },
414 { "legal", "612x1008" },
415 { "letter", "612x792" },
416 { "lettersmall", "612x792" },
417 { "quarto", "610x780" },
418 { "statement", "396x612" },
419 { "tabloid", "792x1224" },
420 { (char *) NULL, (char *) NULL }
429 assert(page_geometry != (char *) NULL);
430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",page_geometry);
431 page=AcquireString(page_geometry);
432 for (i=0; *PageSizes[i] != (char *) NULL; i++)
433 if (LocaleNCompare(PageSizes[i][0],page,strlen(PageSizes[i][0])) == 0)
442 Replace mneumonic with the equivalent size in dots-per-inch.
444 (void) CopyMagickString(page,PageSizes[i][1],MaxTextExtent);
445 (void) ConcatenateMagickString(page,page_geometry+
446 strlen(PageSizes[i][0]),MaxTextExtent);
447 flags=GetGeometry(page,&geometry.x,&geometry.y,&geometry.width,
449 if ((flags & GreaterValue) == 0)
450 (void) ConcatenateMagickString(page,">",MaxTextExtent);
457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
461 % G r a v i t y A d j u s t G e o m e t r y %
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % GravityAdjustGeometry() adjusts the offset of a region with regard to the
467 % given: width, height and gravity; against which it is positioned.
469 % The region should also have an appropriate width and height to correctly
470 % set the right offset of the top left corner of the region.
472 % The format of the GravityAdjustGeometry method is:
474 % void GravityAdjustGeometry(const size_t width, const size_t height,
475 % const GravityType gravity,RectangleInfo *region);
477 % A description of each parameter follows:
479 % o width, height: the larger area the region is relative to
481 % o gravity: the edge/corner the current offset is relative to
483 % o region: The region requiring a offset adjustment relative to gravity
486 MagickExport void GravityAdjustGeometry(const size_t width,
487 const size_t height,const GravityType gravity,RectangleInfo *region)
489 if (region->height == 0)
490 region->height=height;
491 if (region->width == 0)
495 case NorthEastGravity:
497 case SouthEastGravity:
499 region->x=(ssize_t) (width-region->width-region->x);
506 region->x+=(ssize_t) (width/2-region->width/2);
510 case NorthWestGravity:
512 case SouthWestGravity:
518 case SouthWestGravity:
520 case SouthEastGravity:
522 region->y=(ssize_t) (height-region->height-region->y);
529 region->y+=(ssize_t) (height/2-region->height/2);
533 case NorthWestGravity:
535 case NorthEastGravity:
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 + I s G e o m e t r y %
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % IsGeometry() returns MagickTrue if the geometry specification is valid.
554 % Examples are 100, 100x200, x200, 100x200+10+20, +10+20, 200%, 200x200!, etc.
556 % The format of the IsGeometry method is:
558 % MagickBooleanType IsGeometry(const char *geometry)
560 % A description of each parameter follows:
562 % o geometry: This string is the geometry specification.
565 MagickExport MagickBooleanType IsGeometry(const char *geometry)
573 if (geometry == (const char *) NULL)
575 flags=ParseGeometry(geometry,&geometry_info);
576 return(flags != NoValue ? MagickTrue : MagickFalse);
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 + I s S c e n e G e o m e t r y %
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590 % IsSceneGeometry() returns MagickTrue if the geometry is a valid scene
591 % specification (e.g. [1], [1-9], [1,7,4]).
593 % The format of the IsSceneGeometry method is:
595 % MagickBooleanType IsSceneGeometry(const char *geometry,
596 % const MagickBooleanType pedantic)
598 % A description of each parameter follows:
600 % o geometry: This string is the geometry specification.
602 % o pedantic: A value other than 0 invokes a more restrictive set of
603 % conditions for a valid specification (e.g. [1], [1-4], [4-1]).
606 MagickExport MagickBooleanType IsSceneGeometry(const char *geometry,
607 const MagickBooleanType pedantic)
615 if (geometry == (const char *) NULL)
618 value=StringToDouble(geometry,&p);
622 if (strspn(geometry,"0123456789-, ") != strlen(geometry))
624 if ((pedantic != MagickFalse) && (strchr(geometry,',') != (char *) NULL))
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % P a r s e A b s o l u t e G e o m e t r y %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % ParseAbsoluteGeometry() returns a region as defined by the geometry string,
641 % without any modification by percentages or gravity.
643 % It currently just a wrapper around GetGeometry(), but may be expanded in
644 % the future to handle other positioning information.
646 % The format of the ParseAbsoluteGeometry method is:
648 % MagickStatusType ParseAbsoluteGeometry(const char *geometry,
649 % RectangleInfo *region_info)
651 % A description of each parameter follows:
653 % o geometry: The geometry string (e.g. "100x100+10+10").
655 % o region_info: the region as defined by the geometry string.
658 MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry,
659 RectangleInfo *region_info)
664 flags=GetGeometry(geometry,®ion_info->x,®ion_info->y,
665 ®ion_info->width,®ion_info->height);
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % P a r s e A f f i n e G e o m e t r y %
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % ParseAffineGeometry() returns an affine matrix as defined by a string of 4
681 % to 6 comma/space separated floating point values.
683 % The affine matrix determinant is checked for validity of the values.
685 % The format of the ParseAffineGeometry method is:
687 % MagickStatusType ParseAffineGeometry(const char *geometry,
688 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
690 % A description of each parameter follows:
692 % o geometry: The geometry string (e.g. "1.0,0.0,0.0,1.0,3.2,1.2").
694 % o affine_matrix: the affine matrix as defined by the geometry string.
696 % o exception: return any errors or warnings in this structure.
699 MagickExport MagickStatusType ParseAffineGeometry(const char *geometry,
700 AffineMatrix *affine_matrix,ExceptionInfo *exception)
703 token[MaxTextExtent];
717 GetAffineMatrix(affine_matrix);
720 for (i=0; (*p != '\0') && (i < 6); i++)
722 GetMagickToken(p,&p,token);
724 GetMagickToken(p,&p,token);
729 affine_matrix->sx=StringToDouble(token,(char **) NULL);
734 affine_matrix->rx=StringToDouble(token,(char **) NULL);
739 affine_matrix->ry=StringToDouble(token,(char **) NULL);
744 affine_matrix->sy=StringToDouble(token,(char **) NULL);
749 affine_matrix->tx=StringToDouble(token,(char **) NULL);
755 affine_matrix->ty=StringToDouble(token,(char **) NULL);
761 determinant=(affine_matrix->sx*affine_matrix->sy-affine_matrix->rx*
763 if (fabs(determinant) < MagickEpsilon)
764 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
765 "InvalidArgument","'%s' : 'Indeterminate Matrix'",geometry);
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % P a r s e G e o m e t r y %
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % ParseGeometry() parses a geometry specification and returns the sigma,
781 % rho, xi, and psi values. It also returns flags that indicates which
782 % of the four values (sigma, rho, xi, psi) were located in the string, and
783 % whether the xi or pi values are negative.
785 % In addition, it reports if there are any of meta characters (%, !, <, >, @,
786 % and ^) flags present. It does not report the location of the percentage
787 % relative to the values.
789 % Values may also be separated by commas, colons, or slashes, and offsets.
790 % Offsets may be prefixed by multiple signs to make offset string
791 % substitutions easier to handle from shell scripts.
792 % For example: "-10-10", "-+10-+10", or "+-10+-10" will generate negtive
793 % offsets, while "+10+10", "++10++10", or "--10--10" will generate positive
796 % The format of the ParseGeometry method is:
798 % MagickStatusType ParseGeometry(const char *geometry,
799 % GeometryInfo *geometry_info)
801 % A description of each parameter follows:
803 % o geometry: The geometry string (e.g. "100x100+10+10").
805 % o geometry_info: returns the parsed width/height/x/y in this structure.
808 MagickExport MagickStatusType ParseGeometry(const char *geometry,
809 GeometryInfo *geometry_info)
813 pedantic_geometry[MaxTextExtent],
826 Remove whitespaces meta characters from geometry specification.
828 assert(geometry_info != (GeometryInfo *) NULL);
830 if ((geometry == (char *) NULL) || (*geometry == '\0'))
832 if (strlen(geometry) >= (MaxTextExtent-1))
834 (void) CopyMagickString(pedantic_geometry,geometry,MaxTextExtent);
835 for (p=pedantic_geometry; *p != '\0'; )
837 if (isspace((int) ((unsigned char) *p)) != 0)
839 (void) CopyMagickString(p,p+1,MaxTextExtent);
842 c=(int) ((unsigned char) *p);
848 (void) CopyMagickString(p,p+1,MaxTextExtent);
854 (void) CopyMagickString(p,p+1,MaxTextExtent);
860 (void) CopyMagickString(p,p+1,MaxTextExtent);
866 (void) CopyMagickString(p,p+1,MaxTextExtent);
872 (void) CopyMagickString(p,p+1,MaxTextExtent);
878 (void) CopyMagickString(p,p+1,MaxTextExtent);
884 (void) CopyMagickString(p,p+1,MaxTextExtent);
890 flags|=SeparatorValue;
927 Parse rho, sigma, xi, psi, and optionally chi.
933 value=StringToDouble(p,&q);
934 if (LocaleNCompare(p,"0x",2) == 0)
935 value=(double) strtol(p,&q,10);
936 c=(int) ((unsigned char) *q);
937 if ((c == 215) || (*q == 'x') || (*q == 'X') || (*q == ',') ||
938 (*q == '/') || (*q == ':') || (*q =='\0'))
944 if (LocaleNCompare(p,"0x",2) == 0)
945 value=(double) strtol(p,&p,10);
947 value=StringToDouble(p,&p);
951 geometry_info->rho=value;
955 c=(int) ((unsigned char) *p);
956 if ((c == 215) || (*p == 'x') || (*p == 'X') || (*p == ',') || (*p == '/') ||
963 while (isspace((int) ((unsigned char) *p)) != 0)
965 c=(int) ((unsigned char) *q);
966 if (((c != 215) && (*q != 'x') && (*q != 'X')) || ((*p != '+') &&
970 value=StringToDouble(p,&p);
974 geometry_info->sigma=value;
978 while (isspace((int) ((unsigned char) *p)) != 0)
980 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') || (*p == ':'))
985 if ((*p == ',') || (*p == '/') || (*p == ':') )
987 while ((*p == '+') || (*p == '-'))
990 flags^=XiNegative; /* negate sign */
994 value=StringToDouble(p,&p);
998 if ((flags & XiNegative) != 0)
1000 geometry_info->xi=value;
1002 while (isspace((int) ((unsigned char) *p)) != 0)
1004 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
1010 if ((*p == ',') || (*p == '/') || (*p == ':'))
1012 while ((*p == '+') || (*p == '-'))
1015 flags^=PsiNegative; /* negate sign */
1019 value=StringToDouble(p,&p);
1023 if ((flags & PsiNegative) != 0)
1025 geometry_info->psi=value;
1028 while (isspace((int) ((unsigned char) *p)) != 0)
1030 if ((*p == '+') || (*p == '-') || (*p == ',') || (*p == '/') ||
1036 if ((*p == ',') || (*p == '/') || (*p == ':'))
1038 while ((*p == '+') || (*p == '-'))
1041 flags^=ChiNegative; /* negate sign */
1045 value=StringToDouble(p,&p);
1049 if ((flags & ChiNegative) != 0)
1051 geometry_info->chi=value;
1055 if (strchr(pedantic_geometry,':') != (char *) NULL)
1058 Normalize sampling factor (e.g. 4:2:2 => 2x1).
1060 geometry_info->rho/=geometry_info->sigma;
1061 geometry_info->sigma=1.0;
1062 if (geometry_info->xi == 0.0)
1063 geometry_info->sigma=2.0;
1065 if (((flags & SigmaValue) == 0) && ((flags & XiValue) != 0) &&
1066 ((flags & PsiValue) == 0))
1069 Support negative height values (e.g. 30x-20).
1071 geometry_info->sigma=geometry_info->xi;
1072 geometry_info->xi=0.0;
1076 if ((flags & PercentValue) != 0)
1078 if (((flags & SeparatorValue) == 0) && ((flags & SigmaValue) == 0))
1079 geometry_info->sigma=geometry_info->rho;
1080 if (((flags & SeparatorValue) != 0) && ((flags & RhoValue) == 0))
1081 geometry_info->rho=geometry_info->sigma;
1084 /* Debugging Geometry */
1085 (void) fprintf(stderr,"ParseGeometry...\n");
1086 (void) fprintf(stderr,"Flags: %c %c %s %s %s\n",
1087 (flags & RhoValue) ? 'W' : ' ',(flags & SigmaValue) ? 'H' : ' ',
1088 (flags & XiValue) ? ((flags & XiNegative) ? "-X" : "+X") : " ",
1089 (flags & PsiValue) ? ((flags & PsiNegative) ? "-Y" : "+Y") : " ",
1090 (flags & ChiValue) ? ((flags & ChiNegative) ? "-Z" : "+Z") : " ");
1091 (void) fprintf(stderr,"Geometry: %lg,%lg,%lg,%lg,%lg\n",geometry_info->rho,
1092 geometry_info->sigma,geometry_info->xi,geometry_info->psi,
1093 geometry_info->chi);
1099 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103 % P a r s e G r a v i t y G e o m e t r y %
1107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1109 % ParseGravityGeometry() returns a region as defined by the geometry string
1110 % with respect to the given image page (canvas) dimensions and the images
1113 % This is typically used for specifing a area within a given image for
1114 % cropping images to a smaller size, chopping out rows and or columns, or
1115 % resizing and positioning overlay images.
1117 % Percentages are relative to image size and not page size, and are set to
1118 % nearest integer (pixel) size.
1120 % The format of the ParseGravityGeometry method is:
1122 % MagickStatusType ParseGravityGeometry(Image *image,const char *geometry,
1123 % RectangeInfo *region_info,ExceptionInfo *exception)
1125 % A description of each parameter follows:
1127 % o geometry: The geometry string (e.g. "100x100+10+10").
1129 % o region_info: the region as defined by the geometry string with respect
1130 % to the image dimensions and its gravity.
1132 % o exception: return any errors or warnings in this structure.
1135 MagickExport MagickStatusType ParseGravityGeometry(const Image *image,
1136 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1145 SetGeometry(image,region_info);
1146 if (image->page.width != 0)
1147 region_info->width=image->page.width;
1148 if (image->page.height != 0)
1149 region_info->height=image->page.height;
1150 flags=ParseAbsoluteGeometry(geometry,region_info);
1151 if (flags == NoValue)
1153 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1154 "InvalidGeometry","`%s'",geometry);
1157 if ((flags & PercentValue) != 0)
1169 Geometry is a percentage of the image size, not canvas size
1171 if (image->gravity != UndefinedGravity)
1172 flags|=XValue | YValue;
1173 status=ParseGeometry(geometry,&geometry_info);
1174 scale.x=geometry_info.rho;
1175 if ((status & RhoValue) == 0)
1177 scale.y=geometry_info.sigma;
1178 if ((status & SigmaValue) == 0)
1180 region_info->width=(size_t) floor((scale.x*image->columns/100.0)+0.5);
1181 region_info->height=(size_t) floor((scale.y*image->rows/100.0)+0.5);
1184 Adjust offset according to gravity setting.
1186 width=region_info->width;
1187 height=region_info->height;
1189 region_info->width=image->page.width | image->columns;
1191 region_info->height=image->page.height | image->rows;
1192 GravityAdjustGeometry(image->columns,image->rows,image->gravity,region_info);
1193 region_info->width=width;
1194 region_info->height=height;
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1203 + P a r s e M e t a G e o m e t r y %
1207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1209 % ParseMetaGeometry() is similar to GetGeometry() except the returned
1210 % geometry is modified as determined by the meta characters: %, !, <, >, @,
1211 % and ^ in relation to image resizing.
1213 % Final image dimensions are adjusted so as to preserve the aspect ratio as
1214 % much as possible, while generating a integer (pixel) size, and fitting the
1215 % image within the specified geometry width and height.
1217 % Flags are interpreted...
1218 % % geometry size is given percentage of original width and height given
1219 % ! do not try to preserve aspect ratio
1220 % < only enlarge images smaller that geometry
1221 % > only shrink images larger than geometry
1222 % @ Fit image to contain at most this many pixels
1223 % ^ Contain the given geometry given, (minimal dimensions given)
1225 % The format of the ParseMetaGeometry method is:
1227 % MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1228 % ssize_t *y, size_t *width,size_t *height)
1230 % A description of each parameter follows:
1232 % o geometry: The geometry string (e.g. "100x100+10+10").
1234 % o x,y: The x and y offset, set according to the geometry specification.
1236 % o width,height: The width and height of original image, modified by
1237 % the given geometry specification.
1241 static inline size_t MagickMax(const size_t x,
1249 MagickExport MagickStatusType ParseMetaGeometry(const char *geometry,ssize_t *x,
1250 ssize_t *y,size_t *width,size_t *height)
1263 Ensure the image geometry is valid.
1265 assert(x != (ssize_t *) NULL);
1266 assert(y != (ssize_t *) NULL);
1267 assert(width != (size_t *) NULL);
1268 assert(height != (size_t *) NULL);
1269 if ((geometry == (char *) NULL) || (*geometry == '\0'))
1271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",geometry);
1273 Parse geometry using GetGeometry.
1275 SetGeometryInfo(&geometry_info);
1276 former_width=(*width);
1277 former_height=(*height);
1278 flags=GetGeometry(geometry,x,y,width,height);
1279 if ((flags & PercentValue) != 0)
1288 Geometry is a percentage of the image size.
1290 flags=ParseGeometry(geometry,&geometry_info);
1291 scale.x=geometry_info.rho;
1292 if ((flags & RhoValue) == 0)
1294 scale.y=geometry_info.sigma;
1295 if ((flags & SigmaValue) == 0)
1297 *width=(size_t) floor(scale.x*former_width/100.0+0.5);
1298 *height=(size_t) floor(scale.y*former_height/100.0+0.5);
1299 former_width=(*width);
1300 former_height=(*height);
1302 if (((flags & AspectValue) != 0) || ((*width == former_width) &&
1303 (*height == former_height)))
1305 if ((flags & RhoValue) == 0)
1306 *width=former_width;
1307 if ((flags & SigmaValue) == 0)
1308 *height=former_height;
1316 Respect aspect ratio of the image.
1318 if ((former_width == 0) || (former_height == 0))
1321 if (((flags & RhoValue) != 0) && (flags & SigmaValue) != 0)
1323 scale_factor=(double) *width/(double) former_width;
1324 if ((flags & MinimumValue) == 0)
1326 if (scale_factor > ((double) *height/(double) former_height))
1327 scale_factor=(double) *height/(double) former_height;
1330 if (scale_factor < ((double) *height/(double) former_height))
1331 scale_factor=(double) *height/(double) former_height;
1334 if ((flags & RhoValue) != 0)
1336 scale_factor=(double) *width/(double) former_width;
1337 if (((flags & MinimumValue) != 0) &&
1338 (scale_factor < ((double) *width/(double) former_height)))
1339 scale_factor=(double) *width/(double) former_height;
1343 scale_factor=(double) *height/(double) former_height;
1344 if (((flags & MinimumValue) != 0) &&
1345 (scale_factor < ((double) *height/(double) former_width)))
1346 scale_factor=(double) *height/(double) former_width;
1348 *width=MagickMax((size_t) floor(scale_factor*former_width+0.5),1UL);
1349 *height=MagickMax((size_t) floor(scale_factor*former_height+0.5),1UL);
1351 if ((flags & GreaterValue) != 0)
1353 if (former_width < *width)
1354 *width=former_width;
1355 if (former_height < *height)
1356 *height=former_height;
1358 if ((flags & LessValue) != 0)
1360 if (former_width > *width)
1361 *width=former_width;
1362 if (former_height > *height)
1363 *height=former_height;
1365 if ((flags & AreaValue) != 0)
1375 Geometry is a maximum area in pixels.
1377 (void) ParseGeometry(geometry,&geometry_info);
1378 area=geometry_info.rho;
1379 distance=sqrt((double) former_width*former_height);
1380 scale.x=former_width/(distance/sqrt(area));
1381 scale.y=former_height/(distance/sqrt(area));
1382 if ((scale.x < (double) *width) || (scale.y < (double) *height))
1384 *width=(unsigned long) (former_width/(distance/sqrt(area)));
1385 *height=(unsigned long) (former_height/(distance/sqrt(area)));
1387 former_width=(*width);
1388 former_height=(*height);
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 % P a r s e P a g e G e o m e t r y %
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % ParsePageGeometry() returns a region as defined by the geometry string with
1405 % respect to the image page (canvas) dimensions.
1407 % WARNING: Percentage dimensions remain relative to the actual image
1408 % dimensions, and not canvas dimensions.
1410 % The format of the ParsePageGeometry method is:
1412 % MagickStatusType ParsePageGeometry(const Image *image,
1413 % const char *geometry,RectangeInfo *region_info,
1414 % ExceptionInfo *exception)
1416 % A description of each parameter follows:
1418 % o geometry: The geometry string (e.g. "100x100+10+10").
1420 % o region_info: the region as defined by the geometry string with
1421 % respect to the image and its gravity.
1423 % o exception: return any errors or warnings in this structure.
1426 MagickExport MagickStatusType ParsePageGeometry(const Image *image,
1427 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1432 SetGeometry(image,region_info);
1433 if (image->page.width != 0)
1434 region_info->width=image->page.width;
1435 if (image->page.height != 0)
1436 region_info->height=image->page.height;
1437 flags=ParseAbsoluteGeometry(geometry,region_info);
1438 if (flags == NoValue)
1440 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1441 "InvalidGeometry","`%s'",geometry);
1444 if ((flags & PercentValue) != 0)
1446 region_info->width=image->columns;
1447 region_info->height=image->rows;
1449 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1450 ®ion_info->width,®ion_info->height);
1451 if ((((flags & WidthValue) != 0) || ((flags & HeightValue) != 0)) &&
1452 (((flags & PercentValue) != 0) || ((flags & SeparatorValue) == 0)))
1454 if ((flags & WidthValue) == 0)
1455 region_info->width=region_info->height;
1456 if ((flags & HeightValue) == 0)
1457 region_info->height=region_info->width;
1463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % P a r s e R e g i o n G e o m e t r y %
1471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1473 % ParseRegionGeometry() returns a region as defined by the geometry string
1474 % with respect to the image dimensions and aspect ratio.
1476 % This is basically a wrapper around ParseMetaGeometry. This is typically
1477 % used to parse a geometry string to work out the final integer dimensions
1478 % for image resizing.
1480 % The format of the ParseRegionGeometry method is:
1482 % MagickStatusType ParseRegionGeometry(const Image *image,
1483 % const char *geometry,RectangeInfo *region_info,
1484 % ExceptionInfo *exception)
1486 % A description of each parameter follows:
1488 % o geometry: The geometry string (e.g. "100x100+10+10").
1490 % o region_info: the region as defined by the geometry string.
1492 % o exception: return any errors or warnings in this structure.
1495 MagickExport MagickStatusType ParseRegionGeometry(const Image *image,
1496 const char *geometry,RectangleInfo *region_info,ExceptionInfo *exception)
1501 SetGeometry(image,region_info);
1502 flags=ParseMetaGeometry(geometry,®ion_info->x,®ion_info->y,
1503 ®ion_info->width,®ion_info->height);
1504 if (flags == NoValue)
1505 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1506 "InvalidGeometry","`%s'",geometry);
1511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1515 % S e t G e o m e t r y %
1519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1521 % SetGeometry() sets the geometry to its default values.
1523 % The format of the SetGeometry method is:
1525 % SetGeometry(const Image *image,RectangleInfo *geometry)
1527 % A description of each parameter follows:
1529 % o image: the image.
1531 % o geometry: the geometry.
1534 MagickExport void SetGeometry(const Image *image,RectangleInfo *geometry)
1536 assert(image != (Image *) NULL);
1537 assert(image->signature == MagickSignature);
1538 if (image->debug != MagickFalse)
1539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1540 assert(geometry != (RectangleInfo *) NULL);
1541 (void) ResetMagickMemory(geometry,0,sizeof(*geometry));
1542 geometry->width=image->columns;
1543 geometry->height=image->rows;
1547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1551 % S e t G e o m e t r y I n f o %
1555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1557 % SetGeometryInfo sets the GeometryInfo structure to its default values.
1559 % The format of the SetGeometryInfo method is:
1561 % SetGeometryInfo(GeometryInfo *geometry_info)
1563 % A description of each parameter follows:
1565 % o geometry_info: the geometry info structure.
1568 MagickExport void SetGeometryInfo(GeometryInfo *geometry_info)
1570 assert(geometry_info != (GeometryInfo *) NULL);
1571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1572 (void) ResetMagickMemory(geometry_info,0,sizeof(*geometry_info));