PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/ImageMagick-6.3.2/wand/conjure.c

https://bitbucket.org/sisko/operation-caribou
C | 299 lines | 180 code | 12 blank | 107 comment | 60 complexity | 03eaae90db0d7070db879b3863e96286 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  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-2006 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. Include declarations.
  57. */
  58. #include "wand/studio.h"
  59. #include "wand/MagickWand.h"
  60. #include "wand/mogrify-private.h"
  61. /*
  62. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  63. % %
  64. % %
  65. % %
  66. + C o n j u r e I m a g e C o m m a n d %
  67. % %
  68. % %
  69. % %
  70. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  71. %
  72. % ConjureImageCommand() describes the format and characteristics of one or
  73. % more image files. It will also report if an image is incomplete or corrupt.
  74. % The information displayed includes the scene number, the file name, the
  75. % width and height of the image, whether the image is colormapped or not,
  76. % the number of colors in the image, the number of bytes in the image, the
  77. % format of the image (JPEG, PNM, etc.), and finally the number of seconds
  78. % it took to read and process the image.
  79. %
  80. % The format of the ConjureImageCommand method is:
  81. %
  82. % MagickBooleanType ConjureImageCommand(ImageInfo *image_info,int argc,
  83. % char **argv,char **metadata,ExceptionInfo *exception)
  84. %
  85. % A description of each parameter follows:
  86. %
  87. % o image_info: The image info.
  88. %
  89. % o argc: The number of elements in the argument vector.
  90. %
  91. % o argv: A text array containing the command line arguments.
  92. %
  93. % o metadata: any metadata is returned here.
  94. %
  95. % o exception: Return any errors or warnings in this structure.
  96. %
  97. %
  98. */
  99. static void ConjureUsage(void)
  100. {
  101. const char
  102. **p;
  103. static const char
  104. *settings[]=
  105. {
  106. "-debug events display copious debugging information",
  107. "-help print program options",
  108. "-log format format of debugging information",
  109. "-monitor monitor progress",
  110. "-quiet suppress all error or warning messages",
  111. "-regard-warnings pay attention to warning messages",
  112. "-verbose print detailed information about the image",
  113. "-version print version information",
  114. (char *) NULL
  115. };
  116. (void) printf("Version: %s\n",GetMagickVersion((unsigned long *) NULL));
  117. (void) printf("Copyright: %s\n\n",GetMagickCopyright());
  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("\nIn additiion, define any key value pairs required by "
  124. "your script. For\nexample,\n\n");
  125. (void) printf(" conjure -size 100x100 -color blue -foo bar script.msl\n");
  126. exit(0);
  127. }
  128. WandExport MagickBooleanType ConjureImageCommand(ImageInfo *image_info,
  129. int argc,char **argv,char **wand_unused(metadata),ExceptionInfo *exception)
  130. {
  131. #define DestroyConjure() \
  132. { \
  133. image=DestroyImageList(image); \
  134. for (i=0; i < (long) argc; i++) \
  135. argv[i]=(char *) RelinquishMagickMemory(argv[i]); \
  136. argv=(char **) RelinquishMagickMemory(argv); \
  137. }
  138. #define ThrowConjureException(asperity,tag,option) \
  139. { \
  140. if (exception->severity < (asperity)) \
  141. (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
  142. "`%s'",option); \
  143. DestroyConjure(); \
  144. return(MagickFalse); \
  145. }
  146. #define ThrowConjureInvalidArgumentException(option,argument) \
  147. { \
  148. (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
  149. "InvalidArgument","`%s': %s",argument,option); \
  150. DestroyConjure(); \
  151. return(MagickFalse); \
  152. }
  153. char
  154. *option;
  155. Image
  156. *image;
  157. long
  158. number_images;
  159. MagickStatusType
  160. status;
  161. register long
  162. i;
  163. /*
  164. Set defaults.
  165. */
  166. assert(image_info != (ImageInfo *) NULL);
  167. assert(image_info->signature == MagickSignature);
  168. if (image_info->debug != MagickFalse)
  169. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  170. assert(exception != (ExceptionInfo *) NULL);
  171. if (argc < 2)
  172. ConjureUsage();
  173. image=NewImageList();
  174. number_images=0;
  175. option=(char *) NULL;
  176. /*
  177. Conjure an image.
  178. */
  179. ReadCommandlLine(argc,&argv);
  180. status=ExpandFilenames(&argc,&argv);
  181. if (status == MagickFalse)
  182. {
  183. char
  184. *message;
  185. message=GetExceptionMessage(errno);
  186. ThrowConjureException(ResourceLimitError,"MemoryAllocationFailed",
  187. message);
  188. message=(char *) RelinquishMagickMemory(message);
  189. }
  190. for (i=1; i < (long) argc; i++)
  191. {
  192. option=argv[i];
  193. if (IsMagickOption(option) != MagickFalse)
  194. {
  195. if (LocaleCompare("debug",option+1) == 0)
  196. {
  197. long
  198. event;
  199. if (*option == '+')
  200. break;
  201. i++;
  202. if (i == (long) argc)
  203. ThrowConjureException(OptionError,"MissingArgument",option);
  204. event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
  205. if (event < 0)
  206. ThrowConjureException(OptionError,"UnrecognizedEventType",
  207. argv[i]);
  208. (void) SetLogEventMask(argv[i]);
  209. continue;
  210. }
  211. if ((LocaleCompare("help",option+1) == 0) ||
  212. (LocaleCompare("-help",option+1) == 0))
  213. {
  214. if (*option == '-')
  215. ConjureUsage();
  216. continue;
  217. }
  218. if (LocaleCompare("log",option+1) == 0)
  219. {
  220. if (*option == '-')
  221. {
  222. i++;
  223. if (i == (long) argc)
  224. ThrowConjureException(OptionError,"MissingLogFormat",option);
  225. (void) SetLogFormat(argv[i]);
  226. }
  227. continue;
  228. }
  229. if (LocaleCompare("monitor",option+1) == 0)
  230. continue;
  231. if (LocaleCompare("quiet",option+1) == 0)
  232. continue;
  233. if (LocaleCompare("regard-warnings",option+1) == 0)
  234. break;
  235. if (LocaleCompare("verbose",option+1) == 0)
  236. {
  237. image_info->verbose=(*option == '-') ? MagickTrue : MagickFalse;
  238. continue;
  239. }
  240. if ((LocaleCompare("version",option+1) == 0) ||
  241. (LocaleCompare("-version",option+1) == 0))
  242. {
  243. (void) fprintf(stdout,"Version: %s\n",
  244. GetMagickVersion((unsigned long *) NULL));
  245. (void) fprintf(stdout,"Copyright: %s\n\n",GetMagickCopyright());
  246. exit(0);
  247. continue;
  248. }
  249. /*
  250. Persist key/value pair.
  251. */
  252. (void) DeleteImageOption(image_info,option+1);
  253. status&=SetImageOption(image_info,option+1,argv[i+1]);
  254. if (status == MagickFalse)
  255. ThrowConjureException(ImageError,"UnableToPersistKey",option);
  256. i++;
  257. continue;
  258. }
  259. /*
  260. Interpret MSL script.
  261. */
  262. (void) DeleteImageOption(image_info,"filename");
  263. status&=SetImageOption(image_info,"filename",argv[i]);
  264. if (status == MagickFalse)
  265. ThrowConjureException(ImageError,"UnableToPersistKey",argv[i]);
  266. (void) FormatMagickString(image_info->filename,MaxTextExtent,"msl:%s",
  267. argv[i]);
  268. image=ReadImage(image_info,exception);
  269. CatchException(exception);
  270. status&=image != (Image *) NULL;
  271. if (image != (Image *) NULL)
  272. image=DestroyImageList(image);
  273. number_images++;
  274. }
  275. if (i != argc)
  276. ThrowConjureException(OptionError,"MissingAnImageFilename",argv[i]);
  277. if (number_images == 0)
  278. ThrowConjureException(OptionError,"MissingAnImageFilename",argv[argc-1]);
  279. if (image != (Image *) NULL)
  280. image=DestroyImageList(image);
  281. for (i=0; i < (long) argc; i++)
  282. argv[i]=(char *) RelinquishMagickMemory(argv[i]);
  283. argv=(char **) RelinquishMagickMemory(argv);
  284. return(status != 0 ? MagickTrue : MagickFalse);
  285. }