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 (GetMagickSeekableStream(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 (void) DeleteImageProperty(next,"exif:XResolution");
637 value=GetImageProperty(next,"exif:YResolution",exception);
638 if (value != (char *) NULL)
640 geometry_info.rho=next->resolution.y;
641 geometry_info.sigma=1.0;
642 flags=ParseGeometry(value,&geometry_info);
643 if (geometry_info.sigma != 0)
644 next->resolution.y=geometry_info.rho/geometry_info.sigma;
645 (void) DeleteImageProperty(next,"exif:YResolution");
647 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
648 if (value == (char *) NULL)
649 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
650 if (value != (char *) NULL)
652 next->units=(ResolutionType) (StringToLong(value)-1);
653 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
654 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
656 if (next->page.width == 0)
657 next->page.width=next->columns;
658 if (next->page.height == 0)
659 next->page.height=next->rows;
660 option=GetImageOption(read_info,"caption");
661 if (option != (const char *) NULL)
663 property=InterpretImageProperties(read_info,next,option,exception);
664 (void) SetImageProperty(next,"caption",property,exception);
665 property=DestroyString(property);
667 option=GetImageOption(read_info,"comment");
668 if (option != (const char *) NULL)
670 property=InterpretImageProperties(read_info,next,option,exception);
671 (void) SetImageProperty(next,"comment",property,exception);
672 property=DestroyString(property);
674 option=GetImageOption(read_info,"label");
675 if (option != (const char *) NULL)
677 property=InterpretImageProperties(read_info,next,option,exception);
678 (void) SetImageProperty(next,"label",property,exception);
679 property=DestroyString(property);
681 if (LocaleCompare(next->magick,"TEXT") == 0)
682 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
683 if ((read_info->extract != (char *) NULL) &&
684 (read_info->stream == (StreamHandler) NULL))
689 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
690 if ((next->columns != geometry.width) ||
691 (next->rows != geometry.height))
693 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
698 crop_image=CropImage(next,&geometry,exception);
699 if (crop_image != (Image *) NULL)
700 ReplaceImageInList(&next,crop_image);
703 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
708 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
710 size_image=ResizeImage(next,geometry.width,geometry.height,
711 next->filter,exception);
712 if (size_image != (Image *) NULL)
713 ReplaceImageInList(&next,size_image);
717 profile=GetImageProfile(next,"icc");
718 if (profile == (const StringInfo *) NULL)
719 profile=GetImageProfile(next,"icm");
720 profile=GetImageProfile(next,"iptc");
721 if (profile == (const StringInfo *) NULL)
722 profile=GetImageProfile(next,"8bim");
723 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MagickPathExtent,
725 (void) SetImageProperty(next,"date:modify",timestamp,exception);
726 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MagickPathExtent,
728 (void) SetImageProperty(next,"date:create",timestamp,exception);
729 option=GetImageOption(image_info,"delay");
730 if (option != (const char *) NULL)
732 flags=ParseGeometry(option,&geometry_info);
733 if ((flags & GreaterValue) != 0)
735 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
736 next->delay=(size_t) floor(geometry_info.rho+0.5);
739 if ((flags & LessValue) != 0)
741 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
742 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
745 next->delay=(size_t) floor(geometry_info.rho+0.5);
746 if ((flags & SigmaValue) != 0)
747 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
749 option=GetImageOption(image_info,"dispose");
750 if (option != (const char *) NULL)
751 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
753 if (read_info->verbose != MagickFalse)
754 (void) IdentifyImage(next,stderr,MagickFalse,exception);
757 read_info=DestroyImageInfo(read_info);
758 return(GetFirstImageInList(image));
762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % R e a d I m a g e s %
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772 % ReadImages() reads one or more images and returns them as an image list.
774 % The format of the ReadImage method is:
776 % Image *ReadImages(ImageInfo *image_info,const char *filename,
777 % ExceptionInfo *exception)
779 % A description of each parameter follows:
781 % o image_info: the image info.
783 % o filename: the image filename.
785 % o exception: return any errors or warnings in this structure.
788 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
789 ExceptionInfo *exception)
792 read_filename[MagickPathExtent];
802 Read image list from a file.
804 assert(image_info != (ImageInfo *) NULL);
805 assert(image_info->signature == MagickCoreSignature);
806 if (image_info->debug != MagickFalse)
807 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
808 image_info->filename);
809 assert(exception != (ExceptionInfo *) NULL);
810 read_info=CloneImageInfo(image_info);
811 *read_info->magick='\0';
812 (void) SetImageOption(read_info,"filename",filename);
813 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
814 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
815 (int) read_info->scene,read_filename,exception);
816 if (LocaleCompare(read_filename,read_info->filename) != 0)
826 Images of the form image-%d.png[1-5].
828 sans=AcquireExceptionInfo();
829 (void) SetImageInfo(read_info,0,sans);
830 sans=DestroyExceptionInfo(sans);
831 if (read_info->number_scenes != 0)
833 (void) CopyMagickString(read_filename,read_info->filename,
835 images=NewImageList();
836 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
837 scene=(ssize_t) read_info->scene;
838 for ( ; scene < (ssize_t) extent; scene++)
840 (void) InterpretImageFilename(image_info,(Image *) NULL,
841 read_filename,(int) scene,read_info->filename,exception);
842 image=ReadImage(read_info,exception);
843 if (image == (Image *) NULL)
845 AppendImageToList(&images,image);
847 read_info=DestroyImageInfo(read_info);
851 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
852 image=ReadImage(read_info,exception);
853 read_info=DestroyImageInfo(read_info);
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 + R e a d I n l i n e I m a g e %
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
869 % The method returns a NULL if there is a memory shortage or if the image
870 % cannot be read. On failure, a NULL image is returned and exception
871 % describes the reason for the failure.
873 % The format of the ReadInlineImage method is:
875 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
876 % ExceptionInfo *exception)
878 % A description of each parameter follows:
880 % o image_info: the image info.
882 % o content: the image encoded in Base64.
884 % o exception: return any errors or warnings in this structure.
887 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
888 const char *content,ExceptionInfo *exception)
906 Skip over header (e.g. data:image/gif;base64,).
908 image=NewImageList();
909 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
911 ThrowReaderException(CorruptImageError,"CorruptImage");
914 blob=Base64Decode(p,&length);
916 ThrowReaderException(CorruptImageError,"CorruptImage");
917 read_info=CloneImageInfo(image_info);
918 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
920 *read_info->filename='\0';
921 *read_info->magick='\0';
922 image=BlobToImage(read_info,blob,length,exception);
923 blob=(unsigned char *) RelinquishMagickMemory(blob);
924 read_info=DestroyImageInfo(read_info);
929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 % W r i t e I m a g e %
937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939 % WriteImage() writes an image or an image sequence to a file or file handle.
940 % If writing to a file is on disk, the name is defined by the filename member
941 % of the image structure. WriteImage() returns MagickFalse is there is a
942 % memory shortage or if the image cannot be written. Check the exception
943 % member of image to determine the cause for any failure.
945 % The format of the WriteImage method is:
947 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
948 % ExceptionInfo *exception)
950 % A description of each parameter follows:
952 % o image_info: the image info.
954 % o image: the image.
956 % o exception: return any errors or warnings in this structure.
959 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
960 Image *image,ExceptionInfo *exception)
963 filename[MagickPathExtent];
991 Determine image type from filename prefix or suffix (e.g. image.jpg).
993 assert(image_info != (ImageInfo *) NULL);
994 assert(image_info->signature == MagickCoreSignature);
995 if (image->debug != MagickFalse)
996 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
997 image_info->filename);
998 assert(image != (Image *) NULL);
999 assert(image->signature == MagickCoreSignature);
1000 assert(exception != (ExceptionInfo *) NULL);
1001 sans_exception=AcquireExceptionInfo();
1002 write_info=CloneImageInfo(image_info);
1003 (void) CopyMagickString(write_info->filename,image->filename,
1005 (void) SetImageInfo(write_info,1,sans_exception);
1006 if (*write_info->magick == '\0')
1007 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1008 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1009 (void) CopyMagickString(image->filename,write_info->filename,
1011 domain=CoderPolicyDomain;
1012 rights=WritePolicyRights;
1013 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1015 sans_exception=DestroyExceptionInfo(sans_exception);
1016 write_info=DestroyImageInfo(write_info);
1018 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1021 Call appropriate image writer based on image type.
1023 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1024 sans_exception=DestroyExceptionInfo(sans_exception);
1025 if (magick_info != (const MagickInfo *) NULL)
1027 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1028 image->endian=UndefinedEndian;
1030 if ((image_info->endian == UndefinedEndian) &&
1031 (GetMagickRawSupport(magick_info) != MagickFalse))
1037 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1040 (void) SyncImageProfiles(image);
1041 DisassociateImageStream(image);
1042 option=GetImageOption(image_info,"delegate:bimodal");
1043 if ((IsStringTrue(option) != MagickFalse) &&
1044 (write_info->page == (char *) NULL) &&
1045 (GetPreviousImageInList(image) == (Image *) NULL) &&
1046 (GetNextImageInList(image) == (Image *) NULL) &&
1047 (IsTaintImage(image) == MagickFalse) )
1049 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1050 if ((delegate_info != (const DelegateInfo *) NULL) &&
1051 (GetDelegateMode(delegate_info) == 0) &&
1052 (IsPathAccessible(image->magick_filename) != MagickFalse))
1055 Process image with bi-modal delegate.
1057 (void) CopyMagickString(image->filename,image->magick_filename,
1059 status=InvokeDelegate(write_info,image,image->magick,
1060 write_info->magick,exception);
1061 write_info=DestroyImageInfo(write_info);
1062 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1067 temporary=MagickFalse;
1068 if ((magick_info != (const MagickInfo *) NULL) &&
1069 (GetMagickSeekableStream(magick_info) != MagickFalse))
1072 image_filename[MagickPathExtent];
1074 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1075 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1076 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1077 if (status != MagickFalse)
1079 if (IsBlobSeekable(image) == MagickFalse)
1082 A seekable stream is required by the encoder.
1084 write_info->adjoin=MagickTrue;
1085 (void) CopyMagickString(write_info->filename,image->filename,
1087 (void) AcquireUniqueFilename(image->filename);
1088 temporary=MagickTrue;
1090 (void) CloseBlob(image);
1093 if ((magick_info != (const MagickInfo *) NULL) &&
1094 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1097 Call appropriate image writer based on image type.
1099 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1100 LockSemaphoreInfo(magick_info->semaphore);
1101 status=GetImageEncoder(magick_info)(write_info,image,exception);
1102 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1103 UnlockSemaphoreInfo(magick_info->semaphore);
1107 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1108 if (delegate_info != (DelegateInfo *) NULL)
1111 Process the image with delegate.
1113 *write_info->filename='\0';
1114 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1115 LockSemaphoreInfo(delegate_info->semaphore);
1116 status=InvokeDelegate(write_info,image,(char *) NULL,
1117 write_info->magick,exception);
1118 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1119 UnlockSemaphoreInfo(delegate_info->semaphore);
1120 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1124 sans_exception=AcquireExceptionInfo();
1125 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1126 sans_exception=DestroyExceptionInfo(sans_exception);
1127 if ((write_info->affirm == MagickFalse) &&
1128 (magick_info == (const MagickInfo *) NULL))
1130 (void) CopyMagickString(write_info->magick,image->magick,
1132 magick_info=GetMagickInfo(write_info->magick,exception);
1134 if ((magick_info == (const MagickInfo *) NULL) ||
1135 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1138 extension[MagickPathExtent];
1140 GetPathComponent(image->filename,ExtensionPath,extension);
1141 if (*extension != '\0')
1142 magick_info=GetMagickInfo(extension,exception);
1144 magick_info=GetMagickInfo(image->magick,exception);
1145 (void) CopyMagickString(image->filename,filename,
1148 if ((magick_info == (const MagickInfo *) NULL) ||
1149 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1151 magick_info=GetMagickInfo(image->magick,exception);
1152 if ((magick_info == (const MagickInfo *) NULL) ||
1153 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1154 (void) ThrowMagickException(exception,GetMagickModule(),
1155 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1156 "`%s'",write_info->magick);
1158 (void) ThrowMagickException(exception,GetMagickModule(),
1159 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1160 "`%s'",write_info->magick);
1162 if ((magick_info != (const MagickInfo *) NULL) &&
1163 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1166 Call appropriate image writer based on image type.
1168 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1169 LockSemaphoreInfo(magick_info->semaphore);
1170 status=GetImageEncoder(magick_info)(write_info,image,exception);
1171 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1172 UnlockSemaphoreInfo(magick_info->semaphore);
1176 if (temporary != MagickFalse)
1179 Copy temporary image file to permanent.
1181 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1182 if (status != MagickFalse)
1184 (void) RelinquishUniqueFileResource(write_info->filename);
1185 status=ImageToFile(image,write_info->filename,exception);
1187 (void) CloseBlob(image);
1188 (void) RelinquishUniqueFileResource(image->filename);
1189 (void) CopyMagickString(image->filename,write_info->filename,
1192 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1193 (write_info->verbose != MagickFalse))
1194 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1195 write_info=DestroyImageInfo(write_info);
1200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1204 % W r i t e I m a g e s %
1208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210 % WriteImages() writes an image sequence into one or more files. While
1211 % WriteImage() can write an image sequence, it is limited to writing
1212 % the sequence into a single file using a format which supports multiple
1213 % frames. WriteImages(), however, does not have this limitation, instead it
1214 % generates multiple output files if necessary (or when requested). When
1215 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1216 % to include a printf-style formatting string for the frame number (e.g.
1219 % The format of the WriteImages method is:
1221 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1222 % const char *filename,ExceptionInfo *exception)
1224 % A description of each parameter follows:
1226 % o image_info: the image info.
1228 % o images: the image list.
1230 % o filename: the image filename.
1232 % o exception: return any errors or warnings in this structure.
1235 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1236 Image *images,const char *filename,ExceptionInfo *exception)
1238 #define WriteImageTag "Write/Image"
1252 MagickProgressMonitor
1264 assert(image_info != (const ImageInfo *) NULL);
1265 assert(image_info->signature == MagickCoreSignature);
1266 assert(images != (Image *) NULL);
1267 assert(images->signature == MagickCoreSignature);
1268 if (images->debug != MagickFalse)
1269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1270 assert(exception != (ExceptionInfo *) NULL);
1271 write_info=CloneImageInfo(image_info);
1272 *write_info->magick='\0';
1273 images=GetFirstImageInList(images);
1274 if (filename != (const char *) NULL)
1275 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1276 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1277 (void) CopyMagickString(write_info->filename,images->filename,
1279 sans_exception=AcquireExceptionInfo();
1280 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1282 sans_exception=DestroyExceptionInfo(sans_exception);
1283 if (*write_info->magick == '\0')
1284 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1286 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1291 next=GetNextImageInList(p);
1292 if (next == (Image *) NULL)
1294 if (p->scene >= next->scene)
1300 Generate consistent scene numbers.
1302 i=(ssize_t) images->scene;
1303 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1304 p->scene=(size_t) i++;
1312 progress_monitor=(MagickProgressMonitor) NULL;
1314 number_images=GetImageListLength(images);
1315 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1317 if (number_images != 1)
1318 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1320 status&=WriteImage(write_info,p,exception);
1321 if (number_images != 1)
1322 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1323 if (write_info->adjoin != MagickFalse)
1325 if (number_images != 1)
1327 proceed=SetImageProgress(p,WriteImageTag,progress++,number_images);
1328 if (proceed == MagickFalse)
1332 write_info=DestroyImageInfo(write_info);
1333 return(status != 0 ? MagickTrue : MagickFalse);