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-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/cache.h"
48 #include "magick/client.h"
49 #include "magick/constitute.h"
50 #include "magick/delegate.h"
51 #include "magick/geometry.h"
52 #include "magick/identify.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/option.h"
60 #include "magick/pixel.h"
61 #include "magick/policy.h"
62 #include "magick/profile.h"
63 #include "magick/property.h"
64 #include "magick/quantum.h"
65 #include "magick/resize.h"
66 #include "magick/resource_.h"
67 #include "magick/semaphore.h"
68 #include "magick/statistic.h"
69 #include "magick/stream.h"
70 #include "magick/string_.h"
71 #include "magick/string-private.h"
72 #include "magick/timer.h"
73 #include "magick/transform.h"
74 #include "magick/utility.h"
77 *constitute_semaphore = (SemaphoreInfo *) NULL;
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 + 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 %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 % ConstituteComponentGenesis() instantiates the constitute component.
92 % The format of the ConstituteComponentGenesis method is:
94 % MagickBooleanType ConstituteComponentGenesis(void)
97 MagickExport MagickBooleanType ConstituteComponentGenesis(void)
99 AcquireSemaphoreInfo(&constitute_semaphore);
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 + 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 %
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % ConstituteComponentTerminus() destroys the constitute component.
116 % The format of the ConstituteComponentTerminus method is:
118 % ConstituteComponentTerminus(void)
121 MagickExport void ConstituteComponentTerminus(void)
123 if (constitute_semaphore == (SemaphoreInfo *) NULL)
124 AcquireSemaphoreInfo(&constitute_semaphore);
125 DestroySemaphoreInfo(&constitute_semaphore);
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % C o n s t i t u t e I m a g e %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % ConstituteImage() returns an image from the pixel data you supply.
140 % The pixel data must be in scanline order top-to-bottom. The data can be
141 % char, short int, int, float, or double. Float and double require the
142 % pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
143 % create a 640x480 image from unsigned red-green-blue character data, use:
145 % image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
147 % The format of the ConstituteImage method is:
149 % Image *ConstituteImage(const size_t columns,const size_t rows,
150 % const char *map,const StorageType storage,const void *pixels,
151 % ExceptionInfo *exception)
153 % A description of each parameter follows:
155 % o columns: width in pixels of the image.
157 % o rows: height in pixels of the image.
159 % o map: This string reflects the expected ordering of the pixel array.
160 % It can be any combination or order of R = red, G = green, B = blue,
161 % A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
162 % Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
165 % o storage: Define the data type of the pixels. Float and double types are
166 % expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
167 % from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
168 % LongPixel, QuantumPixel, or ShortPixel.
170 % o pixels: This array of values contain the pixel components as defined by
171 % map and type. You must preallocate this array where the expected
172 % length varies depending on the values of width, height, map, and type.
174 % o exception: return any errors or warnings in this structure.
177 MagickExport Image *ConstituteImage(const size_t columns,
178 const size_t rows,const char *map,const StorageType storage,
179 const void *pixels,ExceptionInfo *exception)
188 Allocate image structure.
190 assert(map != (const char *) NULL);
191 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
192 assert(pixels != (void *) NULL);
193 assert(exception != (ExceptionInfo *) NULL);
194 assert(exception->signature == MagickSignature);
195 image=AcquireImage((ImageInfo *) NULL);
196 if (image == (Image *) NULL)
197 return((Image *) NULL);
198 if ((columns == 0) || (rows == 0))
199 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
200 image->columns=columns;
202 (void) SetImageBackgroundColor(image);
203 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels);
204 if (status == MagickFalse)
206 InheritException(exception,&image->exception);
207 image=DestroyImage(image);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % P i n g I m a g e %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % PingImage() returns all the properties of an image or image sequence
224 % except for the pixels. It is much faster and consumes far less memory
225 % than ReadImage(). On failure, a NULL image is returned and exception
226 % describes the reason for the failure.
228 % The format of the PingImage method is:
230 % Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
232 % A description of each parameter follows:
234 % o image_info: Ping the image defined by the file or filename members of
237 % o exception: return any errors or warnings in this structure.
241 #if defined(__cplusplus) || defined(c_plusplus)
245 static size_t PingStream(const Image *magick_unused(image),
246 const void *magick_unused(pixels),const size_t columns)
251 #if defined(__cplusplus) || defined(c_plusplus)
255 MagickExport Image *PingImage(const ImageInfo *image_info,
256 ExceptionInfo *exception)
264 assert(image_info != (ImageInfo *) NULL);
265 assert(image_info->signature == MagickSignature);
266 if (image_info->debug != MagickFalse)
267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
268 image_info->filename);
269 assert(exception != (ExceptionInfo *) NULL);
270 ping_info=CloneImageInfo(image_info);
271 ping_info->ping=MagickTrue;
272 image=ReadStream(ping_info,&PingStream,exception);
273 if (image != (Image *) NULL)
275 ResetTimer(&image->timer);
276 if (ping_info->verbose != MagickFalse)
277 (void) IdentifyImage(image,stdout,MagickFalse);
279 ping_info=DestroyImageInfo(ping_info);
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % P i n g I m a g e s %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % PingImages() pings one or more images and returns them as an image list.
296 % The format of the PingImage method is:
298 % Image *PingImages(const ImageInfo *image_info,ExceptionInfo *exception)
300 % A description of each parameter follows:
302 % o image_info: the image info.
304 % o exception: return any errors or warnings in this structure.
307 MagickExport Image *PingImages(const ImageInfo *image_info,
308 ExceptionInfo *exception)
311 filename[MaxTextExtent];
321 Ping image list from a file.
323 assert(image_info != (ImageInfo *) NULL);
324 assert(image_info->signature == MagickSignature);
325 if (image_info->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
327 image_info->filename);
328 assert(exception != (ExceptionInfo *) NULL);
329 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
330 (int) image_info->scene,filename);
331 if (LocaleCompare(filename,image_info->filename) != 0)
341 Images of the form image-%d.png[1-5].
343 read_info=CloneImageInfo(image_info);
344 sans=AcquireExceptionInfo();
345 (void) SetImageInfo(read_info,0,sans);
346 sans=DestroyExceptionInfo(sans);
347 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
348 images=NewImageList();
349 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
350 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
352 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
353 scene,read_info->filename);
354 image=PingImage(read_info,exception);
355 if (image == (Image *) NULL)
357 AppendImageToList(&images,image);
359 read_info=DestroyImageInfo(read_info);
362 return(PingImage(image_info,exception));
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 % R e a d I m a g e %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % ReadImage() reads an image or image sequence from a file or file handle.
377 % The method returns a NULL if there is a memory shortage or if the image
378 % cannot be read. On failure, a NULL image is returned and exception
379 % describes the reason for the failure.
381 % The format of the ReadImage method is:
383 % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
385 % A description of each parameter follows:
387 % o image_info: Read the image defined by the file or filename members of
390 % o exception: return any errors or warnings in this structure.
393 MagickExport Image *ReadImage(const ImageInfo *image_info,
394 ExceptionInfo *exception)
397 filename[MaxTextExtent],
398 magick[MaxTextExtent],
399 magick_filename[MaxTextExtent];
434 Determine image type from filename prefix or suffix (e.g. image.jpg).
436 assert(image_info != (ImageInfo *) NULL);
437 assert(image_info->signature == MagickSignature);
438 assert(image_info->filename != (char *) NULL);
439 if (image_info->debug != MagickFalse)
440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
441 image_info->filename);
442 assert(exception != (ExceptionInfo *) NULL);
443 read_info=CloneImageInfo(image_info);
444 (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
445 (void) SetImageInfo(read_info,0,exception);
446 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
447 (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
448 domain=CoderPolicyDomain;
449 rights=ReadPolicyRights;
450 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
453 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
454 "NotAuthorized","`%s'",read_info->filename);
455 return((Image *) NULL);
458 Call appropriate image reader based on image type.
460 sans_exception=AcquireExceptionInfo();
461 magick_info=GetMagickInfo(read_info->magick,sans_exception);
462 sans_exception=DestroyExceptionInfo(sans_exception);
463 if (magick_info != (const MagickInfo *) NULL)
465 if (GetMagickEndianSupport(magick_info) == MagickFalse)
466 read_info->endian=UndefinedEndian;
468 if ((image_info->endian == UndefinedEndian) &&
469 (GetMagickRawSupport(magick_info) != MagickFalse))
475 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
479 if ((magick_info != (const MagickInfo *) NULL) &&
480 (GetMagickSeekableStream(magick_info) != MagickFalse))
485 image=AcquireImage(read_info);
486 (void) CopyMagickString(image->filename,read_info->filename,
488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
491 read_info=DestroyImageInfo(read_info);
492 image=DestroyImage(image);
493 return((Image *) NULL);
495 if (IsBlobSeekable(image) == MagickFalse)
498 Coder requires a seekable stream.
500 *read_info->filename='\0';
501 status=ImageToFile(image,read_info->filename,exception);
502 if (status == MagickFalse)
504 (void) CloseBlob(image);
505 read_info=DestroyImageInfo(read_info);
506 image=DestroyImage(image);
507 return((Image *) NULL);
509 read_info->temporary=MagickTrue;
511 (void) CloseBlob(image);
512 image=DestroyImage(image);
514 image=NewImageList();
515 if (constitute_semaphore == (SemaphoreInfo *) NULL)
516 AcquireSemaphoreInfo(&constitute_semaphore);
517 if ((magick_info != (const MagickInfo *) NULL) &&
518 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
520 thread_support=GetMagickThreadSupport(magick_info);
521 if ((thread_support & DecoderThreadSupport) == 0)
522 LockSemaphoreInfo(constitute_semaphore);
523 image=GetImageDecoder(magick_info)(read_info,exception);
524 if ((thread_support & DecoderThreadSupport) == 0)
525 UnlockSemaphoreInfo(constitute_semaphore);
529 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
530 if (delegate_info == (const DelegateInfo *) NULL)
532 (void) ThrowMagickException(exception,GetMagickModule(),
533 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
534 read_info->filename);
535 if (read_info->temporary != MagickFalse)
536 (void) RelinquishUniqueFileResource(read_info->filename);
537 read_info=DestroyImageInfo(read_info);
538 return((Image *) NULL);
541 Let our decoding delegate process the image.
543 image=AcquireImage(read_info);
544 if (image == (Image *) NULL)
546 read_info=DestroyImageInfo(read_info);
547 return((Image *) NULL);
549 (void) CopyMagickString(image->filename,read_info->filename,
551 *read_info->filename='\0';
552 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
553 LockSemaphoreInfo(constitute_semaphore);
554 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
556 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
557 UnlockSemaphoreInfo(constitute_semaphore);
558 image=DestroyImageList(image);
559 read_info->temporary=MagickTrue;
560 (void) SetImageInfo(read_info,0,exception);
561 magick_info=GetMagickInfo(read_info->magick,exception);
562 if ((magick_info == (const MagickInfo *) NULL) ||
563 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
565 if (IsPathAccessible(read_info->filename) != MagickFalse)
566 (void) ThrowMagickException(exception,GetMagickModule(),
567 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
568 read_info->filename);
570 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
571 read_info->filename);
572 read_info=DestroyImageInfo(read_info);
573 return((Image *) NULL);
575 thread_support=GetMagickThreadSupport(magick_info);
576 if ((thread_support & DecoderThreadSupport) == 0)
577 LockSemaphoreInfo(constitute_semaphore);
578 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
579 if ((thread_support & DecoderThreadSupport) == 0)
580 UnlockSemaphoreInfo(constitute_semaphore);
582 if (read_info->temporary != MagickFalse)
584 (void) RelinquishUniqueFileResource(read_info->filename);
585 read_info->temporary=MagickFalse;
586 if (image != (Image *) NULL)
587 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
589 if (image == (Image *) NULL)
591 read_info=DestroyImageInfo(read_info);
594 if (exception->severity >= ErrorException)
595 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
596 "Coder (%s) generated an image despite an error (%d), "
597 "notify the developers",image->magick,exception->severity);
598 if (IsBlobTemporary(image) != MagickFalse)
599 (void) RelinquishUniqueFileResource(read_info->filename);
600 if ((GetNextImageInList(image) != (Image *) NULL) &&
601 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
606 clones=CloneImages(image,read_info->scenes,exception);
607 if (clones == (Image *) NULL)
608 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
609 "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
612 image=DestroyImageList(image);
613 image=GetFirstImageInList(clones);
616 if (GetBlobError(image) != MagickFalse)
618 ThrowFileException(exception,FileOpenError,
619 "AnErrorHasOccurredReadingFromFile",read_info->filename);
620 image=DestroyImageList(image);
621 read_info=DestroyImageInfo(read_info);
622 return((Image *) NULL);
624 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
628 timestamp[MaxTextExtent];
636 next->taint=MagickFalse;
637 if (next->magick_columns == 0)
638 next->magick_columns=next->columns;
639 if (next->magick_rows == 0)
640 next->magick_rows=next->rows;
641 if ((LocaleCompare(magick,"HTTP") != 0) &&
642 (LocaleCompare(magick,"FTP") != 0) && (LocaleCompare(magick,"X") != 0))
643 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
644 (void) CopyMagickString(next->magick_filename,magick_filename,
646 if (IsBlobTemporary(image) != MagickFalse)
647 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
648 value=GetImageProperty(next,"tiff:Orientation");
649 if (value == (char *) NULL)
650 value=GetImageProperty(next,"exif:Orientation");
651 if (value != (char *) NULL)
653 next->orientation=(OrientationType) StringToLong(value);
654 (void) DeleteImageProperty(next,"tiff:Orientation");
655 (void) DeleteImageProperty(next,"exif:Orientation");
657 value=GetImageProperty(next,"exif:XResolution");
658 if (value != (char *) NULL)
660 geometry_info.rho=next->x_resolution;
661 geometry_info.sigma=1.0;
662 flags=ParseGeometry(value,&geometry_info);
663 if (geometry_info.sigma != 0)
664 next->x_resolution=geometry_info.rho/geometry_info.sigma;
665 (void) DeleteImageProperty(next,"exif:XResolution");
667 value=GetImageProperty(next,"exif:YResolution");
668 if (value != (char *) NULL)
670 geometry_info.rho=next->y_resolution;
671 geometry_info.sigma=1.0;
672 flags=ParseGeometry(value,&geometry_info);
673 if (geometry_info.sigma != 0)
674 next->y_resolution=geometry_info.rho/geometry_info.sigma;
675 (void) DeleteImageProperty(next,"exif:YResolution");
677 value=GetImageProperty(next,"tiff:ResolutionUnit");
678 if (value == (char *) NULL)
679 value=GetImageProperty(next,"exif:ResolutionUnit");
680 if (value != (char *) NULL)
682 next->units=(ResolutionType) (StringToLong(value)-1);
683 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
684 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
686 if (next->page.width == 0)
687 next->page.width=next->columns;
688 if (next->page.height == 0)
689 next->page.height=next->rows;
690 option=GetImageOption(read_info,"caption");
691 if (option != (const char *) NULL)
693 property=InterpretImageProperties(read_info,next,option);
694 (void) SetImageProperty(next,"caption",property);
695 property=DestroyString(property);
697 option=GetImageOption(read_info,"comment");
698 if (option != (const char *) NULL)
700 property=InterpretImageProperties(read_info,next,option);
701 (void) SetImageProperty(next,"comment",property);
702 property=DestroyString(property);
704 option=GetImageOption(read_info,"label");
705 if (option != (const char *) NULL)
707 property=InterpretImageProperties(read_info,next,option);
708 (void) SetImageProperty(next,"label",property);
709 property=DestroyString(property);
711 if (LocaleCompare(next->magick,"TEXT") == 0)
712 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
713 if ((read_info->extract != (char *) NULL) &&
714 (read_info->stream == (StreamHandler) NULL))
719 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
720 if ((next->columns != geometry.width) ||
721 (next->rows != geometry.height))
723 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
728 crop_image=CropImage(next,&geometry,exception);
729 if (crop_image != (Image *) NULL)
730 ReplaceImageInList(&next,crop_image);
733 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
738 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
740 size_image=ResizeImage(next,geometry.width,geometry.height,
741 next->filter,next->blur,exception);
742 if (size_image != (Image *) NULL)
743 ReplaceImageInList(&next,size_image);
747 profile=GetImageProfile(next,"icc");
748 if (profile == (const StringInfo *) NULL)
749 profile=GetImageProfile(next,"icm");
750 if (profile != (const StringInfo *) NULL)
752 next->color_profile.length=GetStringInfoLength(profile);
753 next->color_profile.info=GetStringInfoDatum(profile);
755 profile=GetImageProfile(next,"iptc");
756 if (profile == (const StringInfo *) NULL)
757 profile=GetImageProfile(next,"8bim");
758 if (profile != (const StringInfo *) NULL)
760 next->iptc_profile.length=GetStringInfoLength(profile);
761 next->iptc_profile.info=GetStringInfoDatum(profile);
763 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
765 (void) SetImageProperty(next,"date:modify",timestamp);
766 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
768 (void) SetImageProperty(next,"date:create",timestamp);
769 option=GetImageOption(image_info,"delay");
770 if (option != (const char *) NULL)
775 flags=ParseGeometry(option,&geometry_info);
776 if ((flags & GreaterValue) != 0)
778 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
779 next->delay=(size_t) floor(geometry_info.rho+0.5);
782 if ((flags & LessValue) != 0)
784 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
785 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
788 next->delay=(size_t) floor(geometry_info.rho+0.5);
789 if ((flags & SigmaValue) != 0)
790 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
792 option=GetImageOption(image_info,"dispose");
793 if (option != (const char *) NULL)
794 next->dispose=(DisposeType) ParseMagickOption(MagickDisposeOptions,
796 if (read_info->verbose != MagickFalse)
797 (void) IdentifyImage(next,stderr,MagickFalse);
800 read_info=DestroyImageInfo(read_info);
801 return(GetFirstImageInList(image));
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 % R e a d I m a g e s %
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % ReadImages() reads one or more images and returns them as an image list.
817 % The format of the ReadImage method is:
819 % Image *ReadImages(const ImageInfo *image_info,ExceptionInfo *exception)
821 % A description of each parameter follows:
823 % o image_info: the image info.
825 % o exception: return any errors or warnings in this structure.
828 MagickExport Image *ReadImages(const ImageInfo *image_info,
829 ExceptionInfo *exception)
832 filename[MaxTextExtent];
842 Read image list from a file.
844 assert(image_info != (ImageInfo *) NULL);
845 assert(image_info->signature == MagickSignature);
846 if (image_info->debug != MagickFalse)
847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
848 image_info->filename);
849 assert(exception != (ExceptionInfo *) NULL);
850 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
851 (int) image_info->scene,filename);
852 if (LocaleCompare(filename,image_info->filename) != 0)
862 Images of the form image-%d.png[1-5].
864 read_info=CloneImageInfo(image_info);
865 sans=AcquireExceptionInfo();
866 (void) SetImageInfo(read_info,0,sans);
867 sans=DestroyExceptionInfo(sans);
868 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
869 images=NewImageList();
870 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
871 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
873 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
874 scene,read_info->filename);
875 image=ReadImage(read_info,exception);
876 if (image == (Image *) NULL)
878 AppendImageToList(&images,image);
880 read_info=DestroyImageInfo(read_info);
883 return(ReadImage(image_info,exception));
887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 + R e a d I n l i n e I m a g e %
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
898 % The method returns a NULL if there is a memory shortage or if the image
899 % cannot be read. On failure, a NULL image is returned and exception
900 % describes the reason for the failure.
902 % The format of the ReadInlineImage method is:
904 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
905 % ExceptionInfo *exception)
907 % A description of each parameter follows:
909 % o image_info: the image info.
911 % o content: the image encoded in Base64.
913 % o exception: return any errors or warnings in this structure.
916 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
917 const char *content,ExceptionInfo *exception)
935 Skip over header (e.g. data:image/gif;base64,).
937 image=NewImageList();
938 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
940 ThrowReaderException(CorruptImageError,"CorruptImage");
943 blob=Base64Decode(p,&length);
945 ThrowReaderException(CorruptImageError,"CorruptImage");
946 read_info=CloneImageInfo(image_info);
947 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
949 *read_info->filename='\0';
950 *read_info->magick='\0';
951 image=BlobToImage(read_info,blob,length,exception);
952 blob=(unsigned char *) RelinquishMagickMemory(blob);
953 read_info=DestroyImageInfo(read_info);
958 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
962 % W r i t e I m a g e %
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968 % WriteImage() writes an image or an image sequence to a file or file handle.
969 % If writing to a file is on disk, the name is defined by the filename member
970 % of the image structure. WriteImage() returns MagickFalse is there is a
971 % memory shortage or if the image cannot be written. Check the exception
972 % member of image to determine the cause for any failure.
974 % The format of the WriteImage method is:
976 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image)
978 % A description of each parameter follows:
980 % o image_info: the image info.
982 % o image: the image.
985 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
989 filename[MaxTextExtent];
1020 Determine image type from filename prefix or suffix (e.g. image.jpg).
1022 assert(image_info != (ImageInfo *) NULL);
1023 assert(image_info->signature == MagickSignature);
1024 if (image->debug != MagickFalse)
1025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1026 image_info->filename);
1027 assert(image != (Image *) NULL);
1028 assert(image->signature == MagickSignature);
1029 sans_exception=AcquireExceptionInfo();
1030 write_info=CloneImageInfo(image_info);
1031 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1032 if (*write_info->magick == '\0')
1033 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1034 (void) SetImageInfo(write_info,1,sans_exception);
1035 if (LocaleCompare(write_info->magick,"clipmask") == 0)
1037 if (image->clip_mask == (Image *) NULL)
1039 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1040 OptionError,"NoClipPathDefined","`%s'",image->filename);
1041 return(MagickFalse);
1043 image=image->clip_mask;
1044 (void) SetImageInfo(write_info,1,sans_exception);
1046 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1047 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1048 domain=CoderPolicyDomain;
1049 rights=WritePolicyRights;
1050 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1052 sans_exception=DestroyExceptionInfo(sans_exception);
1054 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1056 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1057 sans_exception=DestroyExceptionInfo(sans_exception);
1058 if (magick_info != (const MagickInfo *) NULL)
1060 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1061 image->endian=UndefinedEndian;
1063 if ((image_info->endian == UndefinedEndian) &&
1064 (GetMagickRawSupport(magick_info) != MagickFalse))
1070 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1073 (void) SyncImageProfiles(image);
1074 option=GetImageOption(image_info,"delegate:bimodal");
1075 if ((option != (const char *) NULL) &&
1076 (IsMagickTrue(option) != MagickFalse) &&
1077 (write_info->page == (char *) NULL) &&
1078 (GetPreviousImageInList(image) == (Image *) NULL) &&
1079 (GetNextImageInList(image) == (Image *) NULL) &&
1080 (IsTaintImage(image) == MagickFalse))
1082 delegate_info=GetDelegateInfo(image->magick,write_info->magick,
1084 if ((delegate_info != (const DelegateInfo *) NULL) &&
1085 (GetDelegateMode(delegate_info) == 0) &&
1086 (IsPathAccessible(image->magick_filename) != MagickFalse))
1089 Process image with bi-modal delegate.
1091 (void) CopyMagickString(image->filename,image->magick_filename,
1093 status=InvokeDelegate(write_info,image,image->magick,
1094 write_info->magick,&image->exception);
1095 write_info=DestroyImageInfo(write_info);
1096 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1101 temporary=MagickFalse;
1102 if ((magick_info != (const MagickInfo *) NULL) &&
1103 (GetMagickSeekableStream(magick_info) != MagickFalse))
1106 filename[MaxTextExtent];
1108 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1109 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1110 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1111 if (status != MagickFalse)
1113 if (IsBlobSeekable(image) == MagickFalse)
1116 A seekable stream is required by the encoder.
1118 write_info->adjoin=MagickTrue;
1119 (void) CopyMagickString(write_info->filename,image->filename,
1121 (void) AcquireUniqueFilename(image->filename);
1122 temporary=MagickTrue;
1124 (void) CloseBlob(image);
1127 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1128 AcquireSemaphoreInfo(&constitute_semaphore);
1129 if ((magick_info != (const MagickInfo *) NULL) &&
1130 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1133 Call appropriate image writer based on image type.
1135 thread_support=GetMagickThreadSupport(magick_info);
1136 if ((thread_support & EncoderThreadSupport) == 0)
1137 LockSemaphoreInfo(constitute_semaphore);
1138 status=GetImageEncoder(magick_info)(write_info,image);
1139 if ((thread_support & EncoderThreadSupport) == 0)
1140 UnlockSemaphoreInfo(constitute_semaphore);
1144 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,
1146 if (delegate_info != (DelegateInfo *) NULL)
1149 Process the image with delegate.
1151 *write_info->filename='\0';
1152 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1153 LockSemaphoreInfo(constitute_semaphore);
1154 status=InvokeDelegate(write_info,image,(char *) NULL,
1155 write_info->magick,&image->exception);
1156 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1157 UnlockSemaphoreInfo(constitute_semaphore);
1158 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1162 sans_exception=AcquireExceptionInfo();
1163 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1164 sans_exception=DestroyExceptionInfo(sans_exception);
1165 if ((write_info->affirm == MagickFalse) &&
1166 (magick_info == (const MagickInfo *) NULL))
1168 (void) CopyMagickString(write_info->magick,image->magick,
1170 magick_info=GetMagickInfo(write_info->magick,&image->exception);
1172 if ((magick_info == (const MagickInfo *) NULL) ||
1173 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1176 extension[MaxTextExtent];
1178 GetPathComponent(image->filename,ExtensionPath,extension);
1179 if (*extension != '\0')
1180 magick_info=GetMagickInfo(extension,&image->exception);
1182 magick_info=GetMagickInfo(image->magick,&image->exception);
1183 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1185 if ((magick_info == (const MagickInfo *) NULL) ||
1186 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1187 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1188 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
1193 Call appropriate image writer based on image type.
1195 thread_support=GetMagickThreadSupport(magick_info);
1196 if ((thread_support & EncoderThreadSupport) == 0)
1197 LockSemaphoreInfo(constitute_semaphore);
1198 status=GetImageEncoder(magick_info)(write_info,image);
1199 if ((thread_support & EncoderThreadSupport) == 0)
1200 UnlockSemaphoreInfo(constitute_semaphore);
1204 if (GetBlobError(image) != MagickFalse)
1205 ThrowFileException(&image->exception,FileOpenError,
1206 "AnErrorHasOccurredWritingToFile",image->filename);
1207 if (temporary == MagickTrue)
1210 Copy temporary image file to permanent.
1212 status=OpenBlob(write_info,image,ReadBinaryBlobMode,&image->exception);
1213 if (status != MagickFalse)
1214 status=ImageToFile(image,write_info->filename,&image->exception);
1215 (void) CloseBlob(image);
1216 (void) RelinquishUniqueFileResource(image->filename);
1217 (void) CopyMagickString(image->filename,write_info->filename,
1220 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1221 (write_info->verbose != MagickFalse))
1222 (void) IdentifyImage(image,stdout,MagickFalse);
1223 write_info=DestroyImageInfo(write_info);
1228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1232 % W r i t e I m a g e s %
1236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1238 % WriteImages() writes an image sequence into one or more files. While
1239 % WriteImage() can write an image sequence, it is limited to writing
1240 % the sequence into a single file using a format which supports multiple
1241 % frames. WriteImages(), however, does not have this limitation, instead it
1242 % generates multiple output files if necessary (or when requested). When
1243 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1244 % to include a printf-style formatting string for the frame number (e.g.
1247 % The format of the WriteImages method is:
1249 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1250 % const char *filename,ExceptionInfo *exception)
1252 % A description of each parameter follows:
1254 % o image_info: the image info.
1256 % o images: the image list.
1258 % o filename: the image filename.
1260 % o exception: return any errors or warnings in this structure.
1263 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1264 Image *images,const char *filename,ExceptionInfo *exception)
1266 #define WriteImageTag "Write/Image"
1283 MagickProgressMonitor
1295 assert(image_info != (const ImageInfo *) NULL);
1296 assert(image_info->signature == MagickSignature);
1297 assert(images != (Image *) NULL);
1298 assert(images->signature == MagickSignature);
1299 if (images->debug != MagickFalse)
1300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1301 assert(exception != (ExceptionInfo *) NULL);
1302 write_info=CloneImageInfo(image_info);
1303 images=GetFirstImageInList(images);
1304 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1305 DestroyBlob(images);
1307 if (filename != (const char *) NULL)
1308 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1309 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1310 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1311 if (*write_info->magick == '\0')
1312 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1313 sans_exception=AcquireExceptionInfo();
1314 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1316 sans_exception=DestroyExceptionInfo(sans_exception);
1318 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1319 if (p->scene >= GetNextImageInList(p)->scene)
1325 Generate consistent scene numbers.
1327 i=(ssize_t) images->scene;
1328 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1329 p->scene=(size_t) i++;
1336 progress_monitor=(MagickProgressMonitor) NULL;
1338 number_images=GetImageListLength(images);
1339 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1341 if (number_images != 1)
1342 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1344 status&=WriteImage(write_info,p);
1345 GetImageException(p,exception);
1346 if (number_images != 1)
1347 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1348 if (write_info->adjoin != MagickFalse)
1350 if (number_images != 1)
1352 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1353 if (proceed == MagickFalse)
1357 write_info=DestroyImageInfo(write_info);
1358 return(status != 0 ? MagickTrue : MagickFalse);