2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO N N SSSSS TTTTT IIIII TTTTT U U TTTTT EEEEE %
7 % C O O NN N SS T I T U U T E %
8 % C O O N N N ESSS T I T U U T EEE %
9 % C O O N NN SS T I T U U T E %
10 % CCCC OOO N N SSSSS T IIIII T UUU T EEEEE %
13 % MagickCore Methods to Consitute an Image %
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/client.h"
49 #include "MagickCore/constitute.h"
50 #include "MagickCore/constitute-private.h"
51 #include "MagickCore/delegate.h"
52 #include "MagickCore/geometry.h"
53 #include "MagickCore/identify.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/option.h"
61 #include "MagickCore/pixel.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/policy.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/profile-private.h"
66 #include "MagickCore/property.h"
67 #include "MagickCore/quantum.h"
68 #include "MagickCore/resize.h"
69 #include "MagickCore/resource_.h"
70 #include "MagickCore/semaphore.h"
71 #include "MagickCore/statistic.h"
72 #include "MagickCore/stream.h"
73 #include "MagickCore/string_.h"
74 #include "MagickCore/string-private.h"
75 #include "MagickCore/timer.h"
76 #include "MagickCore/token.h"
77 #include "MagickCore/transform.h"
78 #include "MagickCore/utility.h"
79 #include "MagickCore/utility-private.h"
82 *constitute_semaphore = (SemaphoreInfo *) NULL;
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 + C o n s t i t u t e C o m p o n e n t G e n e s i s %
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % ConstituteComponentGenesis() instantiates the constitute component.
97 % The format of the ConstituteComponentGenesis method is:
99 % MagickBooleanType ConstituteComponentGenesis(void)
102 MagickPrivate MagickBooleanType ConstituteComponentGenesis(void)
104 AcquireSemaphoreInfo(&constitute_semaphore);
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 + C o n s t i t u t e C o m p o n e n t T e r m i n u s %
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % ConstituteComponentTerminus() destroys the constitute component.
121 % The format of the ConstituteComponentTerminus method is:
123 % ConstituteComponentTerminus(void)
126 MagickPrivate void ConstituteComponentTerminus(void)
128 if (constitute_semaphore == (SemaphoreInfo *) NULL)
129 AcquireSemaphoreInfo(&constitute_semaphore);
130 DestroySemaphoreInfo(&constitute_semaphore);
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % C o n s t i t u t e I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ConstituteImage() returns an image from the pixel data you supply.
145 % The pixel data must be in scanline order top-to-bottom. The data can be
146 % char, short int, int, float, or double. Float and double require the
147 % pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
148 % create a 640x480 image from unsigned red-green-blue character data, use:
150 % image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
152 % The format of the ConstituteImage method is:
154 % Image *ConstituteImage(const size_t columns,const size_t rows,
155 % const char *map,const StorageType storage,const void *pixels,
156 % ExceptionInfo *exception)
158 % A description of each parameter follows:
160 % o columns: width in pixels of the image.
162 % o rows: height in pixels of the image.
164 % o map: This string reflects the expected ordering of the pixel array.
165 % It can be any combination or order of R = red, G = green, B = blue,
166 % A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
167 % Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
170 % o storage: Define the data type of the pixels. Float and double types are
171 % expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
172 % from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
173 % LongPixel, QuantumPixel, or ShortPixel.
175 % o pixels: This array of values contain the pixel components as defined by
176 % map and type. You must preallocate this array where the expected
177 % length varies depending on the values of width, height, map, and type.
179 % o exception: return any errors or warnings in this structure.
182 MagickExport Image *ConstituteImage(const size_t columns,const size_t rows,
183 const char *map,const StorageType storage,const void *pixels,
184 ExceptionInfo *exception)
193 Allocate image structure.
195 assert(map != (const char *) NULL);
196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
197 assert(pixels != (void *) NULL);
198 assert(exception != (ExceptionInfo *) NULL);
199 assert(exception->signature == MagickSignature);
200 image=AcquireImage((ImageInfo *) NULL,exception);
201 if (image == (Image *) NULL)
202 return((Image *) NULL);
203 if ((columns == 0) || (rows == 0))
204 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
205 image->columns=columns;
207 (void) SetImageBackgroundColor(image,exception);
208 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
209 if (status == MagickFalse)
210 image=DestroyImage(image);
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % P i n g I m a g e %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % PingImage() returns all the properties of an image or image sequence
226 % except for the pixels. It is much faster and consumes far less memory
227 % than ReadImage(). On failure, a NULL image is returned and exception
228 % describes the reason for the failure.
230 % The format of the PingImage method is:
232 % Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
234 % A description of each parameter follows:
236 % o image_info: Ping the image defined by the file or filename members of
239 % o exception: return any errors or warnings in this structure.
243 #if defined(__cplusplus) || defined(c_plusplus)
247 static size_t PingStream(const Image *magick_unused(image),
248 const void *magick_unused(pixels),const size_t columns)
253 #if defined(__cplusplus) || defined(c_plusplus)
257 MagickExport Image *PingImage(const ImageInfo *image_info,
258 ExceptionInfo *exception)
266 assert(image_info != (ImageInfo *) NULL);
267 assert(image_info->signature == MagickSignature);
268 if (image_info->debug != MagickFalse)
269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
270 image_info->filename);
271 assert(exception != (ExceptionInfo *) NULL);
272 ping_info=CloneImageInfo(image_info);
273 ping_info->ping=MagickTrue;
274 image=ReadStream(ping_info,&PingStream,exception);
275 if (image != (Image *) NULL)
277 ResetTimer(&image->timer);
278 if (ping_info->verbose != MagickFalse)
279 (void) IdentifyImage(image,stdout,MagickFalse,exception);
281 ping_info=DestroyImageInfo(ping_info);
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % P i n g I m a g e s %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % PingImages() pings one or more images and returns them as an image list.
298 % The format of the PingImage method is:
300 % Image *PingImages(ImageInfo *image_info,const char *filename,
301 % ExceptionInfo *exception)
303 % A description of each parameter follows:
305 % o image_info: the image info.
307 % o filename: the image filename.
309 % o exception: return any errors or warnings in this structure.
312 MagickExport Image *PingImages(ImageInfo *image_info,const char *filename,
313 ExceptionInfo *exception)
316 ping_filename[MaxTextExtent];
326 Ping image list from a file.
328 assert(image_info != (ImageInfo *) NULL);
329 assert(image_info->signature == MagickSignature);
330 if (image_info->debug != MagickFalse)
331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
332 image_info->filename);
333 assert(exception != (ExceptionInfo *) NULL);
334 (void) SetImageOption(image_info,"filename",filename);
335 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
336 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
337 (int) image_info->scene,ping_filename,exception);
338 if (LocaleCompare(ping_filename,image_info->filename) != 0)
348 Images of the form image-%d.png[1-5].
350 read_info=CloneImageInfo(image_info);
351 sans=AcquireExceptionInfo();
352 (void) SetImageInfo(read_info,0,sans);
353 sans=DestroyExceptionInfo(sans);
354 if (read_info->number_scenes == 0)
356 read_info=DestroyImageInfo(read_info);
357 return(PingImage(image_info,exception));
359 (void) CopyMagickString(ping_filename,read_info->filename,MaxTextExtent);
360 images=NewImageList();
361 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
362 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
364 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
365 (int) scene,read_info->filename,exception);
366 image=PingImage(read_info,exception);
367 if (image == (Image *) NULL)
369 AppendImageToList(&images,image);
371 read_info=DestroyImageInfo(read_info);
374 return(PingImage(image_info,exception));
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 % R e a d I m a g e %
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 % ReadImage() reads an image or image sequence from a file or file handle.
389 % The method returns a NULL if there is a memory shortage or if the image
390 % cannot be read. On failure, a NULL image is returned and exception
391 % describes the reason for the failure.
393 % The format of the ReadImage method is:
395 % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
397 % A description of each parameter follows:
399 % o image_info: Read the image defined by the file or filename members of
402 % o exception: return any errors or warnings in this structure.
405 MagickExport Image *ReadImage(const ImageInfo *image_info,
406 ExceptionInfo *exception)
409 filename[MaxTextExtent],
410 magick[MaxTextExtent],
411 magick_filename[MaxTextExtent];
446 Determine image type from filename prefix or suffix (e.g. image.jpg).
448 assert(image_info != (ImageInfo *) NULL);
449 assert(image_info->signature == MagickSignature);
450 assert(image_info->filename != (char *) NULL);
451 if (image_info->debug != MagickFalse)
452 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
453 image_info->filename);
454 assert(exception != (ExceptionInfo *) NULL);
455 read_info=CloneImageInfo(image_info);
456 (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
457 (void) SetImageInfo(read_info,0,exception);
458 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
459 (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
460 domain=CoderPolicyDomain;
461 rights=ReadPolicyRights;
462 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
465 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
466 "NotAuthorized","'%s'",read_info->filename);
467 read_info=DestroyImageInfo(read_info);
468 return((Image *) NULL);
471 Call appropriate image reader based on image type.
473 sans_exception=AcquireExceptionInfo();
474 magick_info=GetMagickInfo(read_info->magick,sans_exception);
475 sans_exception=DestroyExceptionInfo(sans_exception);
476 if (magick_info != (const MagickInfo *) NULL)
478 if (GetMagickEndianSupport(magick_info) == MagickFalse)
479 read_info->endian=UndefinedEndian;
481 if ((image_info->endian == UndefinedEndian) &&
482 (GetMagickRawSupport(magick_info) != MagickFalse))
488 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
492 if ((magick_info != (const MagickInfo *) NULL) &&
493 (GetMagickSeekableStream(magick_info) != MagickFalse))
498 image=AcquireImage(read_info,exception);
499 (void) CopyMagickString(image->filename,read_info->filename,
501 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
502 if (status == MagickFalse)
504 read_info=DestroyImageInfo(read_info);
505 image=DestroyImage(image);
506 return((Image *) NULL);
508 if (IsBlobSeekable(image) == MagickFalse)
511 Coder requires a seekable stream.
513 *read_info->filename='\0';
514 status=ImageToFile(image,read_info->filename,exception);
515 if (status == MagickFalse)
517 (void) CloseBlob(image);
518 read_info=DestroyImageInfo(read_info);
519 image=DestroyImage(image);
520 return((Image *) NULL);
522 read_info->temporary=MagickTrue;
524 (void) CloseBlob(image);
525 image=DestroyImage(image);
527 image=NewImageList();
528 if (constitute_semaphore == (SemaphoreInfo *) NULL)
529 AcquireSemaphoreInfo(&constitute_semaphore);
530 if ((magick_info == (const MagickInfo *) NULL) ||
531 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
533 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
534 if (delegate_info == (const DelegateInfo *) NULL)
536 (void) SetImageInfo(read_info,0,exception);
537 (void) CopyMagickString(read_info->filename,filename,MaxTextExtent);
538 magick_info=GetMagickInfo(read_info->magick,exception);
541 if ((magick_info != (const MagickInfo *) NULL) &&
542 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
544 thread_support=GetMagickThreadSupport(magick_info);
545 if ((thread_support & DecoderThreadSupport) == 0)
546 LockSemaphoreInfo(constitute_semaphore);
547 image=GetImageDecoder(magick_info)(read_info,exception);
548 if ((thread_support & DecoderThreadSupport) == 0)
549 UnlockSemaphoreInfo(constitute_semaphore);
553 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
554 if (delegate_info == (const DelegateInfo *) NULL)
556 (void) ThrowMagickException(exception,GetMagickModule(),
557 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
558 read_info->filename);
559 if (read_info->temporary != MagickFalse)
560 (void) RelinquishUniqueFileResource(read_info->filename);
561 read_info=DestroyImageInfo(read_info);
562 return((Image *) NULL);
565 Let our decoding delegate process the image.
567 image=AcquireImage(read_info,exception);
568 if (image == (Image *) NULL)
570 read_info=DestroyImageInfo(read_info);
571 return((Image *) NULL);
573 (void) CopyMagickString(image->filename,read_info->filename,
575 *read_info->filename='\0';
576 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
577 LockSemaphoreInfo(constitute_semaphore);
578 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
580 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
581 UnlockSemaphoreInfo(constitute_semaphore);
582 image=DestroyImageList(image);
583 read_info->temporary=MagickTrue;
584 (void) SetImageInfo(read_info,0,exception);
585 magick_info=GetMagickInfo(read_info->magick,exception);
586 if ((magick_info == (const MagickInfo *) NULL) ||
587 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
589 if (IsPathAccessible(read_info->filename) != MagickFalse)
590 (void) ThrowMagickException(exception,GetMagickModule(),
591 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
592 read_info->filename);
594 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
595 read_info->filename);
596 read_info=DestroyImageInfo(read_info);
597 return((Image *) NULL);
599 thread_support=GetMagickThreadSupport(magick_info);
600 if ((thread_support & DecoderThreadSupport) == 0)
601 LockSemaphoreInfo(constitute_semaphore);
602 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
603 if ((thread_support & DecoderThreadSupport) == 0)
604 UnlockSemaphoreInfo(constitute_semaphore);
606 if (read_info->temporary != MagickFalse)
608 (void) RelinquishUniqueFileResource(read_info->filename);
609 read_info->temporary=MagickFalse;
610 if (image != (Image *) NULL)
611 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
613 if (image == (Image *) NULL)
615 read_info=DestroyImageInfo(read_info);
618 if (exception->severity >= ErrorException)
619 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
620 "Coder (%s) generated an image despite an error (%d), "
621 "notify the developers",image->magick,exception->severity);
622 if (IsBlobTemporary(image) != MagickFalse)
623 (void) RelinquishUniqueFileResource(read_info->filename);
624 if ((GetNextImageInList(image) != (Image *) NULL) &&
625 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
630 clones=CloneImages(image,read_info->scenes,exception);
631 if (clones == (Image *) NULL)
632 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
633 "SubimageSpecificationReturnsNoImages","'%s'",read_info->filename);
636 image=DestroyImageList(image);
637 image=GetFirstImageInList(clones);
640 if (GetBlobError(image) != MagickFalse)
642 ThrowFileException(exception,FileOpenError,
643 "AnErrorHasOccurredReadingFromFile",read_info->filename);
644 image=DestroyImageList(image);
645 read_info=DestroyImageInfo(read_info);
646 return((Image *) NULL);
648 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
651 magick_path[MaxTextExtent],
653 timestamp[MaxTextExtent];
661 next->taint=MagickFalse;
662 GetPathComponent(magick_filename,MagickPath,magick_path);
663 if (*magick_path == '\0')
664 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
665 (void) CopyMagickString(next->magick_filename,magick_filename,
667 if (IsBlobTemporary(image) != MagickFalse)
668 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
669 if (next->magick_columns == 0)
670 next->magick_columns=next->columns;
671 if (next->magick_rows == 0)
672 next->magick_rows=next->rows;
673 value=GetImageProperty(next,"tiff:Orientation",exception);
674 if (value == (char *) NULL)
675 value=GetImageProperty(next,"exif:Orientation",exception);
676 if (value != (char *) NULL)
678 next->orientation=(OrientationType) StringToLong(value);
679 (void) DeleteImageProperty(next,"tiff:Orientation");
680 (void) DeleteImageProperty(next,"exif:Orientation");
682 value=GetImageProperty(next,"exif:XResolution",exception);
683 if (value != (char *) NULL)
685 geometry_info.rho=next->resolution.x;
686 geometry_info.sigma=1.0;
687 flags=ParseGeometry(value,&geometry_info);
688 if (geometry_info.sigma != 0)
689 next->resolution.x=geometry_info.rho/geometry_info.sigma;
690 (void) DeleteImageProperty(next,"exif:XResolution");
692 value=GetImageProperty(next,"exif:YResolution",exception);
693 if (value != (char *) NULL)
695 geometry_info.rho=next->resolution.y;
696 geometry_info.sigma=1.0;
697 flags=ParseGeometry(value,&geometry_info);
698 if (geometry_info.sigma != 0)
699 next->resolution.y=geometry_info.rho/geometry_info.sigma;
700 (void) DeleteImageProperty(next,"exif:YResolution");
702 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
703 if (value == (char *) NULL)
704 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
705 if (value != (char *) NULL)
707 next->units=(ResolutionType) (StringToLong(value)-1);
708 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
709 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
711 if (next->page.width == 0)
712 next->page.width=next->columns;
713 if (next->page.height == 0)
714 next->page.height=next->rows;
715 option=GetImageOption(read_info,"caption");
716 if (option != (const char *) NULL)
718 property=InterpretImageProperties(read_info,next,option,exception);
719 (void) SetImageProperty(next,"caption",property,exception);
720 property=DestroyString(property);
722 option=GetImageOption(read_info,"comment");
723 if (option != (const char *) NULL)
725 property=InterpretImageProperties(read_info,next,option,exception);
726 (void) SetImageProperty(next,"comment",property,exception);
727 property=DestroyString(property);
729 option=GetImageOption(read_info,"label");
730 if (option != (const char *) NULL)
732 property=InterpretImageProperties(read_info,next,option,exception);
733 (void) SetImageProperty(next,"label",property,exception);
734 property=DestroyString(property);
736 if (LocaleCompare(next->magick,"TEXT") == 0)
737 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
738 if ((read_info->extract != (char *) NULL) &&
739 (read_info->stream == (StreamHandler) NULL))
744 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
745 if ((next->columns != geometry.width) ||
746 (next->rows != geometry.height))
748 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
753 crop_image=CropImage(next,&geometry,exception);
754 if (crop_image != (Image *) NULL)
755 ReplaceImageInList(&next,crop_image);
758 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
763 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
765 size_image=ResizeImage(next,geometry.width,geometry.height,
766 next->filter,exception);
767 if (size_image != (Image *) NULL)
768 ReplaceImageInList(&next,size_image);
772 profile=GetImageProfile(next,"icc");
773 if (profile == (const StringInfo *) NULL)
774 profile=GetImageProfile(next,"icm");
775 profile=GetImageProfile(next,"iptc");
776 if (profile == (const StringInfo *) NULL)
777 profile=GetImageProfile(next,"8bim");
778 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
780 (void) SetImageProperty(next,"date:modify",timestamp,exception);
781 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
783 (void) SetImageProperty(next,"date:create",timestamp,exception);
784 option=GetImageOption(image_info,"delay");
785 if (option != (const char *) NULL)
790 flags=ParseGeometry(option,&geometry_info);
791 if ((flags & GreaterValue) != 0)
793 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
794 next->delay=(size_t) floor(geometry_info.rho+0.5);
797 if ((flags & LessValue) != 0)
799 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
800 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
803 next->delay=(size_t) floor(geometry_info.rho+0.5);
804 if ((flags & SigmaValue) != 0)
805 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
807 option=GetImageOption(image_info,"dispose");
808 if (option != (const char *) NULL)
809 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
811 if (read_info->verbose != MagickFalse)
812 (void) IdentifyImage(next,stderr,MagickFalse,exception);
815 read_info=DestroyImageInfo(read_info);
816 return(GetFirstImageInList(image));
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % R e a d I m a g e s %
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 % ReadImages() reads one or more images and returns them as an image list.
832 % The format of the ReadImage method is:
834 % Image *ReadImages(ImageInfo *image_info,const char *filename,
835 % ExceptionInfo *exception)
837 % A description of each parameter follows:
839 % o image_info: the image info.
841 % o filename: the image filename.
843 % o exception: return any errors or warnings in this structure.
846 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
847 ExceptionInfo *exception)
850 read_filename[MaxTextExtent];
860 Read image list from a file.
862 assert(image_info != (ImageInfo *) NULL);
863 assert(image_info->signature == MagickSignature);
864 if (image_info->debug != MagickFalse)
865 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
866 image_info->filename);
867 assert(exception != (ExceptionInfo *) NULL);
868 (void) SetImageOption(image_info,"filename",filename);
869 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
870 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,
871 (int) image_info->scene,read_filename,exception);
872 if (LocaleCompare(read_filename,image_info->filename) != 0)
882 Images of the form image-%d.png[1-5].
884 read_info=CloneImageInfo(image_info);
885 sans=AcquireExceptionInfo();
886 (void) SetImageInfo(read_info,0,sans);
887 sans=DestroyExceptionInfo(sans);
888 if (read_info->number_scenes == 0)
890 read_info=DestroyImageInfo(read_info);
891 return(ReadImage(image_info,exception));
893 (void) CopyMagickString(read_filename,read_info->filename,MaxTextExtent);
894 images=NewImageList();
895 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
896 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
898 (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
899 (int) scene,read_info->filename,exception);
900 image=ReadImage(read_info,exception);
901 if (image == (Image *) NULL)
903 AppendImageToList(&images,image);
905 read_info=DestroyImageInfo(read_info);
908 return(ReadImage(image_info,exception));
912 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916 + R e a d I n l i n e I m a g e %
920 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
922 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
923 % The method returns a NULL if there is a memory shortage or if the image
924 % cannot be read. On failure, a NULL image is returned and exception
925 % describes the reason for the failure.
927 % The format of the ReadInlineImage method is:
929 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
930 % ExceptionInfo *exception)
932 % A description of each parameter follows:
934 % o image_info: the image info.
936 % o content: the image encoded in Base64.
938 % o exception: return any errors or warnings in this structure.
941 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
942 const char *content,ExceptionInfo *exception)
960 Skip over header (e.g. data:image/gif;base64,).
962 image=NewImageList();
963 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
965 ThrowReaderException(CorruptImageError,"CorruptImage");
968 blob=Base64Decode(p,&length);
970 ThrowReaderException(CorruptImageError,"CorruptImage");
971 read_info=CloneImageInfo(image_info);
972 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
974 *read_info->filename='\0';
975 *read_info->magick='\0';
976 image=BlobToImage(read_info,blob,length,exception);
977 blob=(unsigned char *) RelinquishMagickMemory(blob);
978 read_info=DestroyImageInfo(read_info);
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % W r i t e I m a g e %
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % WriteImage() writes an image or an image sequence to a file or file handle.
994 % If writing to a file is on disk, the name is defined by the filename member
995 % of the image structure. WriteImage() returns MagickFalse is there is a
996 % memory shortage or if the image cannot be written. Check the exception
997 % member of image to determine the cause for any failure.
999 % The format of the WriteImage method is:
1001 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
1002 % ExceptionInfo *exception)
1004 % A description of each parameter follows:
1006 % o image_info: the image info.
1008 % o image: the image.
1010 % o exception: return any errors or warnings in this structure.
1013 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1014 Image *image,ExceptionInfo *exception)
1017 filename[MaxTextExtent];
1048 Determine image type from filename prefix or suffix (e.g. image.jpg).
1050 assert(image_info != (ImageInfo *) NULL);
1051 assert(image_info->signature == MagickSignature);
1052 if (image->debug != MagickFalse)
1053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1054 image_info->filename);
1055 assert(image != (Image *) NULL);
1056 assert(image->signature == MagickSignature);
1057 assert(exception != (ExceptionInfo *) NULL);
1058 sans_exception=AcquireExceptionInfo();
1059 write_info=CloneImageInfo(image_info);
1060 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1061 if (*write_info->magick == '\0')
1062 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1063 (void) SetImageInfo(write_info,1,sans_exception);
1064 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1065 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1066 domain=CoderPolicyDomain;
1067 rights=WritePolicyRights;
1068 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1070 sans_exception=DestroyExceptionInfo(sans_exception);
1071 write_info=DestroyImageInfo(write_info);
1073 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1076 Call appropriate image reader based on image type.
1078 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1079 sans_exception=DestroyExceptionInfo(sans_exception);
1080 if (magick_info != (const MagickInfo *) NULL)
1082 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1083 image->endian=UndefinedEndian;
1085 if ((image_info->endian == UndefinedEndian) &&
1086 (GetMagickRawSupport(magick_info) != MagickFalse))
1092 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1095 (void) SyncImageProfiles(image);
1096 option=GetImageOption(image_info,"delegate:bimodal");
1097 if ((IfMagickTrue(IsStringTrue(option))) &&
1098 (write_info->page == (char *) NULL) &&
1099 (GetPreviousImageInList(image) == (Image *) NULL) &&
1100 (GetNextImageInList(image) == (Image *) NULL) &&
1101 (IfMagickFalse(IsTaintImage(image))) )
1103 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1104 if ((delegate_info != (const DelegateInfo *) NULL) &&
1105 (GetDelegateMode(delegate_info) == 0) &&
1106 (IsPathAccessible(image->magick_filename) != MagickFalse))
1109 Process image with bi-modal delegate.
1111 (void) CopyMagickString(image->filename,image->magick_filename,
1113 status=InvokeDelegate(write_info,image,image->magick,
1114 write_info->magick,exception);
1115 write_info=DestroyImageInfo(write_info);
1116 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1121 temporary=MagickFalse;
1122 if ((magick_info != (const MagickInfo *) NULL) &&
1123 (GetMagickSeekableStream(magick_info) != MagickFalse))
1126 filename[MaxTextExtent];
1128 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1129 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1130 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1131 if (status != MagickFalse)
1133 if (IsBlobSeekable(image) == MagickFalse)
1136 A seekable stream is required by the encoder.
1138 write_info->adjoin=MagickTrue;
1139 (void) CopyMagickString(write_info->filename,image->filename,
1141 (void) AcquireUniqueFilename(image->filename);
1142 temporary=MagickTrue;
1144 (void) CloseBlob(image);
1147 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1148 AcquireSemaphoreInfo(&constitute_semaphore);
1149 if ((magick_info != (const MagickInfo *) NULL) &&
1150 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1153 Call appropriate image writer based on image type.
1155 thread_support=GetMagickThreadSupport(magick_info);
1156 if ((thread_support & EncoderThreadSupport) == 0)
1157 LockSemaphoreInfo(constitute_semaphore);
1158 status=GetImageEncoder(magick_info)(write_info,image,exception);
1159 if ((thread_support & EncoderThreadSupport) == 0)
1160 UnlockSemaphoreInfo(constitute_semaphore);
1164 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1165 if (delegate_info != (DelegateInfo *) NULL)
1168 Process the image with delegate.
1170 *write_info->filename='\0';
1171 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1172 LockSemaphoreInfo(constitute_semaphore);
1173 status=InvokeDelegate(write_info,image,(char *) NULL,
1174 write_info->magick,exception);
1175 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1176 UnlockSemaphoreInfo(constitute_semaphore);
1177 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1181 sans_exception=AcquireExceptionInfo();
1182 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1183 sans_exception=DestroyExceptionInfo(sans_exception);
1184 if ((write_info->affirm == MagickFalse) &&
1185 (magick_info == (const MagickInfo *) NULL))
1187 (void) CopyMagickString(write_info->magick,image->magick,
1189 magick_info=GetMagickInfo(write_info->magick,exception);
1191 if ((magick_info == (const MagickInfo *) NULL) ||
1192 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1195 extension[MaxTextExtent];
1197 GetPathComponent(image->filename,ExtensionPath,extension);
1198 if (*extension != '\0')
1199 magick_info=GetMagickInfo(extension,exception);
1201 magick_info=GetMagickInfo(image->magick,exception);
1202 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1204 if ((magick_info == (const MagickInfo *) NULL) ||
1205 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1207 magick_info=GetMagickInfo(image->magick,exception);
1208 if ((magick_info == (const MagickInfo *) NULL) ||
1209 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1210 (void) ThrowMagickException(exception,GetMagickModule(),
1211 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1212 "'%s'",image->filename);
1214 (void) ThrowMagickException(exception,GetMagickModule(),
1215 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1216 "'%s'",image->filename);
1218 if ((magick_info != (const MagickInfo *) NULL) &&
1219 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1222 Call appropriate image writer based on image type.
1224 thread_support=GetMagickThreadSupport(magick_info);
1225 if ((thread_support & EncoderThreadSupport) == 0)
1226 LockSemaphoreInfo(constitute_semaphore);
1227 status=GetImageEncoder(magick_info)(write_info,image,exception);
1228 if ((thread_support & EncoderThreadSupport) == 0)
1229 UnlockSemaphoreInfo(constitute_semaphore);
1233 if (GetBlobError(image) != MagickFalse)
1234 ThrowFileException(exception,FileOpenError,
1235 "AnErrorHasOccurredWritingToFile",image->filename);
1236 if (temporary == MagickTrue)
1239 Copy temporary image file to permanent.
1241 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1242 if (status != MagickFalse)
1244 (void) RelinquishUniqueFileResource(write_info->filename);
1245 status=ImageToFile(image,write_info->filename,exception);
1247 (void) CloseBlob(image);
1248 (void) RelinquishUniqueFileResource(image->filename);
1249 (void) CopyMagickString(image->filename,write_info->filename,
1252 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1253 (write_info->verbose != MagickFalse))
1254 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1255 write_info=DestroyImageInfo(write_info);
1260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264 % W r i t e I m a g e s %
1268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1270 % WriteImages() writes an image sequence into one or more files. While
1271 % WriteImage() can write an image sequence, it is limited to writing
1272 % the sequence into a single file using a format which supports multiple
1273 % frames. WriteImages(), however, does not have this limitation, instead it
1274 % generates multiple output files if necessary (or when requested). When
1275 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1276 % to include a printf-style formatting string for the frame number (e.g.
1279 % The format of the WriteImages method is:
1281 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1282 % const char *filename,ExceptionInfo *exception)
1284 % A description of each parameter follows:
1286 % o image_info: the image info.
1288 % o images: the image list.
1290 % o filename: the image filename.
1292 % o exception: return any errors or warnings in this structure.
1295 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1296 Image *images,const char *filename,ExceptionInfo *exception)
1298 #define WriteImageTag "Write/Image"
1315 MagickProgressMonitor
1327 assert(image_info != (const ImageInfo *) NULL);
1328 assert(image_info->signature == MagickSignature);
1329 assert(images != (Image *) NULL);
1330 assert(images->signature == MagickSignature);
1331 if (images->debug != MagickFalse)
1332 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1333 assert(exception != (ExceptionInfo *) NULL);
1334 write_info=CloneImageInfo(image_info);
1335 images=GetFirstImageInList(images);
1336 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1337 DestroyBlob(images);
1339 if (filename != (const char *) NULL)
1340 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1341 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1342 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1343 if (*write_info->magick == '\0')
1344 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1345 sans_exception=AcquireExceptionInfo();
1346 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1348 sans_exception=DestroyExceptionInfo(sans_exception);
1350 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1351 if (p->scene >= GetNextImageInList(p)->scene)
1357 Generate consistent scene numbers.
1359 i=(ssize_t) images->scene;
1360 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1361 p->scene=(size_t) i++;
1368 progress_monitor=(MagickProgressMonitor) NULL;
1370 number_images=GetImageListLength(images);
1371 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1373 if (number_images != 1)
1374 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1376 status&=WriteImage(write_info,p,exception);
1377 if (number_images != 1)
1378 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1379 if (write_info->adjoin != MagickFalse)
1381 if (number_images != 1)
1383 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1384 if (proceed == MagickFalse)
1388 write_info=DestroyImageInfo(write_info);
1389 return(status != 0 ? MagickTrue : MagickFalse);