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 read_info=DestroyImageInfo(read_info);
463 return((Image *) NULL);
466 Call appropriate image reader based on image type.
468 sans_exception=AcquireExceptionInfo();
469 magick_info=GetMagickInfo(read_info->magick,sans_exception);
470 sans_exception=DestroyExceptionInfo(sans_exception);
471 if (magick_info != (const MagickInfo *) NULL)
473 if (GetMagickEndianSupport(magick_info) == MagickFalse)
474 read_info->endian=UndefinedEndian;
476 if ((image_info->endian == UndefinedEndian) &&
477 (GetMagickRawSupport(magick_info) != MagickFalse))
483 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
487 if ((magick_info != (const MagickInfo *) NULL) &&
488 (GetMagickSeekableStream(magick_info) != MagickFalse))
493 image=AcquireImage(read_info,exception);
494 (void) CopyMagickString(image->filename,read_info->filename,
496 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
497 if (status == MagickFalse)
499 read_info=DestroyImageInfo(read_info);
500 image=DestroyImage(image);
501 return((Image *) NULL);
503 if (IsBlobSeekable(image) == MagickFalse)
506 Coder requires a seekable stream.
508 *read_info->filename='\0';
509 status=ImageToFile(image,read_info->filename,exception);
510 if (status == MagickFalse)
512 (void) CloseBlob(image);
513 read_info=DestroyImageInfo(read_info);
514 image=DestroyImage(image);
515 return((Image *) NULL);
517 read_info->temporary=MagickTrue;
519 (void) CloseBlob(image);
520 image=DestroyImage(image);
522 image=NewImageList();
523 if (constitute_semaphore == (SemaphoreInfo *) NULL)
524 AcquireSemaphoreInfo(&constitute_semaphore);
525 if ((magick_info == (const MagickInfo *) NULL) ||
526 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
528 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
529 if (delegate_info == (const DelegateInfo *) NULL)
531 (void) SetImageInfo(read_info,0,exception);
532 (void) CopyMagickString(read_info->filename,filename,MaxTextExtent);
533 magick_info=GetMagickInfo(read_info->magick,exception);
536 if ((magick_info != (const MagickInfo *) NULL) &&
537 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
539 thread_support=GetMagickThreadSupport(magick_info);
540 if ((thread_support & DecoderThreadSupport) == 0)
541 LockSemaphoreInfo(constitute_semaphore);
542 image=GetImageDecoder(magick_info)(read_info,exception);
543 if ((thread_support & DecoderThreadSupport) == 0)
544 UnlockSemaphoreInfo(constitute_semaphore);
548 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
549 if (delegate_info == (const DelegateInfo *) NULL)
551 (void) ThrowMagickException(exception,GetMagickModule(),
552 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
553 read_info->filename);
554 if (read_info->temporary != MagickFalse)
555 (void) RelinquishUniqueFileResource(read_info->filename);
556 read_info=DestroyImageInfo(read_info);
557 return((Image *) NULL);
560 Let our decoding delegate process the image.
562 image=AcquireImage(read_info,exception);
563 if (image == (Image *) NULL)
565 read_info=DestroyImageInfo(read_info);
566 return((Image *) NULL);
568 (void) CopyMagickString(image->filename,read_info->filename,
570 *read_info->filename='\0';
571 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
572 LockSemaphoreInfo(constitute_semaphore);
573 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
575 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
576 UnlockSemaphoreInfo(constitute_semaphore);
577 image=DestroyImageList(image);
578 read_info->temporary=MagickTrue;
579 (void) SetImageInfo(read_info,0,exception);
580 magick_info=GetMagickInfo(read_info->magick,exception);
581 if ((magick_info == (const MagickInfo *) NULL) ||
582 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
584 if (IsPathAccessible(read_info->filename) != MagickFalse)
585 (void) ThrowMagickException(exception,GetMagickModule(),
586 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
587 read_info->filename);
589 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
590 read_info->filename);
591 read_info=DestroyImageInfo(read_info);
592 return((Image *) NULL);
594 thread_support=GetMagickThreadSupport(magick_info);
595 if ((thread_support & DecoderThreadSupport) == 0)
596 LockSemaphoreInfo(constitute_semaphore);
597 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
598 if ((thread_support & DecoderThreadSupport) == 0)
599 UnlockSemaphoreInfo(constitute_semaphore);
601 if (read_info->temporary != MagickFalse)
603 (void) RelinquishUniqueFileResource(read_info->filename);
604 read_info->temporary=MagickFalse;
605 if (image != (Image *) NULL)
606 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
608 if (image == (Image *) NULL)
610 read_info=DestroyImageInfo(read_info);
613 if (exception->severity >= ErrorException)
614 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
615 "Coder (%s) generated an image despite an error (%d), "
616 "notify the developers",image->magick,exception->severity);
617 if (IsBlobTemporary(image) != MagickFalse)
618 (void) RelinquishUniqueFileResource(read_info->filename);
619 if ((GetNextImageInList(image) != (Image *) NULL) &&
620 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
625 clones=CloneImages(image,read_info->scenes,exception);
626 if (clones == (Image *) NULL)
627 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
628 "SubimageSpecificationReturnsNoImages","'%s'",read_info->filename);
631 image=DestroyImageList(image);
632 image=GetFirstImageInList(clones);
635 if (GetBlobError(image) != MagickFalse)
637 ThrowFileException(exception,FileOpenError,
638 "AnErrorHasOccurredReadingFromFile",read_info->filename);
639 image=DestroyImageList(image);
640 read_info=DestroyImageInfo(read_info);
641 return((Image *) NULL);
643 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
646 magick_path[MaxTextExtent],
648 timestamp[MaxTextExtent];
656 next->taint=MagickFalse;
657 GetPathComponent(magick_filename,MagickPath,magick_path);
658 if (*magick_path == '\0')
659 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
660 (void) CopyMagickString(next->magick_filename,magick_filename,
662 if (IsBlobTemporary(image) != MagickFalse)
663 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
664 if (next->magick_columns == 0)
665 next->magick_columns=next->columns;
666 if (next->magick_rows == 0)
667 next->magick_rows=next->rows;
668 value=GetImageProperty(next,"tiff:Orientation",exception);
669 if (value == (char *) NULL)
670 value=GetImageProperty(next,"exif:Orientation",exception);
671 if (value != (char *) NULL)
673 next->orientation=(OrientationType) StringToLong(value);
674 (void) DeleteImageProperty(next,"tiff:Orientation");
675 (void) DeleteImageProperty(next,"exif:Orientation");
677 value=GetImageProperty(next,"exif:XResolution",exception);
678 if (value != (char *) NULL)
680 geometry_info.rho=next->resolution.x;
681 geometry_info.sigma=1.0;
682 flags=ParseGeometry(value,&geometry_info);
683 if (geometry_info.sigma != 0)
684 next->resolution.x=geometry_info.rho/geometry_info.sigma;
685 (void) DeleteImageProperty(next,"exif:XResolution");
687 value=GetImageProperty(next,"exif:YResolution",exception);
688 if (value != (char *) NULL)
690 geometry_info.rho=next->resolution.y;
691 geometry_info.sigma=1.0;
692 flags=ParseGeometry(value,&geometry_info);
693 if (geometry_info.sigma != 0)
694 next->resolution.y=geometry_info.rho/geometry_info.sigma;
695 (void) DeleteImageProperty(next,"exif:YResolution");
697 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
698 if (value == (char *) NULL)
699 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
700 if (value != (char *) NULL)
702 next->units=(ResolutionType) (StringToLong(value)-1);
703 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
704 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
706 if (next->page.width == 0)
707 next->page.width=next->columns;
708 if (next->page.height == 0)
709 next->page.height=next->rows;
710 option=GetImageOption(read_info,"caption");
711 if (option != (const char *) NULL)
713 property=InterpretImageProperties(read_info,next,option,exception);
714 (void) SetImageProperty(next,"caption",property,exception);
715 property=DestroyString(property);
717 option=GetImageOption(read_info,"comment");
718 if (option != (const char *) NULL)
720 property=InterpretImageProperties(read_info,next,option,exception);
721 (void) SetImageProperty(next,"comment",property,exception);
722 property=DestroyString(property);
724 option=GetImageOption(read_info,"label");
725 if (option != (const char *) NULL)
727 property=InterpretImageProperties(read_info,next,option,exception);
728 (void) SetImageProperty(next,"label",property,exception);
729 property=DestroyString(property);
731 if (LocaleCompare(next->magick,"TEXT") == 0)
732 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
733 if ((read_info->extract != (char *) NULL) &&
734 (read_info->stream == (StreamHandler) NULL))
739 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
740 if ((next->columns != geometry.width) ||
741 (next->rows != geometry.height))
743 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
748 crop_image=CropImage(next,&geometry,exception);
749 if (crop_image != (Image *) NULL)
750 ReplaceImageInList(&next,crop_image);
753 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
758 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
760 size_image=ResizeImage(next,geometry.width,geometry.height,
761 next->filter,exception);
762 if (size_image != (Image *) NULL)
763 ReplaceImageInList(&next,size_image);
767 profile=GetImageProfile(next,"icc");
768 if (profile == (const StringInfo *) NULL)
769 profile=GetImageProfile(next,"icm");
770 profile=GetImageProfile(next,"iptc");
771 if (profile == (const StringInfo *) NULL)
772 profile=GetImageProfile(next,"8bim");
773 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
775 (void) SetImageProperty(next,"date:modify",timestamp,exception);
776 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
778 (void) SetImageProperty(next,"date:create",timestamp,exception);
779 option=GetImageOption(image_info,"delay");
780 if (option != (const char *) NULL)
785 flags=ParseGeometry(option,&geometry_info);
786 if ((flags & GreaterValue) != 0)
788 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
789 next->delay=(size_t) floor(geometry_info.rho+0.5);
792 if ((flags & LessValue) != 0)
794 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
795 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
798 next->delay=(size_t) floor(geometry_info.rho+0.5);
799 if ((flags & SigmaValue) != 0)
800 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
802 option=GetImageOption(image_info,"dispose");
803 if (option != (const char *) NULL)
804 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
806 if (read_info->verbose != MagickFalse)
807 (void) IdentifyImage(next,stderr,MagickFalse,exception);
810 read_info=DestroyImageInfo(read_info);
811 return(GetFirstImageInList(image));
815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819 % R e a d I m a g e s %
823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 % ReadImages() reads one or more images and returns them as an image list.
827 % The format of the ReadImage method is:
829 % Image *ReadImages(ImageInfo *image_info,const char *filename,
830 % ExceptionInfo *exception)
832 % A description of each parameter follows:
834 % o image_info: the image info.
836 % o filename: the image filename.
838 % o exception: return any errors or warnings in this structure.
841 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
842 ExceptionInfo *exception)
845 read_filename[MaxTextExtent];
855 Read image list from a file.
857 assert(image_info != (ImageInfo *) NULL);
858 assert(image_info->signature == MagickSignature);
859 if (image_info->debug != MagickFalse)
860 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
861 image_info->filename);
862 assert(exception != (ExceptionInfo *) NULL);
863 (void) SetImageOption(image_info,"filename",filename);
864 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
865 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,
866 (int) image_info->scene,read_filename,exception);
867 if (LocaleCompare(read_filename,image_info->filename) != 0)
877 Images of the form image-%d.png[1-5].
879 read_info=CloneImageInfo(image_info);
880 sans=AcquireExceptionInfo();
881 (void) SetImageInfo(read_info,0,sans);
882 sans=DestroyExceptionInfo(sans);
883 if (read_info->number_scenes == 0)
885 read_info=DestroyImageInfo(read_info);
886 return(ReadImage(image_info,exception));
888 (void) CopyMagickString(read_filename,read_info->filename,MaxTextExtent);
889 images=NewImageList();
890 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
891 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
893 (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
894 (int) scene,read_info->filename,exception);
895 image=ReadImage(read_info,exception);
896 if (image == (Image *) NULL)
898 AppendImageToList(&images,image);
900 read_info=DestroyImageInfo(read_info);
903 return(ReadImage(image_info,exception));
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 + R e a d I n l i n e I m a g e %
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
918 % The method returns a NULL if there is a memory shortage or if the image
919 % cannot be read. On failure, a NULL image is returned and exception
920 % describes the reason for the failure.
922 % The format of the ReadInlineImage method is:
924 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
925 % ExceptionInfo *exception)
927 % A description of each parameter follows:
929 % o image_info: the image info.
931 % o content: the image encoded in Base64.
933 % o exception: return any errors or warnings in this structure.
936 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
937 const char *content,ExceptionInfo *exception)
955 Skip over header (e.g. data:image/gif;base64,).
957 image=NewImageList();
958 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
960 ThrowReaderException(CorruptImageError,"CorruptImage");
963 blob=Base64Decode(p,&length);
965 ThrowReaderException(CorruptImageError,"CorruptImage");
966 read_info=CloneImageInfo(image_info);
967 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
969 *read_info->filename='\0';
970 *read_info->magick='\0';
971 image=BlobToImage(read_info,blob,length,exception);
972 blob=(unsigned char *) RelinquishMagickMemory(blob);
973 read_info=DestroyImageInfo(read_info);
978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 % W r i t e I m a g e %
986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 % WriteImage() writes an image or an image sequence to a file or file handle.
989 % If writing to a file is on disk, the name is defined by the filename member
990 % of the image structure. WriteImage() returns MagickFalse is there is a
991 % memory shortage or if the image cannot be written. Check the exception
992 % member of image to determine the cause for any failure.
994 % The format of the WriteImage method is:
996 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
997 % ExceptionInfo *exception)
999 % A description of each parameter follows:
1001 % o image_info: the image info.
1003 % o image: the image.
1005 % o exception: return any errors or warnings in this structure.
1008 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1009 Image *image,ExceptionInfo *exception)
1012 filename[MaxTextExtent];
1043 Determine image type from filename prefix or suffix (e.g. image.jpg).
1045 assert(image_info != (ImageInfo *) NULL);
1046 assert(image_info->signature == MagickSignature);
1047 if (image->debug != MagickFalse)
1048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1049 image_info->filename);
1050 assert(image != (Image *) NULL);
1051 assert(image->signature == MagickSignature);
1052 assert(exception != (ExceptionInfo *) NULL);
1053 sans_exception=AcquireExceptionInfo();
1054 write_info=CloneImageInfo(image_info);
1055 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1056 if (*write_info->magick == '\0')
1057 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1058 (void) SetImageInfo(write_info,1,sans_exception);
1059 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1060 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1061 domain=CoderPolicyDomain;
1062 rights=WritePolicyRights;
1063 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1065 sans_exception=DestroyExceptionInfo(sans_exception);
1066 write_info=DestroyImageInfo(write_info);
1068 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1071 Call appropriate image reader based on image type.
1073 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1074 sans_exception=DestroyExceptionInfo(sans_exception);
1075 if (magick_info != (const MagickInfo *) NULL)
1077 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1078 image->endian=UndefinedEndian;
1080 if ((image_info->endian == UndefinedEndian) &&
1081 (GetMagickRawSupport(magick_info) != MagickFalse))
1087 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1090 (void) SyncImageProfiles(image);
1091 option=GetImageOption(image_info,"delegate:bimodal");
1092 if ((IfMagickTrue(IsStringTrue(option))) &&
1093 (write_info->page == (char *) NULL) &&
1094 (GetPreviousImageInList(image) == (Image *) NULL) &&
1095 (GetNextImageInList(image) == (Image *) NULL) &&
1096 (IfMagickFalse(IsTaintImage(image))) )
1098 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1099 if ((delegate_info != (const DelegateInfo *) NULL) &&
1100 (GetDelegateMode(delegate_info) == 0) &&
1101 (IsPathAccessible(image->magick_filename) != MagickFalse))
1104 Process image with bi-modal delegate.
1106 (void) CopyMagickString(image->filename,image->magick_filename,
1108 status=InvokeDelegate(write_info,image,image->magick,
1109 write_info->magick,exception);
1110 write_info=DestroyImageInfo(write_info);
1111 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1116 temporary=MagickFalse;
1117 if ((magick_info != (const MagickInfo *) NULL) &&
1118 (GetMagickSeekableStream(magick_info) != MagickFalse))
1121 filename[MaxTextExtent];
1123 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1124 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1125 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1126 if (status != MagickFalse)
1128 if (IsBlobSeekable(image) == MagickFalse)
1131 A seekable stream is required by the encoder.
1133 write_info->adjoin=MagickTrue;
1134 (void) CopyMagickString(write_info->filename,image->filename,
1136 (void) AcquireUniqueFilename(image->filename);
1137 temporary=MagickTrue;
1139 (void) CloseBlob(image);
1142 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1143 AcquireSemaphoreInfo(&constitute_semaphore);
1144 if ((magick_info != (const MagickInfo *) NULL) &&
1145 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1148 Call appropriate image writer based on image type.
1150 thread_support=GetMagickThreadSupport(magick_info);
1151 if ((thread_support & EncoderThreadSupport) == 0)
1152 LockSemaphoreInfo(constitute_semaphore);
1153 status=GetImageEncoder(magick_info)(write_info,image,exception);
1154 if ((thread_support & EncoderThreadSupport) == 0)
1155 UnlockSemaphoreInfo(constitute_semaphore);
1159 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1160 if (delegate_info != (DelegateInfo *) NULL)
1163 Process the image with delegate.
1165 *write_info->filename='\0';
1166 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1167 LockSemaphoreInfo(constitute_semaphore);
1168 status=InvokeDelegate(write_info,image,(char *) NULL,
1169 write_info->magick,exception);
1170 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1171 UnlockSemaphoreInfo(constitute_semaphore);
1172 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1176 sans_exception=AcquireExceptionInfo();
1177 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1178 sans_exception=DestroyExceptionInfo(sans_exception);
1179 if ((write_info->affirm == MagickFalse) &&
1180 (magick_info == (const MagickInfo *) NULL))
1182 (void) CopyMagickString(write_info->magick,image->magick,
1184 magick_info=GetMagickInfo(write_info->magick,exception);
1186 if ((magick_info == (const MagickInfo *) NULL) ||
1187 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1190 extension[MaxTextExtent];
1192 GetPathComponent(image->filename,ExtensionPath,extension);
1193 if (*extension != '\0')
1194 magick_info=GetMagickInfo(extension,exception);
1196 magick_info=GetMagickInfo(image->magick,exception);
1197 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1199 if ((magick_info == (const MagickInfo *) NULL) ||
1200 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1202 magick_info=GetMagickInfo(image->magick,exception);
1203 if ((magick_info == (const MagickInfo *) NULL) ||
1204 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1205 (void) ThrowMagickException(exception,GetMagickModule(),
1206 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1207 "'%s'",image->filename);
1209 (void) ThrowMagickException(exception,GetMagickModule(),
1210 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1211 "'%s'",image->filename);
1213 if ((magick_info != (const MagickInfo *) NULL) &&
1214 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1217 Call appropriate image writer based on image type.
1219 thread_support=GetMagickThreadSupport(magick_info);
1220 if ((thread_support & EncoderThreadSupport) == 0)
1221 LockSemaphoreInfo(constitute_semaphore);
1222 status=GetImageEncoder(magick_info)(write_info,image,exception);
1223 if ((thread_support & EncoderThreadSupport) == 0)
1224 UnlockSemaphoreInfo(constitute_semaphore);
1228 if (GetBlobError(image) != MagickFalse)
1229 ThrowFileException(exception,FileOpenError,
1230 "AnErrorHasOccurredWritingToFile",image->filename);
1231 if (temporary == MagickTrue)
1234 Copy temporary image file to permanent.
1236 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1237 if (status != MagickFalse)
1239 (void) RelinquishUniqueFileResource(write_info->filename);
1240 status=ImageToFile(image,write_info->filename,exception);
1242 (void) CloseBlob(image);
1243 (void) RelinquishUniqueFileResource(image->filename);
1244 (void) CopyMagickString(image->filename,write_info->filename,
1247 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1248 (write_info->verbose != MagickFalse))
1249 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1250 write_info=DestroyImageInfo(write_info);
1255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 % W r i t e I m a g e s %
1263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1265 % WriteImages() writes an image sequence into one or more files. While
1266 % WriteImage() can write an image sequence, it is limited to writing
1267 % the sequence into a single file using a format which supports multiple
1268 % frames. WriteImages(), however, does not have this limitation, instead it
1269 % generates multiple output files if necessary (or when requested). When
1270 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1271 % to include a printf-style formatting string for the frame number (e.g.
1274 % The format of the WriteImages method is:
1276 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1277 % const char *filename,ExceptionInfo *exception)
1279 % A description of each parameter follows:
1281 % o image_info: the image info.
1283 % o images: the image list.
1285 % o filename: the image filename.
1287 % o exception: return any errors or warnings in this structure.
1290 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1291 Image *images,const char *filename,ExceptionInfo *exception)
1293 #define WriteImageTag "Write/Image"
1310 MagickProgressMonitor
1322 assert(image_info != (const ImageInfo *) NULL);
1323 assert(image_info->signature == MagickSignature);
1324 assert(images != (Image *) NULL);
1325 assert(images->signature == MagickSignature);
1326 if (images->debug != MagickFalse)
1327 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1328 assert(exception != (ExceptionInfo *) NULL);
1329 write_info=CloneImageInfo(image_info);
1330 images=GetFirstImageInList(images);
1331 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1332 DestroyBlob(images);
1334 if (filename != (const char *) NULL)
1335 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1336 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1337 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1338 if (*write_info->magick == '\0')
1339 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1340 sans_exception=AcquireExceptionInfo();
1341 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1343 sans_exception=DestroyExceptionInfo(sans_exception);
1345 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1346 if (p->scene >= GetNextImageInList(p)->scene)
1352 Generate consistent scene numbers.
1354 i=(ssize_t) images->scene;
1355 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1356 p->scene=(size_t) i++;
1363 progress_monitor=(MagickProgressMonitor) NULL;
1365 number_images=GetImageListLength(images);
1366 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1368 if (number_images != 1)
1369 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1371 status&=WriteImage(write_info,p,exception);
1372 if (number_images != 1)
1373 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1374 if (write_info->adjoin != MagickFalse)
1376 if (number_images != 1)
1378 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1379 if (proceed == MagickFalse)
1383 write_info=DestroyImageInfo(write_info);
1384 return(status != 0 ? MagickTrue : MagickFalse);