2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Persistant Cache Image Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colormap.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/linked-list.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/resource_.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/statistic.h"
72 #include "MagickCore/string_.h"
73 #include "MagickCore/string-private.h"
74 #include "MagickCore/utility.h"
75 #include "MagickCore/version-private.h"
80 static MagickBooleanType
81 WriteMPCImage(const ImageInfo *,Image *,ExceptionInfo *);
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % IsMPC() returns MagickTrue if the image format type, identified by the
95 % magick string, is an Magick Persistent Cache image.
97 % The format of the IsMPC method is:
99 % MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
101 % A description of each parameter follows:
103 % o magick: compare image format pattern against these bytes.
105 % o length: Specifies the length of the magick string.
108 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
112 if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % R e a d C A C H E I m a g e %
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 % ReadMPCImage() reads an Magick Persistent Cache image file and returns
129 % it. It allocates the memory necessary for the new Image structure and
130 % returns a pointer to the new image.
132 % The format of the ReadMPCImage method is:
134 % Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
136 % Decompression code contributed by Kyle Shorter.
138 % A description of each parameter follows:
140 % o image_info: the image info.
142 % o exception: return any errors or warnings in this structure.
145 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
148 cache_filename[MagickPathExtent],
149 id[MagickPathExtent],
150 keyword[MagickPathExtent],
196 assert(image_info != (const ImageInfo *) NULL);
197 assert(image_info->signature == MagickCoreSignature);
198 if (image_info->debug != MagickFalse)
199 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
200 image_info->filename);
201 assert(exception != (ExceptionInfo *) NULL);
202 assert(exception->signature == MagickCoreSignature);
203 image=AcquireImage(image_info,exception);
204 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
205 if (status == MagickFalse)
207 image=DestroyImageList(image);
208 return((Image *) NULL);
210 (void) CopyMagickString(cache_filename,image->filename,MagickPathExtent);
211 AppendImageFormat("cache",cache_filename);
212 c=ReadBlobByte(image);
215 image=DestroyImage(image);
216 return((Image *) NULL);
219 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
224 Decode image header; header terminates one character beyond a ':'.
226 profiles=(LinkedListInfo *) NULL;
227 length=MagickPathExtent;
228 options=AcquireString((char *) NULL);
229 signature=GetMagickSignature((const StringInfo *) NULL);
231 image->compression=NoCompression;
232 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
243 Read comment-- any text between { }.
245 length=MagickPathExtent;
246 comment=AcquireString((char *) NULL);
247 for (p=comment; comment != (char *) NULL; p++)
249 c=ReadBlobByte(image);
251 c=ReadBlobByte(image);
253 if ((c == EOF) || (c == (int) '}'))
255 if ((size_t) (p-comment+1) >= length)
259 comment=(char *) ResizeQuantumMemory(comment,length+
260 MagickPathExtent,sizeof(*comment));
261 if (comment == (char *) NULL)
263 p=comment+strlen(comment);
267 if (comment == (char *) NULL)
268 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
270 (void) SetImageProperty(image,"comment",comment,exception);
271 comment=DestroyString(comment);
272 c=ReadBlobByte(image);
275 if (isalnum(c) != MagickFalse)
280 length=MagickPathExtent;
286 if ((size_t) (p-keyword) < (MagickPathExtent-1))
288 c=ReadBlobByte(image);
292 while (isspace((int) ((unsigned char) c)) != 0)
293 c=ReadBlobByte(image);
297 Get the keyword value.
299 c=ReadBlobByte(image);
300 while ((c != (int) '}') && (c != EOF))
302 if ((size_t) (p-options+1) >= length)
306 options=(char *) ResizeQuantumMemory(options,length+
307 MagickPathExtent,sizeof(*options));
308 if (options == (char *) NULL)
310 p=options+strlen(options);
313 c=ReadBlobByte(image);
316 c=ReadBlobByte(image);
320 c=ReadBlobByte(image);
324 if (isspace((int) ((unsigned char) c)) != 0)
327 if (options == (char *) NULL)
328 ThrowReaderException(ResourceLimitError,
329 "MemoryAllocationFailed");
333 (void) CopyMagickString(options,options+1,strlen(options));
335 Assign a value to the specified keyword.
342 if (LocaleCompare(keyword,"alpha-trait") == 0)
347 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
348 MagickFalse,options);
351 image->alpha_trait=(PixelTrait) alpha_trait;
354 (void) SetImageProperty(image,keyword,options,exception);
360 if (LocaleCompare(keyword,"background-color") == 0)
362 (void) QueryColorCompliance(options,AllCompliance,
363 &image->background_color,exception);
366 if (LocaleCompare(keyword,"blue-primary") == 0)
368 flags=ParseGeometry(options,&geometry_info);
369 image->chromaticity.blue_primary.x=geometry_info.rho;
370 image->chromaticity.blue_primary.y=geometry_info.sigma;
371 if ((flags & SigmaValue) == 0)
372 image->chromaticity.blue_primary.y=
373 image->chromaticity.blue_primary.x;
376 if (LocaleCompare(keyword,"border-color") == 0)
378 (void) QueryColorCompliance(options,AllCompliance,
379 &image->border_color,exception);
382 (void) SetImageProperty(image,keyword,options,exception);
388 if (LocaleCompare(keyword,"class") == 0)
393 storage_class=ParseCommandOption(MagickClassOptions,
394 MagickFalse,options);
395 if (storage_class < 0)
397 image->storage_class=(ClassType) storage_class;
400 if (LocaleCompare(keyword,"colors") == 0)
402 image->colors=StringToUnsignedLong(options);
405 if (LocaleCompare(keyword,"colorspace") == 0)
410 colorspace=ParseCommandOption(MagickColorspaceOptions,
411 MagickFalse,options);
414 image->colorspace=(ColorspaceType) colorspace;
417 if (LocaleCompare(keyword,"compression") == 0)
422 compression=ParseCommandOption(MagickCompressOptions,
423 MagickFalse,options);
426 image->compression=(CompressionType) compression;
429 if (LocaleCompare(keyword,"columns") == 0)
431 image->columns=StringToUnsignedLong(options);
434 (void) SetImageProperty(image,keyword,options,exception);
440 if (LocaleCompare(keyword,"delay") == 0)
442 image->delay=StringToUnsignedLong(options);
445 if (LocaleCompare(keyword,"depth") == 0)
447 image->depth=StringToUnsignedLong(options);
450 if (LocaleCompare(keyword,"dispose") == 0)
455 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
459 image->dispose=(DisposeType) dispose;
462 (void) SetImageProperty(image,keyword,options,exception);
468 if (LocaleCompare(keyword,"endian") == 0)
473 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
477 image->endian=(EndianType) endian;
480 if (LocaleCompare(keyword,"error") == 0)
482 image->error.mean_error_per_pixel=StringToDouble(options,
486 (void) SetImageProperty(image,keyword,options,exception);
492 if (LocaleCompare(keyword,"gamma") == 0)
494 image->gamma=StringToDouble(options,(char **) NULL);
497 if (LocaleCompare(keyword,"green-primary") == 0)
499 flags=ParseGeometry(options,&geometry_info);
500 image->chromaticity.green_primary.x=geometry_info.rho;
501 image->chromaticity.green_primary.y=geometry_info.sigma;
502 if ((flags & SigmaValue) == 0)
503 image->chromaticity.green_primary.y=
504 image->chromaticity.green_primary.x;
507 (void) SetImageProperty(image,keyword,options,exception);
513 if (LocaleCompare(keyword,"id") == 0)
515 (void) CopyMagickString(id,options,MagickPathExtent);
518 if (LocaleCompare(keyword,"iterations") == 0)
520 image->iterations=StringToUnsignedLong(options);
523 (void) SetImageProperty(image,keyword,options,exception);
529 if (LocaleCompare(keyword,"magick-signature") == 0)
531 signature=(unsigned int) StringToUnsignedLong(options);
534 if (LocaleCompare(keyword,"mattecolor") == 0)
536 (void) QueryColorCompliance(options,AllCompliance,
537 &image->matte_color,exception);
540 if (LocaleCompare(keyword,"maximum-error") == 0)
542 image->error.normalized_maximum_error=StringToDouble(
543 options,(char **) NULL);
546 if (LocaleCompare(keyword,"mean-error") == 0)
548 image->error.normalized_mean_error=StringToDouble(options,
552 if (LocaleCompare(keyword,"montage") == 0)
554 (void) CloneString(&image->montage,options);
557 (void) SetImageProperty(image,keyword,options,exception);
563 if (LocaleCompare(keyword,"number-channels") == 0)
565 image->number_channels=StringToUnsignedLong(options);
568 if (LocaleCompare(keyword,"number-meta-channels") == 0)
570 image->number_meta_channels=StringToUnsignedLong(options);
571 if (image->number_meta_channels > MaxPixelChannels)
573 options=DestroyString(options);
574 ThrowReaderException(CorruptImageError,
575 "ImproperImageHeader");
584 if (LocaleCompare(keyword,"orientation") == 0)
589 orientation=ParseCommandOption(MagickOrientationOptions,
590 MagickFalse,options);
593 image->orientation=(OrientationType) orientation;
596 (void) SetImageProperty(image,keyword,options,exception);
602 if (LocaleCompare(keyword,"page") == 0)
607 geometry=GetPageGeometry(options);
608 (void) ParseAbsoluteGeometry(geometry,&image->page);
609 geometry=DestroyString(geometry);
612 if (LocaleCompare(keyword,"pixel-intensity") == 0)
617 intensity=ParseCommandOption(MagickPixelIntensityOptions,
618 MagickFalse,options);
621 image->intensity=(PixelIntensityMethod) intensity;
624 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
625 (LocaleNCompare(keyword,"profile-",8) == 0))
627 if (profiles == (LinkedListInfo *) NULL)
628 profiles=NewLinkedList(0);
629 (void) AppendValueToLinkedList(profiles,
630 AcquireString(keyword+8));
631 profile=BlobToStringInfo((const void *) NULL,(size_t)
632 StringToLong(options));
633 if (profile == (StringInfo *) NULL)
634 ThrowReaderException(ResourceLimitError,
635 "MemoryAllocationFailed");
636 (void) SetImageProfile(image,keyword+8,profile,exception);
637 profile=DestroyStringInfo(profile);
640 (void) SetImageProperty(image,keyword,options,exception);
646 if (LocaleCompare(keyword,"quality") == 0)
648 image->quality=StringToUnsignedLong(options);
651 (void) SetImageProperty(image,keyword,options,exception);
657 if (LocaleCompare(keyword,"red-primary") == 0)
659 flags=ParseGeometry(options,&geometry_info);
660 image->chromaticity.red_primary.x=geometry_info.rho;
661 if ((flags & SigmaValue) != 0)
662 image->chromaticity.red_primary.y=geometry_info.sigma;
665 if (LocaleCompare(keyword,"rendering-intent") == 0)
670 rendering_intent=ParseCommandOption(MagickIntentOptions,
671 MagickFalse,options);
672 if (rendering_intent < 0)
674 image->rendering_intent=(RenderingIntent) rendering_intent;
677 if (LocaleCompare(keyword,"resolution") == 0)
679 flags=ParseGeometry(options,&geometry_info);
680 image->resolution.x=geometry_info.rho;
681 image->resolution.y=geometry_info.sigma;
682 if ((flags & SigmaValue) == 0)
683 image->resolution.y=image->resolution.x;
686 if (LocaleCompare(keyword,"rows") == 0)
688 image->rows=StringToUnsignedLong(options);
691 (void) SetImageProperty(image,keyword,options,exception);
697 if (LocaleCompare(keyword,"scene") == 0)
699 image->scene=StringToUnsignedLong(options);
702 (void) SetImageProperty(image,keyword,options,exception);
708 if (LocaleCompare(keyword,"ticks-per-second") == 0)
710 image->ticks_per_second=(ssize_t) StringToLong(options);
713 if (LocaleCompare(keyword,"tile-offset") == 0)
718 geometry=GetPageGeometry(options);
719 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
720 geometry=DestroyString(geometry);
722 if (LocaleCompare(keyword,"type") == 0)
727 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
731 image->type=(ImageType) type;
734 (void) SetImageProperty(image,keyword,options,exception);
740 if (LocaleCompare(keyword,"units") == 0)
745 units=ParseCommandOption(MagickResolutionOptions,
746 MagickFalse,options);
749 image->units=(ResolutionType) units;
752 (void) SetImageProperty(image,keyword,options,exception);
758 if (LocaleCompare(keyword,"white-point") == 0)
760 flags=ParseGeometry(options,&geometry_info);
761 image->chromaticity.white_point.x=geometry_info.rho;
762 image->chromaticity.white_point.y=geometry_info.sigma;
763 if ((flags & SigmaValue) == 0)
764 image->chromaticity.white_point.y=
765 image->chromaticity.white_point.x;
768 (void) SetImageProperty(image,keyword,options,exception);
773 (void) SetImageProperty(image,keyword,options,exception);
779 c=ReadBlobByte(image);
780 while (isspace((int) ((unsigned char) c)) != 0)
781 c=ReadBlobByte(image);
783 options=DestroyString(options);
784 (void) ReadBlobByte(image);
786 Verify that required image information is defined.
788 if ((LocaleCompare(id,"MagickCache") != 0) ||
789 (image->storage_class == UndefinedClass) ||
790 (image->compression == UndefinedCompression) || (image->columns == 0) ||
793 if (profiles != (LinkedListInfo *) NULL)
794 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
795 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
797 if (signature != GetMagickSignature((const StringInfo *) NULL))
799 if (profiles != (LinkedListInfo *) NULL)
800 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
801 ThrowReaderException(CacheError,"IncompatibleAPI");
803 if (image->montage != (char *) NULL)
811 length=MagickPathExtent;
812 image->directory=AcquireString((char *) NULL);
817 if ((strlen(image->directory)+MagickPathExtent) >= length)
820 Allocate more memory for the image directory.
823 image->directory=(char *) ResizeQuantumMemory(image->directory,
824 length+MagickPathExtent,sizeof(*image->directory));
825 if (image->directory == (char *) NULL)
827 if (profiles != (LinkedListInfo *) NULL)
828 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
829 ThrowReaderException(CorruptImageError,
830 "UnableToReadImageData");
832 p=image->directory+strlen(image->directory);
834 c=ReadBlobByte(image);
836 } while (c != (int) '\0');
838 if (profiles != (LinkedListInfo *) NULL)
846 register unsigned char
852 ResetLinkedListIterator(profiles);
853 name=(const char *) GetNextValueInLinkedList(profiles);
854 while (name != (const char *) NULL)
856 profile=GetImageProfile(image,name);
857 if (profile != (StringInfo *) NULL)
859 p=GetStringInfoDatum(profile);
860 count=ReadBlob(image,GetStringInfoLength(profile),p);
862 name=(const char *) GetNextValueInLinkedList(profiles);
864 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
866 depth=GetImageQuantumDepth(image,MagickFalse);
867 if (image->storage_class == PseudoClass)
876 Create image colormap.
878 packet_size=(size_t) (3UL*depth/8UL);
879 if ((packet_size*image->colors) > GetBlobSize(image))
880 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
881 image->colormap=(PixelInfo *) AcquireQuantumMemory(image->colors+1,
882 sizeof(*image->colormap));
883 if (image->colormap == (PixelInfo *) NULL)
884 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
885 if (image->colors != 0)
888 Read image colormap from file.
890 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
891 packet_size*sizeof(*colormap));
892 if (colormap == (unsigned char *) NULL)
893 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
894 count=ReadBlob(image,packet_size*image->colors,colormap);
895 if (count != (ssize_t) (packet_size*image->colors))
897 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
898 ThrowReaderException(CorruptImageError,
899 "InsufficientImageDataInFile");
905 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
906 ThrowReaderException(CorruptImageError,
907 "ImageDepthNotSupported");
913 for (i=0; i < (ssize_t) image->colors; i++)
915 p=PushCharPixel(p,&pixel);
916 image->colormap[i].red=ScaleCharToQuantum(pixel);
917 p=PushCharPixel(p,&pixel);
918 image->colormap[i].green=ScaleCharToQuantum(pixel);
919 p=PushCharPixel(p,&pixel);
920 image->colormap[i].blue=ScaleCharToQuantum(pixel);
929 for (i=0; i < (ssize_t) image->colors; i++)
931 p=PushShortPixel(MSBEndian,p,&pixel);
932 image->colormap[i].red=ScaleShortToQuantum(pixel);
933 p=PushShortPixel(MSBEndian,p,&pixel);
934 image->colormap[i].green=ScaleShortToQuantum(pixel);
935 p=PushShortPixel(MSBEndian,p,&pixel);
936 image->colormap[i].blue=ScaleShortToQuantum(pixel);
945 for (i=0; i < (ssize_t) image->colors; i++)
947 p=PushLongPixel(MSBEndian,p,&pixel);
948 image->colormap[i].red=ScaleLongToQuantum(pixel);
949 p=PushLongPixel(MSBEndian,p,&pixel);
950 image->colormap[i].green=ScaleLongToQuantum(pixel);
951 p=PushLongPixel(MSBEndian,p,&pixel);
952 image->colormap[i].blue=ScaleLongToQuantum(pixel);
957 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
960 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
961 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
963 if ((AcquireMagickResource(WidthResource,image->columns) == MagickFalse) ||
964 (AcquireMagickResource(HeightResource,image->rows) == MagickFalse))
965 ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
967 Attach persistent pixel cache.
969 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
970 if (status == MagickFalse)
971 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
972 if (EOFBlob(image) != MagickFalse)
974 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
979 Proceed to next image.
983 c=ReadBlobByte(image);
984 } while ((isgraph(c) == MagickFalse) && (c != EOF));
988 Allocate next image structure.
990 AcquireNextImage(image_info,image,exception);
991 if (GetNextImageInList(image) == (Image *) NULL)
993 image=DestroyImageList(image);
994 return((Image *) NULL);
996 image=SyncNextImageInList(image);
997 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
999 if (status == MagickFalse)
1003 (void) CloseBlob(image);
1004 return(GetFirstImageInList(image));
1008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012 % R e g i s t e r M P C I m a g e %
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1018 % RegisterMPCImage() adds properties for the Cache image format to
1019 % the list of supported formats. The properties include the image format
1020 % tag, a method to read and/or write the format, whether the format
1021 % supports the saving of more than one frame to the same file or blob,
1022 % whether the format supports native in-memory I/O, and a brief
1023 % description of the format.
1025 % The format of the RegisterMPCImage method is:
1027 % size_t RegisterMPCImage(void)
1030 ModuleExport size_t RegisterMPCImage(void)
1035 entry=AcquireMagickInfo("MPC","CACHE",
1036 "Magick Persistent Cache image format");
1037 entry->flags|=CoderStealthFlag;
1038 (void) RegisterMagickInfo(entry);
1039 entry=AcquireMagickInfo("MPC","MPC","Magick Persistent Cache image format");
1040 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
1041 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
1042 entry->magick=(IsImageFormatHandler *) IsMPC;
1043 entry->flags|=CoderDecoderSeekableStreamFlag;
1044 (void) RegisterMagickInfo(entry);
1045 return(MagickImageCoderSignature);
1049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053 % U n r e g i s t e r M P C I m a g e %
1057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1059 % UnregisterMPCImage() removes format registrations made by the
1060 % MPC module from the list of supported formats.
1062 % The format of the UnregisterMPCImage method is:
1064 % UnregisterMPCImage(void)
1067 ModuleExport void UnregisterMPCImage(void)
1069 (void) UnregisterMagickInfo("CACHE");
1070 (void) UnregisterMagickInfo("MPC");
1074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078 % W r i t e M P C I m a g e %
1082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1086 % The format of the WriteMPCImage method is:
1088 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1089 % Image *image,ExceptionInfo *exception)
1091 % A description of each parameter follows:
1093 % o image_info: the image info.
1095 % o image: the image.
1097 % o exception: return any errors or warnings in this structure.
1100 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1101 ExceptionInfo *exception)
1104 buffer[MagickPathExtent],
1105 cache_filename[MagickPathExtent];
1125 Open persistent cache.
1127 assert(image_info != (const ImageInfo *) NULL);
1128 assert(image_info->signature == MagickCoreSignature);
1129 assert(image != (Image *) NULL);
1130 assert(image->signature == MagickCoreSignature);
1131 if (image->debug != MagickFalse)
1132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1133 assert(exception != (ExceptionInfo *) NULL);
1134 assert(exception->signature == MagickCoreSignature);
1135 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1136 if (status == MagickFalse)
1138 (void) CopyMagickString(cache_filename,image->filename,MagickPathExtent);
1139 AppendImageFormat("cache",cache_filename);
1145 Write persistent cache meta-information.
1147 depth=GetImageQuantumDepth(image,MagickTrue);
1148 if ((image->storage_class == PseudoClass) &&
1149 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1150 (void) SetImageStorageClass(image,DirectClass,exception);
1151 (void) WriteBlobString(image,"id=MagickCache\n");
1152 (void) FormatLocaleString(buffer,MagickPathExtent,"magick-signature=%u\n",
1153 GetMagickSignature((const StringInfo *) NULL));
1154 (void) WriteBlobString(image,buffer);
1155 (void) FormatLocaleString(buffer,MagickPathExtent,
1156 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
1157 MagickClassOptions,image->storage_class),(double) image->colors,
1158 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
1159 image->alpha_trait));
1160 (void) WriteBlobString(image,buffer);
1161 (void) FormatLocaleString(buffer,MagickPathExtent,
1162 "number-channels=%.20g number-meta-channels=%.20g\n",
1163 (double) image->number_channels,(double) image->number_meta_channels);
1164 (void) WriteBlobString(image,buffer);
1165 (void) FormatLocaleString(buffer,MagickPathExtent,
1166 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
1167 (double) image->rows,(double) image->depth);
1168 (void) WriteBlobString(image,buffer);
1169 if (image->type != UndefinedType)
1171 (void) FormatLocaleString(buffer,MagickPathExtent,"type=%s\n",
1172 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1173 (void) WriteBlobString(image,buffer);
1175 (void) FormatLocaleString(buffer,MagickPathExtent,"colorspace=%s\n",
1176 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1177 (void) WriteBlobString(image,buffer);
1178 if (image->intensity != UndefinedPixelIntensityMethod)
1180 (void) FormatLocaleString(buffer,MagickPathExtent,
1181 "pixel-intensity=%s\n",CommandOptionToMnemonic(
1182 MagickPixelIntensityOptions,image->intensity));
1183 (void) WriteBlobString(image,buffer);
1185 if (image->endian != UndefinedEndian)
1187 (void) FormatLocaleString(buffer,MagickPathExtent,"endian=%s\n",
1188 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1189 (void) WriteBlobString(image,buffer);
1191 if (image->compression != UndefinedCompression)
1193 (void) FormatLocaleString(buffer,MagickPathExtent,
1194 "compression=%s quality=%.20g\n",CommandOptionToMnemonic(
1195 MagickCompressOptions,image->compression),(double) image->quality);
1196 (void) WriteBlobString(image,buffer);
1198 if (image->units != UndefinedResolution)
1200 (void) FormatLocaleString(buffer,MagickPathExtent,"units=%s\n",
1201 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1202 (void) WriteBlobString(image,buffer);
1204 if ((image->resolution.x != 0) || (image->resolution.y != 0))
1206 (void) FormatLocaleString(buffer,MagickPathExtent,
1207 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1208 (void) WriteBlobString(image,buffer);
1210 if ((image->page.width != 0) || (image->page.height != 0))
1212 (void) FormatLocaleString(buffer,MagickPathExtent,
1213 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1214 image->page.height,(double) image->page.x,(double) image->page.y);
1215 (void) WriteBlobString(image,buffer);
1218 if ((image->page.x != 0) || (image->page.y != 0))
1220 (void) FormatLocaleString(buffer,MagickPathExtent,"page=%+ld%+ld\n",
1221 (long) image->page.x,(long) image->page.y);
1222 (void) WriteBlobString(image,buffer);
1224 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
1226 (void) FormatLocaleString(buffer,MagickPathExtent,
1227 "tile-offset=%+ld%+ld\n",(long) image->tile_offset.x,(long)
1228 image->tile_offset.y);
1229 (void) WriteBlobString(image,buffer);
1231 if ((GetNextImageInList(image) != (Image *) NULL) ||
1232 (GetPreviousImageInList(image) != (Image *) NULL))
1234 if (image->scene == 0)
1235 (void) FormatLocaleString(buffer,MagickPathExtent,
1236 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1237 image->iterations,(double) image->delay,(double)
1238 image->ticks_per_second);
1240 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g "
1241 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1242 (double) image->scene,(double) image->iterations,(double)
1243 image->delay,(double) image->ticks_per_second);
1244 (void) WriteBlobString(image,buffer);
1248 if (image->scene != 0)
1250 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g\n",
1251 (double) image->scene);
1252 (void) WriteBlobString(image,buffer);
1254 if (image->iterations != 0)
1256 (void) FormatLocaleString(buffer,MagickPathExtent,
1257 "iterations=%.20g\n",(double) image->iterations);
1258 (void) WriteBlobString(image,buffer);
1260 if (image->delay != 0)
1262 (void) FormatLocaleString(buffer,MagickPathExtent,"delay=%.20g\n",
1263 (double) image->delay);
1264 (void) WriteBlobString(image,buffer);
1266 if (image->ticks_per_second != UndefinedTicksPerSecond)
1268 (void) FormatLocaleString(buffer,MagickPathExtent,
1269 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1270 (void) WriteBlobString(image,buffer);
1273 if (image->gravity != UndefinedGravity)
1275 (void) FormatLocaleString(buffer,MagickPathExtent,"gravity=%s\n",
1276 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1277 (void) WriteBlobString(image,buffer);
1279 if (image->dispose != UndefinedDispose)
1281 (void) FormatLocaleString(buffer,MagickPathExtent,"dispose=%s\n",
1282 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1283 (void) WriteBlobString(image,buffer);
1285 if (image->rendering_intent != UndefinedIntent)
1287 (void) FormatLocaleString(buffer,MagickPathExtent,
1288 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1289 image->rendering_intent));
1290 (void) WriteBlobString(image,buffer);
1292 if (image->gamma != 0.0)
1294 (void) FormatLocaleString(buffer,MagickPathExtent,"gamma=%g\n",
1296 (void) WriteBlobString(image,buffer);
1298 if (image->chromaticity.white_point.x != 0.0)
1301 Note chomaticity points.
1303 (void) FormatLocaleString(buffer,MagickPathExtent,"red-primary="
1304 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1305 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1306 image->chromaticity.green_primary.x,
1307 image->chromaticity.green_primary.y,
1308 image->chromaticity.blue_primary.x,
1309 image->chromaticity.blue_primary.y);
1310 (void) WriteBlobString(image,buffer);
1311 (void) FormatLocaleString(buffer,MagickPathExtent,
1312 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1313 image->chromaticity.white_point.y);
1314 (void) WriteBlobString(image,buffer);
1316 if (image->orientation != UndefinedOrientation)
1318 (void) FormatLocaleString(buffer,MagickPathExtent,
1319 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1320 image->orientation));
1321 (void) WriteBlobString(image,buffer);
1323 if (image->profiles != (void *) NULL)
1334 ResetImageProfileIterator(image);
1335 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1337 profile=GetImageProfile(image,name);
1338 if (profile != (StringInfo *) NULL)
1340 (void) FormatLocaleString(buffer,MagickPathExtent,
1341 "profile:%s=%.20g\n",name,(double)
1342 GetStringInfoLength(profile));
1343 (void) WriteBlobString(image,buffer);
1345 name=GetNextImageProfile(image);
1348 if (image->montage != (char *) NULL)
1350 (void) FormatLocaleString(buffer,MagickPathExtent,"montage=%s\n",
1352 (void) WriteBlobString(image,buffer);
1354 ResetImagePropertyIterator(image);
1355 property=GetNextImageProperty(image);
1356 while (property != (const char *) NULL)
1358 (void) FormatLocaleString(buffer,MagickPathExtent,"%s=",property);
1359 (void) WriteBlobString(image,buffer);
1360 value=GetImageProperty(image,property,exception);
1361 if (value != (const char *) NULL)
1366 length=strlen(value);
1367 for (i=0; i < (ssize_t) length; i++)
1368 if (isspace((int) ((unsigned char) value[i])) != 0)
1370 if ((i == (ssize_t) length) && (i != 0))
1371 (void) WriteBlob(image,length,(const unsigned char *) value);
1374 (void) WriteBlobByte(image,'{');
1375 if (strchr(value,'}') == (char *) NULL)
1376 (void) WriteBlob(image,length,(const unsigned char *) value);
1378 for (i=0; i < (ssize_t) length; i++)
1380 if (value[i] == (int) '}')
1381 (void) WriteBlobByte(image,'\\');
1382 (void) WriteBlobByte(image,value[i]);
1384 (void) WriteBlobByte(image,'}');
1387 (void) WriteBlobByte(image,'\n');
1388 property=GetNextImageProperty(image);
1390 (void) WriteBlobString(image,"\f\n:\032");
1391 if (image->montage != (char *) NULL)
1394 Write montage tile directory.
1396 if (image->directory != (char *) NULL)
1397 (void) WriteBlobString(image,image->directory);
1398 (void) WriteBlobByte(image,'\0');
1400 if (image->profiles != 0)
1409 Write image profiles.
1411 ResetImageProfileIterator(image);
1412 name=GetNextImageProfile(image);
1413 while (name != (const char *) NULL)
1415 profile=GetImageProfile(image,name);
1416 (void) WriteBlob(image,GetStringInfoLength(profile),
1417 GetStringInfoDatum(profile));
1418 name=GetNextImageProfile(image);
1421 if (image->storage_class == PseudoClass)
1433 packet_size=(size_t) (3UL*depth/8UL);
1434 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1435 packet_size*sizeof(*colormap));
1436 if (colormap == (unsigned char *) NULL)
1437 return(MagickFalse);
1439 Write colormap to file.
1442 for (i=0; i < (ssize_t) image->colors; i++)
1447 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1453 pixel=ScaleQuantumToLong(image->colormap[i].red);
1454 q=PopLongPixel(MSBEndian,pixel,q);
1455 pixel=ScaleQuantumToLong(image->colormap[i].green);
1456 q=PopLongPixel(MSBEndian,pixel,q);
1457 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1458 q=PopLongPixel(MSBEndian,pixel,q);
1466 pixel=ScaleQuantumToShort(image->colormap[i].red);
1467 q=PopShortPixel(MSBEndian,pixel,q);
1468 pixel=ScaleQuantumToShort(image->colormap[i].green);
1469 q=PopShortPixel(MSBEndian,pixel,q);
1470 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1471 q=PopShortPixel(MSBEndian,pixel,q);
1479 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1480 q=PopCharPixel(pixel,q);
1481 pixel=(unsigned char) ScaleQuantumToChar(
1482 image->colormap[i].green);
1483 q=PopCharPixel(pixel,q);
1484 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1485 q=PopCharPixel(pixel,q);
1490 (void) WriteBlob(image,packet_size*image->colors,colormap);
1491 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1494 Initialize persistent pixel cache.
1496 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1498 if (status == MagickFalse)
1499 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1500 if (GetNextImageInList(image) == (Image *) NULL)
1502 image=SyncNextImageInList(image);
1503 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1505 status=image->progress_monitor(SaveImagesTag,scene,
1506 GetImageListLength(image),image->client_data);
1507 if (status == MagickFalse)
1511 } while (image_info->adjoin != MagickFalse);
1512 (void) CloseBlob(image);