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-2010 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/cache.h"
48 #include "magick/client.h"
49 #include "magick/constitute.h"
50 #include "magick/delegate.h"
51 #include "magick/geometry.h"
52 #include "magick/identify.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/option.h"
60 #include "magick/pixel.h"
61 #include "magick/policy.h"
62 #include "magick/profile.h"
63 #include "magick/property.h"
64 #include "magick/quantum.h"
65 #include "magick/resize.h"
66 #include "magick/resource_.h"
67 #include "magick/semaphore.h"
68 #include "magick/statistic.h"
69 #include "magick/stream.h"
70 #include "magick/string_.h"
71 #include "magick/string-private.h"
72 #include "magick/timer.h"
73 #include "magick/transform.h"
74 #include "magick/utility.h"
77 *constitute_semaphore = (SemaphoreInfo *) NULL;
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 + C o n s t i t u t e C o m p o n e n t G e n e s i s %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 % ConstituteComponentGenesis() instantiates the constitute component.
92 % The format of the ConstituteComponentGenesis method is:
94 % MagickBooleanType ConstituteComponentGenesis(void)
97 MagickExport MagickBooleanType ConstituteComponentGenesis(void)
99 AcquireSemaphoreInfo(&constitute_semaphore);
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 + C o n s t i t u t e C o m p o n e n t T e r m i n u s %
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % ConstituteComponentTerminus() destroys the constitute component.
116 % The format of the ConstituteComponentTerminus method is:
118 % ConstituteComponentTerminus(void)
121 MagickExport void ConstituteComponentTerminus(void)
123 if (constitute_semaphore == (SemaphoreInfo *) NULL)
124 AcquireSemaphoreInfo(&constitute_semaphore);
125 DestroySemaphoreInfo(&constitute_semaphore);
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % C o n s t i t u t e I m a g e %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % ConstituteImage() returns an image from the pixel data you supply.
140 % The pixel data must be in scanline order top-to-bottom. The data can be
141 % char, short int, int, float, or double. Float and double require the
142 % pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
143 % create a 640x480 image from unsigned red-green-blue character data, use:
145 % image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
147 % The format of the ConstituteImage method is:
149 % Image *ConstituteImage(const unsigned long columns,
150 % const unsigned long rows,const char *map,const StorageType storage,
151 % const void *pixels,ExceptionInfo *exception)
153 % A description of each parameter follows:
155 % o columns: width in pixels of the image.
157 % o rows: height in pixels of the image.
159 % o map: This string reflects the expected ordering of the pixel array.
160 % It can be any combination or order of R = red, G = green, B = blue,
161 % A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
162 % Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
165 % o storage: Define the data type of the pixels. Float and double types are
166 % expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
167 % from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
168 % LongPixel, QuantumPixel, or ShortPixel.
170 % o pixels: This array of values contain the pixel components as defined by
171 % map and type. You must preallocate this array where the expected
172 % length varies depending on the values of width, height, map, and type.
174 % o exception: return any errors or warnings in this structure.
177 MagickExport Image *ConstituteImage(const unsigned long columns,
178 const unsigned long rows,const char *map,const StorageType storage,
179 const void *pixels,ExceptionInfo *exception)
188 Allocate image structure.
190 assert(map != (const char *) NULL);
191 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
192 assert(pixels != (void *) NULL);
193 assert(exception != (ExceptionInfo *) NULL);
194 assert(exception->signature == MagickSignature);
195 image=AcquireImage((ImageInfo *) NULL);
196 if (image == (Image *) NULL)
197 return((Image *) NULL);
198 if ((columns == 0) || (rows == 0))
199 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
200 image->columns=columns;
202 (void) SetImageBackgroundColor(image);
203 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels);
204 if (status == MagickFalse)
206 InheritException(exception,&image->exception);
207 image=DestroyImage(image);
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % P i n g I m a g e %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % PingImage() returns all the properties of an image or image sequence
224 % except for the pixels. It is much faster and consumes far less memory
225 % than ReadImage(). On failure, a NULL image is returned and exception
226 % describes the reason for the failure.
228 % The format of the PingImage method is:
230 % Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
232 % A description of each parameter follows:
234 % o image_info: Ping the image defined by the file or filename members of
237 % o exception: return any errors or warnings in this structure.
241 #if defined(__cplusplus) || defined(c_plusplus)
245 static size_t PingStream(const Image *magick_unused(image),
246 const void *magick_unused(pixels),const size_t columns)
251 #if defined(__cplusplus) || defined(c_plusplus)
255 MagickExport Image *PingImage(const ImageInfo *image_info,
256 ExceptionInfo *exception)
264 assert(image_info != (ImageInfo *) NULL);
265 assert(image_info->signature == MagickSignature);
266 if (image_info->debug != MagickFalse)
267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
268 image_info->filename);
269 assert(exception != (ExceptionInfo *) NULL);
270 ping_info=CloneImageInfo(image_info);
271 ping_info->ping=MagickTrue;
272 image=ReadStream(ping_info,&PingStream,exception);
273 if (image != (Image *) NULL)
275 ResetTimer(&image->timer);
276 if (ping_info->verbose != MagickFalse)
277 (void) IdentifyImage(image,stdout,MagickFalse);
279 ping_info=DestroyImageInfo(ping_info);
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % P i n g I m a g e s %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % PingImages() pings one or more images and returns them as an image list.
296 % The format of the PingImage method is:
298 % Image *PingImages(const ImageInfo *image_info,ExceptionInfo *exception)
300 % A description of each parameter follows:
302 % o image_info: the image info.
304 % o exception: return any errors or warnings in this structure.
307 MagickExport Image *PingImages(const ImageInfo *image_info,
308 ExceptionInfo *exception)
311 filename[MaxTextExtent];
321 Ping image list from a file.
323 assert(image_info != (ImageInfo *) NULL);
324 assert(image_info->signature == MagickSignature);
325 if (image_info->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
327 image_info->filename);
328 assert(exception != (ExceptionInfo *) NULL);
329 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
330 (int) image_info->scene,filename);
331 if (LocaleCompare(filename,image_info->filename) != 0)
341 Images of the form image-%d.png[1-5].
343 read_info=CloneImageInfo(image_info);
344 sans=AcquireExceptionInfo();
345 (void) SetImageInfo(read_info,0,sans);
346 sans=DestroyExceptionInfo(sans);
347 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
348 images=NewImageList();
349 extent=(long) (read_info->scene+read_info->number_scenes);
350 for (scene=(long) read_info->scene; scene < (long) extent; scene++)
352 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
353 scene,read_info->filename);
354 image=PingImage(read_info,exception);
355 if (image == (Image *) NULL)
357 AppendImageToList(&images,image);
359 read_info=DestroyImageInfo(read_info);
362 return(PingImage(image_info,exception));
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 % R e a d I m a g e %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % ReadImage() reads an image or image sequence from a file or file handle.
377 % The method returns a NULL if there is a memory shortage or if the image
378 % cannot be read. On failure, a NULL image is returned and exception
379 % describes the reason for the failure.
381 % The format of the ReadImage method is:
383 % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
385 % A description of each parameter follows:
387 % o image_info: Read the image defined by the file or filename members of
390 % o exception: return any errors or warnings in this structure.
393 MagickExport Image *ReadImage(const ImageInfo *image_info,
394 ExceptionInfo *exception)
397 filename[MaxTextExtent],
398 magick[MaxTextExtent],
399 magick_filename[MaxTextExtent];
434 Determine image type from filename prefix or suffix (e.g. image.jpg).
436 assert(image_info != (ImageInfo *) NULL);
437 assert(image_info->signature == MagickSignature);
438 assert(image_info->filename != (char *) NULL);
439 if (image_info->debug != MagickFalse)
440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
441 image_info->filename);
442 assert(exception != (ExceptionInfo *) NULL);
443 read_info=CloneImageInfo(image_info);
444 (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
445 (void) SetImageInfo(read_info,0,exception);
446 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
447 (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
448 domain=CoderPolicyDomain;
449 rights=ReadPolicyRights;
450 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
453 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
454 "NotAuthorized","`%s'",read_info->filename);
455 return((Image *) NULL);
458 Call appropriate image reader based on image type.
460 sans_exception=AcquireExceptionInfo();
461 magick_info=GetMagickInfo(read_info->magick,sans_exception);
462 sans_exception=DestroyExceptionInfo(sans_exception);
463 if (magick_info != (const MagickInfo *) NULL)
465 if (GetMagickEndianSupport(magick_info) == MagickFalse)
466 read_info->endian=UndefinedEndian;
468 if ((image_info->endian == UndefinedEndian) &&
469 (GetMagickRawSupport(magick_info) != MagickFalse))
475 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
479 if ((magick_info != (const MagickInfo *) NULL) &&
480 (GetMagickSeekableStream(magick_info) != MagickFalse))
485 image=AcquireImage(read_info);
486 (void) CopyMagickString(image->filename,read_info->filename,
488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
491 read_info=DestroyImageInfo(read_info);
492 image=DestroyImage(image);
493 return((Image *) NULL);
495 if (IsBlobSeekable(image) == MagickFalse)
498 Coder requires a seekable stream.
500 *read_info->filename='\0';
501 status=ImageToFile(image,read_info->filename,exception);
502 if (status == MagickFalse)
504 (void) CloseBlob(image);
505 read_info=DestroyImageInfo(read_info);
506 image=DestroyImage(image);
507 return((Image *) NULL);
509 read_info->temporary=MagickTrue;
511 (void) CloseBlob(image);
512 image=DestroyImage(image);
514 image=NewImageList();
515 if (constitute_semaphore == (SemaphoreInfo *) NULL)
516 AcquireSemaphoreInfo(&constitute_semaphore);
517 if ((magick_info != (const MagickInfo *) NULL) &&
518 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
520 thread_support=GetMagickThreadSupport(magick_info);
521 if ((thread_support & DecoderThreadSupport) == 0)
522 LockSemaphoreInfo(constitute_semaphore);
523 image=GetImageDecoder(magick_info)(read_info,exception);
524 if ((thread_support & DecoderThreadSupport) == 0)
525 UnlockSemaphoreInfo(constitute_semaphore);
529 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
530 if (delegate_info == (const DelegateInfo *) NULL)
532 (void) ThrowMagickException(exception,GetMagickModule(),
533 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
534 read_info->filename);
535 if (read_info->temporary != MagickFalse)
536 (void) RelinquishUniqueFileResource(read_info->filename);
537 read_info=DestroyImageInfo(read_info);
538 return((Image *) NULL);
541 Let our decoding delegate process the image.
543 image=AcquireImage(read_info);
544 if (image == (Image *) NULL)
546 read_info=DestroyImageInfo(read_info);
547 return((Image *) NULL);
549 (void) CopyMagickString(image->filename,read_info->filename,
551 *read_info->filename='\0';
552 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
553 LockSemaphoreInfo(constitute_semaphore);
554 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
556 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
557 UnlockSemaphoreInfo(constitute_semaphore);
558 image=DestroyImageList(image);
559 read_info->temporary=MagickTrue;
560 (void) SetImageInfo(read_info,0,exception);
561 magick_info=GetMagickInfo(read_info->magick,exception);
562 if ((magick_info == (const MagickInfo *) NULL) ||
563 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
565 if (IsPathAccessible(read_info->filename) != MagickFalse)
566 (void) ThrowMagickException(exception,GetMagickModule(),
567 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
568 read_info->filename);
570 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
571 read_info->filename);
572 read_info=DestroyImageInfo(read_info);
573 return((Image *) NULL);
575 thread_support=GetMagickThreadSupport(magick_info);
576 if ((thread_support & DecoderThreadSupport) == 0)
577 LockSemaphoreInfo(constitute_semaphore);
578 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
579 if ((thread_support & DecoderThreadSupport) == 0)
580 UnlockSemaphoreInfo(constitute_semaphore);
582 if (read_info->temporary != MagickFalse)
584 (void) RelinquishUniqueFileResource(read_info->filename);
585 read_info->temporary=MagickFalse;
586 if (image != (Image *) NULL)
587 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
589 if (image == (Image *) NULL)
591 read_info=DestroyImageInfo(read_info);
594 if (exception->severity >= ErrorException)
595 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
596 "Coder (%s) generated an image despite an error (%d), "
597 "notify the developers",image->magick,exception->severity);
598 if (IsBlobTemporary(image) != MagickFalse)
599 (void) RelinquishUniqueFileResource(read_info->filename);
600 if ((GetNextImageInList(image) != (Image *) NULL) &&
601 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
606 clones=CloneImages(image,read_info->scenes,exception);
607 if (clones == (Image *) NULL)
608 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
609 "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
612 image=DestroyImageList(image);
613 image=GetFirstImageInList(clones);
616 if (GetBlobError(image) != MagickFalse)
618 ThrowFileException(exception,FileOpenError,
619 "AnErrorHasOccurredReadingFromFile",read_info->filename);
620 image=DestroyImageList(image);
621 read_info=DestroyImageInfo(read_info);
622 return((Image *) NULL);
624 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
627 timestamp[MaxTextExtent];
632 next->taint=MagickFalse;
633 if (next->magick_columns == 0)
634 next->magick_columns=next->columns;
635 if (next->magick_rows == 0)
636 next->magick_rows=next->rows;
637 if ((LocaleCompare(magick,"HTTP") != 0) &&
638 (LocaleCompare(magick,"FTP") != 0))
639 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
640 (void) CopyMagickString(next->magick_filename,magick_filename,
642 if (IsBlobTemporary(image) != MagickFalse)
643 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
644 value=GetImageProperty(next,"tiff:Orientation");
645 if (value == (char *) NULL)
646 value=GetImageProperty(next,"exif:Orientation");
647 if (value != (char *) NULL)
649 next->orientation=(OrientationType) StringToLong(value);
650 (void) DeleteImageProperty(next,"tiff:Orientation");
651 (void) DeleteImageProperty(next,"exif:Orientation");
653 value=GetImageProperty(next,"tiff:XResolution");
654 if (value == (char *) NULL)
655 value=GetImageProperty(next,"exif:XResolution");
656 if (value != (char *) NULL)
658 geometry_info.rho=next->x_resolution;
659 geometry_info.sigma=1.0;
660 flags=ParseGeometry(value,&geometry_info);
661 if (geometry_info.sigma != 0)
662 next->x_resolution=geometry_info.rho/geometry_info.sigma;
663 (void) DeleteImageProperty(next,"exif:XResolution");
664 (void) DeleteImageProperty(next,"tiff:XResolution");
666 value=GetImageProperty(next,"tiff:YResolution");
667 if (value == (char *) NULL)
668 value=GetImageProperty(next,"exif:YResolution");
669 if (value != (char *) NULL)
671 geometry_info.rho=next->y_resolution;
672 geometry_info.sigma=1.0;
673 flags=ParseGeometry(value,&geometry_info);
674 if (geometry_info.sigma != 0)
675 next->y_resolution=geometry_info.rho/geometry_info.sigma;
676 (void) DeleteImageProperty(next,"exif:YResolution");
677 (void) DeleteImageProperty(next,"tiff:YResolution");
679 value=GetImageProperty(next,"tiff:ResolutionUnit");
680 if (value == (char *) NULL)
681 value=GetImageProperty(next,"exif:ResolutionUnit");
682 if (value != (char *) NULL)
684 next->units=(ResolutionType) (StringToLong(value)-1);
685 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
686 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
688 if (next->page.width == 0)
689 next->page.width=next->columns;
690 if (next->page.height == 0)
691 next->page.height=next->rows;
692 if (*read_info->filename != '\0')
700 option=GetImageOption(read_info,"caption");
701 if (option != (const char *) NULL)
703 property=InterpretImageProperties(read_info,next,option);
704 (void) SetImageProperty(next,"caption",property);
705 property=DestroyString(property);
707 option=GetImageOption(read_info,"comment");
708 if (option != (const char *) NULL)
710 property=InterpretImageProperties(read_info,next,option);
711 (void) SetImageProperty(next,"comment",property);
712 property=DestroyString(property);
714 option=GetImageOption(read_info,"label");
715 if (option != (const char *) NULL)
717 property=InterpretImageProperties(read_info,next,option);
718 (void) SetImageProperty(next,"label",property);
719 property=DestroyString(property);
722 if (LocaleCompare(next->magick,"TEXT") == 0)
723 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
724 if ((read_info->extract != (char *) NULL) &&
725 (read_info->stream == (StreamHandler) NULL))
730 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
731 if ((next->columns != geometry.width) ||
732 (next->rows != geometry.height))
734 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
739 crop_image=CropImage(next,&geometry,exception);
740 if (crop_image != (Image *) NULL)
741 ReplaceImageInList(&next,crop_image);
744 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
749 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
751 size_image=ResizeImage(next,geometry.width,geometry.height,
752 next->filter,next->blur,exception);
753 if (size_image != (Image *) NULL)
754 ReplaceImageInList(&next,size_image);
758 profile=GetImageProfile(next,"icc");
759 if (profile == (const StringInfo *) NULL)
760 profile=GetImageProfile(next,"icm");
761 if (profile != (const StringInfo *) NULL)
763 next->color_profile.length=GetStringInfoLength(profile);
764 next->color_profile.info=GetStringInfoDatum(profile);
766 profile=GetImageProfile(next,"iptc");
767 if (profile == (const StringInfo *) NULL)
768 profile=GetImageProfile(next,"8bim");
769 if (profile != (const StringInfo *) NULL)
771 next->iptc_profile.length=GetStringInfoLength(profile);
772 next->iptc_profile.info=GetStringInfoDatum(profile);
774 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
776 (void) SetImageProperty(next,"date:modify",timestamp);
777 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
779 (void) SetImageProperty(next,"date:create",timestamp);
780 if (read_info->verbose != MagickFalse)
781 (void) IdentifyImage(next,stdout,MagickFalse);
784 read_info=DestroyImageInfo(read_info);
785 return(GetFirstImageInList(image));
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % R e a d I m a g e s %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % ReadImages() reads one or more images and returns them as an image list.
801 % The format of the ReadImage method is:
803 % Image *ReadImages(const ImageInfo *image_info,ExceptionInfo *exception)
805 % A description of each parameter follows:
807 % o image_info: the image info.
809 % o exception: return any errors or warnings in this structure.
812 MagickExport Image *ReadImages(const ImageInfo *image_info,
813 ExceptionInfo *exception)
816 filename[MaxTextExtent];
826 Read image list from a file.
828 assert(image_info != (ImageInfo *) NULL);
829 assert(image_info->signature == MagickSignature);
830 if (image_info->debug != MagickFalse)
831 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
832 image_info->filename);
833 assert(exception != (ExceptionInfo *) NULL);
834 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
835 (int) image_info->scene,filename);
836 if (LocaleCompare(filename,image_info->filename) != 0)
846 Images of the form image-%d.png[1-5].
848 read_info=CloneImageInfo(image_info);
849 sans=AcquireExceptionInfo();
850 (void) SetImageInfo(read_info,0,sans);
851 sans=DestroyExceptionInfo(sans);
852 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
853 images=NewImageList();
854 extent=(long) (read_info->scene+read_info->number_scenes);
855 for (scene=(long) read_info->scene; scene < (long) extent; scene++)
857 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
858 scene,read_info->filename);
859 image=ReadImage(read_info,exception);
860 if (image == (Image *) NULL)
862 AppendImageToList(&images,image);
864 read_info=DestroyImageInfo(read_info);
867 return(ReadImage(image_info,exception));
871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 + R e a d I n l i n e I m a g e %
879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
882 % The method returns a NULL if there is a memory shortage or if the image
883 % cannot be read. On failure, a NULL image is returned and exception
884 % describes the reason for the failure.
886 % The format of the ReadInlineImage method is:
888 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
889 % ExceptionInfo *exception)
891 % A description of each parameter follows:
893 % o image_info: the image info.
895 % o content: the image encoded in Base64.
897 % o exception: return any errors or warnings in this structure.
900 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
901 const char *content,ExceptionInfo *exception)
918 image=NewImageList();
919 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
921 ThrowReaderException(CorruptImageError,"CorruptImage");
924 blob=Base64Decode(p,&length);
926 ThrowReaderException(CorruptImageError,"CorruptImage");
927 read_info=CloneImageInfo(image_info);
928 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
930 image=BlobToImage(read_info,blob,length,exception);
931 blob=(unsigned char *) RelinquishMagickMemory(blob);
932 read_info=DestroyImageInfo(read_info);
937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941 % W r i t e I m a g e %
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947 % WriteImage() writes an image or an image sequence to a file or filehandle.
948 % If writing to a file on disk, the name is defined by the filename member of
949 % the image structure. Write() returns MagickFalse is these is a memory
950 % shortage or if the image cannot be written. Check the exception member of
951 % image to determine the cause for any failure.
953 % The format of the WriteImage method is:
955 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image)
957 % A description of each parameter follows:
959 % o image_info: the image info.
961 % o image: the image.
964 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
968 filename[MaxTextExtent];
999 Determine image type from filename prefix or suffix (e.g. image.jpg).
1001 assert(image_info != (ImageInfo *) NULL);
1002 assert(image_info->signature == MagickSignature);
1003 if (image->debug != MagickFalse)
1004 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1005 image_info->filename);
1006 assert(image != (Image *) NULL);
1007 assert(image->signature == MagickSignature);
1008 sans_exception=AcquireExceptionInfo();
1009 write_info=CloneImageInfo(image_info);
1010 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1011 if (*write_info->magick == '\0')
1012 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1013 (void) SetImageInfo(write_info,1,sans_exception);
1014 if (LocaleCompare(write_info->magick,"clipmask") == 0)
1016 if (image->clip_mask == (Image *) NULL)
1018 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1019 OptionError,"NoClipPathDefined","`%s'",image->filename);
1020 return(MagickFalse);
1022 image=image->clip_mask;
1023 (void) SetImageInfo(write_info,1,sans_exception);
1025 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1026 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1027 domain=CoderPolicyDomain;
1028 rights=WritePolicyRights;
1029 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1031 sans_exception=DestroyExceptionInfo(sans_exception);
1033 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1035 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1036 sans_exception=DestroyExceptionInfo(sans_exception);
1037 if (magick_info != (const MagickInfo *) NULL)
1039 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1040 image->endian=UndefinedEndian;
1042 if ((image_info->endian == UndefinedEndian) &&
1043 (GetMagickRawSupport(magick_info) != MagickFalse))
1049 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1052 (void) SyncImageProfiles(image);
1053 option=GetImageOption(image_info,"delegate:bimodal");
1054 if ((option != (const char *) NULL) &&
1055 (IsMagickTrue(option) != MagickFalse) &&
1056 (write_info->page == (char *) NULL) &&
1057 (GetPreviousImageInList(image) == (Image *) NULL) &&
1058 (GetNextImageInList(image) == (Image *) NULL) &&
1059 (IsTaintImage(image) == MagickFalse))
1061 delegate_info=GetDelegateInfo(image->magick,write_info->magick,
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,&image->exception);
1074 write_info=DestroyImageInfo(write_info);
1075 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1080 temporary=MagickFalse;
1081 if ((magick_info != (const MagickInfo *) NULL) &&
1082 (GetMagickSeekableStream(magick_info) != MagickFalse))
1085 filename[MaxTextExtent];
1087 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1088 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1089 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1090 if (status != MagickFalse)
1092 if (IsBlobSeekable(image) == MagickFalse)
1095 A seekable stream is required by the encoder.
1097 (void) CopyMagickString(write_info->filename,image->filename,
1099 (void) AcquireUniqueFilename(image->filename);
1100 temporary=MagickTrue;
1102 (void) CloseBlob(image);
1105 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1106 AcquireSemaphoreInfo(&constitute_semaphore);
1107 if ((magick_info != (const MagickInfo *) NULL) &&
1108 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1111 Call appropriate image writer based on image type.
1113 thread_support=GetMagickThreadSupport(magick_info);
1114 if ((thread_support & EncoderThreadSupport) == 0)
1115 LockSemaphoreInfo(constitute_semaphore);
1116 status=GetImageEncoder(magick_info)(write_info,image);
1117 if ((thread_support & EncoderThreadSupport) == 0)
1118 UnlockSemaphoreInfo(constitute_semaphore);
1122 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,
1124 if (delegate_info != (DelegateInfo *) NULL)
1127 Process the image with delegate.
1129 *write_info->filename='\0';
1130 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1131 LockSemaphoreInfo(constitute_semaphore);
1132 status=InvokeDelegate(write_info,image,(char *) NULL,
1133 write_info->magick,&image->exception);
1134 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1135 UnlockSemaphoreInfo(constitute_semaphore);
1136 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1140 sans_exception=AcquireExceptionInfo();
1141 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1142 sans_exception=DestroyExceptionInfo(sans_exception);
1143 if ((write_info->affirm == MagickFalse) &&
1144 (magick_info == (const MagickInfo *) NULL))
1146 (void) CopyMagickString(write_info->magick,image->magick,
1148 magick_info=GetMagickInfo(write_info->magick,&image->exception);
1150 if ((magick_info == (const MagickInfo *) NULL) ||
1151 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1153 magick_info=GetMagickInfo(image->magick,&image->exception);
1154 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1156 if ((magick_info == (const MagickInfo *) NULL) ||
1157 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1158 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1159 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
1164 Call appropriate image writer based on image type.
1166 thread_support=GetMagickThreadSupport(magick_info);
1167 if ((thread_support & EncoderThreadSupport) == 0)
1168 LockSemaphoreInfo(constitute_semaphore);
1169 status=GetImageEncoder(magick_info)(write_info,image);
1170 if ((thread_support & EncoderThreadSupport) == 0)
1171 UnlockSemaphoreInfo(constitute_semaphore);
1175 if (GetBlobError(image) != MagickFalse)
1176 ThrowFileException(&image->exception,FileOpenError,
1177 "AnErrorHasOccurredWritingToFile",image->filename);
1178 if (temporary == MagickTrue)
1181 Copy temporary image file to permanent.
1183 status=OpenBlob(write_info,image,ReadBinaryBlobMode,&image->exception);
1184 if (status != MagickFalse)
1185 status=ImageToFile(image,write_info->filename,&image->exception);
1186 (void) RelinquishUniqueFileResource(image->filename);
1187 (void) CopyMagickString(image->filename,write_info->filename,
1189 (void) CloseBlob(image);
1191 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1192 (write_info->verbose != MagickFalse))
1193 (void) IdentifyImage(image,stdout,MagickFalse);
1194 write_info=DestroyImageInfo(write_info);
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1203 % W r i t e I m a g e s %
1207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1209 % WriteImages() writes an image sequence.
1211 % The format of the WriteImages method is:
1213 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1214 % const char *filename,ExceptionInfo *exception)
1216 % A description of each parameter follows:
1218 % o image_info: the image info.
1220 % o images: the image list.
1222 % o filename: the image filename.
1224 % o exception: return any errors or warnings in this structure.
1227 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1228 Image *images,const char *filename,ExceptionInfo *exception)
1230 #define WriteImageTag "Write/Image"
1247 MagickProgressMonitor
1259 assert(image_info != (const ImageInfo *) NULL);
1260 assert(image_info->signature == MagickSignature);
1261 assert(images != (Image *) NULL);
1262 assert(images->signature == MagickSignature);
1263 if (images->debug != MagickFalse)
1264 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1265 assert(exception != (ExceptionInfo *) NULL);
1266 write_info=CloneImageInfo(image_info);
1267 images=GetFirstImageInList(images);
1268 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1269 DestroyBlob(images);
1271 if (filename != (const char *) NULL)
1272 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1273 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1274 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1275 if (*write_info->magick == '\0')
1276 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1277 sans_exception=AcquireExceptionInfo();
1278 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1280 sans_exception=DestroyExceptionInfo(sans_exception);
1282 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1283 if (p->scene >= GetNextImageInList(p)->scene)
1289 Generate consistent scene numbers.
1291 i=(long) images->scene;
1292 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1293 p->scene=(unsigned long) i++;
1300 progress_monitor=(MagickProgressMonitor) NULL;
1302 number_images=GetImageListLength(images);
1303 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1305 if (number_images != 1)
1306 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1308 status&=WriteImage(write_info,p);
1309 GetImageException(p,exception);
1310 if (number_images != 1)
1311 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1312 if (write_info->adjoin != MagickFalse)
1314 if (number_images != 1)
1316 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1317 if (proceed == MagickFalse)
1321 write_info=DestroyImageInfo(write_info);
1322 return(status != 0 ? MagickTrue : MagickFalse);