MagickWand  7.0.3
conjure.c
Go to the documentation of this file.
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 % Cristy %
17 % December 2001 %
18 % %
19 % %
20 % Copyright 1999-2019 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 % https://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 % The conjure program gives you the ability to perform custom image processing
37 % tasks from a script written in the Magick Scripting Language (MSL). MSL is
38 % XML-based and consists of action statements with attributes. Actions include
39 % reading an image, processing an image, getting attributes from an image,
40 % writing an image, and more. An attribute is a key/value pair that modifies
41 % the behavior of an action.
42 %
43 */
44 
45 /*
46  Include declarations.
47 */
48 #include "MagickWand/studio.h"
49 #include "MagickWand/MagickWand.h"
51 
52 /*
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 % %
55 % %
56 % %
57 + C o n j u r e I m a g e C o m m a n d %
58 % %
59 % %
60 % %
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %
63 % ConjureImageCommand() describes the format and characteristics of one or
64 % more image files. It will also report if an image is incomplete or corrupt.
65 % The information displayed includes the scene number, the file name, the
66 % width and height of the image, whether the image is colormapped or not,
67 % the number of colors in the image, the number of bytes in the image, the
68 % format of the image (JPEG, PNM, etc.), and finally the number of seconds
69 % it took to read and process the image.
70 %
71 % The format of the ConjureImageCommand method is:
72 %
73 % MagickBooleanType ConjureImageCommand(ImageInfo *image_info,int argc,
74 % char **argv,char **metadata,ExceptionInfo *exception)
75 %
76 % A description of each parameter follows:
77 %
78 % o image_info: the image info.
79 %
80 % o argc: the number of elements in the argument vector.
81 %
82 % o argv: A text array containing the command line arguments.
83 %
84 % o metadata: any metadata is returned here.
85 %
86 % o exception: return any errors or warnings in this structure.
87 %
88 */
89 
90 static MagickBooleanType ConjureUsage(void)
91 {
92  static const char
93  miscellaneous[] =
94  " -debug events display copious debugging information\n"
95  " -help print program options\n"
96  " -list type print a list of supported option arguments\n"
97  " -log format format of debugging information\n"
98  " -version print version information",
99  settings[] =
100  " -monitor monitor progress\n"
101  " -quiet suppress all warning messages\n"
102  " -regard-warnings pay attention to warning messages\n"
103  " -seed value seed a new sequence of pseudo-random numbers\n"
104  " -verbose print detailed information about the image";
105 
106  ListMagickVersion(stdout);
107  (void) printf("Usage: %s [options ...] file [ [options ...] file ...]\n",
108  GetClientName());
109  (void) printf("\nImage Settings:\n");
110  (void) puts(settings);
111  (void) printf("\nMiscellaneous Options:\n");
112  (void) puts(miscellaneous);
113  (void) printf("\nIn addition, define any key value pairs required by "
114  "your script. For\nexample,\n\n");
115  (void) printf(" conjure -size 100x100 -color blue -foo bar script.msl\n");
116  return(MagickFalse);
117 }
118 
119 WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
120  int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception)
121 {
122 #define DestroyConjure() \
123 { \
124  image=DestroyImageList(image); \
125  for (i=0; i < (ssize_t) argc; i++) \
126  argv[i]=DestroyString(argv[i]); \
127  argv=(char **) RelinquishMagickMemory(argv); \
128 }
129 #define ThrowConjureException(asperity,tag,option) \
130 { \
131  (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
132  option); \
133  DestroyConjure(); \
134  return(MagickFalse); \
135 }
136 #define ThrowConjureInvalidArgumentException(option,argument) \
137 { \
138  (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
139  "InvalidArgument","'%s': %s",option,argument); \
140  DestroyConjure(); \
141  return(MagickFalse); \
142 }
143 
144  char
145  filename[MagickPathExtent],
146  *option;
147 
148  Image
149  *image;
150 
151  MagickStatusType
152  status;
153 
154  register ssize_t
155  i;
156 
157  ssize_t
158  number_images;
159 
160  wand_unreferenced(metadata);
161 
162  /*
163  Set defaults.
164  */
165  assert(image_info != (ImageInfo *) NULL);
166  assert(image_info->signature == MagickCoreSignature);
167  if (image_info->debug != MagickFalse)
168  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
169  assert(exception != (ExceptionInfo *) NULL);
170  if (argc < 2)
171  return(ConjureUsage());
172  image=NewImageList();
173  number_images=0;
174  option=(char *) NULL;
175  /*
176  Conjure an image.
177  */
178  ReadCommandlLine(argc,&argv);
179  status=ExpandFilenames(&argc,&argv);
180  if (status == MagickFalse)
181  ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
182  GetExceptionMessage(errno));
183  for (i=1; i < (ssize_t) argc; i++)
184  {
185  option=argv[i];
186  if (IsCommandOption(option) != MagickFalse)
187  {
188  if (LocaleCompare("concurrent",option+1) == 0)
189  break;
190  if (LocaleCompare("debug",option+1) == 0)
191  {
192  ssize_t
193  event;
194 
195  if (*option == '+')
196  break;
197  i++;
198  if (i == (ssize_t) argc)
199  ThrowConjureException(OptionError,"MissingArgument",option);
200  event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]);
201  if (event < 0)
202  ThrowConjureException(OptionError,"UnrecognizedEventType",
203  argv[i]);
204  (void) SetLogEventMask(argv[i]);
205  continue;
206  }
207  if (LocaleCompare("duration",option+1) == 0)
208  {
209  if (*option == '+')
210  break;
211  i++;
212  if (i == (ssize_t) argc)
213  ThrowConjureException(OptionError,"MissingArgument",option);
214  if (IsGeometry(argv[i]) == MagickFalse)
215  ThrowConjureInvalidArgumentException(option,argv[i]);
216  continue;
217  }
218  if ((LocaleCompare("help",option+1) == 0) ||
219  (LocaleCompare("-help",option+1) == 0))
220  {
221  if (*option == '-')
222  return(ConjureUsage());
223  continue;
224  }
225  if (LocaleCompare("log",option+1) == 0)
226  {
227  if (*option == '-')
228  {
229  i++;
230  if (i == (ssize_t) argc)
231  ThrowConjureException(OptionError,"MissingLogFormat",option);
232  (void) SetLogFormat(argv[i]);
233  }
234  continue;
235  }
236  if (LocaleCompare("monitor",option+1) == 0)
237  continue;
238  if (LocaleCompare("quiet",option+1) == 0)
239  continue;
240  if (LocaleCompare("regard-warnings",option+1) == 0)
241  break;
242  if (LocaleCompare("seed",option+1) == 0)
243  {
244  if (*option == '+')
245  break;
246  i++;
247  if (i == (ssize_t) argc)
248  ThrowConjureException(OptionError,"MissingArgument",option);
249  if (IsGeometry(argv[i]) == MagickFalse)
250  ThrowConjureInvalidArgumentException(option,argv[i]);
251  break;
252  }
253  if (LocaleCompare("verbose",option+1) == 0)
254  {
255  image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse;
256  continue;
257  }
258  if ((LocaleCompare("version",option+1) == 0) ||
259  (LocaleCompare("-version",option+1) == 0))
260  {
261  ListMagickVersion(stdout);
262  return(MagickTrue);
263  }
264  /*
265  Persist key/value pair.
266  */
267  (void) DeleteImageOption(image_info,option+1);
268  status=SetImageOption(image_info,option+1,argv[i+1]);
269  if (status == MagickFalse)
270  ThrowConjureException(ImageError,"UnableToPersistKey",option);
271  i++;
272  continue;
273  }
274  /*
275  Interpret MSL script.
276  */
277  (void) DeleteImageOption(image_info,"filename");
278  status=SetImageOption(image_info,"filename",argv[i]);
279  if (status == MagickFalse)
280  ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]);
281  (void) FormatLocaleString(filename,MagickPathExtent,"%s",argv[i]);
282  image=ReadImages(image_info,filename,exception);
283  CatchException(exception);
284  if (image != (Image *) NULL)
285  image=DestroyImageList(image);
286  status=image != (Image *) NULL ? MagickTrue : MagickFalse;
287  number_images++;
288  }
289  if (i != (ssize_t) argc)
290  ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]);
291  if (number_images == 0)
292  ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]);
293  if (image != (Image *) NULL)
294  image=DestroyImageList(image);
295  for (i=0; i < (ssize_t) argc; i++)
296  argv[i]=DestroyString(argv[i]);
297  argv=(char **) RelinquishMagickMemory(argv);
298  return(status != 0 ? MagickTrue : MagickFalse);
299 }
#define ThrowConjureInvalidArgumentException(option, argument)
#define wand_unused(x)
#define ThrowConjureException(asperity, tag, option)
#define WandExport
#define MagickPathExtent
#define ReadCommandlLine(argc, argv)
Definition: studio.h:260
WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info, int argc, char **argv, char **wand_unused(metadata), ExceptionInfo *exception)
Definition: conjure.c:119
static MagickBooleanType ConjureUsage(void)
Definition: conjure.c:90
#define wand_unreferenced(x)