2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
10 % SSSSS T R R EEEEE A A M M %
13 % Stream Image to a Raw Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % Stream is a lightweight tool to stream one or more pixel components of the
37 % image or portion of the image to your choice of storage formats. It writes
38 % the pixel components as they are read from the input image a row at a time
39 % making stream desirable when working with large images or when you require
40 % raw pixel components.
47 #include "MagickWand/studio.h"
48 #include "MagickWand/MagickWand.h"
49 #include "MagickWand/mogrify-private.h"
50 #include "MagickCore/stream-private.h"
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 % S t r e a m I m a g e C o m m a n d %
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 % StreamImageCommand() is a lightweight method designed to extract pixels
64 % from large image files to a raw format using a minimum of system resources.
65 % The entire image or any regular portion of the image can be extracted.
67 % The format of the StreamImageCommand method is:
69 % MagickBooleanType StreamImageCommand(ImageInfo *image_info,int argc,
70 % char **argv,char **metadata,ExceptionInfo *exception)
72 % A description of each parameter follows:
74 % o image_info: the image info.
76 % o argc: the number of elements in the argument vector.
78 % o argv: A text array containing the command line arguments.
80 % o metadata: any metadata is returned here.
82 % o exception: return any errors or warnings in this structure.
86 static MagickBooleanType StreamUsage(void)
94 "-debug events display copious debugging information",
95 "-help print program options",
96 "-list type print a list of supported option arguments",
97 "-log format format of debugging information",
98 "-version print version information",
103 "-authenticate password",
104 " decipher image with this password",
105 "-channel type apply option to select image channels",
106 "-colorspace type alternate image colorspace",
107 "-compress type type of pixel compression when writing the image",
108 "-define format:option",
109 " define one or more image format options",
110 "-density geometry horizontal and vertical density of the image",
111 "-depth value image depth",
112 "-extract geometry extract area from image",
113 "-identify identify the format and characteristics of the image",
114 "-interlace type type of image interlacing scheme",
115 "-interpolate method pixel color interpolation method",
116 "-limit type value pixel cache resource limit",
117 "-map components one or more pixel components",
118 "-monitor monitor progress",
119 "-quantize colorspace reduce colors in this colorspace",
120 "-quiet suppress all warning messages",
121 "-regard-warnings pay attention to warning messages",
122 "-respect-parentheses settings remain in effect until parenthesis boundary",
123 "-sampling-factor geometry",
124 " horizontal and vertical sampling factor",
125 "-seed value seed a new sequence of pseudo-random numbers",
126 "-set attribute value set an image attribute",
127 "-size geometry width and height of image",
128 "-storage-type type pixel storage type",
129 "-synchronize synchronize image to storage device",
130 "-taint declare the image as modified",
131 "-transparent-color color",
132 " transparent color",
133 "-verbose print detailed information about the image",
134 "-virtual-pixel method",
135 " virtual pixel access method",
139 (void) printf("Version: %s\n",GetMagickVersion((size_t *) NULL));
140 (void) printf("Copyright: %s\n",GetMagickCopyright());
141 (void) printf("Features: %s\n\n",GetMagickFeatures());
142 (void) printf("Usage: %s [options ...] input-image raw-image\n",
144 (void) printf("\nImage Settings:\n");
145 for (p=settings; *p != (char *) NULL; p++)
146 (void) printf(" %s\n",*p);
147 (void) printf("\nMiscellaneous Options:\n");
148 for (p=miscellaneous; *p != (char *) NULL; p++)
149 (void) printf(" %s\n",*p);
151 "\nBy default, the image format of `file' is determined by its magic\n");
153 "number. To specify a particular image format, precede the filename\n");
155 "with an image format name and a colon (i.e. ps:image) or specify the\n");
157 "image type as the filename suffix (i.e. image.ps). Specify 'file' as\n");
158 (void) printf("'-' for standard input or output.\n");
162 WandExport MagickBooleanType StreamImageCommand(ImageInfo *image_info,
163 int argc,char **argv,char **metadata,ExceptionInfo *exception)
165 #define DestroyStream() \
167 DestroyImageStack(); \
168 stream_info=DestroyStreamInfo(stream_info); \
169 for (i=0; i < (ssize_t) argc; i++) \
170 argv[i]=DestroyString(argv[i]); \
171 argv=(char **) RelinquishMagickMemory(argv); \
173 #define ThrowStreamException(asperity,tag,option) \
175 (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
178 return(MagickFalse); \
180 #define ThrowStreamInvalidArgumentException(option,argument) \
182 (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
183 "InvalidArgument","`%s': %s",option,argument); \
185 return(MagickFalse); \
199 image_stack[MaxImageStackDepth+1];
222 assert(image_info != (ImageInfo *) NULL);
223 assert(image_info->signature == MagickSignature);
224 if (image_info->debug != MagickFalse)
225 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
226 assert(exception != (ExceptionInfo *) NULL);
231 if ((LocaleCompare("version",option+1) == 0) ||
232 (LocaleCompare("-version",option+1) == 0))
234 (void) FormatLocaleFile(stdout,"Version: %s\n",
235 GetMagickVersion((size_t *) NULL));
236 (void) FormatLocaleFile(stdout,"Copyright: %s\n",
237 GetMagickCopyright());
238 (void) FormatLocaleFile(stdout,"Features: %s\n\n",
239 GetMagickFeatures());
244 return(StreamUsage());
250 option=(char *) NULL;
252 respect_parenthesis=MagickFalse;
253 stream_info=AcquireStreamInfo(image_info);
258 ReadCommandlLine(argc,&argv);
259 status=ExpandFilenames(&argc,&argv);
260 if (status == MagickFalse)
261 ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
262 GetExceptionMessage(errno));
263 status=OpenStream(image_info,stream_info,argv[argc-1],exception);
264 if (status == MagickFalse)
269 for (i=1; i < (ssize_t) (argc-1); i++)
272 if (LocaleCompare(option,"(") == 0)
274 FireImageStack(MagickFalse,MagickTrue,pend);
275 if (k == MaxImageStackDepth)
276 ThrowStreamException(OptionError,"ParenthesisNestedTooDeeply",option);
280 if (LocaleCompare(option,")") == 0)
282 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
284 ThrowStreamException(OptionError,"UnableToParseExpression",option);
288 if (IsCommandOption(option) == MagickFalse)
296 FireImageStack(MagickFalse,MagickFalse,pend);
298 if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1)))
300 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
301 images=StreamImage(image_info,stream_info,exception);
302 status&=(images != (Image *) NULL) &&
303 (exception->severity < ErrorException);
304 if (images == (Image *) NULL)
306 AppendImageStack(images);
309 pend=image != (Image *) NULL ? MagickTrue : MagickFalse;
314 if (LocaleCompare("authenticate",option+1) == 0)
319 if (i == (ssize_t) (argc-1))
320 ThrowStreamException(OptionError,"MissingArgument",option);
323 ThrowStreamException(OptionError,"UnrecognizedOption",option)
327 if (LocaleCompare("cache",option+1) == 0)
332 if (i == (ssize_t) argc)
333 ThrowStreamException(OptionError,"MissingArgument",option);
334 if (IsGeometry(argv[i]) == MagickFalse)
335 ThrowStreamInvalidArgumentException(option,argv[i]);
338 if (LocaleCompare("channel",option+1) == 0)
346 if (i == (ssize_t) (argc-1))
347 ThrowStreamException(OptionError,"MissingArgument",option);
348 channel=ParseChannelOption(argv[i]);
350 ThrowStreamException(OptionError,"UnrecognizedChannelType",
354 if (LocaleCompare("colorspace",option+1) == 0)
362 if (i == (ssize_t) (argc-1))
363 ThrowStreamException(OptionError,"MissingArgument",option);
364 colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,
367 ThrowStreamException(OptionError,"UnrecognizedColorspace",
371 if (LocaleCompare("compress",option+1) == 0)
379 if (i == (ssize_t) (argc-1))
380 ThrowStreamException(OptionError,"MissingArgument",option);
381 compress=ParseCommandOption(MagickCompressOptions,MagickFalse,
384 ThrowStreamException(OptionError,"UnrecognizedImageCompression",
388 if (LocaleCompare("concurrent",option+1) == 0)
390 ThrowStreamException(OptionError,"UnrecognizedOption",option)
394 if (LocaleCompare("debug",option+1) == 0)
402 if (i == (ssize_t) argc)
403 ThrowStreamException(OptionError,"MissingArgument",option);
404 event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]);
406 ThrowStreamException(OptionError,"UnrecognizedEventType",argv[i]);
407 (void) SetLogEventMask(argv[i]);
410 if (LocaleCompare("define",option+1) == 0)
413 if (i == (ssize_t) argc)
414 ThrowStreamException(OptionError,"MissingArgument",option);
420 define=GetImageOption(image_info,argv[i]);
421 if (define == (const char *) NULL)
422 ThrowStreamException(OptionError,"NoSuchOption",argv[i]);
427 if (LocaleCompare("density",option+1) == 0)
432 if (i == (ssize_t) argc)
433 ThrowStreamException(OptionError,"MissingArgument",option);
434 if (IsGeometry(argv[i]) == MagickFalse)
435 ThrowStreamInvalidArgumentException(option,argv[i]);
438 if (LocaleCompare("depth",option+1) == 0)
443 if (i == (ssize_t) argc)
444 ThrowStreamException(OptionError,"MissingArgument",option);
445 if (IsGeometry(argv[i]) == MagickFalse)
446 ThrowStreamInvalidArgumentException(option,argv[i]);
449 if (LocaleCompare("duration",option+1) == 0)
454 if (i == (ssize_t) (argc-1))
455 ThrowStreamException(OptionError,"MissingArgument",option);
456 if (IsGeometry(argv[i]) == MagickFalse)
457 ThrowStreamInvalidArgumentException(option,argv[i]);
460 ThrowStreamException(OptionError,"UnrecognizedOption",option)
464 if (LocaleCompare("extract",option+1) == 0)
469 if (i == (ssize_t) (argc-1))
470 ThrowStreamException(OptionError,"MissingArgument",option);
471 if (IsGeometry(argv[i]) == MagickFalse)
472 ThrowStreamInvalidArgumentException(option,argv[i]);
475 ThrowStreamException(OptionError,"UnrecognizedOption",option)
479 if ((LocaleCompare("help",option+1) == 0) ||
480 (LocaleCompare("-help",option+1) == 0))
481 return(StreamUsage());
482 ThrowStreamException(OptionError,"UnrecognizedOption",option)
486 if (LocaleCompare("identify",option+1) == 0)
488 if (LocaleCompare("interlace",option+1) == 0)
496 if (i == (ssize_t) argc)
497 ThrowStreamException(OptionError,"MissingArgument",option);
498 interlace=ParseCommandOption(MagickInterlaceOptions,MagickFalse,
501 ThrowStreamException(OptionError,"UnrecognizedInterlaceType",
505 if (LocaleCompare("interpolate",option+1) == 0)
513 if (i == (ssize_t) argc)
514 ThrowStreamException(OptionError,"MissingArgument",option);
515 interpolate=ParseCommandOption(MagickInterpolateOptions,MagickFalse,
518 ThrowStreamException(OptionError,"UnrecognizedInterpolateMethod",
522 ThrowStreamException(OptionError,"UnrecognizedOption",option)
526 if (LocaleCompare("limit",option+1) == 0)
540 if (i == (ssize_t) argc)
541 ThrowStreamException(OptionError,"MissingArgument",option);
542 resource=ParseCommandOption(MagickResourceOptions,MagickFalse,
545 ThrowStreamException(OptionError,"UnrecognizedResourceType",
548 if (i == (ssize_t) argc)
549 ThrowStreamException(OptionError,"MissingArgument",option);
550 value=InterpretLocaleValue(argv[i],&p);
552 if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0))
553 ThrowStreamInvalidArgumentException(option,argv[i]);
556 if (LocaleCompare("list",option+1) == 0)
564 if (i == (ssize_t) argc)
565 ThrowStreamException(OptionError,"MissingArgument",option);
566 list=ParseCommandOption(MagickListOptions,MagickFalse,argv[i]);
568 ThrowStreamException(OptionError,"UnrecognizedListType",argv[i]);
569 status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **)
572 return(status != 0 ? MagickFalse : MagickTrue);
574 if (LocaleCompare("log",option+1) == 0)
579 if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL))
580 ThrowStreamException(OptionError,"MissingArgument",option);
583 ThrowStreamException(OptionError,"UnrecognizedOption",option)
587 if (LocaleCompare("map",option+1) == 0)
589 (void) CopyMagickString(argv[i]+1,"san",MaxTextExtent);
593 SetStreamInfoMap(stream_info,argv[i]);
596 if (LocaleCompare("monitor",option+1) == 0)
598 ThrowStreamException(OptionError,"UnrecognizedOption",option)
602 if (LocaleCompare("quantize",option+1) == 0)
610 if (i == (ssize_t) (argc-1))
611 ThrowStreamException(OptionError,"MissingArgument",option);
612 colorspace=ParseCommandOption(MagickColorspaceOptions,
613 MagickFalse,argv[i]);
615 ThrowStreamException(OptionError,"UnrecognizedColorspace",
619 if (LocaleCompare("quiet",option+1) == 0)
621 ThrowStreamException(OptionError,"UnrecognizedOption",option)
625 if (LocaleCompare("regard-warnings",option+1) == 0)
627 if (LocaleNCompare("respect-parentheses",option+1,17) == 0)
629 respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse;
632 ThrowStreamException(OptionError,"UnrecognizedOption",option)
636 if (LocaleCompare("sampling-factor",option+1) == 0)
641 if (i == (ssize_t) argc)
642 ThrowStreamException(OptionError,"MissingArgument",option);
643 if (IsGeometry(argv[i]) == MagickFalse)
644 ThrowStreamInvalidArgumentException(option,argv[i]);
647 if (LocaleCompare("seed",option+1) == 0)
652 if (i == (ssize_t) (argc-1))
653 ThrowStreamException(OptionError,"MissingArgument",option);
654 if (IsGeometry(argv[i]) == MagickFalse)
655 ThrowStreamInvalidArgumentException(option,argv[i]);
658 if (LocaleCompare("set",option+1) == 0)
661 if (i == (ssize_t) argc)
662 ThrowStreamException(OptionError,"MissingArgument",option);
666 if (i == (ssize_t) argc)
667 ThrowStreamException(OptionError,"MissingArgument",option);
670 if (LocaleCompare("size",option+1) == 0)
675 if (i == (ssize_t) argc)
676 ThrowStreamException(OptionError,"MissingArgument",option);
677 if (IsGeometry(argv[i]) == MagickFalse)
678 ThrowStreamInvalidArgumentException(option,argv[i]);
681 if (LocaleCompare("storage-type",option+1) == 0)
689 if (i == (ssize_t) (argc-1))
690 ThrowStreamException(OptionError,"MissingArgument",option);
691 type=ParseCommandOption(MagickStorageOptions,MagickFalse,argv[i]);
693 ThrowStreamException(OptionError,"UnrecognizedStorageType",
695 SetStreamInfoStorageType(stream_info,(StorageType) type);
698 if (LocaleCompare("synchronize",option+1) == 0)
700 ThrowStreamException(OptionError,"UnrecognizedOption",option)
704 if (LocaleCompare("taint",option+1) == 0)
706 if (LocaleCompare("transparent-color",option+1) == 0)
711 if (i == (ssize_t) (argc-1))
712 ThrowStreamException(OptionError,"MissingArgument",option);
715 ThrowStreamException(OptionError,"UnrecognizedOption",option)
719 if (LocaleCompare("verbose",option+1) == 0)
721 if ((LocaleCompare("version",option+1) == 0) ||
722 (LocaleCompare("-version",option+1) == 0))
724 (void) FormatLocaleFile(stdout,"Version: %s\n",
725 GetMagickVersion((size_t *) NULL));
726 (void) FormatLocaleFile(stdout,"Copyright: %s\n",
727 GetMagickCopyright());
728 (void) FormatLocaleFile(stdout,"Features: %s\n\n",
729 GetMagickFeatures());
732 if (LocaleCompare("virtual-pixel",option+1) == 0)
740 if (i == (ssize_t) (argc-1))
741 ThrowStreamException(OptionError,"MissingArgument",option);
742 method=ParseCommandOption(MagickVirtualPixelOptions,MagickFalse,
745 ThrowStreamException(OptionError,
746 "UnrecognizedVirtualPixelMethod",argv[i]);
749 ThrowStreamException(OptionError,"UnrecognizedOption",option)
754 ThrowStreamException(OptionError,"UnrecognizedOption",option)
756 fire=(GetCommandOptionFlags(MagickCommandOptions,MagickFalse,option) &
757 FireOptionFlag) == 0 ? MagickFalse : MagickTrue;
758 if (fire != MagickFalse)
759 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
762 ThrowStreamException(OptionError,"UnbalancedParenthesis",argv[i]);
763 if (i-- != (ssize_t) (argc-1))
764 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
765 if (image == (Image *) NULL)
766 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
767 FinalizeImageSettings(image_info,image,MagickTrue);
768 if (image == (Image *) NULL)
769 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
771 return(status != 0 ? MagickTrue : MagickFalse);