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))
917 crop_image=CropImage(next,&geometry,exception);
918 if (crop_image != (Image *) NULL)
919 ReplaceImageInList(&next,crop_image);
922 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
924 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
926 if ((geometry.width != 0) && (geometry.height != 0))
928 Image *resize_image = ResizeImage(next,geometry.width,
929 geometry.height,next->filter,exception);
930 if (resize_image != (Image *) NULL)
931 ReplaceImageInList(&next,resize_image);
936 profile=GetImageProfile(next,
"icc");
937 if (profile == (
const StringInfo *) NULL)
938 profile=GetImageProfile(next,
"icm");
939 profile=GetImageProfile(next,
"iptc");
940 if (profile == (
const StringInfo *) NULL)
941 profile=GetImageProfile(next,
"8bim");
942 if (IsSourceDataEpochSet() == MagickFalse)
945 timestamp[MagickTimeExtent];
947 (void) FormatMagickTime(next->timestamp,
sizeof(timestamp),timestamp);
948 (void) SetImageProperty(next,
"date:timestamp",timestamp,exception);
949 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
950 sizeof(timestamp),timestamp);
951 (void) SetImageProperty(next,
"date:modify",timestamp,exception);
952 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
953 sizeof(timestamp),timestamp);
954 (void) SetImageProperty(next,
"date:create",timestamp,exception);
956 if (constitute_info.delay_flags != NoValue)
958 if ((constitute_info.delay_flags & GreaterValue) != 0)
960 if (next->delay > constitute_info.delay)
961 next->delay=constitute_info.delay;
964 if ((constitute_info.delay_flags & LessValue) != 0)
966 if (next->delay < constitute_info.delay)
967 next->delay=constitute_info.delay;
970 next->delay=constitute_info.delay;
971 if ((constitute_info.delay_flags & SigmaValue) != 0)
972 next->ticks_per_second=constitute_info.ticks_per_second;
974 if (constitute_info.dispose != (
const char *) NULL)
979 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
980 constitute_info.dispose);
981 if (option_type >= 0)
982 next->dispose=(DisposeType) option_type;
984 if (read_info->verbose != MagickFalse)
985 (void) IdentifyImage(next,stderr,MagickFalse,exception);
988 read_info=DestroyImageInfo(read_info);
989 if (GetBlobError(image) != MagickFalse)
990 ThrowReaderException(CorruptImageError,
"UnableToReadImageData");
991 return(GetFirstImageInList(image));
1021MagickExport Image *ReadImages(ImageInfo *image_info,
const char *filename,
1022 ExceptionInfo *exception)
1025 read_filename[MagickPathExtent];
1037 assert(image_info != (ImageInfo *) NULL);
1038 assert(image_info->signature == MagickCoreSignature);
1039 assert(exception != (ExceptionInfo *) NULL);
1040 if (IsEventLogging() != MagickFalse)
1041 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1042 image_info->filename);
1043 read_info=CloneImageInfo(image_info);
1044 *read_info->magick=
'\0';
1045 (void) SetImageOption(read_info,
"filename",filename);
1046 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1047 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
1048 (
int) read_info->scene,read_filename,exception);
1049 if (LocaleCompare(read_filename,read_info->filename) != 0)
1061 sans=AcquireExceptionInfo();
1062 (void) SetImageInfo(read_info,0,sans);
1063 sans=DestroyExceptionInfo(sans);
1064 if (read_info->number_scenes != 0)
1066 (void) CopyMagickString(read_filename,read_info->filename,
1068 images=NewImageList();
1069 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
1070 scene=(ssize_t) read_info->scene;
1071 for ( ; scene < (ssize_t) extent; scene++)
1073 (void) InterpretImageFilename(image_info,(Image *) NULL,
1074 read_filename,(
int) scene,read_info->filename,exception);
1075 image=ReadImage(read_info,exception);
1076 if (image == (Image *) NULL)
1078 AppendImageToList(&images,image);
1080 read_info=DestroyImageInfo(read_info);
1084 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1085 image=ReadImage(read_info,exception);
1086 read_info=DestroyImageInfo(read_info);
1120MagickExport Image *ReadInlineImage(
const ImageInfo *image_info,
1121 const char *content,ExceptionInfo *exception)
1141 image=NewImageList();
1142 for (p=content; (*p !=
',') && (*p !=
'\0'); p++) ;
1144 ThrowReaderException(CorruptImageError,
"CorruptImage");
1145 blob=Base64Decode(++p,&length);
1148 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1149 ThrowReaderException(CorruptImageError,
"CorruptImage");
1151 read_info=CloneImageInfo(image_info);
1152 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1154 *read_info->filename=
'\0';
1155 *read_info->magick=
'\0';
1156 for (p=content; (*p !=
'/') && (*p !=
'\0'); p++) ;
1168 if (LocaleNCompare(++p,
"x-",2) == 0)
1170 (void) CopyMagickString(read_info->filename,
"data.",MagickPathExtent);
1171 q=read_info->filename+5;
1172 for (i=0; (*p !=
';') && (*p !=
'\0') && (i < (MagickPathExtent-6)); i++)
1176 image=BlobToImage(read_info,blob,length,exception);
1177 blob=(
unsigned char *) RelinquishMagickMemory(blob);
1178 read_info=DestroyImageInfo(read_info);
1213MagickExport MagickBooleanType WriteImage(
const ImageInfo *image_info,
1214 Image *image,ExceptionInfo *exception)
1217 filename[MagickPathExtent];
1244 assert(image_info != (ImageInfo *) NULL);
1245 assert(image_info->signature == MagickCoreSignature);
1246 assert(image != (Image *) NULL);
1247 if (IsEventLogging() != MagickFalse)
1248 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
1249 image_info->filename);
1250 assert(image->signature == MagickCoreSignature);
1251 assert(exception != (ExceptionInfo *) NULL);
1252 sans_exception=AcquireExceptionInfo();
1253 write_info=CloneImageInfo(image_info);
1254 (void) CopyMagickString(write_info->filename,image->filename,
1256 (void) SetImageInfo(write_info,1,sans_exception);
1257 if (*write_info->magick ==
'\0')
1258 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1259 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1260 (void) CopyMagickString(image->filename,write_info->filename,
1265 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1266 if (sans_exception->severity == PolicyError)
1267 magick_info=GetMagickInfo(write_info->magick,exception);
1268 sans_exception=DestroyExceptionInfo(sans_exception);
1269 if (magick_info != (
const MagickInfo *) NULL)
1271 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1272 image->endian=UndefinedEndian;
1274 if ((image_info->endian == UndefinedEndian) &&
1275 (GetMagickRawSupport(magick_info) != MagickFalse))
1281 image->endian=(*(
char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1284 SyncImageProfiles(image);
1285 DisassociateImageStream(image);
1286 option=GetImageOption(image_info,
"delegate:bimodal");
1287 if ((IsStringTrue(option) != MagickFalse) &&
1288 (write_info->page == (
char *) NULL) &&
1289 (GetPreviousImageInList(image) == (Image *) NULL) &&
1290 (GetNextImageInList(image) == (Image *) NULL) &&
1291 (IsTaintImage(image) == MagickFalse) )
1293 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1294 if ((delegate_info != (
const DelegateInfo *) NULL) &&
1295 (GetDelegateMode(delegate_info) == 0) &&
1296 (IsPathAccessible(image->magick_filename) != MagickFalse))
1301 (void) CopyMagickString(image->filename,image->magick_filename,
1303 status=InvokeDelegate(write_info,image,image->magick,
1304 write_info->magick,exception);
1305 write_info=DestroyImageInfo(write_info);
1306 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1311 temporary=MagickFalse;
1312 if ((magick_info != (
const MagickInfo *) NULL) &&
1313 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1316 image_filename[MagickPathExtent];
1318 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1319 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1320 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1321 if (status != MagickFalse)
1323 if (IsBlobSeekable(image) == MagickFalse)
1328 write_info->adjoin=MagickTrue;
1329 (void) CopyMagickString(write_info->filename,image->filename,
1331 (void) AcquireUniqueFilename(image->filename);
1332 temporary=MagickTrue;
1334 if (CloseBlob(image) == MagickFalse)
1338 encoder=GetImageEncoder(magick_info);
1339 if (encoder != (EncodeImageHandler *) NULL)
1344 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1345 LockSemaphoreInfo(magick_info->semaphore);
1346 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1347 WritePolicyRights,exception);
1348 if (status != MagickFalse)
1349 status=encoder(write_info,image,exception);
1350 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1351 UnlockSemaphoreInfo(magick_info->semaphore);
1355 delegate_info=GetDelegateInfo((
char *) NULL,write_info->magick,exception);
1356 if (delegate_info != (DelegateInfo *) NULL)
1361 *write_info->filename=
'\0';
1362 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1363 LockSemaphoreInfo(delegate_info->semaphore);
1364 status=InvokeDelegate(write_info,image,(
char *) NULL,
1365 write_info->magick,exception);
1366 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1367 UnlockSemaphoreInfo(delegate_info->semaphore);
1368 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1372 sans_exception=AcquireExceptionInfo();
1373 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1374 if (sans_exception->severity == PolicyError)
1375 magick_info=GetMagickInfo(write_info->magick,exception);
1376 sans_exception=DestroyExceptionInfo(sans_exception);
1377 if ((write_info->affirm == MagickFalse) &&
1378 (magick_info == (
const MagickInfo *) NULL))
1380 (void) CopyMagickString(write_info->magick,image->magick,
1382 magick_info=GetMagickInfo(write_info->magick,exception);
1384 encoder=GetImageEncoder(magick_info);
1385 if (encoder == (EncodeImageHandler *) NULL)
1388 extension[MagickPathExtent];
1390 GetPathComponent(image->filename,ExtensionPath,extension);
1391 if (*extension !=
'\0')
1392 magick_info=GetMagickInfo(extension,exception);
1394 magick_info=GetMagickInfo(image->magick,exception);
1395 (void) CopyMagickString(image->filename,filename,
1397 encoder=GetImageEncoder(magick_info);
1398 (void) ThrowMagickException(exception,GetMagickModule(),
1399 MissingDelegateWarning,
"NoEncodeDelegateForThisImageFormat",
1400 "`%s'",write_info->magick);
1402 if (encoder == (EncodeImageHandler *) NULL)
1404 magick_info=GetMagickInfo(image->magick,exception);
1405 encoder=GetImageEncoder(magick_info);
1406 if (encoder == (EncodeImageHandler *) NULL)
1407 (void) ThrowMagickException(exception,GetMagickModule(),
1408 MissingDelegateError,
"NoEncodeDelegateForThisImageFormat",
1409 "`%s'",write_info->magick);
1411 if (encoder != (EncodeImageHandler *) NULL)
1416 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1417 LockSemaphoreInfo(magick_info->semaphore);
1418 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1419 WritePolicyRights,exception);
1420 if (status != MagickFalse)
1421 status=encoder(write_info,image,exception);
1422 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1423 UnlockSemaphoreInfo(magick_info->semaphore);
1427 if (temporary != MagickFalse)
1432 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1433 if (status != MagickFalse)
1435 (void) RelinquishUniqueFileResource(write_info->filename);
1436 status=ImageToFile(image,write_info->filename,exception);
1438 if (CloseBlob(image) == MagickFalse)
1440 (void) RelinquishUniqueFileResource(image->filename);
1441 (void) CopyMagickString(image->filename,write_info->filename,
1444 if ((LocaleCompare(write_info->magick,
"info") != 0) &&
1445 (write_info->verbose != MagickFalse))
1446 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1447 write_info=DestroyImageInfo(write_info);
1448 if (GetBlobError(image) != MagickFalse)
1449 ThrowWriterException(FileOpenError,
"UnableToWriteFile");
1489MagickExport MagickBooleanType WriteImages(
const ImageInfo *image_info,
1490 Image *images,
const char *filename,ExceptionInfo *exception)
1492#define WriteImageTag "Write/Image"
1506 MagickProgressMonitor
1518 assert(image_info != (
const ImageInfo *) NULL);
1519 assert(image_info->signature == MagickCoreSignature);
1520 assert(images != (Image *) NULL);
1521 assert(images->signature == MagickCoreSignature);
1522 if (IsEventLogging() != MagickFalse)
1523 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",images->filename);
1524 assert(exception != (ExceptionInfo *) NULL);
1525 write_info=CloneImageInfo(image_info);
1526 *write_info->magick=
'\0';
1527 images=GetFirstImageInList(images);
1528 if (images == (Image *) NULL)
1529 return(MagickFalse);
1530 if (filename != (
const char *) NULL)
1531 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1532 (
void) CopyMagickString(p->filename,filename,MagickPathExtent);
1533 (void) CopyMagickString(write_info->filename,images->filename,
1535 sans_exception=AcquireExceptionInfo();
1536 (void) SetImageInfo(write_info,(
unsigned int) GetImageListLength(images),
1538 sans_exception=DestroyExceptionInfo(sans_exception);
1539 if (*write_info->magick ==
'\0')
1540 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1542 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1544 if (p->scene >= GetNextImageInList(p)->scene)
1552 i=(ssize_t) images->scene;
1553 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1554 p->scene=(
size_t) i++;
1562 progress_monitor=(MagickProgressMonitor) NULL;
1564 number_images=GetImageListLength(images);
1565 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1567 if (number_images != 1)
1568 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1570 status&=(MagickStatusType) WriteImage(write_info,p,exception);
1571 if (number_images != 1)
1572 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1573 if (write_info->adjoin != MagickFalse)
1575 if (number_images != 1)
1577#if defined(MAGICKCORE_OPENMP_SUPPORT)
1581 proceed=SetImageProgress(p,WriteImageTag,progress,number_images);
1582 if (proceed == MagickFalse)
1586 write_info=DestroyImageInfo(write_info);
1587 return(status != 0 ? MagickTrue : MagickFalse);