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-2010 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.
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];
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());
247 option=(char *) NULL;
249 stream_info=AcquireStreamInfo(image_info);
254 ReadCommandlLine(argc,&argv);
255 status=ExpandFilenames(&argc,&argv);
256 if (status == MagickFalse)
257 ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
258 GetExceptionMessage(errno));
259 status=OpenStream(image_info,stream_info,argv[argc-1],exception);
260 if (status == MagickFalse)
265 for (i=1; i < (ssize_t) (argc-1); i++)
268 if (LocaleCompare(option,"(") == 0)
270 FireImageStack(MagickFalse,MagickTrue,pend);
271 if (k == MaxImageStackDepth)
272 ThrowStreamException(OptionError,"ParenthesisNestedTooDeeply",option);
276 if (LocaleCompare(option,")") == 0)
278 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
280 ThrowStreamException(OptionError,"UnableToParseExpression",option);
284 if (IsMagickOption(option) == MagickFalse)
292 FireImageStack(MagickFalse,MagickFalse,pend);
294 if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1)))
296 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
297 images=StreamImage(image_info,stream_info,exception);
298 status&=(images != (Image *) NULL) &&
299 (exception->severity < ErrorException);
300 if (images == (Image *) NULL)
302 AppendImageStack(images);
305 pend=image != (Image *) NULL ? MagickTrue : MagickFalse;
310 if (LocaleCompare("authenticate",option+1) == 0)
315 if (i == (ssize_t) (argc-1))
316 ThrowStreamException(OptionError,"MissingArgument",option);
319 ThrowStreamException(OptionError,"UnrecognizedOption",option)
323 if (LocaleCompare("cache",option+1) == 0)
328 if (i == (ssize_t) argc)
329 ThrowStreamException(OptionError,"MissingArgument",option);
330 if (IsGeometry(argv[i]) == MagickFalse)
331 ThrowStreamInvalidArgumentException(option,argv[i]);
334 if (LocaleCompare("channel",option+1) == 0)
342 if (i == (ssize_t) (argc-1))
343 ThrowStreamException(OptionError,"MissingArgument",option);
344 channel=ParseChannelOption(argv[i]);
346 ThrowStreamException(OptionError,"UnrecognizedChannelType",
350 if (LocaleCompare("colorspace",option+1) == 0)
358 if (i == (ssize_t) (argc-1))
359 ThrowStreamException(OptionError,"MissingArgument",option);
360 colorspace=ParseMagickOption(MagickColorspaceOptions,MagickFalse,
363 ThrowStreamException(OptionError,"UnrecognizedColorspace",
367 if (LocaleCompare("compress",option+1) == 0)
375 if (i == (ssize_t) (argc-1))
376 ThrowStreamException(OptionError,"MissingArgument",option);
377 compress=ParseMagickOption(MagickCompressOptions,MagickFalse,
380 ThrowStreamException(OptionError,"UnrecognizedImageCompression",
384 if (LocaleCompare("concurrent",option+1) == 0)
386 ThrowStreamException(OptionError,"UnrecognizedOption",option)
390 if (LocaleCompare("debug",option+1) == 0)
398 if (i == (ssize_t) argc)
399 ThrowStreamException(OptionError,"MissingArgument",option);
400 event=ParseMagickOption(MagickLogEventOptions,MagickFalse,argv[i]);
402 ThrowStreamException(OptionError,"UnrecognizedEventType",argv[i]);
403 (void) SetLogEventMask(argv[i]);
406 if (LocaleCompare("define",option+1) == 0)
409 if (i == (ssize_t) argc)
410 ThrowStreamException(OptionError,"MissingArgument",option);
416 define=GetImageOption(image_info,argv[i]);
417 if (define == (const char *) NULL)
418 ThrowStreamException(OptionError,"NoSuchOption",argv[i]);
423 if (LocaleCompare("density",option+1) == 0)
428 if (i == (ssize_t) argc)
429 ThrowStreamException(OptionError,"MissingArgument",option);
430 if (IsGeometry(argv[i]) == MagickFalse)
431 ThrowStreamInvalidArgumentException(option,argv[i]);
434 if (LocaleCompare("depth",option+1) == 0)
439 if (i == (ssize_t) argc)
440 ThrowStreamException(OptionError,"MissingArgument",option);
441 if (IsGeometry(argv[i]) == MagickFalse)
442 ThrowStreamInvalidArgumentException(option,argv[i]);
445 if (LocaleCompare("duration",option+1) == 0)
450 if (i == (ssize_t) (argc-1))
451 ThrowStreamException(OptionError,"MissingArgument",option);
452 if (IsGeometry(argv[i]) == MagickFalse)
453 ThrowStreamInvalidArgumentException(option,argv[i]);
456 ThrowStreamException(OptionError,"UnrecognizedOption",option)
460 if (LocaleCompare("extract",option+1) == 0)
465 if (i == (ssize_t) (argc-1))
466 ThrowStreamException(OptionError,"MissingArgument",option);
467 if (IsGeometry(argv[i]) == MagickFalse)
468 ThrowStreamInvalidArgumentException(option,argv[i]);
471 ThrowStreamException(OptionError,"UnrecognizedOption",option)
475 if ((LocaleCompare("help",option+1) == 0) ||
476 (LocaleCompare("-help",option+1) == 0))
477 return(StreamUsage());
478 ThrowStreamException(OptionError,"UnrecognizedOption",option)
482 if (LocaleCompare("identify",option+1) == 0)
484 if (LocaleCompare("interlace",option+1) == 0)
492 if (i == (ssize_t) argc)
493 ThrowStreamException(OptionError,"MissingArgument",option);
494 interlace=ParseMagickOption(MagickInterlaceOptions,MagickFalse,
497 ThrowStreamException(OptionError,"UnrecognizedInterlaceType",
501 if (LocaleCompare("interpolate",option+1) == 0)
509 if (i == (ssize_t) argc)
510 ThrowStreamException(OptionError,"MissingArgument",option);
511 interpolate=ParseMagickOption(MagickInterpolateOptions,MagickFalse,
514 ThrowStreamException(OptionError,"UnrecognizedInterpolateMethod",
518 ThrowStreamException(OptionError,"UnrecognizedOption",option)
522 if (LocaleCompare("limit",option+1) == 0)
536 if (i == (ssize_t) argc)
537 ThrowStreamException(OptionError,"MissingArgument",option);
538 resource=ParseMagickOption(MagickResourceOptions,MagickFalse,
541 ThrowStreamException(OptionError,"UnrecognizedResourceType",
544 if (i == (ssize_t) argc)
545 ThrowStreamException(OptionError,"MissingArgument",option);
546 value=strtod(argv[i],&p);
547 if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0))
548 ThrowStreamInvalidArgumentException(option,argv[i]);
551 if (LocaleCompare("list",option+1) == 0)
559 if (i == (ssize_t) argc)
560 ThrowStreamException(OptionError,"MissingArgument",option);
561 list=ParseMagickOption(MagickListOptions,MagickFalse,argv[i]);
563 ThrowStreamException(OptionError,"UnrecognizedListType",argv[i]);
564 status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **)
567 return(status != 0 ? MagickFalse : MagickTrue);
569 if (LocaleCompare("log",option+1) == 0)
574 if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL))
575 ThrowStreamException(OptionError,"MissingArgument",option);
578 ThrowStreamException(OptionError,"UnrecognizedOption",option)
582 if (LocaleCompare("map",option+1) == 0)
584 (void) CopyMagickString(argv[i]+1,"san",MaxTextExtent);
588 SetStreamInfoMap(stream_info,argv[i]);
591 if (LocaleCompare("monitor",option+1) == 0)
593 ThrowStreamException(OptionError,"UnrecognizedOption",option)
597 if (LocaleCompare("quantize",option+1) == 0)
605 if (i == (ssize_t) (argc-1))
606 ThrowStreamException(OptionError,"MissingArgument",option);
607 colorspace=ParseMagickOption(MagickColorspaceOptions,
608 MagickFalse,argv[i]);
610 ThrowStreamException(OptionError,"UnrecognizedColorspace",
614 if (LocaleCompare("quiet",option+1) == 0)
616 ThrowStreamException(OptionError,"UnrecognizedOption",option)
620 if (LocaleCompare("regard-warnings",option+1) == 0)
622 if (LocaleNCompare("respect-parentheses",option+1,17) == 0)
624 respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse;
627 ThrowStreamException(OptionError,"UnrecognizedOption",option)
631 if (LocaleCompare("sampling-factor",option+1) == 0)
636 if (i == (ssize_t) argc)
637 ThrowStreamException(OptionError,"MissingArgument",option);
638 if (IsGeometry(argv[i]) == MagickFalse)
639 ThrowStreamInvalidArgumentException(option,argv[i]);
642 if (LocaleCompare("seed",option+1) == 0)
647 if (i == (ssize_t) (argc-1))
648 ThrowStreamException(OptionError,"MissingArgument",option);
649 if (IsGeometry(argv[i]) == MagickFalse)
650 ThrowStreamInvalidArgumentException(option,argv[i]);
653 if (LocaleCompare("set",option+1) == 0)
656 if (i == (ssize_t) argc)
657 ThrowStreamException(OptionError,"MissingArgument",option);
661 if (i == (ssize_t) argc)
662 ThrowStreamException(OptionError,"MissingArgument",option);
665 if (LocaleCompare("size",option+1) == 0)
670 if (i == (ssize_t) argc)
671 ThrowStreamException(OptionError,"MissingArgument",option);
672 if (IsGeometry(argv[i]) == MagickFalse)
673 ThrowStreamInvalidArgumentException(option,argv[i]);
676 if (LocaleCompare("storage-type",option+1) == 0)
684 if (i == (ssize_t) (argc-1))
685 ThrowStreamException(OptionError,"MissingArgument",option);
686 type=ParseMagickOption(MagickStorageOptions,MagickFalse,argv[i]);
688 ThrowStreamException(OptionError,"UnrecognizedStorageType",
690 SetStreamInfoStorageType(stream_info,(StorageType) type);
693 if (LocaleCompare("synchronize",option+1) == 0)
695 ThrowStreamException(OptionError,"UnrecognizedOption",option)
699 if (LocaleCompare("taint",option+1) == 0)
701 if (LocaleCompare("transparent-color",option+1) == 0)
706 if (i == (ssize_t) (argc-1))
707 ThrowStreamException(OptionError,"MissingArgument",option);
710 ThrowStreamException(OptionError,"UnrecognizedOption",option)
714 if (LocaleCompare("verbose",option+1) == 0)
716 if ((LocaleCompare("version",option+1) == 0) ||
717 (LocaleCompare("-version",option+1) == 0))
719 (void) fprintf(stdout,"Version: %s\n",
720 GetMagickVersion((size_t *) NULL));
721 (void) fprintf(stdout,"Copyright: %s\n",GetMagickCopyright());
722 (void) fprintf(stdout,"Features: %s\n\n",GetMagickFeatures());
725 if (LocaleCompare("virtual-pixel",option+1) == 0)
733 if (i == (ssize_t) (argc-1))
734 ThrowStreamException(OptionError,"MissingArgument",option);
735 method=ParseMagickOption(MagickVirtualPixelOptions,MagickFalse,
738 ThrowStreamException(OptionError,
739 "UnrecognizedVirtualPixelMethod",argv[i]);
742 ThrowStreamException(OptionError,"UnrecognizedOption",option)
747 ThrowStreamException(OptionError,"UnrecognizedOption",option)
749 fire=ParseMagickOption(MagickImageListOptions,MagickFalse,option+1) < 0 ?
750 MagickFalse : MagickTrue;
751 if (fire != MagickFalse)
752 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
755 ThrowStreamException(OptionError,"UnbalancedParenthesis",argv[i]);
756 if (i-- != (ssize_t) (argc-1))
757 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
758 if (image == (Image *) NULL)
759 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
760 FinalizeImageSettings(image_info,image,MagickTrue);
761 if (image == (Image *) NULL)
762 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
764 return(status != 0 ? MagickTrue : MagickFalse);