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/coder-private.h"
51#include "MagickCore/colorspace-private.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/constitute-private.h"
54#include "MagickCore/delegate.h"
55#include "MagickCore/geometry.h"
56#include "MagickCore/identify.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/magick.h"
60#include "MagickCore/memory_.h"
61#include "MagickCore/monitor.h"
62#include "MagickCore/monitor-private.h"
63#include "MagickCore/option.h"
64#include "MagickCore/pixel.h"
65#include "MagickCore/pixel-accessor.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/profile.h"
68#include "MagickCore/profile-private.h"
69#include "MagickCore/property.h"
70#include "MagickCore/quantum.h"
71#include "MagickCore/resize.h"
72#include "MagickCore/resource_.h"
73#include "MagickCore/semaphore.h"
74#include "MagickCore/statistic.h"
75#include "MagickCore/stream.h"
76#include "MagickCore/string_.h"
77#include "MagickCore/string-private.h"
78#include "MagickCore/timer.h"
79#include "MagickCore/timer-private.h"
80#include "MagickCore/token.h"
81#include "MagickCore/transform.h"
82#include "MagickCore/utility.h"
83#include "MagickCore/utility-private.h"
88#define MaxReadRecursionDepth 100
164MagickExport Image *ConstituteImage(
const size_t columns,
const size_t rows,
165 const char *map,
const StorageType storage,
const void *pixels,
166 ExceptionInfo *exception)
183 assert(map != (
const char *) NULL);
184 assert(pixels != (
void *) NULL);
185 assert(exception != (ExceptionInfo *) NULL);
186 assert(exception->signature == MagickCoreSignature);
187 if (IsEventLogging() != MagickFalse)
188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",map);
189 image=AcquireImage((ImageInfo *) NULL,exception);
190 if (image == (Image *) NULL)
191 return((Image *) NULL);
194 case CharPixel: image->depth=8*
sizeof(
unsigned char);
break;
195 case DoublePixel: image->depth=8*
sizeof(double);
break;
196 case FloatPixel: image->depth=8*
sizeof(float);
break;
197 case LongPixel: image->depth=8*
sizeof(
unsigned long);
break;
198 case LongLongPixel: image->depth=8*
sizeof(MagickSizeType);
break;
199 case ShortPixel: image->depth=8*
sizeof(
unsigned short);
break;
203 for (i=0; i < (ssize_t) length; i++)
212 image->alpha_trait=BlendPixelTrait;
224 image->colorspace=CMYKColorspace;
230 image->colorspace=GRAYColorspace;
236 image->colorspace=GRAYColorspace;
241 status=SetImageExtent(image,columns,rows,exception);
242 if (status == MagickFalse)
243 return(DestroyImageList(image));
244 status=ResetImagePixels(image,exception);
245 if (status == MagickFalse)
246 return(DestroyImageList(image));
247 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
248 if (status == MagickFalse)
249 image=DestroyImage(image);
282#if defined(__cplusplus) || defined(c_plusplus)
286static size_t PingStream(
const Image *magick_unused(image),
287 const void *magick_unused(pixels),
const size_t columns)
289 magick_unreferenced(image);
290 magick_unreferenced(pixels);
294#if defined(__cplusplus) || defined(c_plusplus)
298MagickExport Image *PingImage(
const ImageInfo *image_info,
299 ExceptionInfo *exception)
307 assert(image_info != (ImageInfo *) NULL);
308 assert(image_info->signature == MagickCoreSignature);
309 assert(exception != (ExceptionInfo *) NULL);
310 if (IsEventLogging() != MagickFalse)
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
312 image_info->filename);
313 ping_info=CloneImageInfo(image_info);
314 ping_info->ping=MagickTrue;
315 image=ReadStream(ping_info,&PingStream,exception);
316 if (image != (Image *) NULL)
318 ResetTimer(&image->timer);
319 if (ping_info->verbose != MagickFalse)
320 (void) IdentifyImage(image,stdout,MagickFalse,exception);
322 ping_info=DestroyImageInfo(ping_info);
353MagickExport Image *PingImages(ImageInfo *image_info,
const char *filename,
354 ExceptionInfo *exception)
357 ping_filename[MagickPathExtent];
369 assert(image_info != (ImageInfo *) NULL);
370 assert(image_info->signature == MagickCoreSignature);
371 assert(exception != (ExceptionInfo *) NULL);
372 if (IsEventLogging() != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
374 image_info->filename);
375 (void) SetImageOption(image_info,
"filename",filename);
376 (void) CopyMagickString(image_info->filename,filename,MagickPathExtent);
377 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
378 (
int) image_info->scene,ping_filename,exception);
379 if (LocaleCompare(ping_filename,image_info->filename) != 0)
391 read_info=CloneImageInfo(image_info);
392 sans=AcquireExceptionInfo();
393 (void) SetImageInfo(read_info,0,sans);
394 sans=DestroyExceptionInfo(sans);
395 if (read_info->number_scenes == 0)
397 read_info=DestroyImageInfo(read_info);
398 return(PingImage(image_info,exception));
400 (void) CopyMagickString(ping_filename,read_info->filename,
402 images=NewImageList();
403 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
404 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
406 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
407 (
int) scene,read_info->filename,exception);
408 image=PingImage(read_info,exception);
409 if (image == (Image *) NULL)
411 AppendImageToList(&images,image);
413 read_info=DestroyImageInfo(read_info);
416 return(PingImage(image_info,exception));
448static MagickBooleanType IsCoderAuthorized(
const char *module,
449 const char *coder,
const PolicyRights rights,ExceptionInfo *exception)
451 if (IsRightsAuthorized(CoderPolicyDomain,rights,coder) == MagickFalse)
454 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
455 "NotAuthorized",
"`%s'",coder);
458 if (IsRightsAuthorized(ModulePolicyDomain,rights,module) == MagickFalse)
461 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
462 "NotAuthorized",
"`%s'",module);
468static void InitializeConstituteInfo(
const ImageInfo *image_info,
469 ConstituteInfo *constitute_info)
474 memset(constitute_info,0,
sizeof(*constitute_info));
475 constitute_info->sync_from_exif=MagickTrue;
476 constitute_info->sync_from_tiff=MagickTrue;
477 option=GetImageOption(image_info,
"exif:sync-image");
478 if (IsStringFalse(option) != MagickFalse)
479 constitute_info->sync_from_exif=MagickFalse;
480 option=GetImageOption(image_info,
"tiff:sync-image");
481 if (IsStringFalse(option) != MagickFalse)
482 constitute_info->sync_from_tiff=MagickFalse;
483 constitute_info->caption=GetImageOption(image_info,
"caption");
484 constitute_info->comment=GetImageOption(image_info,
"comment");
485 constitute_info->label=GetImageOption(image_info,
"label");
486 option=GetImageOption(image_info,
"delay");
487 if (option != (
const char *) NULL)
492 constitute_info->delay_flags=ParseGeometry(option,&geometry_info);
493 if (constitute_info->delay_flags != NoValue)
495 constitute_info->delay=(size_t) floor(geometry_info.rho+0.5);
496 if ((constitute_info->delay_flags & SigmaValue) != 0)
497 constitute_info->ticks_per_second=CastDoubleToSsizeT(floor(
498 geometry_info.sigma+0.5));
503static void SyncOrientationFromProperties(Image *image,
504 ConstituteInfo *constitute_info,ExceptionInfo *exception)
509 orientation=(
const char *) NULL;
510 if (constitute_info->sync_from_exif != MagickFalse)
512 orientation=GetImageProperty(image,
"exif:Orientation",exception);
513 if (orientation != (
const char *) NULL)
515 image->orientation=(OrientationType) StringToLong(orientation);
516 (void) DeleteImageProperty(image,
"exif:Orientation");
519 if ((orientation == (
const char *) NULL) &&
520 (constitute_info->sync_from_tiff != MagickFalse))
522 orientation=GetImageProperty(image,
"tiff:Orientation",exception);
523 if (orientation != (
const char *) NULL)
525 image->orientation=(OrientationType) StringToLong(orientation);
526 (void) DeleteImageProperty(image,
"tiff:Orientation");
531static void SyncResolutionFromProperties(Image *image,
532 ConstituteInfo *constitute_info, ExceptionInfo *exception)
542 resolution_x=(
const char *) NULL;
543 resolution_y=(
const char *) NULL;
544 resolution_units=(
const char *) NULL;
545 used_tiff=MagickFalse;
546 if (constitute_info->sync_from_exif != MagickFalse)
548 resolution_x=GetImageProperty(image,
"exif:XResolution",exception);
549 resolution_y=GetImageProperty(image,
"exif:YResolution",exception);
550 if ((resolution_x != (
const char *) NULL) &&
551 (resolution_y != (
const char *) NULL))
552 resolution_units=GetImageProperty(image,
"exif:ResolutionUnit",
555 if ((resolution_x == (
const char *) NULL) &&
556 (resolution_y == (
const char *) NULL) &&
557 (constitute_info->sync_from_tiff != MagickFalse))
559 resolution_x=GetImageProperty(image,
"tiff:XResolution",exception);
560 resolution_y=GetImageProperty(image,
"tiff:YResolution",exception);
561 if ((resolution_x != (
const char *) NULL) &&
562 (resolution_y != (
const char *) NULL))
564 used_tiff=MagickTrue;
565 resolution_units=GetImageProperty(image,
"tiff:ResolutionUnit",
569 if ((resolution_x != (
const char *) NULL) &&
570 (resolution_y != (
const char *) NULL))
578 geometry_info.rho=image->resolution.x;
579 geometry_info.sigma=1.0;
580 (void) ParseGeometry(resolution_x,&geometry_info);
581 if (geometry_info.sigma != 0)
582 image->resolution.x=geometry_info.rho/geometry_info.sigma;
583 if (strchr(resolution_x,
',') != (
char *) NULL)
584 image->resolution.x=geometry_info.rho+geometry_info.sigma/1000.0;
585 geometry_info.rho=image->resolution.y;
586 geometry_info.sigma=1.0;
587 (void) ParseGeometry(resolution_y,&geometry_info);
588 if (geometry_info.sigma != 0)
589 image->resolution.y=geometry_info.rho/geometry_info.sigma;
590 if (strchr(resolution_y,
',') != (
char *) NULL)
591 image->resolution.y=geometry_info.rho+geometry_info.sigma/1000.0;
592 if (resolution_units != (
char *) NULL)
594 option_type=ParseCommandOption(MagickResolutionOptions,MagickFalse,
596 if (option_type >= 0)
597 image->units=(ResolutionType) option_type;
599 if (used_tiff == MagickFalse)
601 (void) DeleteImageProperty(image,
"exif:XResolution");
602 (void) DeleteImageProperty(image,
"exif:YResolution");
603 (void) DeleteImageProperty(image,
"exif:ResolutionUnit");
607 (void) DeleteImageProperty(image,
"tiff:XResolution");
608 (void) DeleteImageProperty(image,
"tiff:YResolution");
609 (void) DeleteImageProperty(image,
"tiff:ResolutionUnit");
614MagickExport Image *ReadImage(
const ImageInfo *image_info,
615 ExceptionInfo *exception)
618 filename[MagickPathExtent],
619 magick[MagickPathExtent],
620 magick_filename[MagickPathExtent];
650 assert(image_info != (ImageInfo *) NULL);
651 assert(image_info->signature == MagickCoreSignature);
652 assert(image_info->filename != (
char *) NULL);
653 if (IsEventLogging() != MagickFalse)
654 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
655 image_info->filename);
656 assert(exception != (ExceptionInfo *) NULL);
657 read_info=CloneImageInfo(image_info);
658 (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
659 (void) SetImageInfo(read_info,0,exception);
660 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
661 (void) CopyMagickString(magick,read_info->magick,MagickPathExtent);
665 sans_exception=AcquireExceptionInfo();
666 magick_info=GetMagickInfo(read_info->magick,sans_exception);
667 if (sans_exception->severity == PolicyError)
668 InheritException(exception,sans_exception);
669 sans_exception=DestroyExceptionInfo(sans_exception);
670 if (magick_info != (
const MagickInfo *) NULL)
672 if (GetMagickEndianSupport(magick_info) == MagickFalse)
673 read_info->endian=UndefinedEndian;
675 if ((image_info->endian == UndefinedEndian) &&
676 (GetMagickRawSupport(magick_info) != MagickFalse))
682 read_info->endian=(*(
char *) &lsb_first) == 1 ? LSBEndian :
686 if ((magick_info != (
const MagickInfo *) NULL) &&
687 (GetMagickDecoderSeekableStream(magick_info) != MagickFalse))
689 image=AcquireImage(read_info,exception);
690 (void) CopyMagickString(image->filename,read_info->filename,
692 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
693 if (status == MagickFalse)
695 read_info=DestroyImageInfo(read_info);
696 image=DestroyImage(image);
697 return((Image *) NULL);
699 if (IsBlobSeekable(image) == MagickFalse)
704 *read_info->filename=
'\0';
705 status=ImageToFile(image,read_info->filename,exception);
706 if (status == MagickFalse)
708 (void) CloseBlob(image);
709 read_info=DestroyImageInfo(read_info);
710 image=DestroyImage(image);
711 return((Image *) NULL);
713 read_info->temporary=MagickTrue;
715 (void) CloseBlob(image);
716 image=DestroyImage(image);
718 image=NewImageList();
719 decoder=GetImageDecoder(magick_info);
720 if (decoder == (DecodeImageHandler *) NULL)
722 delegate_info=GetDelegateInfo(read_info->magick,(
char *) NULL,exception);
723 if (delegate_info == (
const DelegateInfo *) NULL)
725 (void) SetImageInfo(read_info,0,exception);
726 (void) CopyMagickString(read_info->filename,filename,
728 magick_info=GetMagickInfo(read_info->magick,exception);
729 decoder=GetImageDecoder(magick_info);
732 if (decoder != (DecodeImageHandler *) NULL)
737 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
738 LockSemaphoreInfo(magick_info->semaphore);
739 status=IsCoderAuthorized(magick_info->magick_module,read_info->magick,
740 ReadPolicyRights,exception);
741 image=(Image *) NULL;
742 if (status != MagickFalse)
743 image=decoder(read_info,exception);
744 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
745 UnlockSemaphoreInfo(magick_info->semaphore);
749 delegate_info=GetDelegateInfo(read_info->magick,(
char *) NULL,exception);
750 if (delegate_info == (
const DelegateInfo *) NULL)
752 (void) ThrowMagickException(exception,GetMagickModule(),
753 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
754 read_info->filename);
755 if (read_info->temporary != MagickFalse)
756 (void) RelinquishUniqueFileResource(read_info->filename);
757 read_info=DestroyImageInfo(read_info);
758 return((Image *) NULL);
763 image=AcquireImage(read_info,exception);
764 if (image == (Image *) NULL)
766 read_info=DestroyImageInfo(read_info);
767 return((Image *) NULL);
769 (void) CopyMagickString(image->filename,read_info->filename,
771 *read_info->filename=
'\0';
772 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
773 LockSemaphoreInfo(delegate_info->semaphore);
774 status=InvokeDelegate(read_info,image,read_info->magick,(
char *) NULL,
776 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
777 UnlockSemaphoreInfo(delegate_info->semaphore);
778 image=DestroyImageList(image);
779 read_info->temporary=MagickTrue;
780 if (status != MagickFalse)
781 (void) SetImageInfo(read_info,0,exception);
782 magick_info=GetMagickInfo(read_info->magick,exception);
783 decoder=GetImageDecoder(magick_info);
784 if (decoder == (DecodeImageHandler *) NULL)
786 if (IsPathAccessible(read_info->filename) != MagickFalse)
787 (void) ThrowMagickException(exception,GetMagickModule(),
788 MissingDelegateError,
"NoDecodeDelegateForThisImageFormat",
"`%s'",
791 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",
792 read_info->filename);
793 read_info=DestroyImageInfo(read_info);
794 return((Image *) NULL);
799 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
800 LockSemaphoreInfo(magick_info->semaphore);
801 status=IsCoderAuthorized(magick_info->magick_module,read_info->magick,
802 ReadPolicyRights,exception);
803 image=(Image *) NULL;
804 if (status != MagickFalse)
805 image=(decoder)(read_info,exception);
806 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
807 UnlockSemaphoreInfo(magick_info->semaphore);
809 if (read_info->temporary != MagickFalse)
811 (void) RelinquishUniqueFileResource(read_info->filename);
812 read_info->temporary=MagickFalse;
813 if (image != (Image *) NULL)
814 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
816 if (image == (Image *) NULL)
818 read_info=DestroyImageInfo(read_info);
821 if (exception->severity >= ErrorException)
822 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
823 "Coder (%s) generated an image despite an error (%d), "
824 "notify the developers",image->magick,exception->severity);
825 if (IsBlobTemporary(image) != MagickFalse)
826 (void) RelinquishUniqueFileResource(read_info->filename);
827 if ((IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse) &&
828 (GetImageListLength(image) != 1))
833 clones=CloneImages(image,read_info->scenes,exception);
834 image=DestroyImageList(image);
835 if (clones != (Image *) NULL)
836 image=GetFirstImageInList(clones);
837 if (image == (Image *) NULL)
839 read_info=DestroyImageInfo(read_info);
843 InitializeConstituteInfo(read_info,&constitute_info);
844 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
847 magick_path[MagickPathExtent],
853 next->taint=MagickFalse;
854 GetPathComponent(magick_filename,MagickPath,magick_path);
855 if ((*magick_path ==
'\0') && (*next->magick ==
'\0'))
856 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
857 (void) CopyMagickString(next->magick_filename,magick_filename,
859 if (IsBlobTemporary(image) != MagickFalse)
860 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
861 if (next->magick_columns == 0)
862 next->magick_columns=next->columns;
863 if (next->magick_rows == 0)
864 next->magick_rows=next->rows;
865 (void) GetImageProperty(next,
"exif:*",exception);
866 (void) GetImageProperty(next,
"icc:*",exception);
867 (void) GetImageProperty(next,
"iptc:*",exception);
868 (void) GetImageProperty(next,
"xmp:*",exception);
869 SyncOrientationFromProperties(next,&constitute_info,exception);
870 SyncResolutionFromProperties(next,&constitute_info,exception);
871 if (next->page.width == 0)
872 next->page.width=next->columns;
873 if (next->page.height == 0)
874 next->page.height=next->rows;
875 if (constitute_info.caption != (
const char *) NULL)
877 property=InterpretImageProperties(read_info,next,
878 constitute_info.caption,exception);
879 (void) SetImageProperty(next,
"caption",property,exception);
880 property=DestroyString(property);
882 if (constitute_info.comment != (
const char *) NULL)
884 property=InterpretImageProperties(read_info,next,
885 constitute_info.comment,exception);
886 (void) SetImageProperty(next,
"comment",property,exception);
887 property=DestroyString(property);
889 if (constitute_info.label != (
const char *) NULL)
891 property=InterpretImageProperties(read_info,next,
892 constitute_info.label,exception);
893 (void) SetImageProperty(next,
"label",property,exception);
894 property=DestroyString(property);
896 if (LocaleCompare(next->magick,
"TEXT") == 0)
897 (void) ParseAbsoluteGeometry(
"0x0+0+0",&next->page);
898 if ((read_info->extract != (
char *) NULL) &&
899 (read_info->stream == (StreamHandler) NULL))
907 SetGeometry(next,&geometry);
908 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
909 if ((next->columns != geometry.width) ||
910 (next->rows != geometry.height))
912 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
914 Image *crop_image = CropImage(next,&geometry,exception);
915 if (crop_image != (Image *) NULL)
916 ReplaceImageInList(&next,crop_image);
919 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
921 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
923 if ((geometry.width != 0) && (geometry.height != 0))
925 Image *resize_image = ResizeImage(next,geometry.width,
926 geometry.height,next->filter,exception);
927 if (resize_image != (Image *) NULL)
928 ReplaceImageInList(&next,resize_image);
933 profile=GetImageProfile(next,
"icc");
934 if (profile == (
const StringInfo *) NULL)
935 profile=GetImageProfile(next,
"icm");
936 profile=GetImageProfile(next,
"iptc");
937 if (profile == (
const StringInfo *) NULL)
938 profile=GetImageProfile(next,
"8bim");
939 if (IsSourceDataEpochSet() == MagickFalse)
942 timestamp[MagickTimeExtent];
944 (void) FormatMagickTime(next->timestamp,
sizeof(timestamp),timestamp);
945 (void) SetImageProperty(next,
"date:timestamp",timestamp,exception);
946 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
947 sizeof(timestamp),timestamp);
948 (void) SetImageProperty(next,
"date:modify",timestamp,exception);
949 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
950 sizeof(timestamp),timestamp);
951 (void) SetImageProperty(next,
"date:create",timestamp,exception);
953 if (constitute_info.delay_flags != NoValue)
955 if ((constitute_info.delay_flags & GreaterValue) != 0)
957 if (next->delay > constitute_info.delay)
958 next->delay=constitute_info.delay;
961 if ((constitute_info.delay_flags & LessValue) != 0)
963 if (next->delay < constitute_info.delay)
964 next->delay=constitute_info.delay;
967 next->delay=constitute_info.delay;
968 if ((constitute_info.delay_flags & SigmaValue) != 0)
969 next->ticks_per_second=constitute_info.ticks_per_second;
971 if (constitute_info.dispose != (
const char *) NULL)
976 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
977 constitute_info.dispose);
978 if (option_type >= 0)
979 next->dispose=(DisposeType) option_type;
981 if (read_info->verbose != MagickFalse)
982 (void) IdentifyImage(next,stderr,MagickFalse,exception);
985 read_info=DestroyImageInfo(read_info);
986 if (GetBlobError(image) != MagickFalse)
987 ThrowReaderException(CorruptImageError,
"UnableToReadImageData");
988 return(GetFirstImageInList(image));
1018MagickExport Image *ReadImages(ImageInfo *image_info,
const char *filename,
1019 ExceptionInfo *exception)
1022 read_filename[MagickPathExtent];
1034 assert(image_info != (ImageInfo *) NULL);
1035 assert(image_info->signature == MagickCoreSignature);
1036 assert(exception != (ExceptionInfo *) NULL);
1037 if (IsEventLogging() != MagickFalse)
1038 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1039 image_info->filename);
1040 read_info=CloneImageInfo(image_info);
1041 *read_info->magick=
'\0';
1042 (void) SetImageOption(read_info,
"filename",filename);
1043 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1044 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
1045 (
int) read_info->scene,read_filename,exception);
1046 if (LocaleCompare(read_filename,read_info->filename) != 0)
1058 sans=AcquireExceptionInfo();
1059 (void) SetImageInfo(read_info,0,sans);
1060 sans=DestroyExceptionInfo(sans);
1061 if (read_info->number_scenes != 0)
1063 (void) CopyMagickString(read_filename,read_info->filename,
1065 images=NewImageList();
1066 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
1067 scene=(ssize_t) read_info->scene;
1068 for ( ; scene < (ssize_t) extent; scene++)
1070 (void) InterpretImageFilename(image_info,(Image *) NULL,
1071 read_filename,(
int) scene,read_info->filename,exception);
1072 image=ReadImage(read_info,exception);
1073 if (image == (Image *) NULL)
1075 AppendImageToList(&images,image);
1077 read_info=DestroyImageInfo(read_info);
1081 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1082 image=ReadImage(read_info,exception);
1083 read_info=DestroyImageInfo(read_info);
1117MagickExport Image *ReadInlineImage(
const ImageInfo *image_info,
1118 const char *content,ExceptionInfo *exception)
1138 image=NewImageList();
1139 for (p=content; (*p !=
',') && (*p !=
'\0'); p++) ;
1141 ThrowReaderException(CorruptImageError,
"CorruptImage");
1142 blob=Base64Decode(++p,&length);
1145 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1146 ThrowReaderException(CorruptImageError,
"CorruptImage");
1148 read_info=CloneImageInfo(image_info);
1149 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1151 *read_info->filename=
'\0';
1152 *read_info->magick=
'\0';
1153 for (p=content; (*p !=
'/') && (*p !=
'\0'); p++) ;
1165 if (LocaleNCompare(++p,
"x-",2) == 0)
1167 (void) CopyMagickString(read_info->filename,
"data.",MagickPathExtent);
1168 q=read_info->filename+5;
1169 for (i=0; (*p !=
';') && (*p !=
'\0') && (i < (MagickPathExtent-6)); i++)
1173 image=BlobToImage(read_info,blob,length,exception);
1174 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1175 read_info=DestroyImageInfo(read_info);
1210MagickExport MagickBooleanType WriteImage(
const ImageInfo *image_info,
1211 Image *image,ExceptionInfo *exception)
1214 filename[MagickPathExtent];
1241 assert(image_info != (ImageInfo *) NULL);
1242 assert(image_info->signature == MagickCoreSignature);
1243 assert(image != (Image *) NULL);
1244 if (IsEventLogging() != MagickFalse)
1245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1246 image_info->filename);
1247 assert(image->signature == MagickCoreSignature);
1248 assert(exception != (ExceptionInfo *) NULL);
1249 sans_exception=AcquireExceptionInfo();
1250 write_info=CloneImageInfo(image_info);
1251 (void) CopyMagickString(write_info->filename,image->filename,
1253 (void) SetImageInfo(write_info,1,sans_exception);
1254 if (*write_info->magick ==
'\0')
1255 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1256 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1257 (void) CopyMagickString(image->filename,write_info->filename,
1262 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1263 if (sans_exception->severity == PolicyError)
1264 magick_info=GetMagickInfo(write_info->magick,exception);
1265 sans_exception=DestroyExceptionInfo(sans_exception);
1266 if (magick_info != (
const MagickInfo *) NULL)
1268 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1269 image->endian=UndefinedEndian;
1271 if ((image_info->endian == UndefinedEndian) &&
1272 (GetMagickRawSupport(magick_info) != MagickFalse))
1278 image->endian=(*(
char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1281 SyncImageProfiles(image);
1282 DisassociateImageStream(image);
1283 option=GetImageOption(image_info,
"delegate:bimodal");
1284 if ((IsStringTrue(option) != MagickFalse) &&
1285 (write_info->page == (
char *) NULL) &&
1286 (GetPreviousImageInList(image) == (Image *) NULL) &&
1287 (GetNextImageInList(image) == (Image *) NULL) &&
1288 (IsTaintImage(image) == MagickFalse) )
1290 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1291 if ((delegate_info != (
const DelegateInfo *) NULL) &&
1292 (GetDelegateMode(delegate_info) == 0) &&
1293 (IsPathAccessible(image->magick_filename) != MagickFalse))
1298 (void) CopyMagickString(image->filename,image->magick_filename,
1300 status=InvokeDelegate(write_info,image,image->magick,
1301 write_info->magick,exception);
1302 write_info=DestroyImageInfo(write_info);
1303 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1308 temporary=MagickFalse;
1309 if ((magick_info != (
const MagickInfo *) NULL) &&
1310 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1313 image_filename[MagickPathExtent];
1315 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1316 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1317 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1318 if (status != MagickFalse)
1320 if (IsBlobSeekable(image) == MagickFalse)
1325 write_info->adjoin=MagickTrue;
1326 (void) CopyMagickString(write_info->filename,image->filename,
1328 (void) AcquireUniqueFilename(image->filename);
1329 temporary=MagickTrue;
1331 if (CloseBlob(image) == MagickFalse)
1335 encoder=GetImageEncoder(magick_info);
1336 if (encoder != (EncodeImageHandler *) NULL)
1341 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1342 LockSemaphoreInfo(magick_info->semaphore);
1343 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1344 WritePolicyRights,exception);
1345 if (status != MagickFalse)
1346 status=encoder(write_info,image,exception);
1347 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1348 UnlockSemaphoreInfo(magick_info->semaphore);
1352 delegate_info=GetDelegateInfo((
char *) NULL,write_info->magick,exception);
1353 if (delegate_info != (DelegateInfo *) NULL)
1358 *write_info->filename=
'\0';
1359 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1360 LockSemaphoreInfo(delegate_info->semaphore);
1361 status=InvokeDelegate(write_info,image,(
char *) NULL,
1362 write_info->magick,exception);
1363 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1364 UnlockSemaphoreInfo(delegate_info->semaphore);
1365 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1369 sans_exception=AcquireExceptionInfo();
1370 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1371 if (sans_exception->severity == PolicyError)
1372 magick_info=GetMagickInfo(write_info->magick,exception);
1373 sans_exception=DestroyExceptionInfo(sans_exception);
1374 if ((write_info->affirm == MagickFalse) &&
1375 (magick_info == (
const MagickInfo *) NULL))
1377 (void) CopyMagickString(write_info->magick,image->magick,
1379 magick_info=GetMagickInfo(write_info->magick,exception);
1381 encoder=GetImageEncoder(magick_info);
1382 if (encoder == (EncodeImageHandler *) NULL)
1385 extension[MagickPathExtent];
1387 GetPathComponent(image->filename,ExtensionPath,extension);
1388 if (*extension !=
'\0')
1389 magick_info=GetMagickInfo(extension,exception);
1391 magick_info=GetMagickInfo(image->magick,exception);
1392 (void) CopyMagickString(image->filename,filename,
1394 encoder=GetImageEncoder(magick_info);
1395 (void) ThrowMagickException(exception,GetMagickModule(),
1396 MissingDelegateWarning,
"NoEncodeDelegateForThisImageFormat",
1397 "`%s'",write_info->magick);
1399 if (encoder == (EncodeImageHandler *) NULL)
1401 magick_info=GetMagickInfo(image->magick,exception);
1402 encoder=GetImageEncoder(magick_info);
1403 if (encoder == (EncodeImageHandler *) NULL)
1404 (void) ThrowMagickException(exception,GetMagickModule(),
1405 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
1406 "`%s'",write_info->magick);
1408 if (encoder != (EncodeImageHandler *) NULL)
1413 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1414 LockSemaphoreInfo(magick_info->semaphore);
1415 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1416 WritePolicyRights,exception);
1417 if (status != MagickFalse)
1418 status=encoder(write_info,image,exception);
1419 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1420 UnlockSemaphoreInfo(magick_info->semaphore);
1424 if (temporary != MagickFalse)
1429 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1430 if (status != MagickFalse)
1432 (void) RelinquishUniqueFileResource(write_info->filename);
1433 status=ImageToFile(image,write_info->filename,exception);
1435 if (CloseBlob(image) == MagickFalse)
1437 (void) RelinquishUniqueFileResource(image->filename);
1438 (void) CopyMagickString(image->filename,write_info->filename,
1441 if ((LocaleCompare(write_info->magick,
"info") != 0) &&
1442 (write_info->verbose != MagickFalse))
1443 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1444 write_info=DestroyImageInfo(write_info);
1445 if (GetBlobError(image) != MagickFalse)
1446 ThrowWriterException(FileOpenError,
"UnableToWriteFile");
1486MagickExport MagickBooleanType WriteImages(
const ImageInfo *image_info,
1487 Image *images,
const char *filename,ExceptionInfo *exception)
1489#define WriteImageTag "Write/Image"
1503 MagickProgressMonitor
1515 assert(image_info != (
const ImageInfo *) NULL);
1516 assert(image_info->signature == MagickCoreSignature);
1517 assert(images != (Image *) NULL);
1518 assert(images->signature == MagickCoreSignature);
1519 if (IsEventLogging() != MagickFalse)
1520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",images->filename);
1521 assert(exception != (ExceptionInfo *) NULL);
1522 write_info=CloneImageInfo(image_info);
1523 *write_info->magick=
'\0';
1524 images=GetFirstImageInList(images);
1525 if (images == (Image *) NULL)
1526 return(MagickFalse);
1527 if (filename != (
const char *) NULL)
1528 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1529 (
void) CopyMagickString(p->filename,filename,MagickPathExtent);
1530 (void) CopyMagickString(write_info->filename,images->filename,
1532 sans_exception=AcquireExceptionInfo();
1533 (void) SetImageInfo(write_info,(
unsigned int) GetImageListLength(images),
1535 sans_exception=DestroyExceptionInfo(sans_exception);
1536 if (*write_info->magick ==
'\0')
1537 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1539 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1541 if (p->scene >= GetNextImageInList(p)->scene)
1549 i=(ssize_t) images->scene;
1550 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1551 p->scene=(
size_t) i++;
1559 progress_monitor=(MagickProgressMonitor) NULL;
1561 number_images=GetImageListLength(images);
1562 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1564 if (number_images != 1)
1565 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1567 status&=(MagickStatusType) WriteImage(write_info,p,exception);
1568 if (number_images != 1)
1569 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1570 if (write_info->adjoin != MagickFalse)
1572 if (number_images != 1)
1574#if defined(MAGICKCORE_OPENMP_SUPPORT)
1578 proceed=SetImageProgress(p,WriteImageTag,progress,number_images);
1579 if (proceed == MagickFalse)
1583 write_info=DestroyImageInfo(write_info);
1584 return(status != 0 ? MagickTrue : MagickFalse);