cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | % % |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | % 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 48 | #include "MagickWand/studio.h" |
| 49 | #include "MagickWand/MagickWand.h" |
| 50 | #include "MagickWand/mogrify-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | const char |
| 93 | **p; |
| 94 | |
| 95 | static const char |
| 96 | *miscellaneous[]= |
| 97 | { |
| 98 | "-debug events display copious debugging information", |
| 99 | "-help print program options", |
| 100 | "-list type print a list of supported option arguments", |
| 101 | "-log format format of debugging information", |
| 102 | "-version print version information", |
| 103 | (char *) NULL |
| 104 | }, |
| 105 | *settings[]= |
| 106 | { |
| 107 | "-monitor monitor progress", |
| 108 | "-quiet suppress all warning messages", |
| 109 | "-regard-warnings pay attention to warning messages", |
| 110 | "-seed value seed a new sequence of pseudo-random numbers", |
| 111 | "-verbose print detailed information about the image", |
| 112 | (char *) NULL |
| 113 | }; |
| 114 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 115 | (void) printf("Version: %s\n",GetMagickVersion((size_t *) NULL)); |
cristy | 610b2e2 | 2009-10-22 14:59:43 +0000 | [diff] [blame] | 116 | (void) printf("Copyright: %s\n",GetMagickCopyright()); |
| 117 | (void) printf("Features: %s\n\n",GetMagickFeatures()); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 118 | (void) printf("Usage: %s [options ...] file [ [options ...] file ...]\n", |
| 119 | GetClientName()); |
| 120 | (void) printf("\nImage Settings:\n"); |
| 121 | for (p=settings; *p != (char *) NULL; p++) |
| 122 | (void) printf(" %s\n",*p); |
| 123 | (void) printf("\nMiscellaneous Options:\n"); |
| 124 | for (p=miscellaneous; *p != (char *) NULL; p++) |
| 125 | (void) printf(" %s\n",*p); |
glennrp | ac09427 | 2010-11-13 13:07:11 +0000 | [diff] [blame] | 126 | (void) printf("\nIn addition, define any key value pairs required by " |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 127 | "your script. For\nexample,\n\n"); |
| 128 | (void) printf(" conjure -size 100x100 -color blue -foo bar script.msl\n"); |
| 129 | return(MagickFalse); |
| 130 | } |
| 131 | |
| 132 | WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info, |
| 133 | int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception) |
| 134 | { |
| 135 | #define DestroyConjure() \ |
| 136 | { \ |
| 137 | image=DestroyImageList(image); \ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 138 | for (i=0; i < (ssize_t) argc; i++) \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 139 | argv[i]=DestroyString(argv[i]); \ |
| 140 | argv=(char **) RelinquishMagickMemory(argv); \ |
| 141 | } |
| 142 | #define ThrowConjureException(asperity,tag,option) \ |
| 143 | { \ |
| 144 | (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \ |
| 145 | option); \ |
| 146 | DestroyConjure(); \ |
| 147 | return(MagickFalse); \ |
| 148 | } |
| 149 | #define ThrowConjureInvalidArgumentException(option,argument) \ |
| 150 | { \ |
| 151 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \ |
| 152 | "InvalidArgument","`%s': %s",option,argument); \ |
| 153 | DestroyConjure(); \ |
| 154 | return(MagickFalse); \ |
| 155 | } |
| 156 | |
| 157 | char |
| 158 | *option; |
| 159 | |
| 160 | Image |
| 161 | *image; |
| 162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 163 | MagickStatusType |
| 164 | status; |
| 165 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 166 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 167 | i; |
| 168 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 169 | ssize_t |
| 170 | number_images; |
| 171 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 172 | /* |
| 173 | Set defaults. |
| 174 | */ |
| 175 | assert(image_info != (ImageInfo *) NULL); |
| 176 | assert(image_info->signature == MagickSignature); |
| 177 | if (image_info->debug != MagickFalse) |
| 178 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 179 | assert(exception != (ExceptionInfo *) NULL); |
| 180 | if (argc < 2) |
cristy | 13e61a1 | 2010-02-04 20:19:00 +0000 | [diff] [blame] | 181 | return(ConjureUsage()); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | image=NewImageList(); |
| 183 | number_images=0; |
| 184 | option=(char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 185 | /* |
| 186 | Conjure an image. |
| 187 | */ |
| 188 | ReadCommandlLine(argc,&argv); |
| 189 | status=ExpandFilenames(&argc,&argv); |
| 190 | if (status == MagickFalse) |
| 191 | ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed", |
| 192 | GetExceptionMessage(errno)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 193 | for (i=1; i < (ssize_t) argc; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | { |
| 195 | option=argv[i]; |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 196 | if (IsCommandOption(option) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 197 | { |
cristy | 2287975 | 2009-10-25 23:55:40 +0000 | [diff] [blame] | 198 | if (LocaleCompare("concurrent",option+1) == 0) |
| 199 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 200 | if (LocaleCompare("debug",option+1) == 0) |
| 201 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 202 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | event; |
| 204 | |
| 205 | if (*option == '+') |
| 206 | break; |
| 207 | i++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 208 | if (i == (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 209 | ThrowConjureException(OptionError,"MissingArgument",option); |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 210 | event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 211 | if (event < 0) |
| 212 | ThrowConjureException(OptionError,"UnrecognizedEventType", |
| 213 | argv[i]); |
| 214 | (void) SetLogEventMask(argv[i]); |
| 215 | continue; |
| 216 | } |
cristy | 2287975 | 2009-10-25 23:55:40 +0000 | [diff] [blame] | 217 | if (LocaleCompare("duration",option+1) == 0) |
| 218 | { |
| 219 | if (*option == '+') |
| 220 | break; |
| 221 | i++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 222 | if (i == (ssize_t) (argc-1)) |
cristy | 2287975 | 2009-10-25 23:55:40 +0000 | [diff] [blame] | 223 | ThrowConjureException(OptionError,"MissingArgument",option); |
| 224 | if (IsGeometry(argv[i]) == MagickFalse) |
| 225 | ThrowConjureInvalidArgumentException(option,argv[i]); |
| 226 | continue; |
| 227 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 228 | if ((LocaleCompare("help",option+1) == 0) || |
| 229 | (LocaleCompare("-help",option+1) == 0)) |
| 230 | { |
| 231 | if (*option == '-') |
| 232 | return(ConjureUsage()); |
| 233 | continue; |
| 234 | } |
| 235 | if (LocaleCompare("log",option+1) == 0) |
| 236 | { |
| 237 | if (*option == '-') |
| 238 | { |
| 239 | i++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 240 | if (i == (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | ThrowConjureException(OptionError,"MissingLogFormat",option); |
| 242 | (void) SetLogFormat(argv[i]); |
| 243 | } |
| 244 | continue; |
| 245 | } |
| 246 | if (LocaleCompare("monitor",option+1) == 0) |
| 247 | continue; |
| 248 | if (LocaleCompare("quiet",option+1) == 0) |
| 249 | continue; |
| 250 | if (LocaleCompare("regard-warnings",option+1) == 0) |
| 251 | break; |
| 252 | if (LocaleCompare("seed",option+1) == 0) |
| 253 | { |
| 254 | if (*option == '+') |
| 255 | break; |
| 256 | i++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 257 | if (i == (ssize_t) (argc-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | ThrowConjureException(OptionError,"MissingArgument",option); |
| 259 | if (IsGeometry(argv[i]) == MagickFalse) |
| 260 | ThrowConjureInvalidArgumentException(option,argv[i]); |
| 261 | break; |
| 262 | } |
| 263 | if (LocaleCompare("verbose",option+1) == 0) |
| 264 | { |
| 265 | image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse; |
| 266 | continue; |
| 267 | } |
| 268 | if ((LocaleCompare("version",option+1) == 0) || |
| 269 | (LocaleCompare("-version",option+1) == 0)) |
| 270 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 271 | (void) FormatLocaleFile(stdout,"Version: %s\n", |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 272 | GetMagickVersion((size_t *) NULL)); |
cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 273 | (void) FormatLocaleFile(stdout,"Copyright: %s\n", |
| 274 | GetMagickCopyright()); |
| 275 | (void) FormatLocaleFile(stdout,"Features: %s\n\n", |
| 276 | GetMagickFeatures()); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | return(MagickFalse); |
| 278 | } |
| 279 | /* |
| 280 | Persist key/value pair. |
| 281 | */ |
| 282 | (void) DeleteImageOption(image_info,option+1); |
| 283 | status=SetImageOption(image_info,option+1,argv[i+1]); |
| 284 | if (status == MagickFalse) |
| 285 | ThrowConjureException(ImageError,"UnableToPersistKey",option); |
| 286 | i++; |
| 287 | continue; |
| 288 | } |
| 289 | /* |
| 290 | Interpret MSL script. |
| 291 | */ |
| 292 | (void) DeleteImageOption(image_info,"filename"); |
| 293 | status=SetImageOption(image_info,"filename",argv[i]); |
| 294 | if (status == MagickFalse) |
| 295 | ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 296 | (void) FormatLocaleString(image_info->filename,MaxTextExtent,"msl:%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 297 | argv[i]); |
| 298 | image=ReadImages(image_info,exception); |
| 299 | CatchException(exception); |
| 300 | if (image != (Image *) NULL) |
| 301 | image=DestroyImageList(image); |
| 302 | status=image != (Image *) NULL ? MagickTrue : MagickFalse; |
| 303 | number_images++; |
| 304 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 305 | if (i != (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 306 | ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]); |
| 307 | if (number_images == 0) |
| 308 | ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]); |
| 309 | if (image != (Image *) NULL) |
| 310 | image=DestroyImageList(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 311 | for (i=0; i < (ssize_t) argc; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 312 | argv[i]=DestroyString(argv[i]); |
| 313 | argv=(char **) RelinquishMagickMemory(argv); |
| 314 | return(status != 0 ? MagickTrue : MagickFalse); |
| 315 | } |