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 (void) CopyMagickString(ping_filename,read_info->filename,MaxTextExtent);
355 images=NewImageList();
356 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
357 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
359 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
360 (int) scene,read_info->filename,exception);
361 image=PingImage(read_info,exception);
362 if (image == (Image *) NULL)
364 AppendImageToList(&images,image);
366 read_info=DestroyImageInfo(read_info);
369 return(PingImage(image_info,exception));
373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 % R e a d I m a g e %
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 % ReadImage() reads an image or image sequence from a file or file handle.
384 % The method returns a NULL if there is a memory shortage or if the image
385 % cannot be read. On failure, a NULL image is returned and exception
386 % describes the reason for the failure.
388 % The format of the ReadImage method is:
390 % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
392 % A description of each parameter follows:
394 % o image_info: Read the image defined by the file or filename members of
397 % o exception: return any errors or warnings in this structure.
400 MagickExport Image *ReadImage(const ImageInfo *image_info,
401 ExceptionInfo *exception)
404 filename[MaxTextExtent],
405 magick[MaxTextExtent],
406 magick_filename[MaxTextExtent];
441 Determine image type from filename prefix or suffix (e.g. image.jpg).
443 assert(image_info != (ImageInfo *) NULL);
444 assert(image_info->signature == MagickSignature);
445 assert(image_info->filename != (char *) NULL);
446 if (image_info->debug != MagickFalse)
447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
448 image_info->filename);
449 assert(exception != (ExceptionInfo *) NULL);
450 read_info=CloneImageInfo(image_info);
451 (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
452 (void) SetImageInfo(read_info,0,exception);
453 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
454 (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
455 domain=CoderPolicyDomain;
456 rights=ReadPolicyRights;
457 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
460 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
461 "NotAuthorized","'%s'",read_info->filename);
462 return((Image *) NULL);
465 Call appropriate image reader based on image type.
467 sans_exception=AcquireExceptionInfo();
468 magick_info=GetMagickInfo(read_info->magick,sans_exception);
469 sans_exception=DestroyExceptionInfo(sans_exception);
470 if (magick_info != (const MagickInfo *) NULL)
472 if (GetMagickEndianSupport(magick_info) == MagickFalse)
473 read_info->endian=UndefinedEndian;
475 if ((image_info->endian == UndefinedEndian) &&
476 (GetMagickRawSupport(magick_info) != MagickFalse))
482 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
486 if ((magick_info != (const MagickInfo *) NULL) &&
487 (GetMagickSeekableStream(magick_info) != MagickFalse))
492 image=AcquireImage(read_info,exception);
493 (void) CopyMagickString(image->filename,read_info->filename,
495 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
496 if (status == MagickFalse)
498 read_info=DestroyImageInfo(read_info);
499 image=DestroyImage(image);
500 return((Image *) NULL);
502 if (IsBlobSeekable(image) == MagickFalse)
505 Coder requires a seekable stream.
507 *read_info->filename='\0';
508 status=ImageToFile(image,read_info->filename,exception);
509 if (status == MagickFalse)
511 (void) CloseBlob(image);
512 read_info=DestroyImageInfo(read_info);
513 image=DestroyImage(image);
514 return((Image *) NULL);
516 read_info->temporary=MagickTrue;
518 (void) CloseBlob(image);
519 image=DestroyImage(image);
521 image=NewImageList();
522 if (constitute_semaphore == (SemaphoreInfo *) NULL)
523 AcquireSemaphoreInfo(&constitute_semaphore);
524 if ((magick_info == (const MagickInfo *) NULL) ||
525 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
527 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
528 if (delegate_info == (const DelegateInfo *) NULL)
530 (void) SetImageInfo(read_info,0,exception);
531 (void) CopyMagickString(read_info->filename,filename,MaxTextExtent);
532 magick_info=GetMagickInfo(read_info->magick,exception);
535 if ((magick_info != (const MagickInfo *) NULL) &&
536 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
538 thread_support=GetMagickThreadSupport(magick_info);
539 if ((thread_support & DecoderThreadSupport) == 0)
540 LockSemaphoreInfo(constitute_semaphore);
541 image=GetImageDecoder(magick_info)(read_info,exception);
542 if ((thread_support & DecoderThreadSupport) == 0)
543 UnlockSemaphoreInfo(constitute_semaphore);
547 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
548 if (delegate_info == (const DelegateInfo *) NULL)
550 (void) ThrowMagickException(exception,GetMagickModule(),
551 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
552 read_info->filename);
553 if (read_info->temporary != MagickFalse)
554 (void) RelinquishUniqueFileResource(read_info->filename);
555 read_info=DestroyImageInfo(read_info);
556 return((Image *) NULL);
559 Let our decoding delegate process the image.
561 image=AcquireImage(read_info,exception);
562 if (image == (Image *) NULL)
564 read_info=DestroyImageInfo(read_info);
565 return((Image *) NULL);
567 (void) CopyMagickString(image->filename,read_info->filename,
569 *read_info->filename='\0';
570 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
571 LockSemaphoreInfo(constitute_semaphore);
572 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
574 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
575 UnlockSemaphoreInfo(constitute_semaphore);
576 image=DestroyImageList(image);
577 read_info->temporary=MagickTrue;
578 (void) SetImageInfo(read_info,0,exception);
579 magick_info=GetMagickInfo(read_info->magick,exception);
580 if ((magick_info == (const MagickInfo *) NULL) ||
581 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
583 if (IsPathAccessible(read_info->filename) != MagickFalse)
584 (void) ThrowMagickException(exception,GetMagickModule(),
585 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
586 read_info->filename);
588 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
589 read_info->filename);
590 read_info=DestroyImageInfo(read_info);
591 return((Image *) NULL);
593 thread_support=GetMagickThreadSupport(magick_info);
594 if ((thread_support & DecoderThreadSupport) == 0)
595 LockSemaphoreInfo(constitute_semaphore);
596 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
597 if ((thread_support & DecoderThreadSupport) == 0)
598 UnlockSemaphoreInfo(constitute_semaphore);
600 if (read_info->temporary != MagickFalse)
602 (void) RelinquishUniqueFileResource(read_info->filename);
603 read_info->temporary=MagickFalse;
604 if (image != (Image *) NULL)
605 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
607 if (image == (Image *) NULL)
609 read_info=DestroyImageInfo(read_info);
612 if (exception->severity >= ErrorException)
613 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
614 "Coder (%s) generated an image despite an error (%d), "
615 "notify the developers",image->magick,exception->severity);
616 if (IsBlobTemporary(image) != MagickFalse)
617 (void) RelinquishUniqueFileResource(read_info->filename);
618 if ((GetNextImageInList(image) != (Image *) NULL) &&
619 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
624 clones=CloneImages(image,read_info->scenes,exception);
625 if (clones == (Image *) NULL)
626 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
627 "SubimageSpecificationReturnsNoImages","'%s'",read_info->filename);
630 image=DestroyImageList(image);
631 image=GetFirstImageInList(clones);
634 if (GetBlobError(image) != MagickFalse)
636 ThrowFileException(exception,FileOpenError,
637 "AnErrorHasOccurredReadingFromFile",read_info->filename);
638 image=DestroyImageList(image);
639 read_info=DestroyImageInfo(read_info);
640 return((Image *) NULL);
642 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
645 magick_path[MaxTextExtent],
647 timestamp[MaxTextExtent];
655 next->taint=MagickFalse;
656 GetPathComponent(magick_filename,MagickPath,magick_path);
657 if (*magick_path == '\0')
658 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
659 (void) CopyMagickString(next->magick_filename,magick_filename,
661 if (IsBlobTemporary(image) != MagickFalse)
662 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
663 if (next->magick_columns == 0)
664 next->magick_columns=next->columns;
665 if (next->magick_rows == 0)
666 next->magick_rows=next->rows;
667 value=GetImageProperty(next,"tiff:Orientation",exception);
668 if (value == (char *) NULL)
669 value=GetImageProperty(next,"exif:Orientation",exception);
670 if (value != (char *) NULL)
672 next->orientation=(OrientationType) StringToLong(value);
673 (void) DeleteImageProperty(next,"tiff:Orientation");
674 (void) DeleteImageProperty(next,"exif:Orientation");
676 value=GetImageProperty(next,"exif:XResolution",exception);
677 if (value != (char *) NULL)
679 geometry_info.rho=next->resolution.x;
680 geometry_info.sigma=1.0;
681 flags=ParseGeometry(value,&geometry_info);
682 if (geometry_info.sigma != 0)
683 next->resolution.x=geometry_info.rho/geometry_info.sigma;
684 (void) DeleteImageProperty(next,"exif:XResolution");
686 value=GetImageProperty(next,"exif:YResolution",exception);
687 if (value != (char *) NULL)
689 geometry_info.rho=next->resolution.y;
690 geometry_info.sigma=1.0;
691 flags=ParseGeometry(value,&geometry_info);
692 if (geometry_info.sigma != 0)
693 next->resolution.y=geometry_info.rho/geometry_info.sigma;
694 (void) DeleteImageProperty(next,"exif:YResolution");
696 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
697 if (value == (char *) NULL)
698 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
699 if (value != (char *) NULL)
701 next->units=(ResolutionType) (StringToLong(value)-1);
702 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
703 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
705 if (next->page.width == 0)
706 next->page.width=next->columns;
707 if (next->page.height == 0)
708 next->page.height=next->rows;
709 option=GetImageOption(read_info,"caption");
710 if (option != (const char *) NULL)
712 property=InterpretImageProperties(read_info,next,option,exception);
713 (void) SetImageProperty(next,"caption",property,exception);
714 property=DestroyString(property);
716 option=GetImageOption(read_info,"comment");
717 if (option != (const char *) NULL)
719 property=InterpretImageProperties(read_info,next,option,exception);
720 (void) SetImageProperty(next,"comment",property,exception);
721 property=DestroyString(property);
723 option=GetImageOption(read_info,"label");
724 if (option != (const char *) NULL)
726 property=InterpretImageProperties(read_info,next,option,exception);
727 (void) SetImageProperty(next,"label",property,exception);
728 property=DestroyString(property);
730 if (LocaleCompare(next->magick,"TEXT") == 0)
731 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
732 if ((read_info->extract != (char *) NULL) &&
733 (read_info->stream == (StreamHandler) NULL))
738 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
739 if ((next->columns != geometry.width) ||
740 (next->rows != geometry.height))
742 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
747 crop_image=CropImage(next,&geometry,exception);
748 if (crop_image != (Image *) NULL)
749 ReplaceImageInList(&next,crop_image);
752 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
757 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
759 size_image=ResizeImage(next,geometry.width,geometry.height,
760 next->filter,exception);
761 if (size_image != (Image *) NULL)
762 ReplaceImageInList(&next,size_image);
766 profile=GetImageProfile(next,"icc");
767 if (profile == (const StringInfo *) NULL)
768 profile=GetImageProfile(next,"icm");
769 profile=GetImageProfile(next,"iptc");
770 if (profile == (const StringInfo *) NULL)
771 profile=GetImageProfile(next,"8bim");
772 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
774 (void) SetImageProperty(next,"date:modify",timestamp,exception);
775 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
777 (void) SetImageProperty(next,"date:create",timestamp,exception);
778 option=GetImageOption(image_info,"delay");
779 if (option != (const char *) NULL)
784 flags=ParseGeometry(option,&geometry_info);
785 if ((flags & GreaterValue) != 0)
787 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
788 next->delay=(size_t) floor(geometry_info.rho+0.5);
791 if ((flags & LessValue) != 0)
793 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
794 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
797 next->delay=(size_t) floor(geometry_info.rho+0.5);
798 if ((flags & SigmaValue) != 0)
799 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
801 option=GetImageOption(image_info,"dispose");
802 if (option != (const char *) NULL)
803 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
805 if (read_info->verbose != MagickFalse)
806 (void) IdentifyImage(next,stderr,MagickFalse,exception);
809 read_info=DestroyImageInfo(read_info);
810 return(GetFirstImageInList(image));
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 % R e a d I m a g e s %
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % ReadImages() reads one or more images and returns them as an image list.
826 % The format of the ReadImage method is:
828 % Image *ReadImages(ImageInfo *image_info,const char *filename,
829 % ExceptionInfo *exception)
831 % A description of each parameter follows:
833 % o image_info: the image info.
835 % o filename: the image filename.
837 % o exception: return any errors or warnings in this structure.
840 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
841 ExceptionInfo *exception)
844 read_filename[MaxTextExtent];
854 Read image list from a file.
856 assert(image_info != (ImageInfo *) NULL);
857 assert(image_info->signature == MagickSignature);
858 if (image_info->debug != MagickFalse)
859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
860 image_info->filename);
861 assert(exception != (ExceptionInfo *) NULL);
862 (void) SetImageOption(image_info,"filename",filename);
863 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
864 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,
865 (int) image_info->scene,read_filename,exception);
866 if (LocaleCompare(read_filename,image_info->filename) != 0)
876 Images of the form image-%d.png[1-5].
878 read_info=CloneImageInfo(image_info);
879 sans=AcquireExceptionInfo();
880 (void) SetImageInfo(read_info,0,sans);
881 sans=DestroyExceptionInfo(sans);
882 if (read_info->number_scenes == 0)
884 read_info=DestroyImageInfo(read_info);
885 return(ReadImage(image_info,exception));
887 (void) CopyMagickString(read_filename,read_info->filename,MaxTextExtent);
888 images=NewImageList();
889 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
890 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
892 (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
893 (int) scene,read_info->filename,exception);
894 image=ReadImage(read_info,exception);
895 if (image == (Image *) NULL)
897 AppendImageToList(&images,image);
899 read_info=DestroyImageInfo(read_info);
902 return(ReadImage(image_info,exception));
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 + R e a d I n l i n e I m a g e %
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
917 % The method returns a NULL if there is a memory shortage or if the image
918 % cannot be read. On failure, a NULL image is returned and exception
919 % describes the reason for the failure.
921 % The format of the ReadInlineImage method is:
923 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
924 % ExceptionInfo *exception)
926 % A description of each parameter follows:
928 % o image_info: the image info.
930 % o content: the image encoded in Base64.
932 % o exception: return any errors or warnings in this structure.
935 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
936 const char *content,ExceptionInfo *exception)
954 Skip over header (e.g. data:image/gif;base64,).
956 image=NewImageList();
957 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
959 ThrowReaderException(CorruptImageError,"CorruptImage");
962 blob=Base64Decode(p,&length);
964 ThrowReaderException(CorruptImageError,"CorruptImage");
965 read_info=CloneImageInfo(image_info);
966 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
968 *read_info->filename='\0';
969 *read_info->magick='\0';
970 image=BlobToImage(read_info,blob,length,exception);
971 blob=(unsigned char *) RelinquishMagickMemory(blob);
972 read_info=DestroyImageInfo(read_info);
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981 % W r i t e I m a g e %
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % WriteImage() writes an image or an image sequence to a file or file handle.
988 % If writing to a file is on disk, the name is defined by the filename member
989 % of the image structure. WriteImage() returns MagickFalse is there is a
990 % memory shortage or if the image cannot be written. Check the exception
991 % member of image to determine the cause for any failure.
993 % The format of the WriteImage method is:
995 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
996 % ExceptionInfo *exception)
998 % A description of each parameter follows:
1000 % o image_info: the image info.
1002 % o image: the image.
1004 % o exception: return any errors or warnings in this structure.
1007 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1008 Image *image,ExceptionInfo *exception)
1011 filename[MaxTextExtent];
1042 Determine image type from filename prefix or suffix (e.g. image.jpg).
1044 assert(image_info != (ImageInfo *) NULL);
1045 assert(image_info->signature == MagickSignature);
1046 if (image->debug != MagickFalse)
1047 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1048 image_info->filename);
1049 assert(image != (Image *) NULL);
1050 assert(image->signature == MagickSignature);
1051 assert(exception != (ExceptionInfo *) NULL);
1052 sans_exception=AcquireExceptionInfo();
1053 write_info=CloneImageInfo(image_info);
1054 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1055 if (*write_info->magick == '\0')
1056 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1057 (void) SetImageInfo(write_info,1,sans_exception);
1058 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1059 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1060 domain=CoderPolicyDomain;
1061 rights=WritePolicyRights;
1062 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1064 sans_exception=DestroyExceptionInfo(sans_exception);
1066 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1069 Call appropriate image reader based on image type.
1071 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1072 sans_exception=DestroyExceptionInfo(sans_exception);
1073 if (magick_info != (const MagickInfo *) NULL)
1075 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1076 image->endian=UndefinedEndian;
1078 if ((image_info->endian == UndefinedEndian) &&
1079 (GetMagickRawSupport(magick_info) != MagickFalse))
1085 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1088 (void) SyncImageProfiles(image);
1089 option=GetImageOption(image_info,"delegate:bimodal");
1090 if ((IfMagickTrue(IsStringTrue(option))) &&
1091 (write_info->page == (char *) NULL) &&
1092 (GetPreviousImageInList(image) == (Image *) NULL) &&
1093 (GetNextImageInList(image) == (Image *) NULL) &&
1094 (IfMagickFalse(IsTaintImage(image))) )
1096 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1097 if ((delegate_info != (const DelegateInfo *) NULL) &&
1098 (GetDelegateMode(delegate_info) == 0) &&
1099 (IsPathAccessible(image->magick_filename) != MagickFalse))
1102 Process image with bi-modal delegate.
1104 (void) CopyMagickString(image->filename,image->magick_filename,
1106 status=InvokeDelegate(write_info,image,image->magick,
1107 write_info->magick,exception);
1108 write_info=DestroyImageInfo(write_info);
1109 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1114 temporary=MagickFalse;
1115 if ((magick_info != (const MagickInfo *) NULL) &&
1116 (GetMagickSeekableStream(magick_info) != MagickFalse))
1119 filename[MaxTextExtent];
1121 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1122 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1123 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1124 if (status != MagickFalse)
1126 if (IsBlobSeekable(image) == MagickFalse)
1129 A seekable stream is required by the encoder.
1131 write_info->adjoin=MagickTrue;
1132 (void) CopyMagickString(write_info->filename,image->filename,
1134 (void) AcquireUniqueFilename(image->filename);
1135 temporary=MagickTrue;
1137 (void) CloseBlob(image);
1140 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1141 AcquireSemaphoreInfo(&constitute_semaphore);
1142 if ((magick_info != (const MagickInfo *) NULL) &&
1143 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1146 Call appropriate image writer based on image type.
1148 thread_support=GetMagickThreadSupport(magick_info);
1149 if ((thread_support & EncoderThreadSupport) == 0)
1150 LockSemaphoreInfo(constitute_semaphore);
1151 status=GetImageEncoder(magick_info)(write_info,image,exception);
1152 if ((thread_support & EncoderThreadSupport) == 0)
1153 UnlockSemaphoreInfo(constitute_semaphore);
1157 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1158 if (delegate_info != (DelegateInfo *) NULL)
1161 Process the image with delegate.
1163 *write_info->filename='\0';
1164 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1165 LockSemaphoreInfo(constitute_semaphore);
1166 status=InvokeDelegate(write_info,image,(char *) NULL,
1167 write_info->magick,exception);
1168 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1169 UnlockSemaphoreInfo(constitute_semaphore);
1170 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1174 sans_exception=AcquireExceptionInfo();
1175 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1176 sans_exception=DestroyExceptionInfo(sans_exception);
1177 if ((write_info->affirm == MagickFalse) &&
1178 (magick_info == (const MagickInfo *) NULL))
1180 (void) CopyMagickString(write_info->magick,image->magick,
1182 magick_info=GetMagickInfo(write_info->magick,exception);
1184 if ((magick_info == (const MagickInfo *) NULL) ||
1185 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1188 extension[MaxTextExtent];
1190 GetPathComponent(image->filename,ExtensionPath,extension);
1191 if (*extension != '\0')
1192 magick_info=GetMagickInfo(extension,exception);
1194 magick_info=GetMagickInfo(image->magick,exception);
1195 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1197 if ((magick_info == (const MagickInfo *) NULL) ||
1198 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1200 magick_info=GetMagickInfo(image->magick,exception);
1201 if ((magick_info == (const MagickInfo *) NULL) ||
1202 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1203 (void) ThrowMagickException(exception,GetMagickModule(),
1204 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1205 "'%s'",image->filename);
1207 (void) ThrowMagickException(exception,GetMagickModule(),
1208 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1209 "'%s'",image->filename);
1211 if ((magick_info != (const MagickInfo *) NULL) &&
1212 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1215 Call appropriate image writer based on image type.
1217 thread_support=GetMagickThreadSupport(magick_info);
1218 if ((thread_support & EncoderThreadSupport) == 0)
1219 LockSemaphoreInfo(constitute_semaphore);
1220 status=GetImageEncoder(magick_info)(write_info,image,exception);
1221 if ((thread_support & EncoderThreadSupport) == 0)
1222 UnlockSemaphoreInfo(constitute_semaphore);
1226 if (GetBlobError(image) != MagickFalse)
1227 ThrowFileException(exception,FileOpenError,
1228 "AnErrorHasOccurredWritingToFile",image->filename);
1229 if (temporary == MagickTrue)
1232 Copy temporary image file to permanent.
1234 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1235 if (status != MagickFalse)
1237 (void) RelinquishUniqueFileResource(write_info->filename);
1238 status=ImageToFile(image,write_info->filename,exception);
1240 (void) CloseBlob(image);
1241 (void) RelinquishUniqueFileResource(image->filename);
1242 (void) CopyMagickString(image->filename,write_info->filename,
1245 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1246 (write_info->verbose != MagickFalse))
1247 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1248 write_info=DestroyImageInfo(write_info);
1253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1257 % W r i t e I m a g e s %
1261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263 % WriteImages() writes an image sequence into one or more files. While
1264 % WriteImage() can write an image sequence, it is limited to writing
1265 % the sequence into a single file using a format which supports multiple
1266 % frames. WriteImages(), however, does not have this limitation, instead it
1267 % generates multiple output files if necessary (or when requested). When
1268 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1269 % to include a printf-style formatting string for the frame number (e.g.
1272 % The format of the WriteImages method is:
1274 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1275 % const char *filename,ExceptionInfo *exception)
1277 % A description of each parameter follows:
1279 % o image_info: the image info.
1281 % o images: the image list.
1283 % o filename: the image filename.
1285 % o exception: return any errors or warnings in this structure.
1288 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1289 Image *images,const char *filename,ExceptionInfo *exception)
1291 #define WriteImageTag "Write/Image"
1308 MagickProgressMonitor
1320 assert(image_info != (const ImageInfo *) NULL);
1321 assert(image_info->signature == MagickSignature);
1322 assert(images != (Image *) NULL);
1323 assert(images->signature == MagickSignature);
1324 if (images->debug != MagickFalse)
1325 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1326 assert(exception != (ExceptionInfo *) NULL);
1327 write_info=CloneImageInfo(image_info);
1328 images=GetFirstImageInList(images);
1329 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1330 DestroyBlob(images);
1332 if (filename != (const char *) NULL)
1333 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1334 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1335 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1336 if (*write_info->magick == '\0')
1337 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1338 sans_exception=AcquireExceptionInfo();
1339 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1341 sans_exception=DestroyExceptionInfo(sans_exception);
1343 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1344 if (p->scene >= GetNextImageInList(p)->scene)
1350 Generate consistent scene numbers.
1352 i=(ssize_t) images->scene;
1353 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1354 p->scene=(size_t) i++;
1361 progress_monitor=(MagickProgressMonitor) NULL;
1363 number_images=GetImageListLength(images);
1364 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1366 if (number_images != 1)
1367 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1369 status&=WriteImage(write_info,p,exception);
1370 if (number_images != 1)
1371 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1372 if (write_info->adjoin != MagickFalse)
1374 if (number_images != 1)
1376 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1377 if (proceed == MagickFalse)
1381 write_info=DestroyImageInfo(write_info);
1382 return(status != 0 ? MagickTrue : MagickFalse);