PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/MagickWand/conjure.c

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