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 % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % December 2001 % |
| 18 | % % |
| 19 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 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 | 4f7a613 | 2012-12-23 00:35:19 +0000 | [diff] [blame] | 115 | ListMagickVersion(stdout); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 116 | (void) printf("Usage: %s [options ...] file [ [options ...] file ...]\n", |
| 117 | GetClientName()); |
| 118 | (void) printf("\nImage Settings:\n"); |
| 119 | for (p=settings; *p != (char *) NULL; p++) |
| 120 | (void) printf(" %s\n",*p); |
| 121 | (void) printf("\nMiscellaneous Options:\n"); |
| 122 | for (p=miscellaneous; *p != (char *) NULL; p++) |
| 123 | (void) printf(" %s\n",*p); |
glennrp | ac09427 | 2010-11-13 13:07:11 +0000 | [diff] [blame] | 124 | (void) printf("\nIn addition, define any key value pairs required by " |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 125 | "your script. For\nexample,\n\n"); |
| 126 | (void) printf(" conjure -size 100x100 -color blue -foo bar script.msl\n"); |
| 127 | return(MagickFalse); |
| 128 | } |
| 129 | |
| 130 | WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info, |
| 131 | int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception) |
| 132 | { |
| 133 | #define DestroyConjure() \ |
| 134 | { \ |
| 135 | image=DestroyImageList(image); \ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 136 | for (i=0; i < (ssize_t) argc; i++) \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 137 | argv[i]=DestroyString(argv[i]); \ |
| 138 | argv=(char **) RelinquishMagickMemory(argv); \ |
| 139 | } |
| 140 | #define ThrowConjureException(asperity,tag,option) \ |
| 141 | { \ |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 142 | (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | option); \ |
| 144 | DestroyConjure(); \ |
| 145 | return(MagickFalse); \ |
| 146 | } |
| 147 | #define ThrowConjureInvalidArgumentException(option,argument) \ |
| 148 | { \ |
| 149 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \ |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 150 | "InvalidArgument","'%s': %s",option,argument); \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 151 | DestroyConjure(); \ |
| 152 | return(MagickFalse); \ |
| 153 | } |
| 154 | |
| 155 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 156 | filename[MagickPathExtent], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 157 | *option; |
| 158 | |
| 159 | Image |
| 160 | *image; |
| 161 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 162 | MagickStatusType |
| 163 | status; |
| 164 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 165 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 166 | i; |
| 167 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 168 | ssize_t |
| 169 | number_images; |
| 170 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | /* |
| 172 | Set defaults. |
| 173 | */ |
| 174 | assert(image_info != (ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 175 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | if (image_info->debug != MagickFalse) |
| 177 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 178 | assert(exception != (ExceptionInfo *) NULL); |
| 179 | if (argc < 2) |
cristy | 13e61a1 | 2010-02-04 20:19:00 +0000 | [diff] [blame] | 180 | return(ConjureUsage()); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | image=NewImageList(); |
| 182 | number_images=0; |
| 183 | option=(char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | /* |
| 185 | Conjure an image. |
| 186 | */ |
| 187 | ReadCommandlLine(argc,&argv); |
| 188 | status=ExpandFilenames(&argc,&argv); |
| 189 | if (status == MagickFalse) |
| 190 | ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed", |
| 191 | GetExceptionMessage(errno)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 192 | for (i=1; i < (ssize_t) argc; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 193 | { |
| 194 | option=argv[i]; |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 195 | if (IsCommandOption(option) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 196 | { |
cristy | 2287975 | 2009-10-25 23:55:40 +0000 | [diff] [blame] | 197 | if (LocaleCompare("concurrent",option+1) == 0) |
| 198 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | if (LocaleCompare("debug",option+1) == 0) |
| 200 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 201 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 202 | event; |
| 203 | |
| 204 | if (*option == '+') |
| 205 | break; |
| 206 | i++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 207 | if (i == (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 208 | ThrowConjureException(OptionError,"MissingArgument",option); |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 209 | event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 210 | if (event < 0) |
| 211 | ThrowConjureException(OptionError,"UnrecognizedEventType", |
| 212 | argv[i]); |
| 213 | (void) SetLogEventMask(argv[i]); |
| 214 | continue; |
| 215 | } |
cristy | 2287975 | 2009-10-25 23:55:40 +0000 | [diff] [blame] | 216 | if (LocaleCompare("duration",option+1) == 0) |
| 217 | { |
| 218 | if (*option == '+') |
| 219 | break; |
| 220 | i++; |
cristy | e81f552 | 2014-05-07 01:25:59 +0000 | [diff] [blame] | 221 | if (i == (ssize_t) argc) |
cristy | 2287975 | 2009-10-25 23:55:40 +0000 | [diff] [blame] | 222 | ThrowConjureException(OptionError,"MissingArgument",option); |
| 223 | if (IsGeometry(argv[i]) == MagickFalse) |
| 224 | ThrowConjureInvalidArgumentException(option,argv[i]); |
| 225 | continue; |
| 226 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | if ((LocaleCompare("help",option+1) == 0) || |
| 228 | (LocaleCompare("-help",option+1) == 0)) |
| 229 | { |
| 230 | if (*option == '-') |
| 231 | return(ConjureUsage()); |
| 232 | continue; |
| 233 | } |
| 234 | if (LocaleCompare("log",option+1) == 0) |
| 235 | { |
| 236 | if (*option == '-') |
| 237 | { |
| 238 | i++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 239 | if (i == (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | ThrowConjureException(OptionError,"MissingLogFormat",option); |
| 241 | (void) SetLogFormat(argv[i]); |
| 242 | } |
| 243 | continue; |
| 244 | } |
| 245 | if (LocaleCompare("monitor",option+1) == 0) |
| 246 | continue; |
| 247 | if (LocaleCompare("quiet",option+1) == 0) |
| 248 | continue; |
| 249 | if (LocaleCompare("regard-warnings",option+1) == 0) |
| 250 | break; |
| 251 | if (LocaleCompare("seed",option+1) == 0) |
| 252 | { |
| 253 | if (*option == '+') |
| 254 | break; |
| 255 | i++; |
cristy | e81f552 | 2014-05-07 01:25:59 +0000 | [diff] [blame] | 256 | if (i == (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 257 | ThrowConjureException(OptionError,"MissingArgument",option); |
| 258 | if (IsGeometry(argv[i]) == MagickFalse) |
| 259 | ThrowConjureInvalidArgumentException(option,argv[i]); |
| 260 | break; |
| 261 | } |
| 262 | if (LocaleCompare("verbose",option+1) == 0) |
| 263 | { |
| 264 | image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse; |
| 265 | continue; |
| 266 | } |
| 267 | if ((LocaleCompare("version",option+1) == 0) || |
| 268 | (LocaleCompare("-version",option+1) == 0)) |
| 269 | { |
cristy | 4f7a613 | 2012-12-23 00:35:19 +0000 | [diff] [blame] | 270 | ListMagickVersion(stdout); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | return(MagickFalse); |
| 272 | } |
| 273 | /* |
| 274 | Persist key/value pair. |
| 275 | */ |
| 276 | (void) DeleteImageOption(image_info,option+1); |
| 277 | status=SetImageOption(image_info,option+1,argv[i+1]); |
| 278 | if (status == MagickFalse) |
| 279 | ThrowConjureException(ImageError,"UnableToPersistKey",option); |
| 280 | i++; |
| 281 | continue; |
| 282 | } |
| 283 | /* |
| 284 | Interpret MSL script. |
| 285 | */ |
| 286 | (void) DeleteImageOption(image_info,"filename"); |
| 287 | status=SetImageOption(image_info,"filename",argv[i]); |
| 288 | if (status == MagickFalse) |
| 289 | ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 290 | (void) FormatLocaleString(filename,MagickPathExtent,"msl:%s",argv[i]); |
cristy | 1b58f25 | 2012-03-01 01:41:41 +0000 | [diff] [blame] | 291 | image=ReadImages(image_info,filename,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 292 | CatchException(exception); |
| 293 | if (image != (Image *) NULL) |
| 294 | image=DestroyImageList(image); |
| 295 | status=image != (Image *) NULL ? MagickTrue : MagickFalse; |
| 296 | number_images++; |
| 297 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 298 | if (i != (ssize_t) argc) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 299 | ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]); |
| 300 | if (number_images == 0) |
| 301 | ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]); |
| 302 | if (image != (Image *) NULL) |
| 303 | image=DestroyImageList(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 304 | for (i=0; i < (ssize_t) argc; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | argv[i]=DestroyString(argv[i]); |
| 306 | argv=(char **) RelinquishMagickMemory(argv); |
| 307 | return(status != 0 ? MagickTrue : MagickFalse); |
| 308 | } |