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-2014 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 ListMagickVersion(stdout);
141 (void) printf("Usage: %s [options ...] input-image raw-image\n",
143 (void) printf("\nImage Settings:\n");
144 for (p=settings; *p != (char *) NULL; p++)
145 (void) printf(" %s\n",*p);
146 (void) printf("\nMiscellaneous Options:\n");
147 for (p=miscellaneous; *p != (char *) NULL; p++)
148 (void) printf(" %s\n",*p);
150 "\nBy default, the image format of 'file' is determined by its magic\n");
152 "number. To specify a particular image format, precede the filename\n");
154 "with an image format name and a colon (i.e. ps:image) or specify the\n");
156 "image type as the filename suffix (i.e. image.ps). Specify 'file' as\n");
157 (void) printf("'-' for standard input or output.\n");
161 WandExport MagickBooleanType StreamImageCommand(ImageInfo *image_info,
162 int argc,char **argv,char **metadata,ExceptionInfo *exception)
164 #define DestroyStream() \
166 DestroyImageStack(); \
167 stream_info=DestroyStreamInfo(stream_info); \
168 for (i=0; i < (ssize_t) argc; i++) \
169 argv[i]=DestroyString(argv[i]); \
170 argv=(char **) RelinquishMagickMemory(argv); \
172 #define ThrowStreamException(asperity,tag,option) \
174 (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \
177 return(MagickFalse); \
179 #define ThrowStreamInvalidArgumentException(option,argument) \
181 (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \
182 "InvalidArgument","'%s': %s",option,argument); \
184 return(MagickFalse); \
198 image_stack[MaxImageStackDepth+1];
221 assert(image_info != (ImageInfo *) NULL);
222 assert(image_info->signature == MagickSignature);
223 if (image_info->debug != MagickFalse)
224 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
225 assert(exception != (ExceptionInfo *) NULL);
230 if ((LocaleCompare("version",option+1) == 0) ||
231 (LocaleCompare("-version",option+1) == 0))
233 ListMagickVersion(stdout);
238 return(StreamUsage());
244 option=(char *) NULL;
246 respect_parenthesis=MagickFalse;
247 stream_info=AcquireStreamInfo(image_info,exception);
252 ReadCommandlLine(argc,&argv);
253 status=ExpandFilenames(&argc,&argv);
254 if (status == MagickFalse)
255 ThrowStreamException(ResourceLimitError,"MemoryAllocationFailed",
256 GetExceptionMessage(errno));
257 status=OpenStream(image_info,stream_info,argv[argc-1],exception);
258 if (status == MagickFalse)
263 for (i=1; i < (ssize_t) (argc-1); i++)
266 if (LocaleCompare(option,"(") == 0)
268 FireImageStack(MagickFalse,MagickTrue,pend);
269 if (k == MaxImageStackDepth)
270 ThrowStreamException(OptionError,"ParenthesisNestedTooDeeply",option);
274 if (LocaleCompare(option,")") == 0)
276 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
278 ThrowStreamException(OptionError,"UnableToParseExpression",option);
282 if (IsCommandOption(option) == MagickFalse)
290 FireImageStack(MagickFalse,MagickFalse,pend);
292 if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1)))
294 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
295 images=StreamImage(image_info,stream_info,exception);
296 status&=(images != (Image *) NULL) &&
297 (exception->severity < ErrorException);
298 if (images == (Image *) NULL)
300 AppendImageStack(images);
303 pend=image != (Image *) NULL ? MagickTrue : MagickFalse;
308 if (LocaleCompare("authenticate",option+1) == 0)
313 if (i == (ssize_t) (argc-1))
314 ThrowStreamException(OptionError,"MissingArgument",option);
317 ThrowStreamException(OptionError,"UnrecognizedOption",option)
321 if (LocaleCompare("cache",option+1) == 0)
326 if (i == (ssize_t) argc)
327 ThrowStreamException(OptionError,"MissingArgument",option);
328 if (IsGeometry(argv[i]) == MagickFalse)
329 ThrowStreamInvalidArgumentException(option,argv[i]);
332 if (LocaleCompare("channel",option+1) == 0)
340 if (i == (ssize_t) (argc-1))
341 ThrowStreamException(OptionError,"MissingArgument",option);
342 channel=ParseChannelOption(argv[i]);
344 ThrowStreamException(OptionError,"UnrecognizedChannelType",
348 if (LocaleCompare("colorspace",option+1) == 0)
356 if (i == (ssize_t) (argc-1))
357 ThrowStreamException(OptionError,"MissingArgument",option);
358 colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse,
361 ThrowStreamException(OptionError,"UnrecognizedColorspace",
365 if (LocaleCompare("compress",option+1) == 0)
373 if (i == (ssize_t) (argc-1))
374 ThrowStreamException(OptionError,"MissingArgument",option);
375 compress=ParseCommandOption(MagickCompressOptions,MagickFalse,
378 ThrowStreamException(OptionError,"UnrecognizedImageCompression",
382 if (LocaleCompare("concurrent",option+1) == 0)
384 ThrowStreamException(OptionError,"UnrecognizedOption",option)
388 if (LocaleCompare("debug",option+1) == 0)
396 if (i == (ssize_t) argc)
397 ThrowStreamException(OptionError,"MissingArgument",option);
398 event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]);
400 ThrowStreamException(OptionError,"UnrecognizedEventType",argv[i]);
401 (void) SetLogEventMask(argv[i]);
404 if (LocaleCompare("define",option+1) == 0)
407 if (i == (ssize_t) argc)
408 ThrowStreamException(OptionError,"MissingArgument",option);
414 define=GetImageOption(image_info,argv[i]);
415 if (define == (const char *) NULL)
416 ThrowStreamException(OptionError,"NoSuchOption",argv[i]);
421 if (LocaleCompare("density",option+1) == 0)
426 if (i == (ssize_t) argc)
427 ThrowStreamException(OptionError,"MissingArgument",option);
428 if (IsGeometry(argv[i]) == MagickFalse)
429 ThrowStreamInvalidArgumentException(option,argv[i]);
432 if (LocaleCompare("depth",option+1) == 0)
437 if (i == (ssize_t) argc)
438 ThrowStreamException(OptionError,"MissingArgument",option);
439 if (IsGeometry(argv[i]) == MagickFalse)
440 ThrowStreamInvalidArgumentException(option,argv[i]);
443 if (LocaleCompare("duration",option+1) == 0)
448 if (i == (ssize_t) (argc-1))
449 ThrowStreamException(OptionError,"MissingArgument",option);
450 if (IsGeometry(argv[i]) == MagickFalse)
451 ThrowStreamInvalidArgumentException(option,argv[i]);
454 ThrowStreamException(OptionError,"UnrecognizedOption",option)
458 if (LocaleCompare("extract",option+1) == 0)
463 if (i == (ssize_t) (argc-1))
464 ThrowStreamException(OptionError,"MissingArgument",option);
465 if (IsGeometry(argv[i]) == MagickFalse)
466 ThrowStreamInvalidArgumentException(option,argv[i]);
469 ThrowStreamException(OptionError,"UnrecognizedOption",option)
473 if ((LocaleCompare("help",option+1) == 0) ||
474 (LocaleCompare("-help",option+1) == 0))
475 return(StreamUsage());
476 ThrowStreamException(OptionError,"UnrecognizedOption",option)
480 if (LocaleCompare("identify",option+1) == 0)
482 if (LocaleCompare("interlace",option+1) == 0)
490 if (i == (ssize_t) argc)
491 ThrowStreamException(OptionError,"MissingArgument",option);
492 interlace=ParseCommandOption(MagickInterlaceOptions,MagickFalse,
495 ThrowStreamException(OptionError,"UnrecognizedInterlaceType",
499 if (LocaleCompare("interpolate",option+1) == 0)
507 if (i == (ssize_t) argc)
508 ThrowStreamException(OptionError,"MissingArgument",option);
509 interpolate=ParseCommandOption(MagickInterpolateOptions,MagickFalse,
512 ThrowStreamException(OptionError,"UnrecognizedInterpolateMethod",
516 ThrowStreamException(OptionError,"UnrecognizedOption",option)
520 if (LocaleCompare("limit",option+1) == 0)
534 if (i == (ssize_t) argc)
535 ThrowStreamException(OptionError,"MissingArgument",option);
536 resource=ParseCommandOption(MagickResourceOptions,MagickFalse,
539 ThrowStreamException(OptionError,"UnrecognizedResourceType",
542 if (i == (ssize_t) argc)
543 ThrowStreamException(OptionError,"MissingArgument",option);
544 value=StringToDouble(argv[i],&p);
546 if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0))
547 ThrowStreamInvalidArgumentException(option,argv[i]);
550 if (LocaleCompare("list",option+1) == 0)
558 if (i == (ssize_t) argc)
559 ThrowStreamException(OptionError,"MissingArgument",option);
560 list=ParseCommandOption(MagickListOptions,MagickFalse,argv[i]);
562 ThrowStreamException(OptionError,"UnrecognizedListType",argv[i]);
563 status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **)
566 return(status != 0 ? MagickFalse : MagickTrue);
568 if (LocaleCompare("log",option+1) == 0)
573 if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL))
574 ThrowStreamException(OptionError,"MissingArgument",option);
577 ThrowStreamException(OptionError,"UnrecognizedOption",option)
581 if (LocaleCompare("map",option+1) == 0)
583 (void) CopyMagickString(argv[i]+1,"san",MaxTextExtent);
587 SetStreamInfoMap(stream_info,argv[i]);
590 if (LocaleCompare("monitor",option+1) == 0)
592 ThrowStreamException(OptionError,"UnrecognizedOption",option)
596 if (LocaleCompare("quantize",option+1) == 0)
604 if (i == (ssize_t) (argc-1))
605 ThrowStreamException(OptionError,"MissingArgument",option);
606 colorspace=ParseCommandOption(MagickColorspaceOptions,
607 MagickFalse,argv[i]);
609 ThrowStreamException(OptionError,"UnrecognizedColorspace",
613 if (LocaleCompare("quiet",option+1) == 0)
615 ThrowStreamException(OptionError,"UnrecognizedOption",option)
619 if (LocaleCompare("regard-warnings",option+1) == 0)
621 if (LocaleNCompare("respect-parentheses",option+1,17) == 0)
623 respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse;
626 ThrowStreamException(OptionError,"UnrecognizedOption",option)
630 if (LocaleCompare("sampling-factor",option+1) == 0)
635 if (i == (ssize_t) argc)
636 ThrowStreamException(OptionError,"MissingArgument",option);
637 if (IsGeometry(argv[i]) == MagickFalse)
638 ThrowStreamInvalidArgumentException(option,argv[i]);
641 if (LocaleCompare("seed",option+1) == 0)
646 if (i == (ssize_t) (argc-1))
647 ThrowStreamException(OptionError,"MissingArgument",option);
648 if (IsGeometry(argv[i]) == MagickFalse)
649 ThrowStreamInvalidArgumentException(option,argv[i]);
652 if (LocaleCompare("set",option+1) == 0)
655 if (i == (ssize_t) argc)
656 ThrowStreamException(OptionError,"MissingArgument",option);
660 if (i == (ssize_t) argc)
661 ThrowStreamException(OptionError,"MissingArgument",option);
664 if (LocaleCompare("size",option+1) == 0)
669 if (i == (ssize_t) argc)
670 ThrowStreamException(OptionError,"MissingArgument",option);
671 if (IsGeometry(argv[i]) == MagickFalse)
672 ThrowStreamInvalidArgumentException(option,argv[i]);
675 if (LocaleCompare("storage-type",option+1) == 0)
683 if (i == (ssize_t) (argc-1))
684 ThrowStreamException(OptionError,"MissingArgument",option);
685 type=ParseCommandOption(MagickStorageOptions,MagickFalse,argv[i]);
687 ThrowStreamException(OptionError,"UnrecognizedStorageType",
689 SetStreamInfoStorageType(stream_info,(StorageType) type);
692 if (LocaleCompare("synchronize",option+1) == 0)
694 ThrowStreamException(OptionError,"UnrecognizedOption",option)
698 if (LocaleCompare("taint",option+1) == 0)
700 if (LocaleCompare("transparent-color",option+1) == 0)
705 if (i == (ssize_t) (argc-1))
706 ThrowStreamException(OptionError,"MissingArgument",option);
709 ThrowStreamException(OptionError,"UnrecognizedOption",option)
713 if (LocaleCompare("verbose",option+1) == 0)
715 if ((LocaleCompare("version",option+1) == 0) ||
716 (LocaleCompare("-version",option+1) == 0))
718 ListMagickVersion(stdout);
721 if (LocaleCompare("virtual-pixel",option+1) == 0)
729 if (i == (ssize_t) (argc-1))
730 ThrowStreamException(OptionError,"MissingArgument",option);
731 method=ParseCommandOption(MagickVirtualPixelOptions,MagickFalse,
734 ThrowStreamException(OptionError,"UnrecognizedVirtualPixelMethod",
738 ThrowStreamException(OptionError,"UnrecognizedOption",option)
743 ThrowStreamException(OptionError,"UnrecognizedOption",option)
745 fire=(GetCommandOptionFlags(MagickCommandOptions,MagickFalse,option) &
746 FireOptionFlag) == 0 ? MagickFalse : MagickTrue;
747 if (fire != MagickFalse)
748 FireImageStack(MagickFalse,MagickTrue,MagickTrue);
751 ThrowStreamException(OptionError,"UnbalancedParenthesis",argv[i]);
752 if (i-- != (ssize_t) (argc-1))
753 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
754 if (image == (Image *) NULL)
755 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
756 FinalizeImageSettings(image_info,image,MagickTrue);
757 if (image == (Image *) NULL)
758 ThrowStreamException(OptionError,"MissingAnImageFilename",argv[i]);
760 return(status != 0 ? MagickTrue : MagickFalse);