MagickWand  6.7.5
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-2012 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 "MagickWand/studio.h"
00049 #include "MagickWand/MagickWand.h"
00050 #include "MagickWand/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((size_t *) 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 addition, 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 < (ssize_t) 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   MagickStatusType
00164     status;
00165 
00166   register ssize_t
00167     i;
00168 
00169   ssize_t
00170     number_images;
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     return(ConjureUsage());
00182   image=NewImageList();
00183   number_images=0;
00184   option=(char *) NULL;
00185   /*
00186     Conjure an image.
00187   */
00188   ReadCommandlLine(argc,&argv);
00189   status=ExpandFilenames(&argc,&argv);
00190   if (status == MagickFalse)
00191     ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
00192       GetExceptionMessage(errno));
00193   for (i=1; i < (ssize_t) argc; i++)
00194   {
00195     option=argv[i];
00196     if (IsCommandOption(option) != MagickFalse)
00197       {
00198         if (LocaleCompare("concurrent",option+1) == 0)
00199           break;
00200         if (LocaleCompare("debug",option+1) == 0)
00201           {
00202             ssize_t
00203               event;
00204 
00205             if (*option == '+')
00206               break;
00207             i++;
00208             if (i == (ssize_t) argc)
00209               ThrowConjureException(OptionError,"MissingArgument",option);
00210             event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]);
00211             if (event < 0)
00212               ThrowConjureException(OptionError,"UnrecognizedEventType",
00213                 argv[i]);
00214             (void) SetLogEventMask(argv[i]);
00215             continue;
00216           }
00217         if (LocaleCompare("duration",option+1) == 0)
00218           {
00219             if (*option == '+')
00220               break;
00221             i++;
00222             if (i == (ssize_t) (argc-1))
00223               ThrowConjureException(OptionError,"MissingArgument",option);
00224             if (IsGeometry(argv[i]) == MagickFalse)
00225               ThrowConjureInvalidArgumentException(option,argv[i]);
00226             continue;
00227           }
00228         if ((LocaleCompare("help",option+1) == 0) ||
00229             (LocaleCompare("-help",option+1) == 0))
00230           {
00231             if (*option == '-')
00232               return(ConjureUsage());
00233             continue;
00234           }
00235         if (LocaleCompare("log",option+1) == 0)
00236           {
00237             if (*option == '-')
00238               {
00239                 i++;
00240                 if (i == (ssize_t) argc)
00241                   ThrowConjureException(OptionError,"MissingLogFormat",option);
00242                 (void) SetLogFormat(argv[i]);
00243               }
00244             continue;
00245           }
00246         if (LocaleCompare("monitor",option+1) == 0)
00247           continue;
00248         if (LocaleCompare("quiet",option+1) == 0)
00249           continue;
00250         if (LocaleCompare("regard-warnings",option+1) == 0)
00251           break;
00252         if (LocaleCompare("seed",option+1) == 0)
00253           {
00254             if (*option == '+')
00255               break;
00256             i++;
00257             if (i == (ssize_t) (argc-1))
00258               ThrowConjureException(OptionError,"MissingArgument",option);
00259             if (IsGeometry(argv[i]) == MagickFalse)
00260               ThrowConjureInvalidArgumentException(option,argv[i]);
00261             break;
00262           }
00263         if (LocaleCompare("verbose",option+1) == 0)
00264           {
00265             image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse;
00266             continue;
00267           }
00268         if ((LocaleCompare("version",option+1) == 0) ||
00269             (LocaleCompare("-version",option+1) == 0))
00270           {
00271             (void) FormatLocaleFile(stdout,"Version: %s\n",
00272               GetMagickVersion((size_t *) NULL));
00273             (void) FormatLocaleFile(stdout,"Copyright: %s\n",
00274               GetMagickCopyright());
00275             (void) FormatLocaleFile(stdout,"Features: %s\n\n",
00276               GetMagickFeatures());
00277             return(MagickFalse);
00278           }
00279         /*
00280           Persist key/value pair.
00281         */
00282         (void) DeleteImageOption(image_info,option+1);
00283         status=SetImageOption(image_info,option+1,argv[i+1]);
00284         if (status == MagickFalse)
00285           ThrowConjureException(ImageError,"UnableToPersistKey",option);
00286         i++;
00287         continue;
00288       }
00289     /*
00290       Interpret MSL script.
00291     */
00292     (void) DeleteImageOption(image_info,"filename");
00293     status=SetImageOption(image_info,"filename",argv[i]);
00294     if (status == MagickFalse)
00295       ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]);
00296     (void) FormatLocaleString(image_info->filename,MaxTextExtent,"msl:%s",
00297       argv[i]);
00298     image=ReadImages(image_info,exception);
00299     CatchException(exception);
00300     if (image != (Image *) NULL)
00301       image=DestroyImageList(image);
00302     status=image != (Image *) NULL ? MagickTrue : MagickFalse;
00303     number_images++;
00304   }
00305   if (i != (ssize_t) argc)
00306     ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]);
00307   if (number_images == 0)
00308     ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]);
00309   if (image != (Image *) NULL)
00310     image=DestroyImageList(image);
00311   for (i=0; i < (ssize_t) argc; i++)
00312     argv[i]=DestroyString(argv[i]);
00313   argv=(char **) RelinquishMagickMemory(argv);
00314   return(status != 0 ? MagickTrue : MagickFalse);
00315 }