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 "wand/studio.h"
48 #include "wand/MagickWand.h"
49 #include "wand/mogrify-private.h"
50 #include "magick/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) fprintf(stdout,"Version: %s\n",
235 GetMagickVersion((size_t *) NULL));
236 (void) fprintf(stdout,"Copyright: %s\n",GetMagickCopyright());
237 (void) fprintf(stdout,"Features: %s\n\n",GetMagickFeatures());
242 return(StreamUsage());
248 option=(char *) NULL;
250 respect_parenthesis=MagickFalse;
251 stream_info=AcquireStreamInfo(image_info);
256 ReadCommandlLine(argc,&argv);
257 status=ExpandFilenames(&argc,&argv);
258 if (status == MagickFalse)
259 ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
260 GetExceptionMessage(errno));
261 status=OpenStream(image_info,stream_info,argv[argc-1],exception);
262 if (status == MagickFalse)
267 for (i=1; i < (ssize_t) (argc-1); i++)
270 if (LocaleCompare(option,"(") == 0)
272 FireImageStack(MagickFalse,MagickTrue,pend);
273 if (k == MaxImageStackDepth)
274 ThrowStreamException(OptionError,"ParenthesisNestedTooDeeply",option);
278 if (LocaleCompare(option,")") == 0)
280 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
282 ThrowStreamException(OptionError,"UnableToParseExpression",option);
286 if (IsMagickOption(option) == MagickFalse)
294 FireImageStack(MagickFalse,MagickFalse,pend);
296 if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1)))
298 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
299 images=StreamImage(image_info,stream_info,exception);
300 status&=(images != (Image *) NULL) &&
301 (exception->severity < ErrorException);
302 if (images == (Image *) NULL)
304 AppendImageStack(images);
307 pend=image != (Image *) NULL ? MagickTrue : MagickFalse;
312 if (LocaleCompare("authenticate",option+1) == 0)
317 if (i == (ssize_t) (argc-1))
318 ThrowStreamException(OptionError,"MissingArgument",option);
321 ThrowStreamException(OptionError,"UnrecognizedOption",option)
325 if (LocaleCompare("cache",option+1) == 0)
330 if (i == (ssize_t) argc)
331 ThrowStreamException(OptionError,"MissingArgument",option);
332 if (IsGeometry(argv[i]) == MagickFalse)
333 ThrowStreamInvalidArgumentException(option,argv[i]);
336 if (LocaleCompare("channel",option+1) == 0)
344 if (i == (ssize_t) (argc-1))
345 ThrowStreamException(OptionError,"MissingArgument",option);
346 channel=ParseChannelOption(argv[i]);
348 ThrowStreamException(OptionError,"UnrecognizedChannelType",
352 if (LocaleCompare("colorspace",option+1) == 0)
360 if (i == (ssize_t) (argc-1))
361 ThrowStreamException(OptionError,"MissingArgument",option);
362 colorspace=ParseMagickOption(MagickColorspaceOptions,MagickFalse,
365 ThrowStreamException(OptionError,"UnrecognizedColorspace",
369 if (LocaleCompare("compress",option+1) == 0)
377 if (i == (ssize_t) (argc-1))
378 ThrowStreamException(OptionError,"MissingArgument",option);
379 compress=ParseMagickOption(MagickCompressOptions,MagickFalse,
382 ThrowStreamException(OptionError,"UnrecognizedImageCompression",
386 if (LocaleCompare("concurrent",option+1) == 0)
388 ThrowStreamException(OptionError,"UnrecognizedOption",option)
392 if (LocaleCompare("debug",option+1) == 0)
400 if (i == (ssize_t) argc)
401 ThrowStreamException(OptionError,"MissingArgument",option);
402 event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
404 ThrowStreamException(OptionError,"UnrecognizedEventType",argv[i]);
405 (void) SetLogEventMask(argv[i]);
408 if (LocaleCompare("define",option+1) == 0)
411 if (i == (ssize_t) argc)
412 ThrowStreamException(OptionError,"MissingArgument",option);
418 define=GetImageOption(image_info,argv[i]);
419 if (define == (const char *) NULL)
420 ThrowStreamException(OptionError,"NoSuchOption",argv[i]);
425 if (LocaleCompare("density",option+1) == 0)
430 if (i == (ssize_t) argc)
431 ThrowStreamException(OptionError,"MissingArgument",option);
432 if (IsGeometry(argv[i]) == MagickFalse)
433 ThrowStreamInvalidArgumentException(option,argv[i]);
436 if (LocaleCompare("depth",option+1) == 0)
441 if (i == (ssize_t) argc)
442 ThrowStreamException(OptionError,"MissingArgument",option);
443 if (IsGeometry(argv[i]) == MagickFalse)
444 ThrowStreamInvalidArgumentException(option,argv[i]);
447 if (LocaleCompare("duration",option+1) == 0)
452 if (i == (ssize_t) (argc-1))
453 ThrowStreamException(OptionError,"MissingArgument",option);
454 if (IsGeometry(argv[i]) == MagickFalse)
455 ThrowStreamInvalidArgumentException(option,argv[i]);
458 ThrowStreamException(OptionError,"UnrecognizedOption",option)
462 if (LocaleCompare("extract",option+1) == 0)
467 if (i == (ssize_t) (argc-1))
468 ThrowStreamException(OptionError,"MissingArgument",option);
469 if (IsGeometry(argv[i]) == MagickFalse)
470 ThrowStreamInvalidArgumentException(option,argv[i]);
473 ThrowStreamException(OptionError,"UnrecognizedOption",option)
477 if ((LocaleCompare("help",option+1) == 0) ||
478 (LocaleCompare("-help",option+1) == 0))
479 return(StreamUsage());
480 ThrowStreamException(OptionError,"UnrecognizedOption",option)
484 if (LocaleCompare("identify",option+1) == 0)
486 if (LocaleCompare("interlace",option+1) == 0)
494 if (i == (ssize_t) argc)
495 ThrowStreamException(OptionError,"MissingArgument",option);
496 interlace=ParseMagickOption(MagickInterlaceOptions,MagickFalse,
499 ThrowStreamException(OptionError,"UnrecognizedInterlaceType",
503 if (LocaleCompare("interpolate",option+1) == 0)
511 if (i == (ssize_t) argc)
512 ThrowStreamException(OptionError,"MissingArgument",option);
513 interpolate=ParseMagickOption(MagickInterpolateOptions,MagickFalse,
516 ThrowStreamException(OptionError,"UnrecognizedInterpolateMethod",
520 ThrowStreamException(OptionError,"UnrecognizedOption",option)
524 if (LocaleCompare("limit",option+1) == 0)
538 if (i == (ssize_t) argc)
539 ThrowStreamException(OptionError,"MissingArgument",option);
540 resource=ParseMagickOption(MagickResourceOptions,MagickFalse,
543 ThrowStreamException(OptionError,"UnrecognizedResourceType",
546 if (i == (ssize_t) argc)
547 ThrowStreamException(OptionError,"MissingArgument",option);
548 value=strtod(argv[i],&p);
550 if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0))
551 ThrowStreamInvalidArgumentException(option,argv[i]);
554 if (LocaleCompare("list",option+1) == 0)
562 if (i == (ssize_t) argc)
563 ThrowStreamException(OptionError,"MissingArgument",option);
564 list=ParseMagickOption(MagickListOptions,MagickFalse,argv[i]);
566 ThrowStreamException(OptionError,"UnrecognizedListType",argv[i]);
567 status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **)
570 return(status != 0 ? MagickFalse : MagickTrue);
572 if (LocaleCompare("log",option+1) == 0)
577 if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL))
578 ThrowStreamException(OptionError,"MissingArgument",option);
581 ThrowStreamException(OptionError,"UnrecognizedOption",option)
585 if (LocaleCompare("map",option+1) == 0)
587 (void) CopyMagickString(argv[i]+1,"san",MaxTextExtent);
591 SetStreamInfoMap(stream_info,argv[i]);
594 if (LocaleCompare("monitor",option+1) == 0)
596 ThrowStreamException(OptionError,"UnrecognizedOption",option)
600 if (LocaleCompare("quantize",option+1) == 0)
608 if (i == (ssize_t) (argc-1))
609 ThrowStreamException(OptionError,"MissingArgument",option);
610 colorspace=ParseMagickOption(MagickColorspaceOptions,
611 MagickFalse,argv[i]);
613 ThrowStreamException(OptionError,"UnrecognizedColorspace",
617 if (LocaleCompare("quiet",option+1) == 0)
619 ThrowStreamException(OptionError,"UnrecognizedOption",option)
623 if (LocaleCompare("regard-warnings",option+1) == 0)
625 if (LocaleNCompare("respect-parentheses",option+1,17) == 0)
627 respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse;
630 ThrowStreamException(OptionError,"UnrecognizedOption",option)
634 if (LocaleCompare("sampling-factor",option+1) == 0)
639 if (i == (ssize_t) argc)
640 ThrowStreamException(OptionError,"MissingArgument",option);
641 if (IsGeometry(argv[i]) == MagickFalse)
642 ThrowStreamInvalidArgumentException(option,argv[i]);
645 if (LocaleCompare("seed",option+1) == 0)
650 if (i == (ssize_t) (argc-1))
651 ThrowStreamException(OptionError,"MissingArgument",option);
652 if (IsGeometry(argv[i]) == MagickFalse)
653 ThrowStreamInvalidArgumentException(option,argv[i]);
656 if (LocaleCompare("set",option+1) == 0)
659 if (i == (ssize_t) argc)
660 ThrowStreamException(OptionError,"MissingArgument",option);
664 if (i == (ssize_t) argc)
665 ThrowStreamException(OptionError,"MissingArgument",option);
668 if (LocaleCompare("size",option+1) == 0)
673 if (i == (ssize_t) argc)
674 ThrowStreamException(OptionError,"MissingArgument",option);
675 if (IsGeometry(argv[i]) == MagickFalse)
676 ThrowStreamInvalidArgumentException(option,argv[i]);
679 if (LocaleCompare("storage-type",option+1) == 0)
687 if (i == (ssize_t) (argc-1))
688 ThrowStreamException(OptionError,"MissingArgument",option);
689 type=ParseMagickOption(MagickStorageOptions,MagickFalse,argv[i]);
691 ThrowStreamException(OptionError,"UnrecognizedStorageType",
693 SetStreamInfoStorageType(stream_info,(StorageType) type);
696 if (LocaleCompare("synchronize",option+1) == 0)
698 ThrowStreamException(OptionError,"UnrecognizedOption",option)
702 if (LocaleCompare("taint",option+1) == 0)
704 if (LocaleCompare("transparent-color",option+1) == 0)
709 if (i == (ssize_t) (argc-1))
710 ThrowStreamException(OptionError,"MissingArgument",option);
713 ThrowStreamException(OptionError,"UnrecognizedOption",option)
717 if (LocaleCompare("verbose",option+1) == 0)
719 if ((LocaleCompare("version",option+1) == 0) ||
720 (LocaleCompare("-version",option+1) == 0))
722 (void) fprintf(stdout,"Version: %s\n",
723 GetMagickVersion((size_t *) NULL));
724 (void) fprintf(stdout,"Copyright: %s\n",GetMagickCopyright());
725 (void) fprintf(stdout,"Features: %s\n\n",GetMagickFeatures());
728 if (LocaleCompare("virtual-pixel",option+1) == 0)
736 if (i == (ssize_t) (argc-1))
737 ThrowStreamException(OptionError,"MissingArgument",option);
738 method=ParseMagickOption(MagickVirtualPixelOptions,MagickFalse,
741 ThrowStreamException(OptionError,
742 "UnrecognizedVirtualPixelMethod",argv[i]);
745 ThrowStreamException(OptionError,"UnrecognizedOption",option)
750 ThrowStreamException(OptionError,"UnrecognizedOption",option)
752 fire=(ParseMagickOption(MagickCommandOptions,MagickFalse,option)
753 &FireOption) == 0 ? MagickFalse : MagickTrue;
754 if (fire != MagickFalse)
755 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
758 ThrowStreamException(OptionError,"UnbalancedParenthesis",argv[i]);
759 if (i-- != (ssize_t) (argc-1))
760 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
761 if (image == (Image *) NULL)
762 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
763 FinalizeImageSettings(image_info,image,MagickTrue);
764 if (image == (Image *) NULL)
765 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
767 return(status != 0 ? MagickTrue : MagickFalse);