root / ImageMagick / trunk / wand / conjure.c

Revision 11956, 11.8 kB (checked in by cristy, 10 days ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%                CCCC   OOO   N   N  JJJJJ  U   U  RRRR   EEEEE               %
7%               C      O   O  NN  N    J    U   U  R   R  E                   %
8%               C      O   O  N N N    J    U   U  RRRR   EEE                 %
9%               C      O   O  N  NN  J J    U   U  R R    E                   %
10%                CCCC   OOO   N   N  JJJ     UUU   R  R   EEEEE               %
11%                                                                             %
12%                                                                             %
13%                     Interpret Magick Scripting Language.                    %
14%                                                                             %
15%                              Software Design                                %
16%                                John Cristy                                  %
17%                               December 2001                                 %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2008 ImageMagick Studio LLC, a non-profit organization      %
21%  dedicated to making software imaging solutions freely available.           %
22%                                                                             %
23%  You may not use this file except in compliance with the License.  You may  %
24%  obtain a copy of the License at                                            %
25%                                                                             %
26%    http://www.imagemagick.org/script/license.php                            %
27%                                                                             %
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.                                             %
33%                                                                             %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%  Conjure interprets and executes scripts in the Magick Scripting Language
37%  (MSL). The Magick scripting language (MSL) will primarily benefit those
38%  that want to accomplish custom image processing tasks but do not wish
39%  to program, or those that do not have access to a Perl interpreter or a
40%  compiler. The interpreter is called conjure and here is an example script:
41%
42%      <?xml version="1.0" encoding="UTF-8"?>
43%      <image size="400x400" >
44%      <read filename="image.gif" />
45%      <get width="base-width" height="base-height" />
46%      <resize geometry="%[dimensions]" />
47%      <get width="width" height="height" />
48%      <print output="Image sized from %[base-width]x%[base-height]
49%          to %[width]x%[height].\n" />
50%      <write filename="image.png" />
51%      </image>
52%
53%
54*/
55
56/*
57  Include declarations.
58*/
59#include "wand/studio.h"
60#include "wand/MagickWand.h"
61#include "wand/mogrify-private.h"
62
63/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65%                                                                             %
66%                                                                             %
67%                                                                             %
68+   C o n j u r e I m a g e C o m m a n d                                     %
69%                                                                             %
70%                                                                             %
71%                                                                             %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74%  ConjureImageCommand() describes the format and characteristics of one or
75%  more image files. It will also report if an image is incomplete or corrupt.
76%  The information displayed includes the scene number, the file name, the
77%  width and height of the image, whether the image is colormapped or not,
78%  the number of colors in the image, the number of bytes in the image, the
79%  format of the image (JPEG, PNM, etc.), and finally the number of seconds
80%  it took to read and process the image.
81%
82%  The format of the ConjureImageCommand method is:
83%
84%      MagickBooleanType ConjureImageCommand(ImageInfo *image_info,int argc,
85%        char **argv,char **metadata,ExceptionInfo *exception)
86%
87%  A description of each parameter follows:
88%
89%    o image_info: the image info.
90%
91%    o argc: the number of elements in the argument vector.
92%
93%    o argv: A text array containing the command line arguments.
94%
95%    o metadata: any metadata is returned here.
96%
97%    o exception: Return any errors or warnings in this structure.
98%
99*/
100
101static void ConjureUsage(void)
102{
103  const char
104    **p;
105
106  static const char
107    *miscellaneous[]=
108    {
109      "-debug events        display copious debugging information",
110      "-help                print program options",
111      "-list type           print a list of supported option arguments",
112      "-log format          format of debugging information",
113      "-version             print version information",
114      (char *) NULL
115    },
116    *settings[]=
117    {
118      "-monitor             monitor progress",
119      "-quiet               suppress all warning messages",
120      "-regard-warnings     pay attention to warning messages",
121      "-seed value          seed a new sequence of pseudo-random numbers",
122      "-verbose             print detailed information about the image",
123      (char *) NULL
124    };
125
126  (void) printf("Version: %s\n",GetMagickVersion((unsigned long *) NULL));
127  (void) printf("Copyright: %s\n\n",GetMagickCopyright());
128  (void) printf("Usage: %s [options ...] file [ [options ...] file ...]\n",
129    GetClientName());
130  (void) printf("\nImage Settings:\n");
131  for (p=settings; *p != (char *) NULL; p++)
132    (void) printf("  %s\n",*p);
133  (void) printf("\nMiscellaneous Options:\n");
134  for (p=miscellaneous; *p != (char *) NULL; p++)
135    (void) printf("  %s\n",*p);
136  (void) printf("\nIn additiion, define any key value pairs required by "
137    "your script.  For\nexample,\n\n");
138  (void) printf("    conjure -size 100x100 -color blue -foo bar script.msl\n");
139  exit(0);
140}
141
142WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
143  int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception)
144{
145#define DestroyConjure() \
146{ \
147  image=DestroyImageList(image); \
148  for (i=0; i < (long) argc; i++) \
149    argv[i]=DestroyString(argv[i]); \
150  argv=(char **) RelinquishMagickMemory(argv); \
151}
152#define ThrowConjureException(asperity,tag,option) \
153{ \
154  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
155     option); \
156  DestroyConjure(); \
157  return(MagickFalse); \
158}
159#define ThrowConjureInvalidArgumentException(option,argument) \
160{ \
161  (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
162    "InvalidArgument","`%s': %s",argument,option); \
163  DestroyConjure(); \
164  return(MagickFalse); \
165}
166
167  char
168    *option;
169
170  Image
171    *image;
172
173  long
174    number_images;
175
176  MagickStatusType
177    status;
178
179  register long
180    i;
181
182  /*
183    Set defaults.
184  */
185  assert(image_info != (ImageInfo *) NULL);
186  assert(image_info->signature == MagickSignature);
187  if (image_info->debug != MagickFalse)
188    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
189  assert(exception != (ExceptionInfo *) NULL);
190  if (argc < 2)
191    ConjureUsage();
192  image=NewImageList();
193  number_images=0;
194  option=(char *) NULL;
195  /*
196    Conjure an image.
197  */
198  ReadCommandlLine(argc,&argv);
199  status=ExpandFilenames(&argc,&argv);
200  if (status == MagickFalse)
201    ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
202      strerror(errno));
203  for (i=1; i < (long) argc; i++)
204  {
205    option=argv[i];
206    if (IsMagickOption(option) != MagickFalse)
207      {
208        if (LocaleCompare("debug",option+1) == 0)
209          {
210            long
211              event;
212
213            if (*option == '+')
214              break;
215            i++;
216            if (i == (long) argc)
217              ThrowConjureException(OptionError,"MissingArgument",option);
218            event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
219            if (event < 0)
220              ThrowConjureException(OptionError,"UnrecognizedEventType",
221                argv[i]);
222            (void) SetLogEventMask(argv[i]);
223            continue;
224          }
225        if ((LocaleCompare("help",option+1) == 0) ||
226            (LocaleCompare("-help",option+1) == 0))
227          {
228            if (*option == '-')
229              ConjureUsage();
230            continue;
231          }
232        if (LocaleCompare("log",option+1) == 0)
233          {
234            if (*option == '-')
235              {
236                i++;
237                if (i == (long) argc)
238                  ThrowConjureException(OptionError,"MissingLogFormat",option);
239                (void) SetLogFormat(argv[i]);
240              }
241            continue;
242          }
243        if (LocaleCompare("monitor",option+1) == 0)
244          continue;
245        if (LocaleCompare("quiet",option+1) == 0)
246          continue;
247        if (LocaleCompare("regard-warnings",option+1) == 0)
248          break;
249        if (LocaleCompare("seed",option+1) == 0)
250          {
251            if (*option == '+')
252              break;
253            i++;
254            if (i == (long) (argc-1))
255              ThrowConjureException(OptionError,"MissingArgument",option);
256            if (IsGeometry(argv[i]) == MagickFalse)
257              ThrowConjureInvalidArgumentException(option,argv[i]);
258            break;
259          }
260        if (LocaleCompare("verbose",option+1) == 0)
261          {
262            image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse;
263            continue;
264          }
265        if ((LocaleCompare("version",option+1) == 0) ||
266            (LocaleCompare("-version",option+1) == 0))
267          {
268            (void) fprintf(stdout,"Version: %s\n",
269              GetMagickVersion((unsigned long *) NULL));
270            (void) fprintf(stdout,"Copyright: %s\n\n",GetMagickCopyright());
271            exit(0);
272            continue;
273          }
274        /*
275          Persist key/value pair.
276        */
277        (void) DeleteImageOption(image_info,option+1);
278        status&=SetImageOption(image_info,option+1,argv[i+1]);
279        if (status == MagickFalse)
280          ThrowConjureException(ImageError,"UnableToPersistKey",option);
281        i++;
282        continue;
283      }
284    /*
285      Interpret MSL script.
286    */
287    (void) DeleteImageOption(image_info,"filename");
288    status&=SetImageOption(image_info,"filename",argv[i]);
289    if (status == MagickFalse)
290      ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]);
291    (void) FormatMagickString(image_info->filename,MaxTextExtent,"msl:%s",
292      argv[i]);
293    image=ReadImage(image_info,exception);
294    CatchException(exception);
295    status&=image != (Image *) NULL;
296    if (image != (Image *) NULL)
297      image=DestroyImageList(image);
298    number_images++;
299  }
300  if (i != argc)
301    ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]);
302  if (number_images == 0)
303    ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]);
304  if (image != (Image *) NULL)
305    image=DestroyImageList(image);
306  for (i=0; i < (long) argc; i++)
307    argv[i]=DestroyString(argv[i]);
308  argv=(char **) RelinquishMagickMemory(argv);
309  return(status != 0 ? MagickTrue : MagickFalse);
310}
Note: See TracBrowser for help on using the browser.