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 % https://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];
392 Determine image type from filename prefix or suffix (e.g. image.jpg).
394 assert(image_info != (ImageInfo *) NULL);
395 assert(image_info->signature == MagickCoreSignature);
396 assert(image_info->filename != (char *) NULL);
397 if (image_info->debug != MagickFalse)
398 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
399 image_info->filename);
400 assert(exception != (ExceptionInfo *) NULL);
401 read_info=CloneImageInfo(image_info);
402 (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
403 (void) SetImageInfo(read_info,0,exception);
404 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
405 (void) CopyMagickString(magick,read_info->magick,MagickPathExtent);
407 Call appropriate image reader based on image type.
409 sans_exception=AcquireExceptionInfo();
410 magick_info=GetMagickInfo(read_info->magick,sans_exception);
411 sans_exception=DestroyExceptionInfo(sans_exception);
412 if (magick_info != (const MagickInfo *) NULL)
420 domain=CoderPolicyDomain;
421 rights=ReadPolicyRights;
422 if (IsRightsAuthorized(domain,rights,magick_info->module) == MagickFalse)
425 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
426 "NotAuthorized","`%s'",read_info->filename);
427 read_info=DestroyImageInfo(read_info);
428 return((Image *) 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];
609 next->taint=MagickFalse;
610 GetPathComponent(magick_filename,MagickPath,magick_path);
611 if (*magick_path == '\0' && *next->magick == '\0')
612 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
613 (void) CopyMagickString(next->magick_filename,magick_filename,
615 if (IsBlobTemporary(image) != MagickFalse)
616 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
617 if (next->magick_columns == 0)
618 next->magick_columns=next->columns;
619 if (next->magick_rows == 0)
620 next->magick_rows=next->rows;
621 value=GetImageProperty(next,"tiff:Orientation",exception);
622 if (value == (char *) NULL)
623 value=GetImageProperty(next,"exif:Orientation",exception);
624 if (value != (char *) NULL)
626 next->orientation=(OrientationType) StringToLong(value);
627 (void) DeleteImageProperty(next,"tiff:Orientation");
628 (void) DeleteImageProperty(next,"exif:Orientation");
630 value=GetImageProperty(next,"exif:XResolution",exception);
631 if (value != (char *) NULL)
633 geometry_info.rho=next->resolution.x;
634 geometry_info.sigma=1.0;
635 flags=ParseGeometry(value,&geometry_info);
636 if (geometry_info.sigma != 0)
637 next->resolution.x=geometry_info.rho/geometry_info.sigma;
638 if (strchr(value,',') != (char *) NULL)
639 next->resolution.x=geometry_info.rho+geometry_info.sigma/1000.0;
640 (void) DeleteImageProperty(next,"exif:XResolution");
642 value=GetImageProperty(next,"exif:YResolution",exception);
643 if (value != (char *) NULL)
645 geometry_info.rho=next->resolution.y;
646 geometry_info.sigma=1.0;
647 flags=ParseGeometry(value,&geometry_info);
648 if (geometry_info.sigma != 0)
649 next->resolution.y=geometry_info.rho/geometry_info.sigma;
650 if (strchr(value,',') != (char *) NULL)
651 next->resolution.y=geometry_info.rho+geometry_info.sigma/1000.0;
652 (void) DeleteImageProperty(next,"exif:YResolution");
654 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
655 if (value == (char *) NULL)
656 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
657 if (value != (char *) NULL)
659 option_type=ParseCommandOption(MagickResolutionOptions,MagickFalse,
661 if (option_type >= 0)
662 next->units=(ResolutionType) option_type;
663 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
664 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
666 if (next->page.width == 0)
667 next->page.width=next->columns;
668 if (next->page.height == 0)
669 next->page.height=next->rows;
670 option=GetImageOption(read_info,"caption");
671 if (option != (const char *) NULL)
673 property=InterpretImageProperties(read_info,next,option,exception);
674 (void) SetImageProperty(next,"caption",property,exception);
675 property=DestroyString(property);
677 option=GetImageOption(read_info,"comment");
678 if (option != (const char *) NULL)
680 property=InterpretImageProperties(read_info,next,option,exception);
681 (void) SetImageProperty(next,"comment",property,exception);
682 property=DestroyString(property);
684 option=GetImageOption(read_info,"label");
685 if (option != (const char *) NULL)
687 property=InterpretImageProperties(read_info,next,option,exception);
688 (void) SetImageProperty(next,"label",property,exception);
689 property=DestroyString(property);
691 if (LocaleCompare(next->magick,"TEXT") == 0)
692 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
693 if ((read_info->extract != (char *) NULL) &&
694 (read_info->stream == (StreamHandler) NULL))
699 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
700 if ((next->columns != geometry.width) ||
701 (next->rows != geometry.height))
703 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
708 crop_image=CropImage(next,&geometry,exception);
709 if (crop_image != (Image *) NULL)
710 ReplaceImageInList(&next,crop_image);
713 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
718 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
720 size_image=ResizeImage(next,geometry.width,geometry.height,
721 next->filter,exception);
722 if (size_image != (Image *) NULL)
723 ReplaceImageInList(&next,size_image);
727 profile=GetImageProfile(next,"icc");
728 if (profile == (const StringInfo *) NULL)
729 profile=GetImageProfile(next,"icm");
730 profile=GetImageProfile(next,"iptc");
731 if (profile == (const StringInfo *) NULL)
732 profile=GetImageProfile(next,"8bim");
733 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MagickPathExtent,
735 (void) SetImageProperty(next,"date:modify",timestamp,exception);
736 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MagickPathExtent,
738 (void) SetImageProperty(next,"date:create",timestamp,exception);
739 option=GetImageOption(image_info,"delay");
740 if (option != (const char *) NULL)
742 flags=ParseGeometry(option,&geometry_info);
743 if ((flags & GreaterValue) != 0)
745 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
746 next->delay=(size_t) floor(geometry_info.rho+0.5);
749 if ((flags & LessValue) != 0)
751 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
752 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
755 next->delay=(size_t) floor(geometry_info.rho+0.5);
756 if ((flags & SigmaValue) != 0)
757 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
759 option=GetImageOption(image_info,"dispose");
760 if (option != (const char *) NULL)
762 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
764 if (option_type >= 0)
765 next->dispose=(DisposeType) option_type;
767 if (read_info->verbose != MagickFalse)
768 (void) IdentifyImage(next,stderr,MagickFalse,exception);
771 read_info=DestroyImageInfo(read_info);
772 return(GetFirstImageInList(image));
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % R e a d I m a g e s %
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 % ReadImages() reads one or more images and returns them as an image list.
788 % The format of the ReadImage method is:
790 % Image *ReadImages(ImageInfo *image_info,const char *filename,
791 % ExceptionInfo *exception)
793 % A description of each parameter follows:
795 % o image_info: the image info.
797 % o filename: the image filename.
799 % o exception: return any errors or warnings in this structure.
802 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
803 ExceptionInfo *exception)
806 read_filename[MagickPathExtent];
816 Read image list from a file.
818 assert(image_info != (ImageInfo *) NULL);
819 assert(image_info->signature == MagickCoreSignature);
820 if (image_info->debug != MagickFalse)
821 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
822 image_info->filename);
823 assert(exception != (ExceptionInfo *) NULL);
824 read_info=CloneImageInfo(image_info);
825 *read_info->magick='\0';
826 (void) SetImageOption(read_info,"filename",filename);
827 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
828 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
829 (int) read_info->scene,read_filename,exception);
830 if (LocaleCompare(read_filename,read_info->filename) != 0)
840 Images of the form image-%d.png[1-5].
842 sans=AcquireExceptionInfo();
843 (void) SetImageInfo(read_info,0,sans);
844 sans=DestroyExceptionInfo(sans);
845 if (read_info->number_scenes != 0)
847 (void) CopyMagickString(read_filename,read_info->filename,
849 images=NewImageList();
850 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
851 scene=(ssize_t) read_info->scene;
852 for ( ; scene < (ssize_t) extent; scene++)
854 (void) InterpretImageFilename(image_info,(Image *) NULL,
855 read_filename,(int) scene,read_info->filename,exception);
856 image=ReadImage(read_info,exception);
857 if (image == (Image *) NULL)
859 AppendImageToList(&images,image);
861 read_info=DestroyImageInfo(read_info);
865 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
866 image=ReadImage(read_info,exception);
867 read_info=DestroyImageInfo(read_info);
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 + R e a d I n l i n e I m a g e %
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
882 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
883 % The method returns a NULL if there is a memory shortage or if the image
884 % cannot be read. On failure, a NULL image is returned and exception
885 % describes the reason for the failure.
887 % The format of the ReadInlineImage method is:
889 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
890 % ExceptionInfo *exception)
892 % A description of each parameter follows:
894 % o image_info: the image info.
896 % o content: the image encoded in Base64.
898 % o exception: return any errors or warnings in this structure.
901 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
902 const char *content,ExceptionInfo *exception)
920 Skip over header (e.g. data:image/gif;base64,).
922 image=NewImageList();
923 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
925 ThrowReaderException(CorruptImageError,"CorruptImage");
928 blob=Base64Decode(p,&length);
930 ThrowReaderException(CorruptImageError,"CorruptImage");
931 read_info=CloneImageInfo(image_info);
932 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
934 *read_info->filename='\0';
935 *read_info->magick='\0';
936 image=BlobToImage(read_info,blob,length,exception);
937 blob=(unsigned char *) RelinquishMagickMemory(blob);
938 read_info=DestroyImageInfo(read_info);
943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947 % W r i t e I m a g e %
951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 % WriteImage() writes an image or an image sequence to a file or file handle.
954 % If writing to a file is on disk, the name is defined by the filename member
955 % of the image structure. WriteImage() returns MagickFalse is there is a
956 % memory shortage or if the image cannot be written. Check the exception
957 % member of image to determine the cause for any failure.
959 % The format of the WriteImage method is:
961 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
962 % ExceptionInfo *exception)
964 % A description of each parameter follows:
966 % o image_info: the image info.
968 % o image: the image.
970 % o exception: return any errors or warnings in this structure.
973 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
974 Image *image,ExceptionInfo *exception)
977 filename[MagickPathExtent];
999 Determine image type from filename prefix or suffix (e.g. image.jpg).
1001 assert(image_info != (ImageInfo *) NULL);
1002 assert(image_info->signature == MagickCoreSignature);
1003 if (image->debug != MagickFalse)
1004 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1005 image_info->filename);
1006 assert(image != (Image *) NULL);
1007 assert(image->signature == MagickCoreSignature);
1008 assert(exception != (ExceptionInfo *) NULL);
1009 sans_exception=AcquireExceptionInfo();
1010 write_info=CloneImageInfo(image_info);
1011 (void) CopyMagickString(write_info->filename,image->filename,
1013 (void) SetImageInfo(write_info,1,sans_exception);
1014 if (*write_info->magick == '\0')
1015 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1016 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1017 (void) CopyMagickString(image->filename,write_info->filename,
1020 Call appropriate image writer based on image type.
1022 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1023 sans_exception=DestroyExceptionInfo(sans_exception);
1024 if (magick_info != (const MagickInfo *) NULL)
1032 domain=CoderPolicyDomain;
1033 rights=WritePolicyRights;
1034 if (IsRightsAuthorized(domain,rights,magick_info->module) == MagickFalse)
1036 write_info=DestroyImageInfo(write_info);
1038 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1040 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1041 image->endian=UndefinedEndian;
1043 if ((image_info->endian == UndefinedEndian) &&
1044 (GetMagickRawSupport(magick_info) != MagickFalse))
1050 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1053 (void) SyncImageProfiles(image);
1054 DisassociateImageStream(image);
1055 option=GetImageOption(image_info,"delegate:bimodal");
1056 if ((IsStringTrue(option) != MagickFalse) &&
1057 (write_info->page == (char *) NULL) &&
1058 (GetPreviousImageInList(image) == (Image *) NULL) &&
1059 (GetNextImageInList(image) == (Image *) NULL) &&
1060 (IsTaintImage(image) == MagickFalse) )
1062 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1063 if ((delegate_info != (const DelegateInfo *) NULL) &&
1064 (GetDelegateMode(delegate_info) == 0) &&
1065 (IsPathAccessible(image->magick_filename) != MagickFalse))
1068 Process image with bi-modal delegate.
1070 (void) CopyMagickString(image->filename,image->magick_filename,
1072 status=InvokeDelegate(write_info,image,image->magick,
1073 write_info->magick,exception);
1074 write_info=DestroyImageInfo(write_info);
1075 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1080 temporary=MagickFalse;
1081 if ((magick_info != (const MagickInfo *) NULL) &&
1082 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1085 image_filename[MagickPathExtent];
1087 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1088 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1089 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1090 if (status != MagickFalse)
1092 if (IsBlobSeekable(image) == MagickFalse)
1095 A seekable stream is required by the encoder.
1097 write_info->adjoin=MagickTrue;
1098 (void) CopyMagickString(write_info->filename,image->filename,
1100 (void) AcquireUniqueFilename(image->filename);
1101 temporary=MagickTrue;
1103 (void) CloseBlob(image);
1106 if ((magick_info != (const MagickInfo *) NULL) &&
1107 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1110 Call appropriate image writer based on image type.
1112 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1113 LockSemaphoreInfo(magick_info->semaphore);
1114 status=GetImageEncoder(magick_info)(write_info,image,exception);
1115 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1116 UnlockSemaphoreInfo(magick_info->semaphore);
1120 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1121 if (delegate_info != (DelegateInfo *) NULL)
1124 Process the image with delegate.
1126 *write_info->filename='\0';
1127 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1128 LockSemaphoreInfo(delegate_info->semaphore);
1129 status=InvokeDelegate(write_info,image,(char *) NULL,
1130 write_info->magick,exception);
1131 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1132 UnlockSemaphoreInfo(delegate_info->semaphore);
1133 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1137 sans_exception=AcquireExceptionInfo();
1138 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1139 sans_exception=DestroyExceptionInfo(sans_exception);
1140 if ((write_info->affirm == MagickFalse) &&
1141 (magick_info == (const MagickInfo *) NULL))
1143 (void) CopyMagickString(write_info->magick,image->magick,
1145 magick_info=GetMagickInfo(write_info->magick,exception);
1147 if ((magick_info == (const MagickInfo *) NULL) ||
1148 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1151 extension[MagickPathExtent];
1153 GetPathComponent(image->filename,ExtensionPath,extension);
1154 if (*extension != '\0')
1155 magick_info=GetMagickInfo(extension,exception);
1157 magick_info=GetMagickInfo(image->magick,exception);
1158 (void) CopyMagickString(image->filename,filename,
1161 if ((magick_info == (const MagickInfo *) NULL) ||
1162 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1164 magick_info=GetMagickInfo(image->magick,exception);
1165 if ((magick_info == (const MagickInfo *) NULL) ||
1166 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1167 (void) ThrowMagickException(exception,GetMagickModule(),
1168 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1169 "`%s'",write_info->magick);
1171 (void) ThrowMagickException(exception,GetMagickModule(),
1172 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1173 "`%s'",write_info->magick);
1175 if ((magick_info != (const MagickInfo *) NULL) &&
1176 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1179 Call appropriate image writer based on image type.
1181 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1182 LockSemaphoreInfo(magick_info->semaphore);
1183 status=GetImageEncoder(magick_info)(write_info,image,exception);
1184 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1185 UnlockSemaphoreInfo(magick_info->semaphore);
1189 if (temporary != MagickFalse)
1192 Copy temporary image file to permanent.
1194 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1195 if (status != MagickFalse)
1197 (void) RelinquishUniqueFileResource(write_info->filename);
1198 status=ImageToFile(image,write_info->filename,exception);
1200 (void) CloseBlob(image);
1201 (void) RelinquishUniqueFileResource(image->filename);
1202 (void) CopyMagickString(image->filename,write_info->filename,
1205 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1206 (write_info->verbose != MagickFalse))
1207 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1208 write_info=DestroyImageInfo(write_info);
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1217 % W r i t e I m a g e s %
1221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1223 % WriteImages() writes an image sequence into one or more files. While
1224 % WriteImage() can write an image sequence, it is limited to writing
1225 % the sequence into a single file using a format which supports multiple
1226 % frames. WriteImages(), however, does not have this limitation, instead it
1227 % generates multiple output files if necessary (or when requested). When
1228 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1229 % to include a printf-style formatting string for the frame number (e.g.
1232 % The format of the WriteImages method is:
1234 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1235 % const char *filename,ExceptionInfo *exception)
1237 % A description of each parameter follows:
1239 % o image_info: the image info.
1241 % o images: the image list.
1243 % o filename: the image filename.
1245 % o exception: return any errors or warnings in this structure.
1248 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1249 Image *images,const char *filename,ExceptionInfo *exception)
1251 #define WriteImageTag "Write/Image"
1265 MagickProgressMonitor
1277 assert(image_info != (const ImageInfo *) NULL);
1278 assert(image_info->signature == MagickCoreSignature);
1279 assert(images != (Image *) NULL);
1280 assert(images->signature == MagickCoreSignature);
1281 if (images->debug != MagickFalse)
1282 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1283 assert(exception != (ExceptionInfo *) NULL);
1284 write_info=CloneImageInfo(image_info);
1285 *write_info->magick='\0';
1286 images=GetFirstImageInList(images);
1287 if (filename != (const char *) NULL)
1288 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1289 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1290 (void) CopyMagickString(write_info->filename,images->filename,
1292 sans_exception=AcquireExceptionInfo();
1293 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1295 sans_exception=DestroyExceptionInfo(sans_exception);
1296 if (*write_info->magick == '\0')
1297 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1299 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1304 next=GetNextImageInList(p);
1305 if (next == (Image *) NULL)
1307 if (p->scene >= next->scene)
1313 Generate consistent scene numbers.
1315 i=(ssize_t) images->scene;
1316 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1317 p->scene=(size_t) i++;
1325 progress_monitor=(MagickProgressMonitor) NULL;
1327 number_images=GetImageListLength(images);
1328 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1330 if (number_images != 1)
1331 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1333 status&=WriteImage(write_info,p,exception);
1334 if (number_images != 1)
1335 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1336 if (write_info->adjoin != MagickFalse)
1338 if (number_images != 1)
1340 proceed=SetImageProgress(p,WriteImageTag,progress++,number_images);
1341 if (proceed == MagickFalse)
1345 write_info=DestroyImageInfo(write_info);
1346 return(status != 0 ? MagickTrue : MagickFalse);