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-2008 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 %  Conjure interprets and executes scripts in the Magick Scripting Language
00037 %  (MSL). The Magick scripting language (MSL) will primarily benefit those
00038 %  that want to accomplish custom image processing tasks but do not wish
00039 %  to program, or those that do not have access to a Perl interpreter or a
00040 %  compiler. The interpreter is called conjure and here is an example script:
00041 %
00042 %      <?xml version="1.0" encoding="UTF-8"?>
00043 %      <image size="400x400" >
00044 %      <read filename="image.gif" />
00045 %      <get width="base-width" height="base-height" />
00046 %      <resize geometry="%[dimensions]" />
00047 %      <get width="width" height="height" />
00048 %      <print output="Image sized from %[base-width]x%[base-height]
00049 %          to %[width]x%[height].\n" />
00050 %      <write filename="image.png" />
00051 %      </image>
00052 %
00053 %
00054 */
00055 
00056 /*
00057   Include declarations.
00058 */
00059 #include "wand/studio.h"
00060 #include "wand/MagickWand.h"
00061 #include "wand/mogrify-private.h"
00062 
00063 /*
00064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00065 %                                                                             %
00066 %                                                                             %
00067 %                                                                             %
00068 +   C o n j u r e I m a g e C o m m a n d                                     %
00069 %                                                                             %
00070 %                                                                             %
00071 %                                                                             %
00072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00073 %
00074 %  ConjureImageCommand() describes the format and characteristics of one or
00075 %  more image files. It will also report if an image is incomplete or corrupt.
00076 %  The information displayed includes the scene number, the file name, the
00077 %  width and height of the image, whether the image is colormapped or not,
00078 %  the number of colors in the image, the number of bytes in the image, the
00079 %  format of the image (JPEG, PNM, etc.), and finally the number of seconds
00080 %  it took to read and process the image.
00081 %
00082 %  The format of the ConjureImageCommand method is:
00083 %
00084 %      MagickBooleanType ConjureImageCommand(ImageInfo *image_info,int argc,
00085 %        char **argv,char **metadata,ExceptionInfo *exception)
00086 %
00087 %  A description of each parameter follows:
00088 %
00089 %    o image_info: the image info.
00090 %
00091 %    o argc: the number of elements in the argument vector.
00092 %
00093 %    o argv: A text array containing the command line arguments.
00094 %
00095 %    o metadata: any metadata is returned here.
00096 %
00097 %    o exception: return any errors or warnings in this structure.
00098 %
00099 */
00100 
00101 static void ConjureUsage(void)
00102 {
00103   const char
00104     **p;
00105 
00106   static const char
00107     *miscellaneous[]=
00108     {
00109       "-debug events        display copious debugging information",
00110       "-help                print program options",
00111       "-list type           print a list of supported option arguments",
00112       "-log format          format of debugging information",
00113       "-version             print version information",
00114       (char *) NULL
00115     },
00116     *settings[]=
00117     {
00118       "-monitor             monitor progress",
00119       "-quiet               suppress all warning messages",
00120       "-regard-warnings     pay attention to warning messages",
00121       "-seed value          seed a new sequence of pseudo-random numbers",
00122       "-verbose             print detailed information about the image",
00123       (char *) NULL
00124     };
00125 
00126   (void) printf("Version: %s\n",GetMagickVersion((unsigned long *) NULL));
00127   (void) printf("Copyright: %s\n\n",GetMagickCopyright());
00128   (void) printf("Usage: %s [options ...] file [ [options ...] file ...]\n",
00129     GetClientName());
00130   (void) printf("\nImage Settings:\n");
00131   for (p=settings; *p != (char *) NULL; p++)
00132     (void) printf("  %s\n",*p);
00133   (void) printf("\nMiscellaneous Options:\n");
00134   for (p=miscellaneous; *p != (char *) NULL; p++)
00135     (void) printf("  %s\n",*p);
00136   (void) printf("\nIn additiion, define any key value pairs required by "
00137     "your script.  For\nexample,\n\n");
00138   (void) printf("    conjure -size 100x100 -color blue -foo bar script.msl\n");
00139   exit(0);
00140 }
00141 
00142 WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
00143   int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception)
00144 {
00145 #define DestroyConjure() \
00146 { \
00147   image=DestroyImageList(image); \
00148   for (i=0; i < (long) argc; i++) \
00149     argv[i]=DestroyString(argv[i]); \
00150   argv=(char **) RelinquishMagickMemory(argv); \
00151 }
00152 #define ThrowConjureException(asperity,tag,option) \
00153 { \
00154   (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
00155      option); \
00156   DestroyConjure(); \
00157   return(MagickFalse); \
00158 }
00159 #define ThrowConjureInvalidArgumentException(option,argument) \
00160 { \
00161   (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
00162     "InvalidArgument","`%s': %s",argument,option); \
00163   DestroyConjure(); \
00164   return(MagickFalse); \
00165 }
00166 
00167   char
00168     *option;
00169 
00170   Image
00171     *image;
00172 
00173   long
00174     number_images;
00175 
00176   MagickStatusType
00177     status;
00178 
00179   register long
00180     i;
00181 
00182   /*
00183     Set defaults.
00184   */
00185   assert(image_info != (ImageInfo *) NULL);
00186   assert(image_info->signature == MagickSignature);
00187   if (image_info->debug != MagickFalse)
00188     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00189   assert(exception != (ExceptionInfo *) NULL);
00190   if (argc < 2)
00191     ConjureUsage();
00192   image=NewImageList();
00193   number_images=0;
00194   option=(char *) NULL;
00195   /*
00196     Conjure an image.
00197   */
00198   ReadCommandlLine(argc,&argv);
00199   status=ExpandFilenames(&argc,&argv);
00200   if (status == MagickFalse)
00201     ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
00202       strerror(errno));
00203   for (i=1; i < (long) argc; i++)
00204   {
00205     option=argv[i];
00206     if (IsMagickOption(option) != MagickFalse)
00207       {
00208         if (LocaleCompare("debug",option+1) == 0)
00209           {
00210             long
00211               event;
00212 
00213             if (*option == '+')
00214               break;
00215             i++;
00216             if (i == (long) argc)
00217               ThrowConjureException(OptionError,"MissingArgument",option);
00218             event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
00219             if (event < 0)
00220               ThrowConjureException(OptionError,"UnrecognizedEventType",
00221                 argv[i]);
00222             (void) SetLogEventMask(argv[i]);
00223             continue;
00224           }
00225         if ((LocaleCompare("help",option+1) == 0) ||
00226             (LocaleCompare("-help",option+1) == 0))
00227           {
00228             if (*option == '-')
00229               ConjureUsage();
00230             continue;
00231           }
00232         if (LocaleCompare("log",option+1) == 0)
00233           {
00234             if (*option == '-')
00235               {
00236                 i++;
00237                 if (i == (long) argc)
00238                   ThrowConjureException(OptionError,"MissingLogFormat",option);
00239                 (void) SetLogFormat(argv[i]);
00240               }
00241             continue;
00242           }
00243         if (LocaleCompare("monitor",option+1) == 0)
00244           continue;
00245         if (LocaleCompare("quiet",option+1) == 0)
00246           continue;
00247         if (LocaleCompare("regard-warnings",option+1) == 0)
00248           break;
00249         if (LocaleCompare("seed",option+1) == 0)
00250           {
00251             if (*option == '+')
00252               break;
00253             i++;
00254             if (i == (long) (argc-1))
00255               ThrowConjureException(OptionError,"MissingArgument",option);
00256             if (IsGeometry(argv[i]) == MagickFalse)
00257               ThrowConjureInvalidArgumentException(option,argv[i]);
00258             break;
00259           }
00260         if (LocaleCompare("verbose",option+1) == 0)
00261           {
00262             image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse;
00263             continue;
00264           }
00265         if ((LocaleCompare("version",option+1) == 0) ||
00266             (LocaleCompare("-version",option+1) == 0))
00267           {
00268             (void) fprintf(stdout,"Version: %s\n",
00269               GetMagickVersion((unsigned long *) NULL));
00270             (void) fprintf(stdout,"Copyright: %s\n\n",GetMagickCopyright());
00271             exit(0);
00272             continue;
00273           }
00274         /*
00275           Persist key/value pair.
00276         */
00277         (void) DeleteImageOption(image_info,option+1);
00278         status=SetImageOption(image_info,option+1,argv[i+1]);
00279         if (status == MagickFalse)
00280           ThrowConjureException(ImageError,"UnableToPersistKey",option);
00281         i++;
00282         continue;
00283       }
00284     /*
00285       Interpret MSL script.
00286     */
00287     (void) DeleteImageOption(image_info,"filename");
00288     status=SetImageOption(image_info,"filename",argv[i]);
00289     if (status == MagickFalse)
00290       ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]);
00291     (void) FormatMagickString(image_info->filename,MaxTextExtent,"msl:%s",
00292       argv[i]);
00293     image=ReadImages(image_info,exception);
00294     CatchException(exception);
00295     if (image != (Image *) NULL)
00296       image=DestroyImageList(image);
00297     status=image != (Image *) NULL ? MagickTrue : MagickFalse;
00298     number_images++;
00299   }
00300   if (i != argc)
00301     ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]);
00302   if (number_images == 0)
00303     ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]);
00304   if (image != (Image *) NULL)
00305     image=DestroyImageList(image);
00306   for (i=0; i < (long) argc; i++)
00307     argv[i]=DestroyString(argv[i]);
00308   argv=(char **) RelinquishMagickMemory(argv);
00309   return(status != 0 ? MagickTrue : MagickFalse);
00310 }

Generated on Sat Nov 22 23:45:25 2008 for MagickWand by  doxygen 1.5.7.1