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-2013 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)
644 ThrowFileException(exception,FileOpenError,
645 "AnErrorHasOccurredReadingFromFile",read_info->filename);
646 image=DestroyImageList(image);
647 read_info=DestroyImageInfo(read_info);
648 return((Image *) NULL);
650 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
653 magick_path[MaxTextExtent],
655 timestamp[MaxTextExtent];
663 next->taint=MagickFalse;
664 GetPathComponent(magick_filename,MagickPath,magick_path);
665 if (*magick_path == '\0')
666 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
667 (void) CopyMagickString(next->magick_filename,magick_filename,
669 if (IsBlobTemporary(image) != MagickFalse)
670 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
671 if (next->magick_columns == 0)
672 next->magick_columns=next->columns;
673 if (next->magick_rows == 0)
674 next->magick_rows=next->rows;
675 value=GetImageProperty(next,"tiff:Orientation",exception);
676 if (value == (char *) NULL)
677 value=GetImageProperty(next,"exif:Orientation",exception);
678 if (value != (char *) NULL)
680 next->orientation=(OrientationType) StringToLong(value);
681 (void) DeleteImageProperty(next,"tiff:Orientation");
682 (void) DeleteImageProperty(next,"exif:Orientation");
684 value=GetImageProperty(next,"exif:XResolution",exception);
685 if (value != (char *) NULL)
687 geometry_info.rho=next->resolution.x;
688 geometry_info.sigma=1.0;
689 flags=ParseGeometry(value,&geometry_info);
690 if (geometry_info.sigma != 0)
691 next->resolution.x=geometry_info.rho/geometry_info.sigma;
692 (void) DeleteImageProperty(next,"exif:XResolution");
694 value=GetImageProperty(next,"exif:YResolution",exception);
695 if (value != (char *) NULL)
697 geometry_info.rho=next->resolution.y;
698 geometry_info.sigma=1.0;
699 flags=ParseGeometry(value,&geometry_info);
700 if (geometry_info.sigma != 0)
701 next->resolution.y=geometry_info.rho/geometry_info.sigma;
702 (void) DeleteImageProperty(next,"exif:YResolution");
704 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
705 if (value == (char *) NULL)
706 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
707 if (value != (char *) NULL)
709 next->units=(ResolutionType) (StringToLong(value)-1);
710 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
711 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
713 if (next->page.width == 0)
714 next->page.width=next->columns;
715 if (next->page.height == 0)
716 next->page.height=next->rows;
717 option=GetImageOption(read_info,"caption");
718 if (option != (const char *) NULL)
720 property=InterpretImageProperties(read_info,next,option,exception);
721 (void) SetImageProperty(next,"caption",property,exception);
722 property=DestroyString(property);
724 option=GetImageOption(read_info,"comment");
725 if (option != (const char *) NULL)
727 property=InterpretImageProperties(read_info,next,option,exception);
728 (void) SetImageProperty(next,"comment",property,exception);
729 property=DestroyString(property);
731 option=GetImageOption(read_info,"label");
732 if (option != (const char *) NULL)
734 property=InterpretImageProperties(read_info,next,option,exception);
735 (void) SetImageProperty(next,"label",property,exception);
736 property=DestroyString(property);
738 if (LocaleCompare(next->magick,"TEXT") == 0)
739 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
740 if ((read_info->extract != (char *) NULL) &&
741 (read_info->stream == (StreamHandler) NULL))
746 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
747 if ((next->columns != geometry.width) ||
748 (next->rows != geometry.height))
750 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
755 crop_image=CropImage(next,&geometry,exception);
756 if (crop_image != (Image *) NULL)
757 ReplaceImageInList(&next,crop_image);
760 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
765 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
767 size_image=ResizeImage(next,geometry.width,geometry.height,
768 next->filter,exception);
769 if (size_image != (Image *) NULL)
770 ReplaceImageInList(&next,size_image);
774 profile=GetImageProfile(next,"icc");
775 if (profile == (const StringInfo *) NULL)
776 profile=GetImageProfile(next,"icm");
777 profile=GetImageProfile(next,"iptc");
778 if (profile == (const StringInfo *) NULL)
779 profile=GetImageProfile(next,"8bim");
780 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
782 (void) SetImageProperty(next,"date:modify",timestamp,exception);
783 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
785 (void) SetImageProperty(next,"date:create",timestamp,exception);
786 option=GetImageOption(image_info,"delay");
787 if (option != (const char *) NULL)
792 flags=ParseGeometry(option,&geometry_info);
793 if ((flags & GreaterValue) != 0)
795 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
796 next->delay=(size_t) floor(geometry_info.rho+0.5);
799 if ((flags & LessValue) != 0)
801 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
802 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
805 next->delay=(size_t) floor(geometry_info.rho+0.5);
806 if ((flags & SigmaValue) != 0)
807 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
809 option=GetImageOption(image_info,"dispose");
810 if (option != (const char *) NULL)
811 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
813 if (read_info->verbose != MagickFalse)
814 (void) IdentifyImage(next,stderr,MagickFalse,exception);
817 read_info=DestroyImageInfo(read_info);
818 return(GetFirstImageInList(image));
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % R e a d I m a g e s %
830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832 % ReadImages() reads one or more images and returns them as an image list.
834 % The format of the ReadImage method is:
836 % Image *ReadImages(ImageInfo *image_info,const char *filename,
837 % ExceptionInfo *exception)
839 % A description of each parameter follows:
841 % o image_info: the image info.
843 % o filename: the image filename.
845 % o exception: return any errors or warnings in this structure.
848 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
849 ExceptionInfo *exception)
852 read_filename[MaxTextExtent];
862 Read image list from a file.
864 assert(image_info != (ImageInfo *) NULL);
865 assert(image_info->signature == MagickSignature);
866 if (image_info->debug != MagickFalse)
867 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
868 image_info->filename);
869 assert(exception != (ExceptionInfo *) NULL);
870 (void) SetImageOption(image_info,"filename",filename);
871 (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
872 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,
873 (int) image_info->scene,read_filename,exception);
874 if (LocaleCompare(read_filename,image_info->filename) != 0)
884 Images of the form image-%d.png[1-5].
886 read_info=CloneImageInfo(image_info);
887 sans=AcquireExceptionInfo();
888 (void) SetImageInfo(read_info,0,sans);
889 sans=DestroyExceptionInfo(sans);
890 if (read_info->number_scenes == 0)
892 read_info=DestroyImageInfo(read_info);
893 return(ReadImage(image_info,exception));
895 (void) CopyMagickString(read_filename,read_info->filename,MaxTextExtent);
896 images=NewImageList();
897 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
898 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
900 (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
901 (int) scene,read_info->filename,exception);
902 image=ReadImage(read_info,exception);
903 if (image == (Image *) NULL)
905 AppendImageToList(&images,image);
907 read_info=DestroyImageInfo(read_info);
910 return(ReadImage(image_info,exception));
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 + R e a d I n l i n e I m a g e %
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
925 % The method returns a NULL if there is a memory shortage or if the image
926 % cannot be read. On failure, a NULL image is returned and exception
927 % describes the reason for the failure.
929 % The format of the ReadInlineImage method is:
931 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
932 % ExceptionInfo *exception)
934 % A description of each parameter follows:
936 % o image_info: the image info.
938 % o content: the image encoded in Base64.
940 % o exception: return any errors or warnings in this structure.
943 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
944 const char *content,ExceptionInfo *exception)
962 Skip over header (e.g. data:image/gif;base64,).
964 image=NewImageList();
965 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
967 ThrowReaderException(CorruptImageError,"CorruptImage");
970 blob=Base64Decode(p,&length);
972 ThrowReaderException(CorruptImageError,"CorruptImage");
973 read_info=CloneImageInfo(image_info);
974 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
976 *read_info->filename='\0';
977 *read_info->magick='\0';
978 image=BlobToImage(read_info,blob,length,exception);
979 blob=(unsigned char *) RelinquishMagickMemory(blob);
980 read_info=DestroyImageInfo(read_info);
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 % W r i t e I m a g e %
993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 % WriteImage() writes an image or an image sequence to a file or file handle.
996 % If writing to a file is on disk, the name is defined by the filename member
997 % of the image structure. WriteImage() returns MagickFalse is there is a
998 % memory shortage or if the image cannot be written. Check the exception
999 % member of image to determine the cause for any failure.
1001 % The format of the WriteImage method is:
1003 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
1004 % ExceptionInfo *exception)
1006 % A description of each parameter follows:
1008 % o image_info: the image info.
1010 % o image: the image.
1012 % o exception: return any errors or warnings in this structure.
1015 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1016 Image *image,ExceptionInfo *exception)
1019 filename[MaxTextExtent];
1050 Determine image type from filename prefix or suffix (e.g. image.jpg).
1052 assert(image_info != (ImageInfo *) NULL);
1053 assert(image_info->signature == MagickSignature);
1054 if (image->debug != MagickFalse)
1055 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1056 image_info->filename);
1057 assert(image != (Image *) NULL);
1058 assert(image->signature == MagickSignature);
1059 assert(exception != (ExceptionInfo *) NULL);
1060 sans_exception=AcquireExceptionInfo();
1061 write_info=CloneImageInfo(image_info);
1062 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1063 if (*write_info->magick == '\0')
1064 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1065 (void) SetImageInfo(write_info,1,sans_exception);
1066 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1067 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1068 domain=CoderPolicyDomain;
1069 rights=WritePolicyRights;
1070 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1072 sans_exception=DestroyExceptionInfo(sans_exception);
1073 write_info=DestroyImageInfo(write_info);
1075 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1078 Call appropriate image reader based on image type.
1080 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1081 sans_exception=DestroyExceptionInfo(sans_exception);
1082 if (magick_info != (const MagickInfo *) NULL)
1084 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1085 image->endian=UndefinedEndian;
1087 if ((image_info->endian == UndefinedEndian) &&
1088 (GetMagickRawSupport(magick_info) != MagickFalse))
1094 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1097 if (IsImageGray(image,exception) == MagickFalse)
1100 sRGB masquerading as a grayscale image?
1102 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1103 (void) SetImageColorspace(image,sRGBColorspace,exception);
1106 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1107 (void) SetImageColorspace(image,GRAYColorspace,exception);
1108 (void) SyncImageProfiles(image);
1109 option=GetImageOption(image_info,"delegate:bimodal");
1110 if ((IfMagickTrue(IsStringTrue(option))) &&
1111 (write_info->page == (char *) NULL) &&
1112 (GetPreviousImageInList(image) == (Image *) NULL) &&
1113 (GetNextImageInList(image) == (Image *) NULL) &&
1114 (IfMagickFalse(IsTaintImage(image))) )
1116 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1117 if ((delegate_info != (const DelegateInfo *) NULL) &&
1118 (GetDelegateMode(delegate_info) == 0) &&
1119 (IsPathAccessible(image->magick_filename) != MagickFalse))
1122 Process image with bi-modal delegate.
1124 (void) CopyMagickString(image->filename,image->magick_filename,
1126 status=InvokeDelegate(write_info,image,image->magick,
1127 write_info->magick,exception);
1128 write_info=DestroyImageInfo(write_info);
1129 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1134 temporary=MagickFalse;
1135 if ((magick_info != (const MagickInfo *) NULL) &&
1136 (GetMagickSeekableStream(magick_info) != MagickFalse))
1139 filename[MaxTextExtent];
1141 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1142 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1143 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1144 if (status != MagickFalse)
1146 if (IsBlobSeekable(image) == MagickFalse)
1149 A seekable stream is required by the encoder.
1151 write_info->adjoin=MagickTrue;
1152 (void) CopyMagickString(write_info->filename,image->filename,
1154 (void) AcquireUniqueFilename(image->filename);
1155 temporary=MagickTrue;
1157 (void) CloseBlob(image);
1160 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1161 AcquireSemaphoreInfo(&constitute_semaphore);
1162 if ((magick_info != (const MagickInfo *) NULL) &&
1163 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1166 Call appropriate image writer based on image type.
1168 thread_support=GetMagickThreadSupport(magick_info);
1169 if ((thread_support & EncoderThreadSupport) == 0)
1170 LockSemaphoreInfo(constitute_semaphore);
1171 status=GetImageEncoder(magick_info)(write_info,image,exception);
1172 if ((thread_support & EncoderThreadSupport) == 0)
1173 UnlockSemaphoreInfo(constitute_semaphore);
1177 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1178 if (delegate_info != (DelegateInfo *) NULL)
1181 Process the image with delegate.
1183 *write_info->filename='\0';
1184 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1185 LockSemaphoreInfo(constitute_semaphore);
1186 status=InvokeDelegate(write_info,image,(char *) NULL,
1187 write_info->magick,exception);
1188 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1189 UnlockSemaphoreInfo(constitute_semaphore);
1190 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1194 sans_exception=AcquireExceptionInfo();
1195 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1196 sans_exception=DestroyExceptionInfo(sans_exception);
1197 if ((write_info->affirm == MagickFalse) &&
1198 (magick_info == (const MagickInfo *) NULL))
1200 (void) CopyMagickString(write_info->magick,image->magick,
1202 magick_info=GetMagickInfo(write_info->magick,exception);
1204 if ((magick_info == (const MagickInfo *) NULL) ||
1205 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1208 extension[MaxTextExtent];
1210 GetPathComponent(image->filename,ExtensionPath,extension);
1211 if (*extension != '\0')
1212 magick_info=GetMagickInfo(extension,exception);
1214 magick_info=GetMagickInfo(image->magick,exception);
1215 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1217 if ((magick_info == (const MagickInfo *) NULL) ||
1218 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1220 magick_info=GetMagickInfo(image->magick,exception);
1221 if ((magick_info == (const MagickInfo *) NULL) ||
1222 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1223 (void) ThrowMagickException(exception,GetMagickModule(),
1224 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1225 "`%s'",image->filename);
1227 (void) ThrowMagickException(exception,GetMagickModule(),
1228 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1229 "`%s'",image->filename);
1231 if ((magick_info != (const MagickInfo *) NULL) &&
1232 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1235 Call appropriate image writer based on image type.
1237 thread_support=GetMagickThreadSupport(magick_info);
1238 if ((thread_support & EncoderThreadSupport) == 0)
1239 LockSemaphoreInfo(constitute_semaphore);
1240 status=GetImageEncoder(magick_info)(write_info,image,exception);
1241 if ((thread_support & EncoderThreadSupport) == 0)
1242 UnlockSemaphoreInfo(constitute_semaphore);
1246 if (GetBlobError(image) != MagickFalse)
1247 ThrowFileException(exception,FileOpenError,
1248 "AnErrorHasOccurredWritingToFile",image->filename);
1249 if (temporary == MagickTrue)
1252 Copy temporary image file to permanent.
1254 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1255 if (status != MagickFalse)
1257 (void) RelinquishUniqueFileResource(write_info->filename);
1258 status=ImageToFile(image,write_info->filename,exception);
1260 (void) CloseBlob(image);
1261 (void) RelinquishUniqueFileResource(image->filename);
1262 (void) CopyMagickString(image->filename,write_info->filename,
1265 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1266 (write_info->verbose != MagickFalse))
1267 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1268 write_info=DestroyImageInfo(write_info);
1273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1277 % W r i t e I m a g e s %
1281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1283 % WriteImages() writes an image sequence into one or more files. While
1284 % WriteImage() can write an image sequence, it is limited to writing
1285 % the sequence into a single file using a format which supports multiple
1286 % frames. WriteImages(), however, does not have this limitation, instead it
1287 % generates multiple output files if necessary (or when requested). When
1288 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1289 % to include a printf-style formatting string for the frame number (e.g.
1292 % The format of the WriteImages method is:
1294 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1295 % const char *filename,ExceptionInfo *exception)
1297 % A description of each parameter follows:
1299 % o image_info: the image info.
1301 % o images: the image list.
1303 % o filename: the image filename.
1305 % o exception: return any errors or warnings in this structure.
1308 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1309 Image *images,const char *filename,ExceptionInfo *exception)
1311 #define WriteImageTag "Write/Image"
1328 MagickProgressMonitor
1340 assert(image_info != (const ImageInfo *) NULL);
1341 assert(image_info->signature == MagickSignature);
1342 assert(images != (Image *) NULL);
1343 assert(images->signature == MagickSignature);
1344 if (images->debug != MagickFalse)
1345 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1346 assert(exception != (ExceptionInfo *) NULL);
1347 write_info=CloneImageInfo(image_info);
1348 images=GetFirstImageInList(images);
1349 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1350 DestroyBlob(images);
1352 if (filename != (const char *) NULL)
1353 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1354 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1355 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1356 if (*write_info->magick == '\0')
1357 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1358 sans_exception=AcquireExceptionInfo();
1359 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1361 sans_exception=DestroyExceptionInfo(sans_exception);
1363 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1364 if (p->scene >= GetNextImageInList(p)->scene)
1370 Generate consistent scene numbers.
1372 i=(ssize_t) images->scene;
1373 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1374 p->scene=(size_t) i++;
1381 progress_monitor=(MagickProgressMonitor) NULL;
1383 number_images=GetImageListLength(images);
1384 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1386 if (number_images != 1)
1387 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1389 status&=WriteImage(write_info,p,exception);
1390 if (number_images != 1)
1391 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1392 if (write_info->adjoin != MagickFalse)
1394 if (number_images != 1)
1396 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1397 if (proceed == MagickFalse)
1401 write_info=DestroyImageInfo(write_info);
1402 return(status != 0 ? MagickTrue : MagickFalse);