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

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

https://bitbucket.org/sisko/operation-caribou
C | 686 lines | 573 code | 26 blank | 87 comment | 224 complexity | 35a5e1fd18d8098acc9cbedea461f5b4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % SSSSS TTTTT RRRR EEEEE AAA M M %
  7. % SS T R R E A A MM MM %
  8. % SSS T RRRR EEE AAAAA M M M %
  9. % SS T R R E A A M M %
  10. % SSSSS T R R EEEEE A A M M %
  11. % %
  12. % %
  13. % Stream image to a raw image format. %
  14. % %
  15. % Software Design %
  16. % John Cristy %
  17. % July 1992 %
  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. % Use Stream is a lightweight utility designed to extract pixels from large
  37. % image files to a raw format using a minimum of system resources. The
  38. % entire image or any regular portion of the image can be extracted.
  39. %
  40. %
  41. */
  42. /*
  43. Include declarations.
  44. */
  45. #include "wand/studio.h"
  46. #include "wand/MagickWand.h"
  47. #include "wand/mogrify-private.h"
  48. #include "magick/stream-private.h"
  49. /*
  50. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  51. % %
  52. % %
  53. % %
  54. % S t r e a m I m a g e C o m m a n d %
  55. % %
  56. % %
  57. % %
  58. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  59. %
  60. % StreamImageCommand() is a lightweight method designed to extract pixels
  61. % from large image files to a raw format using a minimum of system resources.
  62. % The entire image or any regular portion of the image can be extracted.
  63. %
  64. % The format of the StreamImageCommand method is:
  65. %
  66. % MagickBooleanType StreamImageCommand(ImageInfo *image_info,int argc,
  67. % char **argv,char **metadata,ExceptionInfo *exception)
  68. %
  69. % A description of each parameter follows:
  70. %
  71. % o image_info: The image info.
  72. %
  73. % o argc: The number of elements in the argument vector.
  74. %
  75. % o argv: A text array containing the command line arguments.
  76. %
  77. % o metadata: any metadata is returned here.
  78. %
  79. % o exception: Return any errors or warnings in this structure.
  80. %
  81. %
  82. */
  83. static void StreamUsage(void)
  84. {
  85. const char
  86. **p;
  87. static const char
  88. *settings[]=
  89. {
  90. "-authenticate value decrypt image with this password",
  91. "-channel type apply option to select image channels",
  92. "-colorspace type alternate image colorspace",
  93. "-compress type type of pixel compression when writing the image",
  94. "-debug events display copious debugging information",
  95. "-define format:option",
  96. " define one or more image format options",
  97. "-density geometry horizontal and vertical density of the image",
  98. "-depth value image depth",
  99. "-extract geometry extract area from image",
  100. "-help print program options",
  101. "-identify identify the format and characteristics of the image",
  102. "-interlace type type of image interlacing scheme",
  103. "-interpolate method pixel color interpolation method",
  104. "-limit type value pixel cache resource limit",
  105. "-log format format of debugging information",
  106. "-map components one or more pixel components",
  107. "-monitor monitor progress",
  108. "-quantize colorspace reduce colors in this colorspace",
  109. "-quiet suppress all error or warning messages",
  110. "-regard-warnings pay attention to warning messages",
  111. "-sampling-factor geometry",
  112. " horizontal and vertical sampling factor",
  113. "-set attribute value set an image attribute",
  114. "-size geometry width and height of image",
  115. "-storage-type type pixel storage type",
  116. "-transparent-color color",
  117. " transparent color",
  118. "-verbose print detailed information about the image",
  119. "-version print version information",
  120. "-virtual-pixel method",
  121. " virtual pixel access method",
  122. (char *) NULL
  123. };
  124. (void) printf("Version: %s\n",GetMagickVersion((unsigned long *) NULL));
  125. (void) printf("Copyright: %s\n\n",GetMagickCopyright());
  126. (void) printf("Usage: %s [options ...] input-image raw-image\n",
  127. GetClientName());
  128. (void) printf("\nImage Settings:\n");
  129. for (p=settings; *p != (char *) NULL; p++)
  130. (void) printf(" %s\n",*p);
  131. (void) printf(
  132. "\nBy default, the image format of `file' is determined by its magic\n");
  133. (void) printf(
  134. "number. To specify a particular image format, precede the filename\n");
  135. (void) printf(
  136. "with an image format name and a colon (i.e. ps:image) or specify the\n");
  137. (void) printf(
  138. "image type as the filename suffix (i.e. image.ps). Specify 'file' as\n");
  139. (void) printf("'-' for standard input or output.\n");
  140. exit(0);
  141. }
  142. WandExport MagickBooleanType StreamImageCommand(ImageInfo *image_info,
  143. int argc,char **argv,char **metadata,ExceptionInfo *exception)
  144. {
  145. #define DestroyStream() \
  146. { \
  147. for ( ; k >= 0; k--) \
  148. image_stack[k]=DestroyImageList(image_stack[k]); \
  149. stream_info=DestroyStreamInfo(stream_info); \
  150. for (i=0; i < (long) argc; i++) \
  151. argv[i]=(char *) RelinquishMagickMemory(argv[i]); \
  152. argv=(char **) RelinquishMagickMemory(argv); \
  153. }
  154. #define ThrowStreamException(asperity,tag,option) \
  155. { \
  156. if (exception->severity < (asperity)) \
  157. (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \
  158. "`%s'",option); \
  159. DestroyStream(); \
  160. return(MagickFalse); \
  161. }
  162. #define ThrowStreamInvalidArgumentException(option,argument) \
  163. { \
  164. (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
  165. "InvalidArgument","`%s': %s",argument,option); \
  166. DestroyStream(); \
  167. return(MagickFalse); \
  168. }
  169. char
  170. *filename,
  171. *option;
  172. const char
  173. *format;
  174. ChannelType
  175. channel;
  176. Image
  177. *image,
  178. *image_stack[MaxImageStackDepth+1];
  179. long
  180. j,
  181. k;
  182. MagickBooleanType
  183. fire,
  184. pend;
  185. MagickStatusType
  186. status;
  187. register long
  188. i;
  189. StreamInfo
  190. *stream_info;
  191. /*
  192. Set defaults.
  193. */
  194. assert(image_info != (ImageInfo *) NULL);
  195. assert(image_info->signature == MagickSignature);
  196. if (image_info->debug != MagickFalse)
  197. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  198. assert(exception != (ExceptionInfo *) NULL);
  199. if (argc < 3)
  200. StreamUsage();
  201. channel=AllChannels;
  202. format="%w,%h,%m";
  203. j=1;
  204. k=0;
  205. image_stack[k]=NewImageList();
  206. option=(char *) NULL;
  207. pend=MagickFalse;
  208. stream_info=AcquireStreamInfo(image_info);
  209. status=MagickTrue;
  210. /*
  211. Stream an image.
  212. */
  213. ReadCommandlLine(argc,&argv);
  214. status=ExpandFilenames(&argc,&argv);
  215. if (status == MagickFalse)
  216. {
  217. char
  218. *message;
  219. message=GetExceptionMessage(errno);
  220. ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
  221. message);
  222. message=(char *) RelinquishMagickMemory(message);
  223. }
  224. status=OpenStream(image_info,stream_info,argv[argc-1],exception);
  225. if (status == MagickFalse)
  226. {
  227. DestroyStream();
  228. return(MagickFalse);
  229. }
  230. for (i=1; i < (long) (argc-1); i++)
  231. {
  232. option=argv[i];
  233. if (LocaleCompare(option,"(") == 0)
  234. {
  235. if (k == MaxImageStackDepth)
  236. ThrowStreamException(OptionError,"ParenthesisNestedTooDeeply",
  237. option);
  238. MogrifyImageStack(image_stack[k],MagickTrue,pend);
  239. k++;
  240. image_stack[k]=NewImageList();
  241. continue;
  242. }
  243. if (LocaleCompare(option,")") == 0)
  244. {
  245. if (k == 0)
  246. ThrowStreamException(OptionError,"UnableToParseExpression",option);
  247. if (image_stack[k] != (Image *) NULL)
  248. {
  249. MogrifyImageStack(image_stack[k],MagickTrue,MagickTrue);
  250. AppendImageToList(&image_stack[k-1],image_stack[k]);
  251. }
  252. k--;
  253. continue;
  254. }
  255. if (IsMagickOption(option) == MagickFalse)
  256. {
  257. /*
  258. Stream input image.
  259. */
  260. MogrifyImageStack(image_stack[k],MagickTrue,pend);
  261. filename=argv[i];
  262. if ((LocaleCompare(filename,"--") == 0) && (i < (argc-1)))
  263. filename=argv[++i];
  264. (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
  265. image=StreamImage(image_info,stream_info,exception);
  266. status&=(image != (Image *) NULL) &&
  267. (exception->severity < ErrorException);
  268. if (image == (Image *) NULL)
  269. continue;
  270. AppendImageToList(&image_stack[k],image);
  271. continue;
  272. }
  273. pend=image_stack[k] != (Image *) NULL ? MagickTrue : MagickFalse;
  274. switch (*(option+1))
  275. {
  276. case 'a':
  277. {
  278. if (LocaleCompare("authenticate",option+1) == 0)
  279. {
  280. if (*option == '+')
  281. break;
  282. i++;
  283. if (i == (long) (argc-1))
  284. ThrowStreamException(OptionError,"MissingArgument",option);
  285. break;
  286. }
  287. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  288. }
  289. case 'c':
  290. {
  291. if (LocaleCompare("cache",option+1) == 0)
  292. {
  293. if (*option == '+')
  294. break;
  295. i++;
  296. if (i == (long) argc)
  297. ThrowStreamException(OptionError,"MissingArgument",option);
  298. if (IsGeometry(argv[i]) == MagickFalse)
  299. ThrowStreamInvalidArgumentException(option,argv[i]);
  300. break;
  301. }
  302. if (LocaleCompare("channel",option+1) == 0)
  303. {
  304. long
  305. channel;
  306. if (*option == '+')
  307. break;
  308. i++;
  309. if (i == (long) (argc-1))
  310. ThrowStreamException(OptionError,"MissingArgument",option);
  311. channel=ParseChannelOption(argv[i]);
  312. if (channel < 0)
  313. ThrowStreamException(OptionError,"UnrecognizedChannelType",
  314. argv[i]);
  315. break;
  316. }
  317. if (LocaleCompare("colorspace",option+1) == 0)
  318. {
  319. long
  320. colorspace;
  321. if (*option == '+')
  322. break;
  323. i++;
  324. if (i == (long) (argc-1))
  325. ThrowStreamException(OptionError,"MissingArgument",option);
  326. colorspace=ParseMagickOption(MagickColorspaceOptions,MagickFalse,
  327. argv[i]);
  328. if (colorspace < 0)
  329. ThrowStreamException(OptionError,"UnrecognizedColorspace",
  330. argv[i]);
  331. break;
  332. }
  333. if (LocaleCompare("compress",option+1) == 0)
  334. {
  335. long
  336. compression;
  337. if (*option == '+')
  338. break;
  339. i++;
  340. if (i == (long) (argc-1))
  341. ThrowStreamException(OptionError,"MissingArgument",option);
  342. compression=ParseMagickOption(MagickCompressionOptions,
  343. MagickFalse,argv[i]);
  344. if (compression < 0)
  345. ThrowStreamException(OptionError,
  346. "UnrecognizedImageCompression",argv[i]);
  347. break;
  348. }
  349. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  350. }
  351. case 'd':
  352. {
  353. if (LocaleCompare("debug",option+1) == 0)
  354. {
  355. long
  356. event;
  357. if (*option == '+')
  358. break;
  359. i++;
  360. if (i == (long) argc)
  361. ThrowStreamException(OptionError,"MissingArgument",option);
  362. event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
  363. if (event < 0)
  364. ThrowStreamException(OptionError,"UnrecognizedEventType",argv[i]);
  365. (void) SetLogEventMask(argv[i]);
  366. break;
  367. break;
  368. }
  369. if (LocaleCompare("define",option+1) == 0)
  370. {
  371. i++;
  372. if (i == (long) argc)
  373. ThrowStreamException(OptionError,"MissingArgument",option);
  374. if (*option == '+')
  375. {
  376. const char
  377. *define;
  378. define=GetImageOption(image_info,argv[i]);
  379. if (define == (const char *) NULL)
  380. ThrowStreamException(OptionError,"NoSuchOption",argv[i]);
  381. break;
  382. }
  383. break;
  384. }
  385. if (LocaleCompare("density",option+1) == 0)
  386. {
  387. if (*option == '+')
  388. break;
  389. i++;
  390. if (i == (long) argc)
  391. ThrowStreamException(OptionError,"MissingArgument",option);
  392. if (IsGeometry(argv[i]) == MagickFalse)
  393. ThrowStreamInvalidArgumentException(option,argv[i]);
  394. break;
  395. }
  396. if (LocaleCompare("depth",option+1) == 0)
  397. {
  398. if (*option == '+')
  399. break;
  400. i++;
  401. if (i == (long) argc)
  402. ThrowStreamException(OptionError,"MissingArgument",option);
  403. if (IsGeometry(argv[i]) == MagickFalse)
  404. ThrowStreamInvalidArgumentException(option,argv[i]);
  405. break;
  406. }
  407. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  408. }
  409. case 'e':
  410. {
  411. if (LocaleCompare("extract",option+1) == 0)
  412. {
  413. if (*option == '+')
  414. break;
  415. i++;
  416. if (i == (long) (argc-1))
  417. ThrowStreamException(OptionError,"MissingArgument",option);
  418. if (IsGeometry(argv[i]) == MagickFalse)
  419. ThrowStreamInvalidArgumentException(option,argv[i]);
  420. break;
  421. }
  422. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  423. }
  424. case 'h':
  425. {
  426. if ((LocaleCompare("help",option+1) == 0) ||
  427. (LocaleCompare("-help",option+1) == 0))
  428. StreamUsage();
  429. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  430. }
  431. case 'i':
  432. {
  433. if (LocaleCompare("identify",option+1) == 0)
  434. break;
  435. if (LocaleCompare("interlace",option+1) == 0)
  436. {
  437. long
  438. interlace;
  439. if (*option == '+')
  440. break;
  441. i++;
  442. if (i == (long) argc)
  443. ThrowStreamException(OptionError,"MissingArgument",option);
  444. interlace=ParseMagickOption(MagickInterlaceOptions,MagickFalse,
  445. argv[i]);
  446. if (interlace < 0)
  447. ThrowStreamException(OptionError,"UnrecognizedInterlaceType",
  448. argv[i]);
  449. break;
  450. }
  451. if (LocaleCompare("interpolate",option+1) == 0)
  452. {
  453. long
  454. interpolate;
  455. if (*option == '+')
  456. break;
  457. i++;
  458. if (i == (long) argc)
  459. ThrowStreamException(OptionError,"MissingArgument",option);
  460. interpolate=ParseMagickOption(MagickInterpolateOptions,MagickFalse,
  461. argv[i]);
  462. if (interpolate < 0)
  463. ThrowStreamException(OptionError,"UnrecognizedInterpolateMethod",
  464. argv[i]);
  465. break;
  466. }
  467. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  468. }
  469. case 'l':
  470. {
  471. if (LocaleCompare("limit",option+1) == 0)
  472. {
  473. long
  474. resource;
  475. if (*option == '+')
  476. break;
  477. i++;
  478. if (i == (long) argc)
  479. ThrowStreamException(OptionError,"MissingArgument",option);
  480. resource=ParseMagickOption(MagickResourceOptions,MagickFalse,
  481. argv[i]);
  482. if (resource < 0)
  483. ThrowStreamException(OptionError,"UnrecognizedResourceType",
  484. argv[i]);
  485. i++;
  486. if (i == (long) argc)
  487. ThrowStreamException(OptionError,"MissingArgument",option);
  488. if ((LocaleCompare("unlimited",argv[i]) != 0) &&
  489. (IsGeometry(argv[i]) == MagickFalse))
  490. ThrowStreamInvalidArgumentException(option,argv[i]);
  491. break;
  492. }
  493. if (LocaleCompare("log",option+1) == 0)
  494. {
  495. if (*option == '+')
  496. break;
  497. i++;
  498. if ((i == (long) argc) || (strchr(argv[i],'%') == (char *) NULL))
  499. ThrowStreamException(OptionError,"MissingArgument",option);
  500. break;
  501. }
  502. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  503. }
  504. case 'm':
  505. {
  506. if (LocaleCompare("map",option+1) == 0)
  507. {
  508. (void) CopyMagickString(argv[i]+1,"san",MaxTextExtent);
  509. if (*option == '+')
  510. break;
  511. i++;
  512. SetStreamInfoMap(stream_info,argv[i]);
  513. break;
  514. }
  515. if (LocaleCompare("monitor",option+1) == 0)
  516. break;
  517. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  518. }
  519. case 'q':
  520. {
  521. if (LocaleCompare("quantize",option+1) == 0)
  522. {
  523. long
  524. colorspace;
  525. if (*option == '+')
  526. break;
  527. i++;
  528. if (i == (long) (argc-1))
  529. ThrowStreamException(OptionError,"MissingArgument",option);
  530. colorspace=ParseMagickOption(MagickColorspaceOptions,
  531. MagickFalse,argv[i]);
  532. if (colorspace < 0)
  533. ThrowStreamException(OptionError,"UnrecognizedColorspace",
  534. argv[i]);
  535. break;
  536. }
  537. if (LocaleCompare("quiet",option+1) == 0)
  538. break;
  539. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  540. }
  541. case 'r':
  542. {
  543. if (LocaleCompare("regard-warnings",option+1) == 0)
  544. break;
  545. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  546. }
  547. case 's':
  548. {
  549. if (LocaleCompare("sampling-factor",option+1) == 0)
  550. {
  551. if (*option == '+')
  552. break;
  553. i++;
  554. if (i == (long) argc)
  555. ThrowStreamException(OptionError,"MissingArgument",option);
  556. if (IsGeometry(argv[i]) == MagickFalse)
  557. ThrowStreamInvalidArgumentException(option,argv[i]);
  558. break;
  559. }
  560. if (LocaleCompare("set",option+1) == 0)
  561. {
  562. i++;
  563. if (i == (long) argc)
  564. ThrowStreamException(OptionError,"MissingArgument",option);
  565. if (*option == '+')
  566. break;
  567. i++;
  568. if (i == (long) argc)
  569. ThrowStreamException(OptionError,"MissingArgument",option);
  570. break;
  571. }
  572. if (LocaleCompare("size",option+1) == 0)
  573. {
  574. if (*option == '+')
  575. break;
  576. i++;
  577. if (i == (long) argc)
  578. ThrowStreamException(OptionError,"MissingArgument",option);
  579. if (IsGeometry(argv[i]) == MagickFalse)
  580. ThrowStreamInvalidArgumentException(option,argv[i]);
  581. break;
  582. }
  583. if (LocaleCompare("storage-type",option+1) == 0)
  584. {
  585. long
  586. type;
  587. if (*option == '+')
  588. break;
  589. i++;
  590. if (i == (long) (argc-1))
  591. ThrowStreamException(OptionError,"MissingArgument",option);
  592. type=ParseMagickOption(MagickStorageOptions,MagickFalse,argv[i]);
  593. if (type < 0)
  594. ThrowStreamException(OptionError,"UnrecognizedStorageType",
  595. argv[i]);
  596. SetStreamInfoStorageType(stream_info,(StorageType) type);
  597. break;
  598. }
  599. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  600. }
  601. case 't':
  602. {
  603. if (LocaleCompare("transparent-color",option+1) == 0)
  604. {
  605. if (*option == '+')
  606. break;
  607. i++;
  608. if (i == (long) (argc-1))
  609. ThrowStreamException(OptionError,"MissingArgument",option);
  610. break;
  611. }
  612. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  613. }
  614. case 'v':
  615. {
  616. if (LocaleCompare("verbose",option+1) == 0)
  617. break;
  618. if ((LocaleCompare("version",option+1) == 0) ||
  619. (LocaleCompare("-version",option+1) == 0))
  620. break;
  621. if (LocaleCompare("virtual-pixel",option+1) == 0)
  622. {
  623. long
  624. method;
  625. if (*option == '+')
  626. break;
  627. i++;
  628. if (i == (long) (argc-1))
  629. ThrowStreamException(OptionError,"MissingArgument",option);
  630. method=ParseMagickOption(MagickVirtualPixelOptions,MagickFalse,
  631. argv[i]);
  632. if (method < 0)
  633. ThrowStreamException(OptionError,
  634. "UnrecognizedVirtualPixelMethod",argv[i]);
  635. break;
  636. }
  637. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  638. }
  639. case '?':
  640. break;
  641. default:
  642. ThrowStreamException(OptionError,"UnrecognizedOption",option)
  643. }
  644. fire=(MagickBooleanType) ParseMagickOption(MagickMogrifyOptions,
  645. MagickFalse,option+1);
  646. if (fire == MagickTrue)
  647. MogrifyImageStack(image_stack[k],MagickTrue,MagickTrue);
  648. }
  649. if (k != 0)
  650. ThrowStreamException(OptionError,"UnbalancedParenthesis",argv[i]);
  651. if (i-- != (long) (argc-1))
  652. ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
  653. if (image_stack[k] == (Image *) NULL)
  654. ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
  655. MogrifyImageStack(image_stack[k],MagickTrue,MagickTrue);
  656. DestroyStream();
  657. return(status != 0 ? MagickTrue : MagickFalse);
  658. }