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 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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-color") == 0)
344 (void) QueryColorCompliance(options,AllCompliance,
345 &image->alpha_color,exception);
348 if (LocaleCompare(keyword,"alpha-trait") == 0)
353 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
354 MagickFalse,options);
357 image->alpha_trait=(PixelTrait) alpha_trait;
360 (void) SetImageProperty(image,keyword,options,exception);
366 if (LocaleCompare(keyword,"background-color") == 0)
368 (void) QueryColorCompliance(options,AllCompliance,
369 &image->background_color,exception);
372 if (LocaleCompare(keyword,"blue-primary") == 0)
374 flags=ParseGeometry(options,&geometry_info);
375 image->chromaticity.blue_primary.x=geometry_info.rho;
376 image->chromaticity.blue_primary.y=geometry_info.sigma;
377 if ((flags & SigmaValue) == 0)
378 image->chromaticity.blue_primary.y=
379 image->chromaticity.blue_primary.x;
382 if (LocaleCompare(keyword,"border-color") == 0)
384 (void) QueryColorCompliance(options,AllCompliance,
385 &image->border_color,exception);
388 (void) SetImageProperty(image,keyword,options,exception);
394 if (LocaleCompare(keyword,"class") == 0)
399 storage_class=ParseCommandOption(MagickClassOptions,
400 MagickFalse,options);
401 if (storage_class < 0)
403 image->storage_class=(ClassType) storage_class;
406 if (LocaleCompare(keyword,"colors") == 0)
408 image->colors=StringToUnsignedLong(options);
411 if (LocaleCompare(keyword,"colorspace") == 0)
416 colorspace=ParseCommandOption(MagickColorspaceOptions,
417 MagickFalse,options);
420 image->colorspace=(ColorspaceType) colorspace;
423 if (LocaleCompare(keyword,"compression") == 0)
428 compression=ParseCommandOption(MagickCompressOptions,
429 MagickFalse,options);
432 image->compression=(CompressionType) compression;
435 if (LocaleCompare(keyword,"columns") == 0)
437 image->columns=StringToUnsignedLong(options);
440 (void) SetImageProperty(image,keyword,options,exception);
446 if (LocaleCompare(keyword,"delay") == 0)
448 image->delay=StringToUnsignedLong(options);
451 if (LocaleCompare(keyword,"depth") == 0)
453 image->depth=StringToUnsignedLong(options);
456 if (LocaleCompare(keyword,"dispose") == 0)
461 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
465 image->dispose=(DisposeType) dispose;
468 (void) SetImageProperty(image,keyword,options,exception);
474 if (LocaleCompare(keyword,"endian") == 0)
479 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
483 image->endian=(EndianType) endian;
486 if (LocaleCompare(keyword,"error") == 0)
488 image->error.mean_error_per_pixel=StringToDouble(options,
492 (void) SetImageProperty(image,keyword,options,exception);
498 if (LocaleCompare(keyword,"gamma") == 0)
500 image->gamma=StringToDouble(options,(char **) NULL);
503 if (LocaleCompare(keyword,"green-primary") == 0)
505 flags=ParseGeometry(options,&geometry_info);
506 image->chromaticity.green_primary.x=geometry_info.rho;
507 image->chromaticity.green_primary.y=geometry_info.sigma;
508 if ((flags & SigmaValue) == 0)
509 image->chromaticity.green_primary.y=
510 image->chromaticity.green_primary.x;
513 (void) SetImageProperty(image,keyword,options,exception);
519 if (LocaleCompare(keyword,"id") == 0)
521 (void) CopyMagickString(id,options,MagickPathExtent);
524 if (LocaleCompare(keyword,"iterations") == 0)
526 image->iterations=StringToUnsignedLong(options);
529 (void) SetImageProperty(image,keyword,options,exception);
535 if (LocaleCompare(keyword,"magick-signature") == 0)
537 signature=(unsigned int) StringToUnsignedLong(options);
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,"orientation") == 0)
568 orientation=ParseCommandOption(MagickOrientationOptions,
569 MagickFalse,options);
572 image->orientation=(OrientationType) orientation;
575 (void) SetImageProperty(image,keyword,options,exception);
581 if (LocaleCompare(keyword,"page") == 0)
586 geometry=GetPageGeometry(options);
587 (void) ParseAbsoluteGeometry(geometry,&image->page);
588 geometry=DestroyString(geometry);
591 if (LocaleCompare(keyword,"pixel-intensity") == 0)
596 intensity=ParseCommandOption(MagickPixelIntensityOptions,
597 MagickFalse,options);
600 image->intensity=(PixelIntensityMethod) intensity;
603 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
604 (LocaleNCompare(keyword,"profile-",8) == 0))
606 if (profiles == (LinkedListInfo *) NULL)
607 profiles=NewLinkedList(0);
608 (void) AppendValueToLinkedList(profiles,
609 AcquireString(keyword+8));
610 profile=BlobToStringInfo((const void *) NULL,(size_t)
611 StringToLong(options));
612 if (profile == (StringInfo *) NULL)
613 ThrowReaderException(ResourceLimitError,
614 "MemoryAllocationFailed");
615 (void) SetImageProfile(image,keyword+8,profile,exception);
616 profile=DestroyStringInfo(profile);
619 (void) SetImageProperty(image,keyword,options,exception);
625 if (LocaleCompare(keyword,"quality") == 0)
627 image->quality=StringToUnsignedLong(options);
630 (void) SetImageProperty(image,keyword,options,exception);
636 if (LocaleCompare(keyword,"red-primary") == 0)
638 flags=ParseGeometry(options,&geometry_info);
639 image->chromaticity.red_primary.x=geometry_info.rho;
640 if ((flags & SigmaValue) != 0)
641 image->chromaticity.red_primary.y=geometry_info.sigma;
644 if (LocaleCompare(keyword,"rendering-intent") == 0)
649 rendering_intent=ParseCommandOption(MagickIntentOptions,
650 MagickFalse,options);
651 if (rendering_intent < 0)
653 image->rendering_intent=(RenderingIntent) rendering_intent;
656 if (LocaleCompare(keyword,"resolution") == 0)
658 flags=ParseGeometry(options,&geometry_info);
659 image->resolution.x=geometry_info.rho;
660 image->resolution.y=geometry_info.sigma;
661 if ((flags & SigmaValue) == 0)
662 image->resolution.y=image->resolution.x;
665 if (LocaleCompare(keyword,"rows") == 0)
667 image->rows=StringToUnsignedLong(options);
670 (void) SetImageProperty(image,keyword,options,exception);
676 if (LocaleCompare(keyword,"scene") == 0)
678 image->scene=StringToUnsignedLong(options);
681 (void) SetImageProperty(image,keyword,options,exception);
687 if (LocaleCompare(keyword,"ticks-per-second") == 0)
689 image->ticks_per_second=(ssize_t) StringToLong(options);
692 if (LocaleCompare(keyword,"tile-offset") == 0)
697 geometry=GetPageGeometry(options);
698 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
699 geometry=DestroyString(geometry);
701 if (LocaleCompare(keyword,"type") == 0)
706 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
710 image->type=(ImageType) type;
713 (void) SetImageProperty(image,keyword,options,exception);
719 if (LocaleCompare(keyword,"units") == 0)
724 units=ParseCommandOption(MagickResolutionOptions,
725 MagickFalse,options);
728 image->units=(ResolutionType) units;
731 (void) SetImageProperty(image,keyword,options,exception);
737 if (LocaleCompare(keyword,"white-point") == 0)
739 flags=ParseGeometry(options,&geometry_info);
740 image->chromaticity.white_point.x=geometry_info.rho;
741 image->chromaticity.white_point.y=geometry_info.sigma;
742 if ((flags & SigmaValue) == 0)
743 image->chromaticity.white_point.y=
744 image->chromaticity.white_point.x;
747 (void) SetImageProperty(image,keyword,options,exception);
752 (void) SetImageProperty(image,keyword,options,exception);
758 c=ReadBlobByte(image);
759 while (isspace((int) ((unsigned char) c)) != 0)
760 c=ReadBlobByte(image);
762 options=DestroyString(options);
763 (void) ReadBlobByte(image);
765 Verify that required image information is defined.
767 if ((LocaleCompare(id,"MagickCache") != 0) ||
768 (image->storage_class == UndefinedClass) ||
769 (image->compression == UndefinedCompression) || (image->columns == 0) ||
771 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
772 if (signature != GetMagickSignature((const StringInfo *) NULL))
773 ThrowReaderException(CacheError,"IncompatibleAPI");
774 if (image->montage != (char *) NULL)
782 length=MagickPathExtent;
783 image->directory=AcquireString((char *) NULL);
788 if ((strlen(image->directory)+MagickPathExtent) >= length)
791 Allocate more memory for the image directory.
794 image->directory=(char *) ResizeQuantumMemory(image->directory,
795 length+MagickPathExtent,sizeof(*image->directory));
796 if (image->directory == (char *) NULL)
797 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
798 p=image->directory+strlen(image->directory);
800 c=ReadBlobByte(image);
802 } while (c != (int) '\0');
804 if (profiles != (LinkedListInfo *) NULL)
812 register unsigned char
818 ResetLinkedListIterator(profiles);
819 name=(const char *) GetNextValueInLinkedList(profiles);
820 while (name != (const char *) NULL)
822 profile=GetImageProfile(image,name);
823 if (profile != (StringInfo *) NULL)
825 p=GetStringInfoDatum(profile);
826 count=ReadBlob(image,GetStringInfoLength(profile),p);
828 name=(const char *) GetNextValueInLinkedList(profiles);
830 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
832 depth=GetImageQuantumDepth(image,MagickFalse);
833 if (image->storage_class == PseudoClass)
836 Create image colormap.
838 image->colormap=(PixelInfo *) AcquireQuantumMemory(image->colors+1,
839 sizeof(*image->colormap));
840 if (image->colormap == (PixelInfo *) NULL)
841 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
842 if (image->colors != 0)
851 Read image colormap from file.
853 packet_size=(size_t) (3UL*depth/8UL);
854 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
855 packet_size*sizeof(*colormap));
856 if (colormap == (unsigned char *) NULL)
857 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
858 count=ReadBlob(image,packet_size*image->colors,colormap);
859 if (count != (ssize_t) (packet_size*image->colors))
860 ThrowReaderException(CorruptImageError,
861 "InsufficientImageDataInFile");
866 ThrowReaderException(CorruptImageError,
867 "ImageDepthNotSupported");
873 for (i=0; i < (ssize_t) image->colors; i++)
875 p=PushCharPixel(p,&pixel);
876 image->colormap[i].red=ScaleCharToQuantum(pixel);
877 p=PushCharPixel(p,&pixel);
878 image->colormap[i].green=ScaleCharToQuantum(pixel);
879 p=PushCharPixel(p,&pixel);
880 image->colormap[i].blue=ScaleCharToQuantum(pixel);
889 for (i=0; i < (ssize_t) image->colors; i++)
891 p=PushShortPixel(MSBEndian,p,&pixel);
892 image->colormap[i].red=ScaleShortToQuantum(pixel);
893 p=PushShortPixel(MSBEndian,p,&pixel);
894 image->colormap[i].green=ScaleShortToQuantum(pixel);
895 p=PushShortPixel(MSBEndian,p,&pixel);
896 image->colormap[i].blue=ScaleShortToQuantum(pixel);
905 for (i=0; i < (ssize_t) image->colors; i++)
907 p=PushLongPixel(MSBEndian,p,&pixel);
908 image->colormap[i].red=ScaleLongToQuantum(pixel);
909 p=PushLongPixel(MSBEndian,p,&pixel);
910 image->colormap[i].green=ScaleLongToQuantum(pixel);
911 p=PushLongPixel(MSBEndian,p,&pixel);
912 image->colormap[i].blue=ScaleLongToQuantum(pixel);
917 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
920 if (EOFBlob(image) != MagickFalse)
922 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
926 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
927 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
929 if ((AcquireMagickResource(WidthResource,image->columns) == MagickFalse) ||
930 (AcquireMagickResource(HeightResource,image->rows) == MagickFalse))
931 ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
933 Attach persistent pixel cache.
935 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
936 if (status == MagickFalse)
937 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
939 Proceed to next image.
943 c=ReadBlobByte(image);
944 } while ((isgraph(c) == MagickFalse) && (c != EOF));
948 Allocate next image structure.
950 AcquireNextImage(image_info,image,exception);
951 if (GetNextImageInList(image) == (Image *) NULL)
953 image=DestroyImageList(image);
954 return((Image *) NULL);
956 image=SyncNextImageInList(image);
957 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
959 if (status == MagickFalse)
963 (void) CloseBlob(image);
964 return(GetFirstImageInList(image));
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 % R e g i s t e r M P C I m a g e %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 % RegisterMPCImage() adds properties for the Cache image format to
979 % the list of supported formats. The properties include the image format
980 % tag, a method to read and/or write the format, whether the format
981 % supports the saving of more than one frame to the same file or blob,
982 % whether the format supports native in-memory I/O, and a brief
983 % description of the format.
985 % The format of the RegisterMPCImage method is:
987 % size_t RegisterMPCImage(void)
990 ModuleExport size_t RegisterMPCImage(void)
995 entry=AcquireMagickInfo("MPC","CACHE",
996 "Magick Persistent Cache image format");
997 entry->flags|=CoderStealthFlag;
998 (void) RegisterMagickInfo(entry);
999 entry=AcquireMagickInfo("MPC","MPC","Magick Persistent Cache image format");
1000 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
1001 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
1002 entry->magick=(IsImageFormatHandler *) IsMPC;
1003 (void) RegisterMagickInfo(entry);
1004 return(MagickImageCoderSignature);
1008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012 % U n r e g i s t e r M P C I m a g e %
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1018 % UnregisterMPCImage() removes format registrations made by the
1019 % MPC module from the list of supported formats.
1021 % The format of the UnregisterMPCImage method is:
1023 % UnregisterMPCImage(void)
1026 ModuleExport void UnregisterMPCImage(void)
1028 (void) UnregisterMagickInfo("CACHE");
1029 (void) UnregisterMagickInfo("MPC");
1033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037 % W r i t e M P C I m a g e %
1041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1043 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1045 % The format of the WriteMPCImage method is:
1047 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1048 % Image *image,ExceptionInfo *exception)
1050 % A description of each parameter follows:
1052 % o image_info: the image info.
1054 % o image: the image.
1056 % o exception: return any errors or warnings in this structure.
1059 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1060 ExceptionInfo *exception)
1063 buffer[MagickPathExtent],
1064 cache_filename[MagickPathExtent];
1084 Open persistent cache.
1086 assert(image_info != (const ImageInfo *) NULL);
1087 assert(image_info->signature == MagickCoreSignature);
1088 assert(image != (Image *) NULL);
1089 assert(image->signature == MagickCoreSignature);
1090 if (image->debug != MagickFalse)
1091 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1092 assert(exception != (ExceptionInfo *) NULL);
1093 assert(exception->signature == MagickCoreSignature);
1094 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1095 if (status == MagickFalse)
1097 (void) CopyMagickString(cache_filename,image->filename,MagickPathExtent);
1098 AppendImageFormat("cache",cache_filename);
1104 Write persistent cache meta-information.
1106 depth=GetImageQuantumDepth(image,MagickTrue);
1107 if ((image->storage_class == PseudoClass) &&
1108 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1109 (void) SetImageStorageClass(image,DirectClass,exception);
1110 (void) WriteBlobString(image,"id=MagickCache\n");
1111 (void) FormatLocaleString(buffer,MagickPathExtent,"magick-signature=%u\n",
1112 GetMagickSignature((const StringInfo *) NULL));
1113 (void) WriteBlobString(image,buffer);
1114 (void) FormatLocaleString(buffer,MagickPathExtent,
1115 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
1116 MagickClassOptions,image->storage_class),(double) image->colors,
1117 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
1118 image->alpha_trait));
1119 (void) WriteBlobString(image,buffer);
1120 (void) FormatLocaleString(buffer,MagickPathExtent,
1121 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
1122 (double) image->rows,(double) image->depth);
1123 (void) WriteBlobString(image,buffer);
1124 if (image->type != UndefinedType)
1126 (void) FormatLocaleString(buffer,MagickPathExtent,"type=%s\n",
1127 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1128 (void) WriteBlobString(image,buffer);
1130 if (image->colorspace != UndefinedColorspace)
1132 (void) FormatLocaleString(buffer,MagickPathExtent,"colorspace=%s\n",
1133 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1134 (void) WriteBlobString(image,buffer);
1136 if (image->intensity != UndefinedPixelIntensityMethod)
1138 (void) FormatLocaleString(buffer,MagickPathExtent,
1139 "pixel-intensity=%s\n",CommandOptionToMnemonic(
1140 MagickPixelIntensityOptions,image->intensity));
1141 (void) WriteBlobString(image,buffer);
1143 if (image->endian != UndefinedEndian)
1145 (void) FormatLocaleString(buffer,MagickPathExtent,"endian=%s\n",
1146 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1147 (void) WriteBlobString(image,buffer);
1149 if (image->compression != UndefinedCompression)
1151 (void) FormatLocaleString(buffer,MagickPathExtent,
1152 "compression=%s quality=%.20g\n",CommandOptionToMnemonic(
1153 MagickCompressOptions,image->compression),(double) image->quality);
1154 (void) WriteBlobString(image,buffer);
1156 if (image->units != UndefinedResolution)
1158 (void) FormatLocaleString(buffer,MagickPathExtent,"units=%s\n",
1159 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1160 (void) WriteBlobString(image,buffer);
1162 if ((image->resolution.x != 0) || (image->resolution.y != 0))
1164 (void) FormatLocaleString(buffer,MagickPathExtent,
1165 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1166 (void) WriteBlobString(image,buffer);
1168 if ((image->page.width != 0) || (image->page.height != 0))
1170 (void) FormatLocaleString(buffer,MagickPathExtent,
1171 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1172 image->page.height,(double) image->page.x,(double) image->page.y);
1173 (void) WriteBlobString(image,buffer);
1176 if ((image->page.x != 0) || (image->page.y != 0))
1178 (void) FormatLocaleString(buffer,MagickPathExtent,"page=%+ld%+ld\n",
1179 (long) image->page.x,(long) image->page.y);
1180 (void) WriteBlobString(image,buffer);
1182 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
1184 (void) FormatLocaleString(buffer,MagickPathExtent,
1185 "tile-offset=%+ld%+ld\n",(long) image->tile_offset.x,(long)
1186 image->tile_offset.y);
1187 (void) WriteBlobString(image,buffer);
1189 if ((GetNextImageInList(image) != (Image *) NULL) ||
1190 (GetPreviousImageInList(image) != (Image *) NULL))
1192 if (image->scene == 0)
1193 (void) FormatLocaleString(buffer,MagickPathExtent,
1194 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1195 image->iterations,(double) image->delay,(double)
1196 image->ticks_per_second);
1198 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g "
1199 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1200 (double) image->scene,(double) image->iterations,(double)
1201 image->delay,(double) image->ticks_per_second);
1202 (void) WriteBlobString(image,buffer);
1206 if (image->scene != 0)
1208 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g\n",
1209 (double) image->scene);
1210 (void) WriteBlobString(image,buffer);
1212 if (image->iterations != 0)
1214 (void) FormatLocaleString(buffer,MagickPathExtent,
1215 "iterations=%.20g\n",(double) image->iterations);
1216 (void) WriteBlobString(image,buffer);
1218 if (image->delay != 0)
1220 (void) FormatLocaleString(buffer,MagickPathExtent,"delay=%.20g\n",
1221 (double) image->delay);
1222 (void) WriteBlobString(image,buffer);
1224 if (image->ticks_per_second != UndefinedTicksPerSecond)
1226 (void) FormatLocaleString(buffer,MagickPathExtent,
1227 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1228 (void) WriteBlobString(image,buffer);
1231 if (image->gravity != UndefinedGravity)
1233 (void) FormatLocaleString(buffer,MagickPathExtent,"gravity=%s\n",
1234 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1235 (void) WriteBlobString(image,buffer);
1237 if (image->dispose != UndefinedDispose)
1239 (void) FormatLocaleString(buffer,MagickPathExtent,"dispose=%s\n",
1240 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1241 (void) WriteBlobString(image,buffer);
1243 if (image->rendering_intent != UndefinedIntent)
1245 (void) FormatLocaleString(buffer,MagickPathExtent,
1246 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1247 image->rendering_intent));
1248 (void) WriteBlobString(image,buffer);
1250 if (image->gamma != 0.0)
1252 (void) FormatLocaleString(buffer,MagickPathExtent,"gamma=%g\n",
1254 (void) WriteBlobString(image,buffer);
1256 if (image->chromaticity.white_point.x != 0.0)
1259 Note chomaticity points.
1261 (void) FormatLocaleString(buffer,MagickPathExtent,"red-primary="
1262 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1263 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1264 image->chromaticity.green_primary.x,
1265 image->chromaticity.green_primary.y,
1266 image->chromaticity.blue_primary.x,
1267 image->chromaticity.blue_primary.y);
1268 (void) WriteBlobString(image,buffer);
1269 (void) FormatLocaleString(buffer,MagickPathExtent,
1270 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1271 image->chromaticity.white_point.y);
1272 (void) WriteBlobString(image,buffer);
1274 if (image->orientation != UndefinedOrientation)
1276 (void) FormatLocaleString(buffer,MagickPathExtent,
1277 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1278 image->orientation));
1279 (void) WriteBlobString(image,buffer);
1281 if (image->profiles != (void *) NULL)
1292 ResetImageProfileIterator(image);
1293 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1295 profile=GetImageProfile(image,name);
1296 if (profile != (StringInfo *) NULL)
1298 (void) FormatLocaleString(buffer,MagickPathExtent,
1299 "profile:%s=%.20g\n",name,(double)
1300 GetStringInfoLength(profile));
1301 (void) WriteBlobString(image,buffer);
1303 name=GetNextImageProfile(image);
1306 if (image->montage != (char *) NULL)
1308 (void) FormatLocaleString(buffer,MagickPathExtent,"montage=%s\n",
1310 (void) WriteBlobString(image,buffer);
1312 ResetImagePropertyIterator(image);
1313 property=GetNextImageProperty(image);
1314 while (property != (const char *) NULL)
1316 (void) FormatLocaleString(buffer,MagickPathExtent,"%s=",property);
1317 (void) WriteBlobString(image,buffer);
1318 value=GetImageProperty(image,property,exception);
1319 if (value != (const char *) NULL)
1324 length=strlen(value);
1325 for (i=0; i < (ssize_t) length; i++)
1326 if (isspace((int) ((unsigned char) value[i])) != 0)
1328 if ((i == (ssize_t) length) && (i != 0))
1329 (void) WriteBlob(image,length,(const unsigned char *) value);
1332 (void) WriteBlobByte(image,'{');
1333 if (strchr(value,'}') == (char *) NULL)
1334 (void) WriteBlob(image,length,(const unsigned char *) value);
1336 for (i=0; i < (ssize_t) length; i++)
1338 if (value[i] == (int) '}')
1339 (void) WriteBlobByte(image,'\\');
1340 (void) WriteBlobByte(image,value[i]);
1342 (void) WriteBlobByte(image,'}');
1345 (void) WriteBlobByte(image,'\n');
1346 property=GetNextImageProperty(image);
1348 (void) WriteBlobString(image,"\f\n:\032");
1349 if (image->montage != (char *) NULL)
1352 Write montage tile directory.
1354 if (image->directory != (char *) NULL)
1355 (void) WriteBlobString(image,image->directory);
1356 (void) WriteBlobByte(image,'\0');
1358 if (image->profiles != 0)
1367 Write image profiles.
1369 ResetImageProfileIterator(image);
1370 name=GetNextImageProfile(image);
1371 while (name != (const char *) NULL)
1373 profile=GetImageProfile(image,name);
1374 (void) WriteBlob(image,GetStringInfoLength(profile),
1375 GetStringInfoDatum(profile));
1376 name=GetNextImageProfile(image);
1379 if (image->storage_class == PseudoClass)
1391 packet_size=(size_t) (3UL*depth/8UL);
1392 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1393 packet_size*sizeof(*colormap));
1394 if (colormap == (unsigned char *) NULL)
1395 return(MagickFalse);
1397 Write colormap to file.
1400 for (i=0; i < (ssize_t) image->colors; i++)
1405 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1411 pixel=ScaleQuantumToLong(image->colormap[i].red);
1412 q=PopLongPixel(MSBEndian,pixel,q);
1413 pixel=ScaleQuantumToLong(image->colormap[i].green);
1414 q=PopLongPixel(MSBEndian,pixel,q);
1415 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1416 q=PopLongPixel(MSBEndian,pixel,q);
1424 pixel=ScaleQuantumToShort(image->colormap[i].red);
1425 q=PopShortPixel(MSBEndian,pixel,q);
1426 pixel=ScaleQuantumToShort(image->colormap[i].green);
1427 q=PopShortPixel(MSBEndian,pixel,q);
1428 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1429 q=PopShortPixel(MSBEndian,pixel,q);
1437 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1438 q=PopCharPixel(pixel,q);
1439 pixel=(unsigned char) ScaleQuantumToChar(
1440 image->colormap[i].green);
1441 q=PopCharPixel(pixel,q);
1442 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1443 q=PopCharPixel(pixel,q);
1448 (void) WriteBlob(image,packet_size*image->colors,colormap);
1449 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1452 Initialize persistent pixel cache.
1454 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1456 if (status == MagickFalse)
1457 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1458 if (GetNextImageInList(image) == (Image *) NULL)
1460 image=SyncNextImageInList(image);
1461 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1463 status=image->progress_monitor(SaveImagesTag,scene,
1464 GetImageListLength(image),image->client_data);
1465 if (status == MagickFalse)
1469 } while (image_info->adjoin != MagickFalse);
1470 (void) CloseBlob(image);