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-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/client.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/constitute.h"
52 #include "MagickCore/constitute-private.h"
53 #include "MagickCore/delegate.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/identify.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/policy.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/profile-private.h"
68 #include "MagickCore/property.h"
69 #include "MagickCore/quantum.h"
70 #include "MagickCore/resize.h"
71 #include "MagickCore/resource_.h"
72 #include "MagickCore/semaphore.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/stream.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/string-private.h"
77 #include "MagickCore/timer.h"
78 #include "MagickCore/token.h"
79 #include "MagickCore/transform.h"
80 #include "MagickCore/utility.h"
81 #include "MagickCore/utility-private.h"
84 *constitute_semaphore = (SemaphoreInfo *) NULL;
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 + 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 %
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % ConstituteComponentGenesis() instantiates the constitute component.
99 % The format of the ConstituteComponentGenesis method is:
101 % MagickBooleanType ConstituteComponentGenesis(void)
104 MagickPrivate MagickBooleanType ConstituteComponentGenesis(void)
106 AcquireSemaphoreInfo(&constitute_semaphore);
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 + 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 %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % ConstituteComponentTerminus() destroys the constitute component.
123 % The format of the ConstituteComponentTerminus method is:
125 % ConstituteComponentTerminus(void)
128 MagickPrivate void ConstituteComponentTerminus(void)
130 if (constitute_semaphore == (SemaphoreInfo *) NULL)
131 AcquireSemaphoreInfo(&constitute_semaphore);
132 DestroySemaphoreInfo(&constitute_semaphore);
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 % C o n s t i t u t e I m a g e %
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % ConstituteImage() returns an image from the pixel data you supply.
147 % The pixel data must be in scanline order top-to-bottom. The data can be
148 % char, short int, int, float, or double. Float and double require the
149 % pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
150 % create a 640x480 image from unsigned red-green-blue character data, use:
152 % image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
154 % The format of the ConstituteImage method is:
156 % Image *ConstituteImage(const size_t columns,const size_t rows,
157 % const char *map,const StorageType storage,const void *pixels,
158 % ExceptionInfo *exception)
160 % A description of each parameter follows:
162 % o columns: width in pixels of the image.
164 % o rows: height in pixels of the image.
166 % o map: This string reflects the expected ordering of the pixel array.
167 % It can be any combination or order of R = red, G = green, B = blue,
168 % A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
169 % Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
172 % o storage: Define the data type of the pixels. Float and double types are
173 % expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
174 % from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
175 % LongPixel, QuantumPixel, or ShortPixel.
177 % o pixels: This array of values contain the pixel components as defined by
178 % map and type. You must preallocate this array where the expected
179 % length varies depending on the values of width, height, map, and type.
181 % o exception: return any errors or warnings in this structure.
184 MagickExport Image *ConstituteImage(const size_t columns,const size_t rows,
185 const char *map,const StorageType storage,const void *pixels,
186 ExceptionInfo *exception)
195 Allocate image structure.
197 assert(map != (const char *) NULL);
198 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
199 assert(pixels != (void *) NULL);
200 assert(exception != (ExceptionInfo *) NULL);
201 assert(exception->signature == MagickSignature);
202 image=AcquireImage((ImageInfo *) NULL,exception);
203 if (image == (Image *) NULL)
204 return((Image *) NULL);
205 if ((columns == 0) || (rows == 0))
206 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
207 image->columns=columns;
209 (void) SetImageBackgroundColor(image,exception);
210 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
211 if (status == MagickFalse)
212 image=DestroyImage(image);
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 % P i n g I m a g e %
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 % PingImage() returns all the properties of an image or image sequence
228 % except for the pixels. It is much faster and consumes far less memory
229 % than ReadImage(). On failure, a NULL image is returned and exception
230 % describes the reason for the failure.
232 % The format of the PingImage method is:
234 % Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
236 % A description of each parameter follows:
238 % o image_info: Ping the image defined by the file or filename members of
241 % o exception: return any errors or warnings in this structure.
245 #if defined(__cplusplus) || defined(c_plusplus)
249 static size_t PingStream(const Image *magick_unused(image),
250 const void *magick_unused(pixels),const size_t columns)
255 #if defined(__cplusplus) || defined(c_plusplus)
259 MagickExport Image *PingImage(const ImageInfo *image_info,
260 ExceptionInfo *exception)
268 assert(image_info != (ImageInfo *) NULL);
269 assert(image_info->signature == MagickSignature);
270 if (image_info->debug != MagickFalse)
271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
272 image_info->filename);
273 assert(exception != (ExceptionInfo *) NULL);
274 ping_info=CloneImageInfo(image_info);
275 ping_info->ping=MagickTrue;
276 image=ReadStream(ping_info,&PingStream,exception);
277 if (image != (Image *) NULL)
279 ResetTimer(&image->timer);
280 if (ping_info->verbose != MagickFalse)
281 (void) IdentifyImage(image,stdout,MagickFalse,exception);
283 ping_info=DestroyImageInfo(ping_info);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % P i n g I m a g e s %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % PingImages() pings one or more images and returns them as an image list.
300 % The format of the PingImage method is:
302 % Image *PingImages(ImageInfo *image_info,const char *filename,
303 % ExceptionInfo *exception)
305 % A description of each parameter follows:
307 % o image_info: the image info.
309 % o filename: the image filename.
311 % o exception: return any errors or warnings in this structure.
314 MagickExport Image *PingImages(ImageInfo *image_info,const char *filename,
315 ExceptionInfo *exception)
318 ping_filename[MaxTextExtent];
328 Ping image list from a file.
330 assert(image_info != (ImageInfo *) NULL);
331 assert(image_info->signature == MagickSignature);
332 if (image_info->debug != MagickFalse)
333 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
334 image_info->filename);
335 assert(exception != (ExceptionInfo *) NULL);
336 (void) SetImageOption(image_info,"filename",filename);
337 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
338 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
339 (int) image_info->scene,ping_filename,exception);
340 if (LocaleCompare(ping_filename,image_info->filename) != 0)
350 Images of the form image-%d.png[1-5].
352 read_info=CloneImageInfo(image_info);
353 sans=AcquireExceptionInfo();
354 (void) SetImageInfo(read_info,0,sans);
355 sans=DestroyExceptionInfo(sans);
356 if (read_info->number_scenes == 0)
358 read_info=DestroyImageInfo(read_info);
359 return(PingImage(image_info,exception));
361 (void) CopyMagickString(ping_filename,read_info->filename,MaxTextExtent);
362 images=NewImageList();
363 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
364 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
366 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
367 (int) scene,read_info->filename,exception);
368 image=PingImage(read_info,exception);
369 if (image == (Image *) NULL)
371 AppendImageToList(&images,image);
373 read_info=DestroyImageInfo(read_info);
376 return(PingImage(image_info,exception));
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 % R e a d I m a g e %
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % ReadImage() reads an image or image sequence from a file or file handle.
391 % The method returns a NULL if there is a memory shortage or if the image
392 % cannot be read. On failure, a NULL image is returned and exception
393 % describes the reason for the failure.
395 % The format of the ReadImage method is:
397 % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
399 % A description of each parameter follows:
401 % o image_info: Read the image defined by the file or filename members of
404 % o exception: return any errors or warnings in this structure.
407 MagickExport Image *ReadImage(const ImageInfo *image_info,
408 ExceptionInfo *exception)
411 filename[MaxTextExtent],
412 magick[MaxTextExtent],
413 magick_filename[MaxTextExtent];
448 Determine image type from filename prefix or suffix (e.g. image.jpg).
450 assert(image_info != (ImageInfo *) NULL);
451 assert(image_info->signature == MagickSignature);
452 assert(image_info->filename != (char *) NULL);
453 if (image_info->debug != MagickFalse)
454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
455 image_info->filename);
456 assert(exception != (ExceptionInfo *) NULL);
457 read_info=CloneImageInfo(image_info);
458 (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
459 (void) SetImageInfo(read_info,0,exception);
460 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
461 (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
462 domain=CoderPolicyDomain;
463 rights=ReadPolicyRights;
464 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
467 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
468 "NotAuthorized","`%s'",read_info->filename);
469 read_info=DestroyImageInfo(read_info);
470 return((Image *) NULL);
473 Call appropriate image reader based on image type.
475 sans_exception=AcquireExceptionInfo();
476 magick_info=GetMagickInfo(read_info->magick,sans_exception);
477 sans_exception=DestroyExceptionInfo(sans_exception);
478 if (magick_info != (const MagickInfo *) NULL)
480 if (GetMagickEndianSupport(magick_info) == MagickFalse)
481 read_info->endian=UndefinedEndian;
483 if ((image_info->endian == UndefinedEndian) &&
484 (GetMagickRawSupport(magick_info) != MagickFalse))
490 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
494 if ((magick_info != (const MagickInfo *) NULL) &&
495 (GetMagickSeekableStream(magick_info) != MagickFalse))
500 image=AcquireImage(read_info,exception);
501 (void) CopyMagickString(image->filename,read_info->filename,
503 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
504 if (status == MagickFalse)
506 read_info=DestroyImageInfo(read_info);
507 image=DestroyImage(image);
508 return((Image *) NULL);
510 if (IsBlobSeekable(image) == MagickFalse)
513 Coder requires a seekable stream.
515 *read_info->filename='\0';
516 status=ImageToFile(image,read_info->filename,exception);
517 if (status == MagickFalse)
519 (void) CloseBlob(image);
520 read_info=DestroyImageInfo(read_info);
521 image=DestroyImage(image);
522 return((Image *) NULL);
524 read_info->temporary=MagickTrue;
526 (void) CloseBlob(image);
527 image=DestroyImage(image);
529 image=NewImageList();
530 if (constitute_semaphore == (SemaphoreInfo *) NULL)
531 AcquireSemaphoreInfo(&constitute_semaphore);
532 if ((magick_info == (const MagickInfo *) NULL) ||
533 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
535 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
536 if (delegate_info == (const DelegateInfo *) NULL)
538 (void) SetImageInfo(read_info,0,exception);
539 (void) CopyMagickString(read_info->filename,filename,MaxTextExtent);
540 magick_info=GetMagickInfo(read_info->magick,exception);
543 if ((magick_info != (const MagickInfo *) NULL) &&
544 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
546 thread_support=GetMagickThreadSupport(magick_info);
547 if ((thread_support & DecoderThreadSupport) == 0)
548 LockSemaphoreInfo(constitute_semaphore);
549 image=GetImageDecoder(magick_info)(read_info,exception);
550 if ((thread_support & DecoderThreadSupport) == 0)
551 UnlockSemaphoreInfo(constitute_semaphore);
555 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
556 if (delegate_info == (const DelegateInfo *) NULL)
558 (void) ThrowMagickException(exception,GetMagickModule(),
559 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
560 read_info->filename);
561 if (read_info->temporary != MagickFalse)
562 (void) RelinquishUniqueFileResource(read_info->filename);
563 read_info=DestroyImageInfo(read_info);
564 return((Image *) NULL);
567 Let our decoding delegate process the image.
569 image=AcquireImage(read_info,exception);
570 if (image == (Image *) NULL)
572 read_info=DestroyImageInfo(read_info);
573 return((Image *) NULL);
575 (void) CopyMagickString(image->filename,read_info->filename,
577 *read_info->filename='\0';
578 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
579 LockSemaphoreInfo(constitute_semaphore);
580 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
582 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
583 UnlockSemaphoreInfo(constitute_semaphore);
584 image=DestroyImageList(image);
585 read_info->temporary=MagickTrue;
586 (void) SetImageInfo(read_info,0,exception);
587 magick_info=GetMagickInfo(read_info->magick,exception);
588 if ((magick_info == (const MagickInfo *) NULL) ||
589 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
591 if (IsPathAccessible(read_info->filename) != MagickFalse)
592 (void) ThrowMagickException(exception,GetMagickModule(),
593 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
594 read_info->filename);
596 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
597 read_info->filename);
598 read_info=DestroyImageInfo(read_info);
599 return((Image *) NULL);
601 thread_support=GetMagickThreadSupport(magick_info);
602 if ((thread_support & DecoderThreadSupport) == 0)
603 LockSemaphoreInfo(constitute_semaphore);
604 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
605 if ((thread_support & DecoderThreadSupport) == 0)
606 UnlockSemaphoreInfo(constitute_semaphore);
608 if (read_info->temporary != MagickFalse)
610 (void) RelinquishUniqueFileResource(read_info->filename);
611 read_info->temporary=MagickFalse;
612 if (image != (Image *) NULL)
613 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
615 if (image == (Image *) NULL)
617 read_info=DestroyImageInfo(read_info);
620 if (exception->severity >= ErrorException)
621 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
622 "Coder (%s) generated an image despite an error (%d), "
623 "notify the developers",image->magick,exception->severity);
624 if (IsBlobTemporary(image) != MagickFalse)
625 (void) RelinquishUniqueFileResource(read_info->filename);
626 if ((GetNextImageInList(image) != (Image *) NULL) &&
627 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
632 clones=CloneImages(image,read_info->scenes,exception);
633 if (clones == (Image *) NULL)
634 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
635 "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
638 image=DestroyImageList(image);
639 image=GetFirstImageInList(clones);
642 if (GetBlobError(image) != MagickFalse)
643 ThrowFileException(exception,FileOpenError,
644 "AnErrorHasOccurredReadingFromFile",read_info->filename);
645 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
648 magick_path[MaxTextExtent],
650 timestamp[MaxTextExtent];
658 next->taint=MagickFalse;
659 GetPathComponent(magick_filename,MagickPath,magick_path);
660 if (*magick_path == '\0')
661 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
662 (void) CopyMagickString(next->magick_filename,magick_filename,
664 if (IsBlobTemporary(image) != MagickFalse)
665 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
666 if (next->magick_columns == 0)
667 next->magick_columns=next->columns;
668 if (next->magick_rows == 0)
669 next->magick_rows=next->rows;
670 value=GetImageProperty(next,"tiff:Orientation",exception);
671 if (value == (char *) NULL)
672 value=GetImageProperty(next,"exif:Orientation",exception);
673 if (value != (char *) NULL)
675 next->orientation=(OrientationType) StringToLong(value);
676 (void) DeleteImageProperty(next,"tiff:Orientation");
677 (void) DeleteImageProperty(next,"exif:Orientation");
679 value=GetImageProperty(next,"exif:XResolution",exception);
680 if (value != (char *) NULL)
682 geometry_info.rho=next->resolution.x;
683 geometry_info.sigma=1.0;
684 flags=ParseGeometry(value,&geometry_info);
685 if (geometry_info.sigma != 0)
686 next->resolution.x=geometry_info.rho/geometry_info.sigma;
687 (void) DeleteImageProperty(next,"exif:XResolution");
689 value=GetImageProperty(next,"exif:YResolution",exception);
690 if (value != (char *) NULL)
692 geometry_info.rho=next->resolution.y;
693 geometry_info.sigma=1.0;
694 flags=ParseGeometry(value,&geometry_info);
695 if (geometry_info.sigma != 0)
696 next->resolution.y=geometry_info.rho/geometry_info.sigma;
697 (void) DeleteImageProperty(next,"exif:YResolution");
699 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
700 if (value == (char *) NULL)
701 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
702 if (value != (char *) NULL)
704 next->units=(ResolutionType) (StringToLong(value)-1);
705 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
706 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
708 if (next->page.width == 0)
709 next->page.width=next->columns;
710 if (next->page.height == 0)
711 next->page.height=next->rows;
712 option=GetImageOption(read_info,"caption");
713 if (option != (const char *) NULL)
715 property=InterpretImageProperties(read_info,next,option,exception);
716 (void) SetImageProperty(next,"caption",property,exception);
717 property=DestroyString(property);
719 option=GetImageOption(read_info,"comment");
720 if (option != (const char *) NULL)
722 property=InterpretImageProperties(read_info,next,option,exception);
723 (void) SetImageProperty(next,"comment",property,exception);
724 property=DestroyString(property);
726 option=GetImageOption(read_info,"label");
727 if (option != (const char *) NULL)
729 property=InterpretImageProperties(read_info,next,option,exception);
730 (void) SetImageProperty(next,"label",property,exception);
731 property=DestroyString(property);
733 if (LocaleCompare(next->magick,"TEXT") == 0)
734 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
735 if ((read_info->extract != (char *) NULL) &&
736 (read_info->stream == (StreamHandler) NULL))
741 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
742 if ((next->columns != geometry.width) ||
743 (next->rows != geometry.height))
745 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
750 crop_image=CropImage(next,&geometry,exception);
751 if (crop_image != (Image *) NULL)
752 ReplaceImageInList(&next,crop_image);
755 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
760 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
762 size_image=ResizeImage(next,geometry.width,geometry.height,
763 next->filter,exception);
764 if (size_image != (Image *) NULL)
765 ReplaceImageInList(&next,size_image);
769 profile=GetImageProfile(next,"icc");
770 if (profile == (const StringInfo *) NULL)
771 profile=GetImageProfile(next,"icm");
772 profile=GetImageProfile(next,"iptc");
773 if (profile == (const StringInfo *) NULL)
774 profile=GetImageProfile(next,"8bim");
775 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
777 (void) SetImageProperty(next,"date:modify",timestamp,exception);
778 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
780 (void) SetImageProperty(next,"date:create",timestamp,exception);
781 option=GetImageOption(image_info,"delay");
782 if (option != (const char *) NULL)
787 flags=ParseGeometry(option,&geometry_info);
788 if ((flags & GreaterValue) != 0)
790 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
791 next->delay=(size_t) floor(geometry_info.rho+0.5);
794 if ((flags & LessValue) != 0)
796 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
797 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
800 next->delay=(size_t) floor(geometry_info.rho+0.5);
801 if ((flags & SigmaValue) != 0)
802 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
804 option=GetImageOption(image_info,"dispose");
805 if (option != (const char *) NULL)
806 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
808 if (read_info->verbose != MagickFalse)
809 (void) IdentifyImage(next,stderr,MagickFalse,exception);
812 read_info=DestroyImageInfo(read_info);
813 return(GetFirstImageInList(image));
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % R e a d I m a g e s %
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % ReadImages() reads one or more images and returns them as an image list.
829 % The format of the ReadImage method is:
831 % Image *ReadImages(ImageInfo *image_info,const char *filename,
832 % ExceptionInfo *exception)
834 % A description of each parameter follows:
836 % o image_info: the image info.
838 % o filename: the image filename.
840 % o exception: return any errors or warnings in this structure.
843 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
844 ExceptionInfo *exception)
847 read_filename[MaxTextExtent];
857 Read image list from a file.
859 assert(image_info != (ImageInfo *) NULL);
860 assert(image_info->signature == MagickSignature);
861 if (image_info->debug != MagickFalse)
862 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
863 image_info->filename);
864 assert(exception != (ExceptionInfo *) NULL);
865 (void) SetImageOption(image_info,"filename",filename);
866 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
867 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,
868 (int) image_info->scene,read_filename,exception);
869 if (LocaleCompare(read_filename,image_info->filename) != 0)
879 Images of the form image-%d.png[1-5].
881 read_info=CloneImageInfo(image_info);
882 sans=AcquireExceptionInfo();
883 (void) SetImageInfo(read_info,0,sans);
884 sans=DestroyExceptionInfo(sans);
885 if (read_info->number_scenes == 0)
887 read_info=DestroyImageInfo(read_info);
888 return(ReadImage(image_info,exception));
890 (void) CopyMagickString(read_filename,read_info->filename,MaxTextExtent);
891 images=NewImageList();
892 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
893 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
895 (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
896 (int) scene,read_info->filename,exception);
897 image=ReadImage(read_info,exception);
898 if (image == (Image *) NULL)
900 AppendImageToList(&images,image);
902 read_info=DestroyImageInfo(read_info);
905 return(ReadImage(image_info,exception));
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913 + R e a d I n l i n e I m a g e %
917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
920 % The method returns a NULL if there is a memory shortage or if the image
921 % cannot be read. On failure, a NULL image is returned and exception
922 % describes the reason for the failure.
924 % The format of the ReadInlineImage method is:
926 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
927 % ExceptionInfo *exception)
929 % A description of each parameter follows:
931 % o image_info: the image info.
933 % o content: the image encoded in Base64.
935 % o exception: return any errors or warnings in this structure.
938 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
939 const char *content,ExceptionInfo *exception)
957 Skip over header (e.g. data:image/gif;base64,).
959 image=NewImageList();
960 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
962 ThrowReaderException(CorruptImageError,"CorruptImage");
965 blob=Base64Decode(p,&length);
967 ThrowReaderException(CorruptImageError,"CorruptImage");
968 read_info=CloneImageInfo(image_info);
969 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
971 *read_info->filename='\0';
972 *read_info->magick='\0';
973 image=BlobToImage(read_info,blob,length,exception);
974 blob=(unsigned char *) RelinquishMagickMemory(blob);
975 read_info=DestroyImageInfo(read_info);
980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 % W r i t e I m a g e %
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
990 % WriteImage() writes an image or an image sequence to a file or file handle.
991 % If writing to a file is on disk, the name is defined by the filename member
992 % of the image structure. WriteImage() returns MagickFalse is there is a
993 % memory shortage or if the image cannot be written. Check the exception
994 % member of image to determine the cause for any failure.
996 % The format of the WriteImage method is:
998 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
999 % ExceptionInfo *exception)
1001 % A description of each parameter follows:
1003 % o image_info: the image info.
1005 % o image: the image.
1007 % o exception: return any errors or warnings in this structure.
1010 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1011 Image *image,ExceptionInfo *exception)
1014 filename[MaxTextExtent];
1045 Determine image type from filename prefix or suffix (e.g. image.jpg).
1047 assert(image_info != (ImageInfo *) NULL);
1048 assert(image_info->signature == MagickSignature);
1049 if (image->debug != MagickFalse)
1050 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1051 image_info->filename);
1052 assert(image != (Image *) NULL);
1053 assert(image->signature == MagickSignature);
1054 assert(exception != (ExceptionInfo *) NULL);
1055 sans_exception=AcquireExceptionInfo();
1056 write_info=CloneImageInfo(image_info);
1057 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1058 if (*write_info->magick == '\0')
1059 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1060 (void) SetImageInfo(write_info,1,sans_exception);
1061 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1062 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1063 domain=CoderPolicyDomain;
1064 rights=WritePolicyRights;
1065 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1067 sans_exception=DestroyExceptionInfo(sans_exception);
1068 write_info=DestroyImageInfo(write_info);
1070 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1073 Call appropriate image reader based on image type.
1075 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1076 sans_exception=DestroyExceptionInfo(sans_exception);
1077 if (magick_info != (const MagickInfo *) NULL)
1079 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1080 image->endian=UndefinedEndian;
1082 if ((image_info->endian == UndefinedEndian) &&
1083 (GetMagickRawSupport(magick_info) != MagickFalse))
1089 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1092 (void) SyncImageProfiles(image);
1093 option=GetImageOption(image_info,"delegate:bimodal");
1094 if ((IfMagickTrue(IsStringTrue(option))) &&
1095 (write_info->page == (char *) NULL) &&
1096 (GetPreviousImageInList(image) == (Image *) NULL) &&
1097 (GetNextImageInList(image) == (Image *) NULL) &&
1098 (IfMagickFalse(IsTaintImage(image))) )
1100 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1101 if ((delegate_info != (const DelegateInfo *) NULL) &&
1102 (GetDelegateMode(delegate_info) == 0) &&
1103 (IsPathAccessible(image->magick_filename) != MagickFalse))
1106 Process image with bi-modal delegate.
1108 (void) CopyMagickString(image->filename,image->magick_filename,
1110 status=InvokeDelegate(write_info,image,image->magick,
1111 write_info->magick,exception);
1112 write_info=DestroyImageInfo(write_info);
1113 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1118 temporary=MagickFalse;
1119 if ((magick_info != (const MagickInfo *) NULL) &&
1120 (GetMagickSeekableStream(magick_info) != MagickFalse))
1123 filename[MaxTextExtent];
1125 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1126 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1127 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1128 if (status != MagickFalse)
1130 if (IsBlobSeekable(image) == MagickFalse)
1133 A seekable stream is required by the encoder.
1135 write_info->adjoin=MagickTrue;
1136 (void) CopyMagickString(write_info->filename,image->filename,
1138 (void) AcquireUniqueFilename(image->filename);
1139 temporary=MagickTrue;
1141 (void) CloseBlob(image);
1144 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1145 AcquireSemaphoreInfo(&constitute_semaphore);
1146 if ((magick_info != (const MagickInfo *) NULL) &&
1147 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1150 Call appropriate image writer based on image type.
1152 thread_support=GetMagickThreadSupport(magick_info);
1153 if ((thread_support & EncoderThreadSupport) == 0)
1154 LockSemaphoreInfo(constitute_semaphore);
1155 status=GetImageEncoder(magick_info)(write_info,image,exception);
1156 if ((thread_support & EncoderThreadSupport) == 0)
1157 UnlockSemaphoreInfo(constitute_semaphore);
1161 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1162 if (delegate_info != (DelegateInfo *) NULL)
1165 Process the image with delegate.
1167 *write_info->filename='\0';
1168 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1169 LockSemaphoreInfo(constitute_semaphore);
1170 status=InvokeDelegate(write_info,image,(char *) NULL,
1171 write_info->magick,exception);
1172 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1173 UnlockSemaphoreInfo(constitute_semaphore);
1174 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1178 sans_exception=AcquireExceptionInfo();
1179 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1180 sans_exception=DestroyExceptionInfo(sans_exception);
1181 if ((write_info->affirm == MagickFalse) &&
1182 (magick_info == (const MagickInfo *) NULL))
1184 (void) CopyMagickString(write_info->magick,image->magick,
1186 magick_info=GetMagickInfo(write_info->magick,exception);
1188 if ((magick_info == (const MagickInfo *) NULL) ||
1189 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1192 extension[MaxTextExtent];
1194 GetPathComponent(image->filename,ExtensionPath,extension);
1195 if (*extension != '\0')
1196 magick_info=GetMagickInfo(extension,exception);
1198 magick_info=GetMagickInfo(image->magick,exception);
1199 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1201 if ((magick_info == (const MagickInfo *) NULL) ||
1202 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1204 magick_info=GetMagickInfo(image->magick,exception);
1205 if ((magick_info == (const MagickInfo *) NULL) ||
1206 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1207 (void) ThrowMagickException(exception,GetMagickModule(),
1208 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1209 "`%s'",image->filename);
1211 (void) ThrowMagickException(exception,GetMagickModule(),
1212 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1213 "`%s'",image->filename);
1215 if ((magick_info != (const MagickInfo *) NULL) &&
1216 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1219 Call appropriate image writer based on image type.
1221 thread_support=GetMagickThreadSupport(magick_info);
1222 if ((thread_support & EncoderThreadSupport) == 0)
1223 LockSemaphoreInfo(constitute_semaphore);
1224 status=GetImageEncoder(magick_info)(write_info,image,exception);
1225 if ((thread_support & EncoderThreadSupport) == 0)
1226 UnlockSemaphoreInfo(constitute_semaphore);
1230 if (GetBlobError(image) != MagickFalse)
1231 ThrowFileException(exception,FileOpenError,
1232 "AnErrorHasOccurredWritingToFile",image->filename);
1233 if (temporary != MagickFalse)
1236 Copy temporary image file to permanent.
1238 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1239 if (status != MagickFalse)
1241 (void) RelinquishUniqueFileResource(write_info->filename);
1242 status=ImageToFile(image,write_info->filename,exception);
1244 (void) CloseBlob(image);
1245 (void) RelinquishUniqueFileResource(image->filename);
1246 (void) CopyMagickString(image->filename,write_info->filename,
1249 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1250 (write_info->verbose != MagickFalse))
1251 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1252 write_info=DestroyImageInfo(write_info);
1257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1261 % W r i t e I m a g e s %
1265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267 % WriteImages() writes an image sequence into one or more files. While
1268 % WriteImage() can write an image sequence, it is limited to writing
1269 % the sequence into a single file using a format which supports multiple
1270 % frames. WriteImages(), however, does not have this limitation, instead it
1271 % generates multiple output files if necessary (or when requested). When
1272 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1273 % to include a printf-style formatting string for the frame number (e.g.
1276 % The format of the WriteImages method is:
1278 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1279 % const char *filename,ExceptionInfo *exception)
1281 % A description of each parameter follows:
1283 % o image_info: the image info.
1285 % o images: the image list.
1287 % o filename: the image filename.
1289 % o exception: return any errors or warnings in this structure.
1292 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1293 Image *images,const char *filename,ExceptionInfo *exception)
1295 #define WriteImageTag "Write/Image"
1312 MagickProgressMonitor
1324 assert(image_info != (const ImageInfo *) NULL);
1325 assert(image_info->signature == MagickSignature);
1326 assert(images != (Image *) NULL);
1327 assert(images->signature == MagickSignature);
1328 if (images->debug != MagickFalse)
1329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1330 assert(exception != (ExceptionInfo *) NULL);
1331 write_info=CloneImageInfo(image_info);
1332 images=GetFirstImageInList(images);
1333 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1334 DestroyBlob(images);
1336 if (filename != (const char *) NULL)
1337 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1338 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1339 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1340 if (*write_info->magick == '\0')
1341 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1342 sans_exception=AcquireExceptionInfo();
1343 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1345 sans_exception=DestroyExceptionInfo(sans_exception);
1347 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1348 if (p->scene >= GetNextImageInList(p)->scene)
1354 Generate consistent scene numbers.
1356 i=(ssize_t) images->scene;
1357 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1358 p->scene=(size_t) i++;
1365 progress_monitor=(MagickProgressMonitor) NULL;
1367 number_images=GetImageListLength(images);
1368 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1370 if (number_images != 1)
1371 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1373 status&=WriteImage(write_info,p,exception);
1374 if (number_images != 1)
1375 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1376 if (write_info->adjoin != MagickFalse)
1378 if (number_images != 1)
1380 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1381 if (proceed == MagickFalse)
1385 write_info=DestroyImageInfo(write_info);
1386 return(status != 0 ? MagickTrue : MagickFalse);