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-2017 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % C o n s t i t u t e I m a g e %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % ConstituteImage() returns an image from the pixel data you supply.
95 % The pixel data must be in scanline order top-to-bottom. The data can be
96 % char, short int, int, float, or double. Float and double require the
97 % pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
98 % create a 640x480 image from unsigned red-green-blue character data, use:
100 % image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
102 % The format of the ConstituteImage method is:
104 % Image *ConstituteImage(const size_t columns,const size_t rows,
105 % const char *map,const StorageType storage,const void *pixels,
106 % ExceptionInfo *exception)
108 % A description of each parameter follows:
110 % o columns: width in pixels of the image.
112 % o rows: height in pixels of the image.
114 % o map: This string reflects the expected ordering of the pixel array.
115 % It can be any combination or order of R = red, G = green, B = blue,
116 % A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
117 % Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
120 % o storage: Define the data type of the pixels. Float and double types are
121 % expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
122 % from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
123 % LongPixel, QuantumPixel, or ShortPixel.
125 % o pixels: This array of values contain the pixel components as defined by
126 % map and type. You must preallocate this array where the expected
127 % length varies depending on the values of width, height, map, and type.
129 % o exception: return any errors or warnings in this structure.
132 MagickExport Image *ConstituteImage(const size_t columns,const size_t rows,
133 const char *map,const StorageType storage,const void *pixels,
134 ExceptionInfo *exception)
143 Allocate image structure.
145 assert(map != (const char *) NULL);
146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
147 assert(pixels != (void *) NULL);
148 assert(exception != (ExceptionInfo *) NULL);
149 assert(exception->signature == MagickCoreSignature);
150 image=AcquireImage((ImageInfo *) NULL,exception);
151 if (image == (Image *) NULL)
152 return((Image *) NULL);
153 if ((columns == 0) || (rows == 0))
154 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
155 image->columns=columns;
157 (void) SetImageBackgroundColor(image,exception);
158 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
159 if (status == MagickFalse)
160 image=DestroyImage(image);
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 % P i n g I m a g e %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 % PingImage() returns all the properties of an image or image sequence
176 % except for the pixels. It is much faster and consumes far less memory
177 % than ReadImage(). On failure, a NULL image is returned and exception
178 % describes the reason for the failure.
180 % The format of the PingImage method is:
182 % Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
184 % A description of each parameter follows:
186 % o image_info: Ping the image defined by the file or filename members of
189 % o exception: return any errors or warnings in this structure.
193 #if defined(__cplusplus) || defined(c_plusplus)
197 static size_t PingStream(const Image *magick_unused(image),
198 const void *magick_unused(pixels),const size_t columns)
200 magick_unreferenced(image);
201 magick_unreferenced(pixels);
205 #if defined(__cplusplus) || defined(c_plusplus)
209 MagickExport Image *PingImage(const ImageInfo *image_info,
210 ExceptionInfo *exception)
218 assert(image_info != (ImageInfo *) NULL);
219 assert(image_info->signature == MagickCoreSignature);
220 if (image_info->debug != MagickFalse)
221 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
222 image_info->filename);
223 assert(exception != (ExceptionInfo *) NULL);
224 ping_info=CloneImageInfo(image_info);
225 ping_info->ping=MagickTrue;
226 image=ReadStream(ping_info,&PingStream,exception);
227 if (image != (Image *) NULL)
229 ResetTimer(&image->timer);
230 if (ping_info->verbose != MagickFalse)
231 (void) IdentifyImage(image,stdout,MagickFalse,exception);
233 ping_info=DestroyImageInfo(ping_info);
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % P i n g I m a g e s %
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 % PingImages() pings one or more images and returns them as an image list.
250 % The format of the PingImage method is:
252 % Image *PingImages(ImageInfo *image_info,const char *filename,
253 % ExceptionInfo *exception)
255 % A description of each parameter follows:
257 % o image_info: the image info.
259 % o filename: the image filename.
261 % o exception: return any errors or warnings in this structure.
264 MagickExport Image *PingImages(ImageInfo *image_info,const char *filename,
265 ExceptionInfo *exception)
268 ping_filename[MagickPathExtent];
278 Ping image list from a file.
280 assert(image_info != (ImageInfo *) NULL);
281 assert(image_info->signature == MagickCoreSignature);
282 if (image_info->debug != MagickFalse)
283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
284 image_info->filename);
285 assert(exception != (ExceptionInfo *) NULL);
286 (void) SetImageOption(image_info,"filename",filename);
287 (void) CopyMagickString(image_info->filename,filename,MagickPathExtent);
288 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
289 (int) image_info->scene,ping_filename,exception);
290 if (LocaleCompare(ping_filename,image_info->filename) != 0)
300 Images of the form image-%d.png[1-5].
302 read_info=CloneImageInfo(image_info);
303 sans=AcquireExceptionInfo();
304 (void) SetImageInfo(read_info,0,sans);
305 sans=DestroyExceptionInfo(sans);
306 if (read_info->number_scenes == 0)
308 read_info=DestroyImageInfo(read_info);
309 return(PingImage(image_info,exception));
311 (void) CopyMagickString(ping_filename,read_info->filename,
313 images=NewImageList();
314 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
315 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
317 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
318 (int) scene,read_info->filename,exception);
319 image=PingImage(read_info,exception);
320 if (image == (Image *) NULL)
322 AppendImageToList(&images,image);
324 read_info=DestroyImageInfo(read_info);
327 return(PingImage(image_info,exception));
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % R e a d I m a g e %
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 % ReadImage() reads an image or image sequence from a file or file handle.
342 % The method returns a NULL if there is a memory shortage or if the image
343 % cannot be read. On failure, a NULL image is returned and exception
344 % describes the reason for the failure.
346 % The format of the ReadImage method is:
348 % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
350 % A description of each parameter follows:
352 % o image_info: Read the image defined by the file or filename members of
355 % o exception: return any errors or warnings in this structure.
358 MagickExport Image *ReadImage(const ImageInfo *image_info,
359 ExceptionInfo *exception)
362 filename[MagickPathExtent],
363 magick[MagickPathExtent],
364 magick_filename[MagickPathExtent];
398 Determine image type from filename prefix or suffix (e.g. image.jpg).
400 assert(image_info != (ImageInfo *) NULL);
401 assert(image_info->signature == MagickCoreSignature);
402 assert(image_info->filename != (char *) NULL);
403 if (image_info->debug != MagickFalse)
404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
405 image_info->filename);
406 assert(exception != (ExceptionInfo *) NULL);
407 read_info=CloneImageInfo(image_info);
408 (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
409 (void) SetImageInfo(read_info,0,exception);
410 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
411 (void) CopyMagickString(magick,read_info->magick,MagickPathExtent);
412 domain=CoderPolicyDomain;
413 rights=ReadPolicyRights;
414 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
417 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
418 "NotAuthorized","`%s'",read_info->filename);
419 read_info=DestroyImageInfo(read_info);
420 return((Image *) NULL);
423 Call appropriate image reader based on image type.
425 sans_exception=AcquireExceptionInfo();
426 magick_info=GetMagickInfo(read_info->magick,sans_exception);
427 sans_exception=DestroyExceptionInfo(sans_exception);
428 if (magick_info != (const MagickInfo *) NULL)
430 if (GetMagickEndianSupport(magick_info) == MagickFalse)
431 read_info->endian=UndefinedEndian;
433 if ((image_info->endian == UndefinedEndian) &&
434 (GetMagickRawSupport(magick_info) != MagickFalse))
440 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
444 if ((magick_info != (const MagickInfo *) NULL) &&
445 (GetMagickDecoderSeekableStream(magick_info) != MagickFalse))
450 image=AcquireImage(read_info,exception);
451 (void) CopyMagickString(image->filename,read_info->filename,
453 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
454 if (status == MagickFalse)
456 read_info=DestroyImageInfo(read_info);
457 image=DestroyImage(image);
458 return((Image *) NULL);
460 if (IsBlobSeekable(image) == MagickFalse)
463 Coder requires a seekable stream.
465 *read_info->filename='\0';
466 status=ImageToFile(image,read_info->filename,exception);
467 if (status == MagickFalse)
469 (void) CloseBlob(image);
470 read_info=DestroyImageInfo(read_info);
471 image=DestroyImage(image);
472 return((Image *) NULL);
474 read_info->temporary=MagickTrue;
476 (void) CloseBlob(image);
477 image=DestroyImage(image);
479 image=NewImageList();
480 if ((magick_info == (const MagickInfo *) NULL) ||
481 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
483 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
484 if (delegate_info == (const DelegateInfo *) NULL)
486 (void) SetImageInfo(read_info,0,exception);
487 (void) CopyMagickString(read_info->filename,filename,
489 magick_info=GetMagickInfo(read_info->magick,exception);
492 if ((magick_info != (const MagickInfo *) NULL) &&
493 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
495 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
496 LockSemaphoreInfo(magick_info->semaphore);
497 image=GetImageDecoder(magick_info)(read_info,exception);
498 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
499 UnlockSemaphoreInfo(magick_info->semaphore);
506 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
507 if (delegate_info == (const DelegateInfo *) NULL)
509 (void) ThrowMagickException(exception,GetMagickModule(),
510 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
512 if (read_info->temporary != MagickFalse)
513 (void) RelinquishUniqueFileResource(read_info->filename);
514 read_info=DestroyImageInfo(read_info);
515 return((Image *) NULL);
518 Let our decoding delegate process the image.
520 image=AcquireImage(read_info,exception);
521 if (image == (Image *) NULL)
523 read_info=DestroyImageInfo(read_info);
524 return((Image *) NULL);
526 (void) CopyMagickString(image->filename,read_info->filename,
528 *read_info->filename='\0';
529 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
530 LockSemaphoreInfo(delegate_info->semaphore);
531 status=InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
533 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
534 UnlockSemaphoreInfo(delegate_info->semaphore);
535 image=DestroyImageList(image);
536 read_info->temporary=MagickTrue;
537 if (status != MagickFalse)
538 (void) SetImageInfo(read_info,0,exception);
539 magick_info=GetMagickInfo(read_info->magick,exception);
540 if ((magick_info == (const MagickInfo *) NULL) ||
541 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
543 if (IsPathAccessible(read_info->filename) != MagickFalse)
544 (void) ThrowMagickException(exception,GetMagickModule(),
545 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
548 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
549 read_info->filename);
550 read_info=DestroyImageInfo(read_info);
551 return((Image *) NULL);
553 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
554 LockSemaphoreInfo(magick_info->semaphore);
555 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
556 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
557 UnlockSemaphoreInfo(magick_info->semaphore);
559 if (read_info->temporary != MagickFalse)
561 (void) RelinquishUniqueFileResource(read_info->filename);
562 read_info->temporary=MagickFalse;
563 if (image != (Image *) NULL)
564 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
566 if (image == (Image *) NULL)
568 read_info=DestroyImageInfo(read_info);
571 if (exception->severity >= ErrorException)
572 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
573 "Coder (%s) generated an image despite an error (%d), "
574 "notify the developers",image->magick,exception->severity);
575 if (IsBlobTemporary(image) != MagickFalse)
576 (void) RelinquishUniqueFileResource(read_info->filename);
577 if ((GetNextImageInList(image) != (Image *) NULL) &&
578 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
583 clones=CloneImages(image,read_info->scenes,exception);
584 if (clones == (Image *) NULL)
585 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
586 "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
589 image=DestroyImageList(image);
590 image=GetFirstImageInList(clones);
593 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
596 magick_path[MagickPathExtent],
598 timestamp[MagickPathExtent];
606 next->taint=MagickFalse;
607 GetPathComponent(magick_filename,MagickPath,magick_path);
608 if (*magick_path == '\0' && *next->magick == '\0')
609 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
610 (void) CopyMagickString(next->magick_filename,magick_filename,
612 if (IsBlobTemporary(image) != MagickFalse)
613 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
614 if (next->magick_columns == 0)
615 next->magick_columns=next->columns;
616 if (next->magick_rows == 0)
617 next->magick_rows=next->rows;
618 value=GetImageProperty(next,"tiff:Orientation",exception);
619 if (value == (char *) NULL)
620 value=GetImageProperty(next,"exif:Orientation",exception);
621 if (value != (char *) NULL)
623 next->orientation=(OrientationType) StringToLong(value);
624 (void) DeleteImageProperty(next,"tiff:Orientation");
625 (void) DeleteImageProperty(next,"exif:Orientation");
627 value=GetImageProperty(next,"exif:XResolution",exception);
628 if (value != (char *) NULL)
630 geometry_info.rho=next->resolution.x;
631 geometry_info.sigma=1.0;
632 flags=ParseGeometry(value,&geometry_info);
633 if (geometry_info.sigma != 0)
634 next->resolution.x=geometry_info.rho/geometry_info.sigma;
635 if (strchr(value,',') != (char *) NULL)
636 next->resolution.x=geometry_info.rho+geometry_info.sigma/1000.0;
637 (void) DeleteImageProperty(next,"exif:XResolution");
639 value=GetImageProperty(next,"exif:YResolution",exception);
640 if (value != (char *) NULL)
642 geometry_info.rho=next->resolution.y;
643 geometry_info.sigma=1.0;
644 flags=ParseGeometry(value,&geometry_info);
645 if (geometry_info.sigma != 0)
646 next->resolution.y=geometry_info.rho/geometry_info.sigma;
647 if (strchr(value,',') != (char *) NULL)
648 next->resolution.y=geometry_info.rho+geometry_info.sigma/1000.0;
649 (void) DeleteImageProperty(next,"exif:YResolution");
651 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
652 if (value == (char *) NULL)
653 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
654 if (value != (char *) NULL)
656 next->units=(ResolutionType) ParseCommandOption(MagickResolutionOptions,
658 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
659 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
661 if (next->page.width == 0)
662 next->page.width=next->columns;
663 if (next->page.height == 0)
664 next->page.height=next->rows;
665 option=GetImageOption(read_info,"caption");
666 if (option != (const char *) NULL)
668 property=InterpretImageProperties(read_info,next,option,exception);
669 (void) SetImageProperty(next,"caption",property,exception);
670 property=DestroyString(property);
672 option=GetImageOption(read_info,"comment");
673 if (option != (const char *) NULL)
675 property=InterpretImageProperties(read_info,next,option,exception);
676 (void) SetImageProperty(next,"comment",property,exception);
677 property=DestroyString(property);
679 option=GetImageOption(read_info,"label");
680 if (option != (const char *) NULL)
682 property=InterpretImageProperties(read_info,next,option,exception);
683 (void) SetImageProperty(next,"label",property,exception);
684 property=DestroyString(property);
686 if (LocaleCompare(next->magick,"TEXT") == 0)
687 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
688 if ((read_info->extract != (char *) NULL) &&
689 (read_info->stream == (StreamHandler) NULL))
694 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
695 if ((next->columns != geometry.width) ||
696 (next->rows != geometry.height))
698 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
703 crop_image=CropImage(next,&geometry,exception);
704 if (crop_image != (Image *) NULL)
705 ReplaceImageInList(&next,crop_image);
708 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
713 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
715 size_image=ResizeImage(next,geometry.width,geometry.height,
716 next->filter,exception);
717 if (size_image != (Image *) NULL)
718 ReplaceImageInList(&next,size_image);
722 profile=GetImageProfile(next,"icc");
723 if (profile == (const StringInfo *) NULL)
724 profile=GetImageProfile(next,"icm");
725 profile=GetImageProfile(next,"iptc");
726 if (profile == (const StringInfo *) NULL)
727 profile=GetImageProfile(next,"8bim");
728 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MagickPathExtent,
730 (void) SetImageProperty(next,"date:modify",timestamp,exception);
731 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MagickPathExtent,
733 (void) SetImageProperty(next,"date:create",timestamp,exception);
734 option=GetImageOption(image_info,"delay");
735 if (option != (const char *) NULL)
737 flags=ParseGeometry(option,&geometry_info);
738 if ((flags & GreaterValue) != 0)
740 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
741 next->delay=(size_t) floor(geometry_info.rho+0.5);
744 if ((flags & LessValue) != 0)
746 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
747 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
750 next->delay=(size_t) floor(geometry_info.rho+0.5);
751 if ((flags & SigmaValue) != 0)
752 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
754 option=GetImageOption(image_info,"dispose");
755 if (option != (const char *) NULL)
756 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
758 if (read_info->verbose != MagickFalse)
759 (void) IdentifyImage(next,stderr,MagickFalse,exception);
762 read_info=DestroyImageInfo(read_info);
763 return(GetFirstImageInList(image));
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
771 % R e a d I m a g e s %
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 % ReadImages() reads one or more images and returns them as an image list.
779 % The format of the ReadImage method is:
781 % Image *ReadImages(ImageInfo *image_info,const char *filename,
782 % ExceptionInfo *exception)
784 % A description of each parameter follows:
786 % o image_info: the image info.
788 % o filename: the image filename.
790 % o exception: return any errors or warnings in this structure.
793 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
794 ExceptionInfo *exception)
797 read_filename[MagickPathExtent];
807 Read image list from a file.
809 assert(image_info != (ImageInfo *) NULL);
810 assert(image_info->signature == MagickCoreSignature);
811 if (image_info->debug != MagickFalse)
812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
813 image_info->filename);
814 assert(exception != (ExceptionInfo *) NULL);
815 read_info=CloneImageInfo(image_info);
816 *read_info->magick='\0';
817 (void) SetImageOption(read_info,"filename",filename);
818 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
819 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
820 (int) read_info->scene,read_filename,exception);
821 if (LocaleCompare(read_filename,read_info->filename) != 0)
831 Images of the form image-%d.png[1-5].
833 sans=AcquireExceptionInfo();
834 (void) SetImageInfo(read_info,0,sans);
835 sans=DestroyExceptionInfo(sans);
836 if (read_info->number_scenes != 0)
838 (void) CopyMagickString(read_filename,read_info->filename,
840 images=NewImageList();
841 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
842 scene=(ssize_t) read_info->scene;
843 for ( ; scene < (ssize_t) extent; scene++)
845 (void) InterpretImageFilename(image_info,(Image *) NULL,
846 read_filename,(int) scene,read_info->filename,exception);
847 image=ReadImage(read_info,exception);
848 if (image == (Image *) NULL)
850 AppendImageToList(&images,image);
852 read_info=DestroyImageInfo(read_info);
856 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
857 image=ReadImage(read_info,exception);
858 read_info=DestroyImageInfo(read_info);
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 + R e a d I n l i n e I m a g e %
871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
874 % The method returns a NULL if there is a memory shortage or if the image
875 % cannot be read. On failure, a NULL image is returned and exception
876 % describes the reason for the failure.
878 % The format of the ReadInlineImage method is:
880 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
881 % ExceptionInfo *exception)
883 % A description of each parameter follows:
885 % o image_info: the image info.
887 % o content: the image encoded in Base64.
889 % o exception: return any errors or warnings in this structure.
892 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
893 const char *content,ExceptionInfo *exception)
911 Skip over header (e.g. data:image/gif;base64,).
913 image=NewImageList();
914 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
916 ThrowReaderException(CorruptImageError,"CorruptImage");
919 blob=Base64Decode(p,&length);
921 ThrowReaderException(CorruptImageError,"CorruptImage");
922 read_info=CloneImageInfo(image_info);
923 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
925 *read_info->filename='\0';
926 *read_info->magick='\0';
927 image=BlobToImage(read_info,blob,length,exception);
928 blob=(unsigned char *) RelinquishMagickMemory(blob);
929 read_info=DestroyImageInfo(read_info);
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938 % W r i t e I m a g e %
942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 % WriteImage() writes an image or an image sequence to a file or file handle.
945 % If writing to a file is on disk, the name is defined by the filename member
946 % of the image structure. WriteImage() returns MagickFalse is there is a
947 % memory shortage or if the image cannot be written. Check the exception
948 % member of image to determine the cause for any failure.
950 % The format of the WriteImage method is:
952 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
953 % ExceptionInfo *exception)
955 % A description of each parameter follows:
957 % o image_info: the image info.
959 % o image: the image.
961 % o exception: return any errors or warnings in this structure.
964 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
965 Image *image,ExceptionInfo *exception)
968 filename[MagickPathExtent];
996 Determine image type from filename prefix or suffix (e.g. image.jpg).
998 assert(image_info != (ImageInfo *) NULL);
999 assert(image_info->signature == MagickCoreSignature);
1000 if (image->debug != MagickFalse)
1001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1002 image_info->filename);
1003 assert(image != (Image *) NULL);
1004 assert(image->signature == MagickCoreSignature);
1005 assert(exception != (ExceptionInfo *) NULL);
1006 sans_exception=AcquireExceptionInfo();
1007 write_info=CloneImageInfo(image_info);
1008 (void) CopyMagickString(write_info->filename,image->filename,
1010 (void) SetImageInfo(write_info,1,sans_exception);
1011 if (*write_info->magick == '\0')
1012 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1013 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1014 (void) CopyMagickString(image->filename,write_info->filename,
1016 domain=CoderPolicyDomain;
1017 rights=WritePolicyRights;
1018 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1020 sans_exception=DestroyExceptionInfo(sans_exception);
1021 write_info=DestroyImageInfo(write_info);
1023 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1026 Call appropriate image writer based on image type.
1028 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1029 sans_exception=DestroyExceptionInfo(sans_exception);
1030 if (magick_info != (const MagickInfo *) NULL)
1032 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1033 image->endian=UndefinedEndian;
1035 if ((image_info->endian == UndefinedEndian) &&
1036 (GetMagickRawSupport(magick_info) != MagickFalse))
1042 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1045 (void) SyncImageProfiles(image);
1046 DisassociateImageStream(image);
1047 option=GetImageOption(image_info,"delegate:bimodal");
1048 if ((IsStringTrue(option) != MagickFalse) &&
1049 (write_info->page == (char *) NULL) &&
1050 (GetPreviousImageInList(image) == (Image *) NULL) &&
1051 (GetNextImageInList(image) == (Image *) NULL) &&
1052 (IsTaintImage(image) == MagickFalse) )
1054 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1055 if ((delegate_info != (const DelegateInfo *) NULL) &&
1056 (GetDelegateMode(delegate_info) == 0) &&
1057 (IsPathAccessible(image->magick_filename) != MagickFalse))
1060 Process image with bi-modal delegate.
1062 (void) CopyMagickString(image->filename,image->magick_filename,
1064 status=InvokeDelegate(write_info,image,image->magick,
1065 write_info->magick,exception);
1066 write_info=DestroyImageInfo(write_info);
1067 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1072 temporary=MagickFalse;
1073 if ((magick_info != (const MagickInfo *) NULL) &&
1074 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1077 image_filename[MagickPathExtent];
1079 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1080 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1081 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1082 if (status != MagickFalse)
1084 if (IsBlobSeekable(image) == MagickFalse)
1087 A seekable stream is required by the encoder.
1089 write_info->adjoin=MagickTrue;
1090 (void) CopyMagickString(write_info->filename,image->filename,
1092 (void) AcquireUniqueFilename(image->filename);
1093 temporary=MagickTrue;
1095 (void) CloseBlob(image);
1098 if ((magick_info != (const MagickInfo *) NULL) &&
1099 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1102 Call appropriate image writer based on image type.
1104 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1105 LockSemaphoreInfo(magick_info->semaphore);
1106 status=GetImageEncoder(magick_info)(write_info,image,exception);
1107 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1108 UnlockSemaphoreInfo(magick_info->semaphore);
1112 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1113 if (delegate_info != (DelegateInfo *) NULL)
1116 Process the image with delegate.
1118 *write_info->filename='\0';
1119 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1120 LockSemaphoreInfo(delegate_info->semaphore);
1121 status=InvokeDelegate(write_info,image,(char *) NULL,
1122 write_info->magick,exception);
1123 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1124 UnlockSemaphoreInfo(delegate_info->semaphore);
1125 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1129 sans_exception=AcquireExceptionInfo();
1130 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1131 sans_exception=DestroyExceptionInfo(sans_exception);
1132 if ((write_info->affirm == MagickFalse) &&
1133 (magick_info == (const MagickInfo *) NULL))
1135 (void) CopyMagickString(write_info->magick,image->magick,
1137 magick_info=GetMagickInfo(write_info->magick,exception);
1139 if ((magick_info == (const MagickInfo *) NULL) ||
1140 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1143 extension[MagickPathExtent];
1145 GetPathComponent(image->filename,ExtensionPath,extension);
1146 if (*extension != '\0')
1147 magick_info=GetMagickInfo(extension,exception);
1149 magick_info=GetMagickInfo(image->magick,exception);
1150 (void) CopyMagickString(image->filename,filename,
1153 if ((magick_info == (const MagickInfo *) NULL) ||
1154 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1156 magick_info=GetMagickInfo(image->magick,exception);
1157 if ((magick_info == (const MagickInfo *) NULL) ||
1158 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1159 (void) ThrowMagickException(exception,GetMagickModule(),
1160 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1161 "`%s'",write_info->magick);
1163 (void) ThrowMagickException(exception,GetMagickModule(),
1164 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1165 "`%s'",write_info->magick);
1167 if ((magick_info != (const MagickInfo *) NULL) &&
1168 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1171 Call appropriate image writer based on image type.
1173 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1174 LockSemaphoreInfo(magick_info->semaphore);
1175 status=GetImageEncoder(magick_info)(write_info,image,exception);
1176 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1177 UnlockSemaphoreInfo(magick_info->semaphore);
1181 if (temporary != MagickFalse)
1184 Copy temporary image file to permanent.
1186 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1187 if (status != MagickFalse)
1189 (void) RelinquishUniqueFileResource(write_info->filename);
1190 status=ImageToFile(image,write_info->filename,exception);
1192 (void) CloseBlob(image);
1193 (void) RelinquishUniqueFileResource(image->filename);
1194 (void) CopyMagickString(image->filename,write_info->filename,
1197 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1198 (write_info->verbose != MagickFalse))
1199 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1200 write_info=DestroyImageInfo(write_info);
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1209 % W r i t e I m a g e s %
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1215 % WriteImages() writes an image sequence into one or more files. While
1216 % WriteImage() can write an image sequence, it is limited to writing
1217 % the sequence into a single file using a format which supports multiple
1218 % frames. WriteImages(), however, does not have this limitation, instead it
1219 % generates multiple output files if necessary (or when requested). When
1220 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1221 % to include a printf-style formatting string for the frame number (e.g.
1224 % The format of the WriteImages method is:
1226 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1227 % const char *filename,ExceptionInfo *exception)
1229 % A description of each parameter follows:
1231 % o image_info: the image info.
1233 % o images: the image list.
1235 % o filename: the image filename.
1237 % o exception: return any errors or warnings in this structure.
1240 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1241 Image *images,const char *filename,ExceptionInfo *exception)
1243 #define WriteImageTag "Write/Image"
1257 MagickProgressMonitor
1269 assert(image_info != (const ImageInfo *) NULL);
1270 assert(image_info->signature == MagickCoreSignature);
1271 assert(images != (Image *) NULL);
1272 assert(images->signature == MagickCoreSignature);
1273 if (images->debug != MagickFalse)
1274 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1275 assert(exception != (ExceptionInfo *) NULL);
1276 write_info=CloneImageInfo(image_info);
1277 *write_info->magick='\0';
1278 images=GetFirstImageInList(images);
1279 if (filename != (const char *) NULL)
1280 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1281 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1282 (void) CopyMagickString(write_info->filename,images->filename,
1284 sans_exception=AcquireExceptionInfo();
1285 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1287 sans_exception=DestroyExceptionInfo(sans_exception);
1288 if (*write_info->magick == '\0')
1289 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1291 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1296 next=GetNextImageInList(p);
1297 if (next == (Image *) NULL)
1299 if (p->scene >= next->scene)
1305 Generate consistent scene numbers.
1307 i=(ssize_t) images->scene;
1308 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1309 p->scene=(size_t) i++;
1317 progress_monitor=(MagickProgressMonitor) NULL;
1319 number_images=GetImageListLength(images);
1320 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1322 if (number_images != 1)
1323 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1325 status&=WriteImage(write_info,p,exception);
1326 if (number_images != 1)
1327 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1328 if (write_info->adjoin != MagickFalse)
1330 if (number_images != 1)
1332 proceed=SetImageProgress(p,WriteImageTag,progress++,number_images);
1333 if (proceed == MagickFalse)
1337 write_info=DestroyImageInfo(write_info);
1338 return(status != 0 ? MagickTrue : MagickFalse);