2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Persistant Cache Image Format %
20 % Copyright 1999-2015 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/hashmap.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.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/static.h"
70 #include "MagickCore/statistic.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/string-private.h"
73 #include "MagickCore/utility.h"
74 #include "MagickCore/version-private.h"
79 static MagickBooleanType
80 WriteMPCImage(const ImageInfo *,Image *,ExceptionInfo *);
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % IsMPC() returns MagickTrue if the image format type, identified by the
94 % magick string, is an Magick Persistent Cache image.
96 % The format of the IsMPC method is:
98 % MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
100 % A description of each parameter follows:
102 % o magick: compare image format pattern against these bytes.
104 % o length: Specifies the length of the magick string.
107 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
111 if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % R e a d C A C H E I m a g e %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % ReadMPCImage() reads an Magick Persistent Cache image file and returns
128 % it. It allocates the memory necessary for the new Image structure and
129 % returns a pointer to the new image.
131 % The format of the ReadMPCImage method is:
133 % Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
135 % Decompression code contributed by Kyle Shorter.
137 % A description of each parameter follows:
139 % o image_info: the image info.
141 % o exception: return any errors or warnings in this structure.
144 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
147 cache_filename[MaxTextExtent],
149 keyword[MaxTextExtent],
195 assert(image_info != (const ImageInfo *) NULL);
196 assert(image_info->signature == MagickSignature);
197 if (image_info->debug != MagickFalse)
198 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
199 image_info->filename);
200 assert(exception != (ExceptionInfo *) NULL);
201 assert(exception->signature == MagickSignature);
202 image=AcquireImage(image_info,exception);
203 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
204 if (status == MagickFalse)
206 image=DestroyImageList(image);
207 return((Image *) NULL);
209 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
210 AppendImageFormat("cache",cache_filename);
211 c=ReadBlobByte(image);
214 image=DestroyImage(image);
215 return((Image *) NULL);
218 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
223 Decode image header; header terminates one character beyond a ':'.
225 profiles=(LinkedListInfo *) NULL;
226 length=MaxTextExtent;
227 options=AcquireString((char *) NULL);
228 signature=GetMagickSignature((const StringInfo *) NULL);
230 image->compression=NoCompression;
231 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
242 Read comment-- any text between { }.
244 length=MaxTextExtent;
245 comment=AcquireString((char *) NULL);
246 for (p=comment; comment != (char *) NULL; p++)
248 c=ReadBlobByte(image);
250 c=ReadBlobByte(image);
252 if ((c == EOF) || (c == (int) '}'))
254 if ((size_t) (p-comment+1) >= length)
258 comment=(char *) ResizeQuantumMemory(comment,length+
259 MaxTextExtent,sizeof(*comment));
260 if (comment == (char *) NULL)
262 p=comment+strlen(comment);
266 if (comment == (char *) NULL)
267 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
269 (void) SetImageProperty(image,"comment",comment,exception);
270 comment=DestroyString(comment);
271 c=ReadBlobByte(image);
274 if (isalnum(c) != MagickFalse)
279 length=MaxTextExtent;
285 if ((size_t) (p-keyword) < (MaxTextExtent-1))
287 c=ReadBlobByte(image);
291 while (isspace((int) ((unsigned char) c)) != 0)
292 c=ReadBlobByte(image);
296 Get the keyword value.
298 c=ReadBlobByte(image);
299 while ((c != (int) '}') && (c != EOF))
301 if ((size_t) (p-options+1) >= length)
305 options=(char *) ResizeQuantumMemory(options,length+
306 MaxTextExtent,sizeof(*options));
307 if (options == (char *) NULL)
309 p=options+strlen(options);
312 c=ReadBlobByte(image);
315 c=ReadBlobByte(image);
319 c=ReadBlobByte(image);
323 if (isspace((int) ((unsigned char) c)) != 0)
326 if (options == (char *) NULL)
327 ThrowReaderException(ResourceLimitError,
328 "MemoryAllocationFailed");
332 (void) CopyMagickString(options,options+1,strlen(options));
334 Assign a value to the specified keyword.
341 if (LocaleCompare(keyword,"alpha-trait") == 0)
346 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
347 MagickFalse,options);
350 image->alpha_trait=(PixelTrait) alpha_trait;
353 (void) SetImageProperty(image,keyword,options,exception);
359 if (LocaleCompare(keyword,"background-color") == 0)
361 (void) QueryColorCompliance(options,AllCompliance,
362 &image->background_color,exception);
365 if (LocaleCompare(keyword,"blue-primary") == 0)
367 flags=ParseGeometry(options,&geometry_info);
368 image->chromaticity.blue_primary.x=geometry_info.rho;
369 image->chromaticity.blue_primary.y=geometry_info.sigma;
370 if ((flags & SigmaValue) == 0)
371 image->chromaticity.blue_primary.y=
372 image->chromaticity.blue_primary.x;
375 if (LocaleCompare(keyword,"border-color") == 0)
377 (void) QueryColorCompliance(options,AllCompliance,
378 &image->border_color,exception);
381 (void) SetImageProperty(image,keyword,options,exception);
387 if (LocaleCompare(keyword,"class") == 0)
392 storage_class=ParseCommandOption(MagickClassOptions,
393 MagickFalse,options);
394 if (storage_class < 0)
396 image->storage_class=(ClassType) storage_class;
399 if (LocaleCompare(keyword,"colors") == 0)
401 image->colors=StringToUnsignedLong(options);
404 if (LocaleCompare(keyword,"colorspace") == 0)
409 colorspace=ParseCommandOption(MagickColorspaceOptions,
410 MagickFalse,options);
413 image->colorspace=(ColorspaceType) colorspace;
416 if (LocaleCompare(keyword,"compression") == 0)
421 compression=ParseCommandOption(MagickCompressOptions,
422 MagickFalse,options);
425 image->compression=(CompressionType) compression;
428 if (LocaleCompare(keyword,"columns") == 0)
430 image->columns=StringToUnsignedLong(options);
433 (void) SetImageProperty(image,keyword,options,exception);
439 if (LocaleCompare(keyword,"delay") == 0)
441 image->delay=StringToUnsignedLong(options);
444 if (LocaleCompare(keyword,"depth") == 0)
446 image->depth=StringToUnsignedLong(options);
449 if (LocaleCompare(keyword,"dispose") == 0)
454 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
458 image->dispose=(DisposeType) dispose;
461 (void) SetImageProperty(image,keyword,options,exception);
467 if (LocaleCompare(keyword,"endian") == 0)
472 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
476 image->endian=(EndianType) endian;
479 if (LocaleCompare(keyword,"error") == 0)
481 image->error.mean_error_per_pixel=StringToDouble(options,
485 (void) SetImageProperty(image,keyword,options,exception);
491 if (LocaleCompare(keyword,"gamma") == 0)
493 image->gamma=StringToDouble(options,(char **) NULL);
496 if (LocaleCompare(keyword,"green-primary") == 0)
498 flags=ParseGeometry(options,&geometry_info);
499 image->chromaticity.green_primary.x=geometry_info.rho;
500 image->chromaticity.green_primary.y=geometry_info.sigma;
501 if ((flags & SigmaValue) == 0)
502 image->chromaticity.green_primary.y=
503 image->chromaticity.green_primary.x;
506 (void) SetImageProperty(image,keyword,options,exception);
512 if (LocaleCompare(keyword,"id") == 0)
514 (void) CopyMagickString(id,options,MaxTextExtent);
517 if (LocaleCompare(keyword,"iterations") == 0)
519 image->iterations=StringToUnsignedLong(options);
522 (void) SetImageProperty(image,keyword,options,exception);
528 if (LocaleCompare(keyword,"magick-signature") == 0)
530 signature=(unsigned int) StringToUnsignedLong(options);
533 if (LocaleCompare(keyword,"matte-color") == 0)
535 (void) QueryColorCompliance(options,AllCompliance,
536 &image->matte_color,exception);
539 if (LocaleCompare(keyword,"maximum-error") == 0)
541 image->error.normalized_maximum_error=StringToDouble(
542 options,(char **) NULL);
545 if (LocaleCompare(keyword,"mean-error") == 0)
547 image->error.normalized_mean_error=StringToDouble(options,
551 if (LocaleCompare(keyword,"montage") == 0)
553 (void) CloneString(&image->montage,options);
556 (void) SetImageProperty(image,keyword,options,exception);
562 if (LocaleCompare(keyword,"orientation") == 0)
567 orientation=ParseCommandOption(MagickOrientationOptions,
568 MagickFalse,options);
571 image->orientation=(OrientationType) orientation;
574 (void) SetImageProperty(image,keyword,options,exception);
580 if (LocaleCompare(keyword,"page") == 0)
585 geometry=GetPageGeometry(options);
586 (void) ParseAbsoluteGeometry(geometry,&image->page);
587 geometry=DestroyString(geometry);
590 if (LocaleCompare(keyword,"pixel-intensity") == 0)
595 intensity=ParseCommandOption(MagickPixelIntensityOptions,
596 MagickFalse,options);
599 image->intensity=(PixelIntensityMethod) intensity;
602 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
603 (LocaleNCompare(keyword,"profile-",8) == 0))
605 if (profiles == (LinkedListInfo *) NULL)
606 profiles=NewLinkedList(0);
607 (void) AppendValueToLinkedList(profiles,
608 AcquireString(keyword+8));
609 profile=BlobToStringInfo((const void *) NULL,(size_t)
610 StringToLong(options));
611 if (profile == (StringInfo *) NULL)
612 ThrowReaderException(ResourceLimitError,
613 "MemoryAllocationFailed");
614 (void) SetImageProfile(image,keyword+8,profile,exception);
615 profile=DestroyStringInfo(profile);
618 (void) SetImageProperty(image,keyword,options,exception);
624 if (LocaleCompare(keyword,"quality") == 0)
626 image->quality=StringToUnsignedLong(options);
629 (void) SetImageProperty(image,keyword,options,exception);
635 if (LocaleCompare(keyword,"red-primary") == 0)
637 flags=ParseGeometry(options,&geometry_info);
638 image->chromaticity.red_primary.x=geometry_info.rho;
639 if ((flags & SigmaValue) != 0)
640 image->chromaticity.red_primary.y=geometry_info.sigma;
643 if (LocaleCompare(keyword,"rendering-intent") == 0)
648 rendering_intent=ParseCommandOption(MagickIntentOptions,
649 MagickFalse,options);
650 if (rendering_intent < 0)
652 image->rendering_intent=(RenderingIntent) rendering_intent;
655 if (LocaleCompare(keyword,"resolution") == 0)
657 flags=ParseGeometry(options,&geometry_info);
658 image->resolution.x=geometry_info.rho;
659 image->resolution.y=geometry_info.sigma;
660 if ((flags & SigmaValue) == 0)
661 image->resolution.y=image->resolution.x;
664 if (LocaleCompare(keyword,"rows") == 0)
666 image->rows=StringToUnsignedLong(options);
669 (void) SetImageProperty(image,keyword,options,exception);
675 if (LocaleCompare(keyword,"scene") == 0)
677 image->scene=StringToUnsignedLong(options);
680 (void) SetImageProperty(image,keyword,options,exception);
686 if (LocaleCompare(keyword,"ticks-per-second") == 0)
688 image->ticks_per_second=(ssize_t) StringToLong(options);
691 if (LocaleCompare(keyword,"tile-offset") == 0)
696 geometry=GetPageGeometry(options);
697 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
698 geometry=DestroyString(geometry);
700 if (LocaleCompare(keyword,"type") == 0)
705 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
709 image->type=(ImageType) type;
712 (void) SetImageProperty(image,keyword,options,exception);
718 if (LocaleCompare(keyword,"units") == 0)
723 units=ParseCommandOption(MagickResolutionOptions,
724 MagickFalse,options);
727 image->units=(ResolutionType) units;
730 (void) SetImageProperty(image,keyword,options,exception);
736 if (LocaleCompare(keyword,"white-point") == 0)
738 flags=ParseGeometry(options,&geometry_info);
739 image->chromaticity.white_point.x=geometry_info.rho;
740 image->chromaticity.white_point.y=geometry_info.sigma;
741 if ((flags & SigmaValue) == 0)
742 image->chromaticity.white_point.y=
743 image->chromaticity.white_point.x;
746 (void) SetImageProperty(image,keyword,options,exception);
751 (void) SetImageProperty(image,keyword,options,exception);
757 c=ReadBlobByte(image);
758 while (isspace((int) ((unsigned char) c)) != 0)
759 c=ReadBlobByte(image);
761 options=DestroyString(options);
762 (void) ReadBlobByte(image);
764 Verify that required image information is defined.
766 if ((LocaleCompare(id,"MagickCache") != 0) ||
767 (image->storage_class == UndefinedClass) ||
768 (image->compression == UndefinedCompression) || (image->columns == 0) ||
770 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
771 if (signature != GetMagickSignature((const StringInfo *) NULL))
772 ThrowReaderException(CacheError,"IncompatibleAPI");
773 if (image->montage != (char *) NULL)
781 length=MaxTextExtent;
782 image->directory=AcquireString((char *) NULL);
787 if ((strlen(image->directory)+MaxTextExtent) >= length)
790 Allocate more memory for the image directory.
793 image->directory=(char *) ResizeQuantumMemory(image->directory,
794 length+MaxTextExtent,sizeof(*image->directory));
795 if (image->directory == (char *) NULL)
796 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
797 p=image->directory+strlen(image->directory);
799 c=ReadBlobByte(image);
801 } while (c != (int) '\0');
803 if (profiles != (LinkedListInfo *) NULL)
811 register unsigned char
817 ResetLinkedListIterator(profiles);
818 name=(const char *) GetNextValueInLinkedList(profiles);
819 while (name != (const char *) NULL)
821 profile=GetImageProfile(image,name);
822 if (profile != (StringInfo *) NULL)
824 p=GetStringInfoDatum(profile);
825 count=ReadBlob(image,GetStringInfoLength(profile),p);
827 name=(const char *) GetNextValueInLinkedList(profiles);
829 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
831 depth=GetImageQuantumDepth(image,MagickFalse);
832 if (image->storage_class == PseudoClass)
835 Create image colormap.
837 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
838 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
839 if (image->colors != 0)
848 Read image colormap from file.
850 packet_size=(size_t) (3UL*depth/8UL);
851 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
852 packet_size*sizeof(*colormap));
853 if (colormap == (unsigned char *) NULL)
854 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
855 count=ReadBlob(image,packet_size*image->colors,colormap);
856 if (count != (ssize_t) (packet_size*image->colors))
857 ThrowReaderException(CorruptImageError,
858 "InsufficientImageDataInFile");
863 ThrowReaderException(CorruptImageError,
864 "ImageDepthNotSupported");
870 for (i=0; i < (ssize_t) image->colors; i++)
872 p=PushCharPixel(p,&pixel);
873 image->colormap[i].red=ScaleCharToQuantum(pixel);
874 p=PushCharPixel(p,&pixel);
875 image->colormap[i].green=ScaleCharToQuantum(pixel);
876 p=PushCharPixel(p,&pixel);
877 image->colormap[i].blue=ScaleCharToQuantum(pixel);
886 for (i=0; i < (ssize_t) image->colors; i++)
888 p=PushShortPixel(MSBEndian,p,&pixel);
889 image->colormap[i].red=ScaleShortToQuantum(pixel);
890 p=PushShortPixel(MSBEndian,p,&pixel);
891 image->colormap[i].green=ScaleShortToQuantum(pixel);
892 p=PushShortPixel(MSBEndian,p,&pixel);
893 image->colormap[i].blue=ScaleShortToQuantum(pixel);
902 for (i=0; i < (ssize_t) image->colors; i++)
904 p=PushLongPixel(MSBEndian,p,&pixel);
905 image->colormap[i].red=ScaleLongToQuantum(pixel);
906 p=PushLongPixel(MSBEndian,p,&pixel);
907 image->colormap[i].green=ScaleLongToQuantum(pixel);
908 p=PushLongPixel(MSBEndian,p,&pixel);
909 image->colormap[i].blue=ScaleLongToQuantum(pixel);
914 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
917 if (EOFBlob(image) != MagickFalse)
919 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
923 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
924 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
926 status=SetImageExtent(image,image->columns,image->rows,exception);
927 if (status == MagickFalse)
928 return(DestroyImageList(image));
930 Attach persistent pixel cache.
932 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
933 if (status == MagickFalse)
934 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
936 Proceed to next image.
940 c=ReadBlobByte(image);
941 } while ((isgraph(c) == MagickFalse) && (c != EOF));
945 Allocate next image structure.
947 AcquireNextImage(image_info,image,exception);
948 if (GetNextImageInList(image) == (Image *) NULL)
950 image=DestroyImageList(image);
951 return((Image *) NULL);
953 image=SyncNextImageInList(image);
954 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
956 if (status == MagickFalse)
960 (void) CloseBlob(image);
961 return(GetFirstImageInList(image));
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 % R e g i s t e r M P C I m a g e %
973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 % RegisterMPCImage() adds properties for the Cache image format to
976 % the list of supported formats. The properties include the image format
977 % tag, a method to read and/or write the format, whether the format
978 % supports the saving of more than one frame to the same file or blob,
979 % whether the format supports native in-memory I/O, and a brief
980 % description of the format.
982 % The format of the RegisterMPCImage method is:
984 % size_t RegisterMPCImage(void)
987 ModuleExport size_t RegisterMPCImage(void)
992 entry=SetMagickInfo("CACHE");
993 entry->description=ConstantString("Magick Persistent Cache image format");
994 entry->module=ConstantString("CACHE");
995 entry->stealth=MagickTrue;
996 (void) RegisterMagickInfo(entry);
997 entry=SetMagickInfo("MPC");
998 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
999 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
1000 entry->magick=(IsImageFormatHandler *) IsMPC;
1001 entry->description=ConstantString("Magick Persistent Cache image format");
1002 entry->module=ConstantString("MPC");
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[MaxTextExtent],
1064 cache_filename[MaxTextExtent];
1084 Open persistent cache.
1086 assert(image_info != (const ImageInfo *) NULL);
1087 assert(image_info->signature == MagickSignature);
1088 assert(image != (Image *) NULL);
1089 assert(image->signature == MagickSignature);
1090 if (image->debug != MagickFalse)
1091 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1092 assert(exception != (ExceptionInfo *) NULL);
1093 assert(exception->signature == MagickSignature);
1094 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1095 if (status == MagickFalse)
1097 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
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,MaxTextExtent,"magick-signature=%u\n",
1112 GetMagickSignature((const StringInfo *) NULL));
1113 (void) WriteBlobString(image,buffer);
1114 (void) FormatLocaleString(buffer,MaxTextExtent,
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,MaxTextExtent,
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,MaxTextExtent,"type=%s\n",
1127 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1128 (void) WriteBlobString(image,buffer);
1130 if (image->colorspace != UndefinedColorspace)
1132 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1133 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1134 (void) WriteBlobString(image,buffer);
1136 if (image->intensity != UndefinedPixelIntensityMethod)
1138 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
1139 CommandOptionToMnemonic(MagickPixelIntensityOptions,
1141 (void) WriteBlobString(image,buffer);
1143 if (image->endian != UndefinedEndian)
1145 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
1146 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1147 (void) WriteBlobString(image,buffer);
1149 if (image->compression != UndefinedCompression)
1151 (void) FormatLocaleString(buffer,MaxTextExtent,
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,MaxTextExtent,"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,MaxTextExtent,
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,MaxTextExtent,
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,MaxTextExtent,"page=%+ld%+ld\n",
1179 (long) image->page.x,(long) image->page.y);
1180 (void) WriteBlobString(image,buffer);
1182 if ((image->page.x != 0) || (image->page.y != 0))
1184 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1185 (long) image->tile_offset.x,(long) image->tile_offset.y);
1186 (void) WriteBlobString(image,buffer);
1188 if ((GetNextImageInList(image) != (Image *) NULL) ||
1189 (GetPreviousImageInList(image) != (Image *) NULL))
1191 if (image->scene == 0)
1192 (void) FormatLocaleString(buffer,MaxTextExtent,
1193 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1194 image->iterations,(double) image->delay,(double)
1195 image->ticks_per_second);
1197 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
1198 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1199 (double) image->scene,(double) image->iterations,(double)
1200 image->delay,(double) image->ticks_per_second);
1201 (void) WriteBlobString(image,buffer);
1205 if (image->scene != 0)
1207 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
1208 (double) image->scene);
1209 (void) WriteBlobString(image,buffer);
1211 if (image->iterations != 0)
1213 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
1214 (double) image->iterations);
1215 (void) WriteBlobString(image,buffer);
1217 if (image->delay != 0)
1219 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
1220 (double) image->delay);
1221 (void) WriteBlobString(image,buffer);
1223 if (image->ticks_per_second != UndefinedTicksPerSecond)
1225 (void) FormatLocaleString(buffer,MaxTextExtent,
1226 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1227 (void) WriteBlobString(image,buffer);
1230 if (image->gravity != UndefinedGravity)
1232 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
1233 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1234 (void) WriteBlobString(image,buffer);
1236 if (image->dispose != UndefinedDispose)
1238 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
1239 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1240 (void) WriteBlobString(image,buffer);
1242 if (image->rendering_intent != UndefinedIntent)
1244 (void) FormatLocaleString(buffer,MaxTextExtent,
1245 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1246 image->rendering_intent));
1247 (void) WriteBlobString(image,buffer);
1249 if (image->gamma != 0.0)
1251 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
1253 (void) WriteBlobString(image,buffer);
1255 if (image->chromaticity.white_point.x != 0.0)
1258 Note chomaticity points.
1260 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary="
1261 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1262 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1263 image->chromaticity.green_primary.x,
1264 image->chromaticity.green_primary.y,
1265 image->chromaticity.blue_primary.x,
1266 image->chromaticity.blue_primary.y);
1267 (void) WriteBlobString(image,buffer);
1268 (void) FormatLocaleString(buffer,MaxTextExtent,
1269 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1270 image->chromaticity.white_point.y);
1271 (void) WriteBlobString(image,buffer);
1273 if (image->orientation != UndefinedOrientation)
1275 (void) FormatLocaleString(buffer,MaxTextExtent,
1276 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1277 image->orientation));
1278 (void) WriteBlobString(image,buffer);
1280 if (image->profiles != (void *) NULL)
1291 ResetImageProfileIterator(image);
1292 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1294 profile=GetImageProfile(image,name);
1295 if (profile != (StringInfo *) NULL)
1297 (void) FormatLocaleString(buffer,MaxTextExtent,
1298 "profile:%s=%.20g\n",name,(double)
1299 GetStringInfoLength(profile));
1300 (void) WriteBlobString(image,buffer);
1302 name=GetNextImageProfile(image);
1305 if (image->montage != (char *) NULL)
1307 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
1309 (void) WriteBlobString(image,buffer);
1311 ResetImagePropertyIterator(image);
1312 property=GetNextImageProperty(image);
1313 while (property != (const char *) NULL)
1315 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
1316 (void) WriteBlobString(image,buffer);
1317 value=GetImageProperty(image,property,exception);
1318 if (value != (const char *) NULL)
1323 length=strlen(value);
1324 for (i=0; i < (ssize_t) length; i++)
1325 if (isspace((int) ((unsigned char) value[i])) != 0)
1327 if ((i == (ssize_t) length) && (i != 0))
1328 (void) WriteBlob(image,length,(const unsigned char *) value);
1331 (void) WriteBlobByte(image,'{');
1332 if (strchr(value,'}') == (char *) NULL)
1333 (void) WriteBlob(image,length,(const unsigned char *) value);
1335 for (i=0; i < (ssize_t) length; i++)
1337 if (value[i] == (int) '}')
1338 (void) WriteBlobByte(image,'\\');
1339 (void) WriteBlobByte(image,value[i]);
1341 (void) WriteBlobByte(image,'}');
1344 (void) WriteBlobByte(image,'\n');
1345 property=GetNextImageProperty(image);
1347 (void) WriteBlobString(image,"\f\n:\032");
1348 if (image->montage != (char *) NULL)
1351 Write montage tile directory.
1353 if (image->directory != (char *) NULL)
1354 (void) WriteBlobString(image,image->directory);
1355 (void) WriteBlobByte(image,'\0');
1357 if (image->profiles != 0)
1366 Write image profiles.
1368 ResetImageProfileIterator(image);
1369 name=GetNextImageProfile(image);
1370 while (name != (const char *) NULL)
1372 profile=GetImageProfile(image,name);
1373 (void) WriteBlob(image,GetStringInfoLength(profile),
1374 GetStringInfoDatum(profile));
1375 name=GetNextImageProfile(image);
1378 if (image->storage_class == PseudoClass)
1390 packet_size=(size_t) (3UL*depth/8UL);
1391 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1392 packet_size*sizeof(*colormap));
1393 if (colormap == (unsigned char *) NULL)
1394 return(MagickFalse);
1396 Write colormap to file.
1399 for (i=0; i < (ssize_t) image->colors; i++)
1404 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1410 pixel=ScaleQuantumToLong(image->colormap[i].red);
1411 q=PopLongPixel(MSBEndian,pixel,q);
1412 pixel=ScaleQuantumToLong(image->colormap[i].green);
1413 q=PopLongPixel(MSBEndian,pixel,q);
1414 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1415 q=PopLongPixel(MSBEndian,pixel,q);
1423 pixel=ScaleQuantumToShort(image->colormap[i].red);
1424 q=PopShortPixel(MSBEndian,pixel,q);
1425 pixel=ScaleQuantumToShort(image->colormap[i].green);
1426 q=PopShortPixel(MSBEndian,pixel,q);
1427 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1428 q=PopShortPixel(MSBEndian,pixel,q);
1436 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1437 q=PopCharPixel(pixel,q);
1438 pixel=(unsigned char) ScaleQuantumToChar(
1439 image->colormap[i].green);
1440 q=PopCharPixel(pixel,q);
1441 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1442 q=PopCharPixel(pixel,q);
1447 (void) WriteBlob(image,packet_size*image->colors,colormap);
1448 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1451 Initialize persistent pixel cache.
1453 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1455 if (status == MagickFalse)
1456 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1457 if (GetNextImageInList(image) == (Image *) NULL)
1459 image=SyncNextImageInList(image);
1460 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1462 status=image->progress_monitor(SaveImagesTag,scene,
1463 GetImageListLength(image),image->client_data);
1464 if (status == MagickFalse)
1468 } while (image_info->adjoin != MagickFalse);
1469 (void) CloseBlob(image);