conjure.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                CCCC   OOO   N   N  JJJJJ  U   U  RRRR   EEEEE               %
00007 %               C      O   O  NN  N    J    U   U  R   R  E                   %
00008 %               C      O   O  N N N    J    U   U  RRRR   EEE                 %
00009 %               C      O   O  N  NN  J J    U   U  R R    E                   %
00010 %                CCCC   OOO   N   N  JJJ     UUU   R  R   EEEEE               %
00011 %                                                                             %
00012 %                                                                             %
00013 %                     Interpret Magick Scripting Language.                    %
00014 %                                                                             %
00015 %                              Software Design                                %
00016 %                                John Cristy                                  %
00017 %                               December 2001                                 %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.imagemagick.org/script/license.php                            %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %  The conjure program gives you the ability to perform custom image processing
00037 %  tasks from a script written in the Magick Scripting Language (MSL). MSL is
00038 %  XML-based and consists of action statements with attributes. Actions include
00039 %  reading an image, processing an image, getting attributes from an image,
00040 %  writing an image, and more. An attribute is a key/value pair that modifies
00041 %  the behavior of an action.
00042 %
00043 */
00044 
00045 /*
00046   Include declarations.
00047 */
00048 #include "wand/studio.h"
00049 #include "wand/MagickWand.h"
00050 #include "wand/mogrify-private.h"
00051 
00052 /*
00053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00054 %                                                                             %
00055 %                                                                             %
00056 %                                                                             %
00057 +   C o n j u r e I m a g e C o m m a n d                                     %
00058 %                                                                             %
00059 %                                                                             %
00060 %                                                                             %
00061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00062 %
00063 %  ConjureImageCommand() describes the format and characteristics of one or
00064 %  more image files. It will also report if an image is incomplete or corrupt.
00065 %  The information displayed includes the scene number, the file name, the
00066 %  width and height of the image, whether the image is colormapped or not,
00067 %  the number of colors in the image, the number of bytes in the image, the
00068 %  format of the image (JPEG, PNM, etc.), and finally the number of seconds
00069 %  it took to read and process the image.
00070 %
00071 %  The format of the ConjureImageCommand method is:
00072 %
00073 %      MagickBooleanType ConjureImageCommand(ImageInfo *image_info,int argc,
00074 %        char **argv,char **metadata,ExceptionInfo *exception)
00075 %
00076 %  A description of each parameter follows:
00077 %
00078 %    o image_info: the image info.
00079 %
00080 %    o argc: the number of elements in the argument vector.
00081 %
00082 %    o argv: A text array containing the command line arguments.
00083 %
00084 %    o metadata: any metadata is returned here.
00085 %
00086 %    o exception: return any errors or warnings in this structure.
00087 %
00088 */
00089 
00090 static MagickBooleanType ConjureUsage(void)
00091 {
00092   const char
00093     **p;
00094 
00095   static const char
00096     *miscellaneous[]=
00097     {
00098       "-debug events        display copious debugging information",
00099       "-help                print program options",
00100       "-list type           print a list of supported option arguments",
00101       "-log format          format of debugging information",
00102       "-version             print version information",
00103       (char *) NULL
00104     },
00105     *settings[]=
00106     {
00107       "-monitor             monitor progress",
00108       "-quiet               suppress all warning messages",
00109       "-regard-warnings     pay attention to warning messages",
00110       "-seed value          seed a new sequence of pseudo-random numbers",
00111       "-verbose             print detailed information about the image",
00112       (char *) NULL
00113     };
00114 
00115   (void) printf("Version: %s\n",GetMagickVersion((unsigned long *) NULL));
00116   (void) printf("Copyright: %s\n",GetMagickCopyright());
00117   (void) printf("Features: %s\n\n",GetMagickFeatures());
00118   (void) printf("Usage: %s [options ...] file [ [options ...] file ...]\n",
00119     GetClientName());
00120   (void) printf("\nImage Settings:\n");
00121   for (p=settings; *p != (char *) NULL; p++)
00122     (void) printf("  %s\n",*p);
00123   (void) printf("\nMiscellaneous Options:\n");
00124   for (p=miscellaneous; *p != (char *) NULL; p++)
00125     (void) printf("  %s\n",*p);
00126   (void) printf("\nIn additiion, define any key value pairs required by "
00127     "your script.  For\nexample,\n\n");
00128   (void) printf("    conjure -size 100x100 -color blue -foo bar script.msl\n");
00129   return(MagickFalse);
00130 }
00131 
00132 WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
00133   int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception)
00134 {
00135 #define DestroyConjure() \
00136 { \
00137   image=DestroyImageList(image); \
00138   for (i=0; i < (long) argc; i++) \
00139     argv[i]=DestroyString(argv[i]); \
00140   argv=(char **) RelinquishMagickMemory(argv); \
00141 }
00142 #define ThrowConjureException(asperity,tag,option) \
00143 { \
00144   (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
00145      option); \
00146   DestroyConjure(); \
00147   return(MagickFalse); \
00148 }
00149 #define ThrowConjureInvalidArgumentException(option,argument) \
00150 { \
00151   (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
00152     "InvalidArgument","`%s': %s",option,argument); \
00153   DestroyConjure(); \
00154   return(MagickFalse); \
00155 }
00156 
00157   char
00158     *option;
00159 
00160   Image
00161     *image;
00162 
00163   long
00164     number_images;
00165 
00166   MagickStatusType
00167     status;
00168 
00169   register long
00170     i;
00171 
00172   /*
00173     Set defaults.
00174   */
00175   assert(image_info != (ImageInfo *) NULL);
00176   assert(image_info->signature == MagickSignature);
00177   if (image_info->debug != MagickFalse)
00178     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00179   assert(exception != (ExceptionInfo *) NULL);
00180   if (argc < 2)
00181     {
00182       (void) ConjureUsage();
00183       return(MagickTrue);
00184     }
00185   image=NewImageList();
00186   number_images=0;
00187   option=(char *) NULL;
00188   (void) respect_parenthesis;
00189   /*
00190     Conjure an image.
00191   */
00192   ReadCommandlLine(argc,&argv);
00193   status=ExpandFilenames(&argc,&argv);
00194   if (status == MagickFalse)
00195     ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
00196       GetExceptionMessage(errno));
00197   for (i=1; i < (long) argc; i++)
00198   {
00199     option=argv[i];
00200     if (IsMagickOption(option) != MagickFalse)
00201       {
00202         if (LocaleCompare("concurrent",option+1) == 0)
00203           break;
00204         if (LocaleCompare("debug",option+1) == 0)
00205           {
00206             long
00207               event;
00208 
00209             if (*option == '+')
00210               break;
00211             i++;
00212             if (i == (long) argc)
00213               ThrowConjureException(OptionError,"MissingArgument",option);
00214             event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
00215             if (event < 0)
00216               ThrowConjureException(OptionError,"UnrecognizedEventType",
00217                 argv[i]);
00218             (void) SetLogEventMask(argv[i]);
00219             continue;
00220           }
00221         if (LocaleCompare("duration",option+1) == 0)
00222           {
00223             if (*option == '+')
00224               break;
00225             i++;
00226             if (i == (long) (argc-1))
00227               ThrowConjureException(OptionError,"MissingArgument",option);
00228             if (IsGeometry(argv[i]) == MagickFalse)
00229               ThrowConjureInvalidArgumentException(option,argv[i]);
00230             continue;
00231           }
00232         if ((LocaleCompare("help",option+1) == 0) ||
00233             (LocaleCompare("-help",option+1) == 0))
00234           {
00235             if (*option == '-')
00236               return(ConjureUsage());
00237             continue;
00238           }
00239         if (LocaleCompare("log",option+1) == 0)
00240           {
00241             if (*option == '-')
00242               {
00243                 i++;
00244                 if (i == (long) argc)
00245                   ThrowConjureException(OptionError,"MissingLogFormat",option);
00246                 (void) SetLogFormat(argv[i]);
00247               }
00248             continue;
00249           }
00250         if (LocaleCompare("monitor",option+1) == 0)
00251           continue;
00252         if (LocaleCompare("quiet",option+1) == 0)
00253           continue;
00254         if (LocaleCompare("regard-warnings",option+1) == 0)
00255           break;
00256         if (LocaleCompare("seed",option+1) == 0)
00257           {
00258             if (*option == '+')
00259               break;
00260             i++;
00261             if (i == (long) (argc-1))
00262               ThrowConjureException(OptionError,"MissingArgument",option);
00263             if (IsGeometry(argv[i]) == MagickFalse)
00264               ThrowConjureInvalidArgumentException(option,argv[i]);
00265             break;
00266           }
00267         if (LocaleCompare("verbose",option+1) == 0)
00268           {
00269             image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse;
00270             continue;
00271           }
00272         if ((LocaleCompare("version",option+1) == 0) ||
00273             (LocaleCompare("-version",option+1) == 0))
00274           {
00275             (void) fprintf(stdout,"Version: %s\n",
00276               GetMagickVersion((unsigned long *) NULL));
00277             (void) fprintf(stdout,"Copyright: %s\n",GetMagickCopyright());
00278             (void) fprintf(stdout,"Features: %s\n\n",GetMagickFeatures());
00279             return(MagickFalse);
00280           }
00281         /*
00282           Persist key/value pair.
00283         */
00284         (void) DeleteImageOption(image_info,option+1);
00285         status=SetImageOption(image_info,option+1,argv[i+1]);
00286         if (status == MagickFalse)
00287           ThrowConjureException(ImageError,"UnableToPersistKey",option);
00288         i++;
00289         continue;
00290       }
00291     /*
00292       Interpret MSL script.
00293     */
00294     (void) DeleteImageOption(image_info,"filename");
00295     status=SetImageOption(image_info,"filename",argv[i]);
00296     if (status == MagickFalse)
00297       ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]);
00298     (void) FormatMagickString(image_info->filename,MaxTextExtent,"msl:%s",
00299       argv[i]);
00300     image=ReadImages(image_info,exception);
00301     CatchException(exception);
00302     if (image != (Image *) NULL)
00303       image=DestroyImageList(image);
00304     status=image != (Image *) NULL ? MagickTrue : MagickFalse;
00305     number_images++;
00306   }
00307   if (i != argc)
00308     ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]);
00309   if (number_images == 0)
00310     ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]);
00311   if (image != (Image *) NULL)
00312     image=DestroyImageList(image);
00313   for (i=0; i < (long) argc; i++)
00314     argv[i]=DestroyString(argv[i]);
00315   argv=(char **) RelinquishMagickMemory(argv);
00316   return(status != 0 ? MagickTrue : MagickFalse);
00317 }

Generated on 19 Nov 2009 for MagickWand by  doxygen 1.6.1