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-2012 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"
51 #include "MagickCore/string-private.h"
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 % S t r e a m I m a g e C o m m a n d %
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 % StreamImageCommand() is a lightweight method designed to extract pixels
65 % from large image files to a raw format using a minimum of system resources.
66 % The entire image or any regular portion of the image can be extracted.
68 % The format of the StreamImageCommand method is:
70 % MagickBooleanType StreamImageCommand(ImageInfo *image_info,int argc,
71 % char **argv,char **metadata,ExceptionInfo *exception)
73 % A description of each parameter follows:
75 % o image_info: the image info.
77 % o argc: the number of elements in the argument vector.
79 % o argv: A text array containing the command line arguments.
81 % o metadata: any metadata is returned here.
83 % o exception: return any errors or warnings in this structure.
87 static MagickBooleanType StreamUsage(void)
95 "-debug events display copious debugging information",
96 "-help print program options",
97 "-list type print a list of supported option arguments",
98 "-log format format of debugging information",
99 "-version print version information",
104 "-authenticate password",
105 " decipher image with this password",
106 "-channel type apply option to select image channels",
107 "-colorspace type alternate image colorspace",
108 "-compress type type of pixel compression when writing the image",
109 "-define format:option",
110 " define one or more image format options",
111 "-density geometry horizontal and vertical density of the image",
112 "-depth value image depth",
113 "-extract geometry extract area from image",
114 "-identify identify the format and characteristics of the image",
115 "-interlace type type of image interlacing scheme",
116 "-interpolate method pixel color interpolation method",
117 "-limit type value pixel cache resource limit",
118 "-map components one or more pixel components",
119 "-monitor monitor progress",
120 "-quantize colorspace reduce colors in this colorspace",
121 "-quiet suppress all warning messages",
122 "-regard-warnings pay attention to warning messages",
123 "-respect-parentheses settings remain in effect until parenthesis boundary",
124 "-sampling-factor geometry",
125 " horizontal and vertical sampling factor",
126 "-seed value seed a new sequence of pseudo-random numbers",
127 "-set attribute value set an image attribute",
128 "-size geometry width and height of image",
129 "-storage-type type pixel storage type",
130 "-synchronize synchronize image to storage device",
131 "-taint declare the image as modified",
132 "-transparent-color color",
133 " transparent color",
134 "-verbose print detailed information about the image",
135 "-virtual-pixel method",
136 " virtual pixel access method",
140 (void) printf("Version: %s\n",GetMagickVersion((size_t *) NULL));
141 (void) printf("Copyright: %s\n",GetMagickCopyright());
142 (void) printf("Features: %s\n\n",GetMagickFeatures());
143 (void) printf("Usage: %s [options ...] input-image raw-image\n",
145 (void) printf("\nImage Settings:\n");
146 for (p=settings; *p != (char *) NULL; p++)
147 (void) printf(" %s\n",*p);
148 (void) printf("\nMiscellaneous Options:\n");
149 for (p=miscellaneous; *p != (char *) NULL; p++)
150 (void) printf(" %s\n",*p);
152 "\nBy default, the image format of `file' is determined by its magic\n");
154 "number. To specify a particular image format, precede the filename\n");
156 "with an image format name and a colon (i.e. ps:image) or specify the\n");
158 "image type as the filename suffix (i.e. image.ps). Specify 'file' as\n");
159 (void) printf("'-' for standard input or output.\n");
163 WandExport MagickBooleanType StreamImageCommand(ImageInfo *image_info,
164 int argc,char **argv,char **metadata,ExceptionInfo *exception)
166 #define DestroyStream() \
168 DestroyImageStack(); \
169 stream_info=DestroyStreamInfo(stream_info); \
170 for (i=0; i < (ssize_t) argc; i++) \
171 argv[i]=DestroyString(argv[i]); \
172 argv=(char **) RelinquishMagickMemory(argv); \
174 #define ThrowStreamException(asperity,tag,option) \
176 (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
179 return(MagickFalse); \
181 #define ThrowStreamInvalidArgumentException(option,argument) \
183 (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
184 "InvalidArgument","`%s': %s",option,argument); \
186 return(MagickFalse); \
200 image_stack[MaxImageStackDepth+1];
223 assert(image_info != (ImageInfo *) NULL);
224 assert(image_info->signature == MagickSignature);
225 if (image_info->debug != MagickFalse)
226 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
227 assert(exception != (ExceptionInfo *) NULL);
232 if ((LocaleCompare("version",option+1) == 0) ||
233 (LocaleCompare("-version",option+1) == 0))
235 (void) FormatLocaleFile(stdout,"Version: %s\n",
236 GetMagickVersion((size_t *) NULL));
237 (void) FormatLocaleFile(stdout,"Copyright: %s\n",
238 GetMagickCopyright());
239 (void) FormatLocaleFile(stdout,"Features: %s\n\n",
240 GetMagickFeatures());
245 return(StreamUsage());
251 option=(char *) NULL;
253 respect_parenthesis=MagickFalse;
254 stream_info=AcquireStreamInfo(image_info,exception);
259 ReadCommandlLine(argc,&argv);
260 status=ExpandFilenames(&argc,&argv);
261 if (status == MagickFalse)
262 ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
263 GetExceptionMessage(errno));
264 status=OpenStream(image_info,stream_info,argv[argc-1],exception);
265 if (status == MagickFalse)
270 for (i=1; i < (ssize_t) (argc-1); i++)
273 if (LocaleCompare(option,"(") == 0)
275 FireImageStack(MagickFalse,MagickTrue,pend);
276 if (k == MaxImageStackDepth)
277 ThrowStreamException(OptionError,"ParenthesisNestedTooDeeply",option);
281 if (LocaleCompare(option,")") == 0)
283 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
285 ThrowStreamException(OptionError,"UnableToParseExpression",option);
289 if (IsCommandOption(option) == MagickFalse)
297 FireImageStack(MagickFalse,MagickFalse,pend);
299 if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1)))
301 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
302 images=StreamImage(image_info,stream_info,exception);
303 status&=(images != (Image *) NULL) &&
304 (exception->severity < ErrorException);
305 if (images == (Image *) NULL)
307 AppendImageStack(images);
310 pend=image != (Image *) NULL ? MagickTrue : MagickFalse;
315 if (LocaleCompare("authenticate",option+1) == 0)
320 if (i == (ssize_t) (argc-1))
321 ThrowStreamException(OptionError,"MissingArgument",option);
324 ThrowStreamException(OptionError,"UnrecognizedOption",option)
328 if (LocaleCompare("cache",option+1) == 0)
333 if (i == (ssize_t) argc)
334 ThrowStreamException(OptionError,"MissingArgument",option);
335 if (IsGeometry(argv[i]) == MagickFalse)
336 ThrowStreamInvalidArgumentException(option,argv[i]);
339 if (LocaleCompare("channel",option+1) == 0)
347 if (i == (ssize_t) (argc-1))
348 ThrowStreamException(OptionError,"MissingArgument",option);
349 channel=ParseChannelOption(argv[i]);
351 ThrowStreamException(OptionError,"UnrecognizedChannelType",
355 if (LocaleCompare("colorspace",option+1) == 0)
363 if (i == (ssize_t) (argc-1))
364 ThrowStreamException(OptionError,"MissingArgument",option);
365 colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,
368 ThrowStreamException(OptionError,"UnrecognizedColorspace",
372 if (LocaleCompare("compress",option+1) == 0)
380 if (i == (ssize_t) (argc-1))
381 ThrowStreamException(OptionError,"MissingArgument",option);
382 compress=ParseCommandOption(MagickCompressOptions,MagickFalse,
385 ThrowStreamException(OptionError,"UnrecognizedImageCompression",
389 if (LocaleCompare("concurrent",option+1) == 0)
391 ThrowStreamException(OptionError,"UnrecognizedOption",option)
395 if (LocaleCompare("debug",option+1) == 0)
403 if (i == (ssize_t) argc)
404 ThrowStreamException(OptionError,"MissingArgument",option);
405 event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]);
407 ThrowStreamException(OptionError,"UnrecognizedEventType",argv[i]);
408 (void) SetLogEventMask(argv[i]);
411 if (LocaleCompare("define",option+1) == 0)
414 if (i == (ssize_t) argc)
415 ThrowStreamException(OptionError,"MissingArgument",option);
421 define=GetImageOption(image_info,argv[i]);
422 if (define == (const char *) NULL)
423 ThrowStreamException(OptionError,"NoSuchOption",argv[i]);
428 if (LocaleCompare("density",option+1) == 0)
433 if (i == (ssize_t) argc)
434 ThrowStreamException(OptionError,"MissingArgument",option);
435 if (IsGeometry(argv[i]) == MagickFalse)
436 ThrowStreamInvalidArgumentException(option,argv[i]);
439 if (LocaleCompare("depth",option+1) == 0)
444 if (i == (ssize_t) argc)
445 ThrowStreamException(OptionError,"MissingArgument",option);
446 if (IsGeometry(argv[i]) == MagickFalse)
447 ThrowStreamInvalidArgumentException(option,argv[i]);
450 if (LocaleCompare("duration",option+1) == 0)
455 if (i == (ssize_t) (argc-1))
456 ThrowStreamException(OptionError,"MissingArgument",option);
457 if (IsGeometry(argv[i]) == MagickFalse)
458 ThrowStreamInvalidArgumentException(option,argv[i]);
461 ThrowStreamException(OptionError,"UnrecognizedOption",option)
465 if (LocaleCompare("extract",option+1) == 0)
470 if (i == (ssize_t) (argc-1))
471 ThrowStreamException(OptionError,"MissingArgument",option);
472 if (IsGeometry(argv[i]) == MagickFalse)
473 ThrowStreamInvalidArgumentException(option,argv[i]);
476 ThrowStreamException(OptionError,"UnrecognizedOption",option)
480 if ((LocaleCompare("help",option+1) == 0) ||
481 (LocaleCompare("-help",option+1) == 0))
482 return(StreamUsage());
483 ThrowStreamException(OptionError,"UnrecognizedOption",option)
487 if (LocaleCompare("identify",option+1) == 0)
489 if (LocaleCompare("interlace",option+1) == 0)
497 if (i == (ssize_t) argc)
498 ThrowStreamException(OptionError,"MissingArgument",option);
499 interlace=ParseCommandOption(MagickInterlaceOptions,MagickFalse,
502 ThrowStreamException(OptionError,"UnrecognizedInterlaceType",
506 if (LocaleCompare("interpolate",option+1) == 0)
514 if (i == (ssize_t) argc)
515 ThrowStreamException(OptionError,"MissingArgument",option);
516 interpolate=ParseCommandOption(MagickInterpolateOptions,MagickFalse,
519 ThrowStreamException(OptionError,"UnrecognizedInterpolateMethod",
523 ThrowStreamException(OptionError,"UnrecognizedOption",option)
527 if (LocaleCompare("limit",option+1) == 0)
541 if (i == (ssize_t) argc)
542 ThrowStreamException(OptionError,"MissingArgument",option);
543 resource=ParseCommandOption(MagickResourceOptions,MagickFalse,
546 ThrowStreamException(OptionError,"UnrecognizedResourceType",
549 if (i == (ssize_t) argc)
550 ThrowStreamException(OptionError,"MissingArgument",option);
551 value=StringToDouble(argv[i],&p);
553 if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0))
554 ThrowStreamInvalidArgumentException(option,argv[i]);
557 if (LocaleCompare("list",option+1) == 0)
565 if (i == (ssize_t) argc)
566 ThrowStreamException(OptionError,"MissingArgument",option);
567 list=ParseCommandOption(MagickListOptions,MagickFalse,argv[i]);
569 ThrowStreamException(OptionError,"UnrecognizedListType",argv[i]);
570 status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **)
573 return(status != 0 ? MagickFalse : MagickTrue);
575 if (LocaleCompare("log",option+1) == 0)
580 if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL))
581 ThrowStreamException(OptionError,"MissingArgument",option);
584 ThrowStreamException(OptionError,"UnrecognizedOption",option)
588 if (LocaleCompare("map",option+1) == 0)
590 (void) CopyMagickString(argv[i]+1,"san",MaxTextExtent);
594 SetStreamInfoMap(stream_info,argv[i]);
597 if (LocaleCompare("monitor",option+1) == 0)
599 ThrowStreamException(OptionError,"UnrecognizedOption",option)
603 if (LocaleCompare("quantize",option+1) == 0)
611 if (i == (ssize_t) (argc-1))
612 ThrowStreamException(OptionError,"MissingArgument",option);
613 colorspace=ParseCommandOption(MagickColorspaceOptions,
614 MagickFalse,argv[i]);
616 ThrowStreamException(OptionError,"UnrecognizedColorspace",
620 if (LocaleCompare("quiet",option+1) == 0)
622 ThrowStreamException(OptionError,"UnrecognizedOption",option)
626 if (LocaleCompare("regard-warnings",option+1) == 0)
628 if (LocaleNCompare("respect-parentheses",option+1,17) == 0)
630 respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse;
633 ThrowStreamException(OptionError,"UnrecognizedOption",option)
637 if (LocaleCompare("sampling-factor",option+1) == 0)
642 if (i == (ssize_t) argc)
643 ThrowStreamException(OptionError,"MissingArgument",option);
644 if (IsGeometry(argv[i]) == MagickFalse)
645 ThrowStreamInvalidArgumentException(option,argv[i]);
648 if (LocaleCompare("seed",option+1) == 0)
653 if (i == (ssize_t) (argc-1))
654 ThrowStreamException(OptionError,"MissingArgument",option);
655 if (IsGeometry(argv[i]) == MagickFalse)
656 ThrowStreamInvalidArgumentException(option,argv[i]);
659 if (LocaleCompare("set",option+1) == 0)
662 if (i == (ssize_t) argc)
663 ThrowStreamException(OptionError,"MissingArgument",option);
667 if (i == (ssize_t) argc)
668 ThrowStreamException(OptionError,"MissingArgument",option);
671 if (LocaleCompare("size",option+1) == 0)
676 if (i == (ssize_t) argc)
677 ThrowStreamException(OptionError,"MissingArgument",option);
678 if (IsGeometry(argv[i]) == MagickFalse)
679 ThrowStreamInvalidArgumentException(option,argv[i]);
682 if (LocaleCompare("storage-type",option+1) == 0)
690 if (i == (ssize_t) (argc-1))
691 ThrowStreamException(OptionError,"MissingArgument",option);
692 type=ParseCommandOption(MagickStorageOptions,MagickFalse,argv[i]);
694 ThrowStreamException(OptionError,"UnrecognizedStorageType",
696 SetStreamInfoStorageType(stream_info,(StorageType) type);
699 if (LocaleCompare("synchronize",option+1) == 0)
701 ThrowStreamException(OptionError,"UnrecognizedOption",option)
705 if (LocaleCompare("taint",option+1) == 0)
707 if (LocaleCompare("transparent-color",option+1) == 0)
712 if (i == (ssize_t) (argc-1))
713 ThrowStreamException(OptionError,"MissingArgument",option);
716 ThrowStreamException(OptionError,"UnrecognizedOption",option)
720 if (LocaleCompare("verbose",option+1) == 0)
722 if ((LocaleCompare("version",option+1) == 0) ||
723 (LocaleCompare("-version",option+1) == 0))
725 (void) FormatLocaleFile(stdout,"Version: %s\n",
726 GetMagickVersion((size_t *) NULL));
727 (void) FormatLocaleFile(stdout,"Copyright: %s\n",
728 GetMagickCopyright());
729 (void) FormatLocaleFile(stdout,"Features: %s\n\n",
730 GetMagickFeatures());
733 if (LocaleCompare("virtual-pixel",option+1) == 0)
741 if (i == (ssize_t) (argc-1))
742 ThrowStreamException(OptionError,"MissingArgument",option);
743 method=ParseCommandOption(MagickVirtualPixelOptions,MagickFalse,
746 ThrowStreamException(OptionError,
747 "UnrecognizedVirtualPixelMethod",argv[i]);
750 ThrowStreamException(OptionError,"UnrecognizedOption",option)
755 ThrowStreamException(OptionError,"UnrecognizedOption",option)
757 fire=(GetCommandOptionFlags(MagickCommandOptions,MagickFalse,option) &
758 FireOptionFlag) == 0 ? MagickFalse : MagickTrue;
759 if (fire != MagickFalse)
760 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
763 ThrowStreamException(OptionError,"UnbalancedParenthesis",argv[i]);
764 if (i-- != (ssize_t) (argc-1))
765 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
766 if (image == (Image *) NULL)
767 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
768 FinalizeImageSettings(image_info,image,MagickTrue);
769 if (image == (Image *) NULL)
770 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
772 return(status != 0 ? MagickTrue : MagickFalse);