2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Persistant Cache Image Format %
20 % Copyright 1999-2012 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"
78 static MagickBooleanType
79 WriteMPCImage(const ImageInfo *,Image *,ExceptionInfo *);
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % IsMPC() returns MagickTrue if the image format type, identified by the
93 % magick string, is an Magick Persistent Cache image.
95 % The format of the IsMPC method is:
97 % MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
99 % A description of each parameter follows:
101 % o magick: compare image format pattern against these bytes.
103 % o length: Specifies the length of the magick string.
106 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
110 if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % R e a d C A C H E I m a g e %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % ReadMPCImage() reads an Magick Persistent Cache image file and returns
127 % it. It allocates the memory necessary for the new Image structure and
128 % returns a pointer to the new image.
130 % The format of the ReadMPCImage method is:
132 % Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
134 % Decompression code contributed by Kyle Shorter.
136 % A description of each parameter follows:
138 % o image_info: the image info.
140 % o exception: return any errors or warnings in this structure.
143 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
146 cache_filename[MaxTextExtent],
148 keyword[MaxTextExtent],
192 assert(image_info != (const ImageInfo *) NULL);
193 assert(image_info->signature == MagickSignature);
194 if (image_info->debug != MagickFalse)
195 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
196 image_info->filename);
197 assert(exception != (ExceptionInfo *) NULL);
198 assert(exception->signature == MagickSignature);
199 image=AcquireImage(image_info,exception);
200 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
201 if (status == MagickFalse)
203 image=DestroyImageList(image);
204 return((Image *) NULL);
206 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
207 AppendImageFormat("cache",cache_filename);
208 c=ReadBlobByte(image);
211 image=DestroyImage(image);
212 return((Image *) NULL);
215 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
220 Decode image header; header terminates one character beyond a ':'.
222 profiles=(LinkedListInfo *) NULL;
223 length=MaxTextExtent;
224 options=AcquireString((char *) NULL);
225 quantum_depth=MAGICKCORE_QUANTUM_DEPTH;
227 image->compression=NoCompression;
228 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
239 Read comment-- any text between { }.
241 length=MaxTextExtent;
242 comment=AcquireString((char *) NULL);
243 for (p=comment; comment != (char *) NULL; p++)
245 c=ReadBlobByte(image);
246 if ((c == EOF) || (c == (int) '}'))
248 if ((size_t) (p-comment+1) >= length)
252 comment=(char *) ResizeQuantumMemory(comment,length+
253 MaxTextExtent,sizeof(*comment));
254 if (comment == (char *) NULL)
256 p=comment+strlen(comment);
260 if (comment == (char *) NULL)
261 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
263 (void) SetImageProperty(image,"comment",comment,exception);
264 comment=DestroyString(comment);
265 c=ReadBlobByte(image);
268 if (isalnum(c) != MagickFalse)
278 if ((size_t) (p-keyword) < (MaxTextExtent-1))
280 c=ReadBlobByte(image);
284 while (isspace((int) ((unsigned char) c)) != 0)
285 c=ReadBlobByte(image);
289 Get the keyword value.
291 c=ReadBlobByte(image);
292 while ((c != (int) '}') && (c != EOF))
294 if ((size_t) (p-options+1) >= length)
298 options=(char *) ResizeQuantumMemory(options,length+
299 MaxTextExtent,sizeof(*options));
300 if (options == (char *) NULL)
302 p=options+strlen(options);
304 if (options == (char *) NULL)
305 ThrowReaderException(ResourceLimitError,
306 "MemoryAllocationFailed");
308 c=ReadBlobByte(image);
310 if (isspace((int) ((unsigned char) c)) != 0)
316 (void) CopyMagickString(options,options+1,MaxTextExtent);
318 Assign a value to the specified keyword.
325 if (LocaleCompare(keyword,"background-color") == 0)
327 (void) QueryColorCompliance(options,AllCompliance,
328 &image->background_color,exception);
331 if (LocaleCompare(keyword,"blue-primary") == 0)
333 flags=ParseGeometry(options,&geometry_info);
334 image->chromaticity.blue_primary.x=geometry_info.rho;
335 image->chromaticity.blue_primary.y=geometry_info.sigma;
336 if ((flags & SigmaValue) == 0)
337 image->chromaticity.blue_primary.y=
338 image->chromaticity.blue_primary.x;
341 if (LocaleCompare(keyword,"border-color") == 0)
343 (void) QueryColorCompliance(options,AllCompliance,
344 &image->border_color,exception);
347 (void) SetImageProperty(image,keyword,options,exception);
353 if (LocaleCompare(keyword,"class") == 0)
358 storage_class=ParseCommandOption(MagickClassOptions,
359 MagickFalse,options);
360 if (storage_class < 0)
362 image->storage_class=(ClassType) storage_class;
365 if (LocaleCompare(keyword,"colors") == 0)
367 image->colors=StringToUnsignedLong(options);
370 if (LocaleCompare(keyword,"colorspace") == 0)
375 colorspace=ParseCommandOption(MagickColorspaceOptions,
376 MagickFalse,options);
379 image->colorspace=(ColorspaceType) colorspace;
382 if (LocaleCompare(keyword,"compression") == 0)
387 compression=ParseCommandOption(MagickCompressOptions,
388 MagickFalse,options);
391 image->compression=(CompressionType) compression;
394 if (LocaleCompare(keyword,"columns") == 0)
396 image->columns=StringToUnsignedLong(options);
399 (void) SetImageProperty(image,keyword,options,exception);
405 if (LocaleCompare(keyword,"delay") == 0)
407 image->delay=StringToUnsignedLong(options);
410 if (LocaleCompare(keyword,"depth") == 0)
412 image->depth=StringToUnsignedLong(options);
415 if (LocaleCompare(keyword,"dispose") == 0)
420 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
424 image->dispose=(DisposeType) dispose;
427 (void) SetImageProperty(image,keyword,options,exception);
433 if (LocaleCompare(keyword,"endian") == 0)
438 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
442 image->endian=(EndianType) endian;
445 if (LocaleCompare(keyword,"error") == 0)
447 image->error.mean_error_per_pixel=StringToDouble(options,
451 (void) SetImageProperty(image,keyword,options,exception);
457 if (LocaleCompare(keyword,"gamma") == 0)
459 image->gamma=StringToDouble(options,(char **) NULL);
462 if (LocaleCompare(keyword,"green-primary") == 0)
464 flags=ParseGeometry(options,&geometry_info);
465 image->chromaticity.green_primary.x=geometry_info.rho;
466 image->chromaticity.green_primary.y=geometry_info.sigma;
467 if ((flags & SigmaValue) == 0)
468 image->chromaticity.green_primary.y=
469 image->chromaticity.green_primary.x;
472 (void) SetImageProperty(image,keyword,options,exception);
478 if (LocaleCompare(keyword,"id") == 0)
480 (void) CopyMagickString(id,options,MaxTextExtent);
483 if (LocaleCompare(keyword,"iterations") == 0)
485 image->iterations=StringToUnsignedLong(options);
488 (void) SetImageProperty(image,keyword,options,exception);
494 if (LocaleCompare(keyword,"matte") == 0)
499 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
503 image->matte=(MagickBooleanType) matte;
506 if (LocaleCompare(keyword,"matte-color") == 0)
508 (void) QueryColorCompliance(options,AllCompliance,
509 &image->matte_color,exception);
512 if (LocaleCompare(keyword,"maximum-error") == 0)
514 image->error.normalized_maximum_error=StringToDouble(
515 options,(char **) NULL);
518 if (LocaleCompare(keyword,"mean-error") == 0)
520 image->error.normalized_mean_error=StringToDouble(options,
524 if (LocaleCompare(keyword,"montage") == 0)
526 (void) CloneString(&image->montage,options);
529 (void) SetImageProperty(image,keyword,options,exception);
535 if (LocaleCompare(keyword,"opaque") == 0)
540 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
544 image->matte=(MagickBooleanType) matte;
547 if (LocaleCompare(keyword,"orientation") == 0)
552 orientation=ParseCommandOption(MagickOrientationOptions,
553 MagickFalse,options);
556 image->orientation=(OrientationType) orientation;
559 (void) SetImageProperty(image,keyword,options,exception);
565 if (LocaleCompare(keyword,"page") == 0)
570 geometry=GetPageGeometry(options);
571 (void) ParseAbsoluteGeometry(geometry,&image->page);
572 geometry=DestroyString(geometry);
575 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
576 (LocaleNCompare(keyword,"profile-",8) == 0))
578 if (profiles == (LinkedListInfo *) NULL)
579 profiles=NewLinkedList(0);
580 (void) AppendValueToLinkedList(profiles,
581 AcquireString(keyword+8));
582 profile=BlobToStringInfo((const void *) NULL,(size_t)
583 StringToLong(options));
584 if (profile == (StringInfo *) NULL)
585 ThrowReaderException(ResourceLimitError,
586 "MemoryAllocationFailed");
587 (void) SetImageProfile(image,keyword+8,profile,exception);
588 profile=DestroyStringInfo(profile);
591 (void) SetImageProperty(image,keyword,options,exception);
597 if (LocaleCompare(keyword,"quality") == 0)
599 image->quality=StringToUnsignedLong(options);
602 if (LocaleCompare(keyword,"quantum-depth") == 0)
604 quantum_depth=StringToUnsignedLong(options);
607 (void) SetImageProperty(image,keyword,options,exception);
613 if (LocaleCompare(keyword,"red-primary") == 0)
615 flags=ParseGeometry(options,&geometry_info);
616 image->chromaticity.red_primary.x=geometry_info.rho;
617 if ((flags & SigmaValue) != 0)
618 image->chromaticity.red_primary.y=geometry_info.sigma;
621 if (LocaleCompare(keyword,"rendering-intent") == 0)
626 rendering_intent=ParseCommandOption(MagickIntentOptions,
627 MagickFalse,options);
628 if (rendering_intent < 0)
630 image->rendering_intent=(RenderingIntent) rendering_intent;
633 if (LocaleCompare(keyword,"resolution") == 0)
635 flags=ParseGeometry(options,&geometry_info);
636 image->resolution.x=geometry_info.rho;
637 image->resolution.y=geometry_info.sigma;
638 if ((flags & SigmaValue) == 0)
639 image->resolution.y=image->resolution.x;
642 if (LocaleCompare(keyword,"rows") == 0)
644 image->rows=StringToUnsignedLong(options);
647 (void) SetImageProperty(image,keyword,options,exception);
653 if (LocaleCompare(keyword,"scene") == 0)
655 image->scene=StringToUnsignedLong(options);
658 (void) SetImageProperty(image,keyword,options,exception);
664 if (LocaleCompare(keyword,"ticks-per-second") == 0)
666 image->ticks_per_second=(ssize_t) StringToLong(options);
669 if (LocaleCompare(keyword,"tile-offset") == 0)
674 geometry=GetPageGeometry(options);
675 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
676 geometry=DestroyString(geometry);
678 if (LocaleCompare(keyword,"type") == 0)
683 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
687 image->type=(ImageType) type;
690 (void) SetImageProperty(image,keyword,options,exception);
696 if (LocaleCompare(keyword,"units") == 0)
701 units=ParseCommandOption(MagickResolutionOptions,
702 MagickFalse,options);
705 image->units=(ResolutionType) units;
708 (void) SetImageProperty(image,keyword,options,exception);
714 if (LocaleCompare(keyword,"white-point") == 0)
716 flags=ParseGeometry(options,&geometry_info);
717 image->chromaticity.white_point.x=geometry_info.rho;
718 image->chromaticity.white_point.y=geometry_info.sigma;
719 if ((flags & SigmaValue) == 0)
720 image->chromaticity.white_point.y=
721 image->chromaticity.white_point.x;
724 (void) SetImageProperty(image,keyword,options,exception);
729 (void) SetImageProperty(image,keyword,options,exception);
735 c=ReadBlobByte(image);
736 while (isspace((int) ((unsigned char) c)) != 0)
737 c=ReadBlobByte(image);
739 options=DestroyString(options);
740 (void) ReadBlobByte(image);
742 Verify that required image information is defined.
744 if ((LocaleCompare(id,"MagickCache") != 0) ||
745 (image->storage_class == UndefinedClass) ||
746 (image->compression == UndefinedCompression) || (image->columns == 0) ||
748 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
749 if (quantum_depth != MAGICKCORE_QUANTUM_DEPTH)
750 ThrowReaderException(CacheError,"InconsistentPersistentCacheDepth");
751 if (image->montage != (char *) NULL)
759 length=MaxTextExtent;
760 image->directory=AcquireString((char *) NULL);
765 if ((strlen(image->directory)+MaxTextExtent) >= length)
768 Allocate more memory for the image directory.
771 image->directory=(char *) ResizeQuantumMemory(image->directory,
772 length+MaxTextExtent,sizeof(*image->directory));
773 if (image->directory == (char *) NULL)
774 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
775 p=image->directory+strlen(image->directory);
777 c=ReadBlobByte(image);
779 } while (c != (int) '\0');
781 if (profiles != (LinkedListInfo *) NULL)
789 register unsigned char
795 ResetLinkedListIterator(profiles);
796 name=(const char *) GetNextValueInLinkedList(profiles);
797 while (name != (const char *) NULL)
799 profile=GetImageProfile(image,name);
800 if (profile != (StringInfo *) NULL)
802 p=GetStringInfoDatum(profile);
803 count=ReadBlob(image,GetStringInfoLength(profile),p);
805 name=(const char *) GetNextValueInLinkedList(profiles);
807 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
809 depth=GetImageQuantumDepth(image,MagickFalse);
810 if (image->storage_class == PseudoClass)
813 Create image colormap.
815 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
816 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
817 if (image->colors != 0)
826 Read image colormap from file.
828 packet_size=(size_t) (3UL*depth/8UL);
829 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
830 packet_size*sizeof(*colormap));
831 if (colormap == (unsigned char *) NULL)
832 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
833 count=ReadBlob(image,packet_size*image->colors,colormap);
834 if (count != (ssize_t) (packet_size*image->colors))
835 ThrowReaderException(CorruptImageError,
836 "InsufficientImageDataInFile");
841 ThrowReaderException(CorruptImageError,
842 "ImageDepthNotSupported");
848 for (i=0; i < (ssize_t) image->colors; i++)
850 p=PushCharPixel(p,&pixel);
851 image->colormap[i].red=ScaleCharToQuantum(pixel);
852 p=PushCharPixel(p,&pixel);
853 image->colormap[i].green=ScaleCharToQuantum(pixel);
854 p=PushCharPixel(p,&pixel);
855 image->colormap[i].blue=ScaleCharToQuantum(pixel);
864 for (i=0; i < (ssize_t) image->colors; i++)
866 p=PushShortPixel(MSBEndian,p,&pixel);
867 image->colormap[i].red=ScaleShortToQuantum(pixel);
868 p=PushShortPixel(MSBEndian,p,&pixel);
869 image->colormap[i].green=ScaleShortToQuantum(pixel);
870 p=PushShortPixel(MSBEndian,p,&pixel);
871 image->colormap[i].blue=ScaleShortToQuantum(pixel);
880 for (i=0; i < (ssize_t) image->colors; i++)
882 p=PushLongPixel(MSBEndian,p,&pixel);
883 image->colormap[i].red=ScaleLongToQuantum(pixel);
884 p=PushLongPixel(MSBEndian,p,&pixel);
885 image->colormap[i].green=ScaleLongToQuantum(pixel);
886 p=PushLongPixel(MSBEndian,p,&pixel);
887 image->colormap[i].blue=ScaleLongToQuantum(pixel);
892 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
895 if (EOFBlob(image) != MagickFalse)
897 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
901 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
902 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
905 Attach persistent pixel cache.
907 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
908 if (status == MagickFalse)
909 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
911 Proceed to next image.
915 c=ReadBlobByte(image);
916 } while ((isgraph(c) == MagickFalse) && (c != EOF));
920 Allocate next image structure.
922 AcquireNextImage(image_info,image,exception);
923 if (GetNextImageInList(image) == (Image *) NULL)
925 image=DestroyImageList(image);
926 return((Image *) NULL);
928 image=SyncNextImageInList(image);
929 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
931 if (status == MagickFalse)
935 (void) CloseBlob(image);
936 return(GetFirstImageInList(image));
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 % R e g i s t e r M P C I m a g e %
948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 % RegisterMPCImage() adds properties for the Cache image format to
951 % the list of supported formats. The properties include the image format
952 % tag, a method to read and/or write the format, whether the format
953 % supports the saving of more than one frame to the same file or blob,
954 % whether the format supports native in-memory I/O, and a brief
955 % description of the format.
957 % The format of the RegisterMPCImage method is:
959 % size_t RegisterMPCImage(void)
962 ModuleExport size_t RegisterMPCImage(void)
967 entry=SetMagickInfo("CACHE");
968 entry->description=ConstantString("Magick Persistent Cache image format");
969 entry->module=ConstantString("CACHE");
970 entry->stealth=MagickTrue;
971 (void) RegisterMagickInfo(entry);
972 entry=SetMagickInfo("MPC");
973 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
974 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
975 entry->magick=(IsImageFormatHandler *) IsMPC;
976 entry->description=ConstantString("Magick Persistent Cache image format");
977 entry->module=ConstantString("MPC");
978 (void) RegisterMagickInfo(entry);
979 return(MagickImageCoderSignature);
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % U n r e g i s t e r M P C I m a g e %
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % UnregisterMPCImage() removes format registrations made by the
994 % MPC module from the list of supported formats.
996 % The format of the UnregisterMPCImage method is:
998 % UnregisterMPCImage(void)
1001 ModuleExport void UnregisterMPCImage(void)
1003 (void) UnregisterMagickInfo("CACHE");
1004 (void) UnregisterMagickInfo("MPC");
1008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012 % W r i t e M P C I m a g e %
1016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1018 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1020 % The format of the WriteMPCImage method is:
1022 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1023 % Image *image,ExceptionInfo *exception)
1025 % A description of each parameter follows:
1027 % o image_info: the image info.
1029 % o image: the image.
1031 % o exception: return any errors or warnings in this structure.
1034 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1035 ExceptionInfo *exception)
1038 buffer[MaxTextExtent],
1039 cache_filename[MaxTextExtent];
1060 Open persistent cache.
1062 assert(image_info != (const ImageInfo *) NULL);
1063 assert(image_info->signature == MagickSignature);
1064 assert(image != (Image *) NULL);
1065 assert(image->signature == MagickSignature);
1066 if (image->debug != MagickFalse)
1067 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1068 assert(exception != (ExceptionInfo *) NULL);
1069 assert(exception->signature == MagickSignature);
1070 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1071 if (status == MagickFalse)
1073 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
1074 AppendImageFormat("cache",cache_filename);
1081 Write persistent cache meta-information.
1083 depth=GetImageQuantumDepth(image,MagickTrue);
1084 if ((image->storage_class == PseudoClass) &&
1085 (image->colors > (one << depth)))
1086 image->storage_class=DirectClass;
1087 (void) WriteBlobString(image,"id=MagickCache\n");
1088 (void) FormatLocaleString(buffer,MaxTextExtent,"quantum-depth=%d\n",
1089 MAGICKCORE_QUANTUM_DEPTH);
1090 (void) WriteBlobString(image,buffer);
1091 (void) FormatLocaleString(buffer,MaxTextExtent,
1092 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1093 MagickClassOptions,image->storage_class),(double) image->colors,
1094 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1095 (void) WriteBlobString(image,buffer);
1096 (void) FormatLocaleString(buffer,MaxTextExtent,
1097 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
1098 (double) image->rows,(double) image->depth);
1099 (void) WriteBlobString(image,buffer);
1100 if (image->type != UndefinedType)
1102 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1103 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1104 (void) WriteBlobString(image,buffer);
1106 if (image->colorspace != UndefinedColorspace)
1108 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1109 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1110 (void) WriteBlobString(image,buffer);
1112 if (image->endian != UndefinedEndian)
1114 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
1115 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1116 (void) WriteBlobString(image,buffer);
1118 if (image->compression != UndefinedCompression)
1120 (void) FormatLocaleString(buffer,MaxTextExtent,
1121 "compression=%s quality=%.20g\n",CommandOptionToMnemonic(
1122 MagickCompressOptions,image->compression),(double) image->quality);
1123 (void) WriteBlobString(image,buffer);
1125 if (image->units != UndefinedResolution)
1127 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
1128 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1129 (void) WriteBlobString(image,buffer);
1131 if ((image->resolution.x != 0) || (image->resolution.y != 0))
1133 (void) FormatLocaleString(buffer,MaxTextExtent,
1134 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1135 (void) WriteBlobString(image,buffer);
1137 if ((image->page.width != 0) || (image->page.height != 0))
1139 (void) FormatLocaleString(buffer,MaxTextExtent,
1140 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1141 image->page.height,(double) image->page.x,(double) image->page.y);
1142 (void) WriteBlobString(image,buffer);
1145 if ((image->page.x != 0) || (image->page.y != 0))
1147 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
1148 (long) image->page.x,(long) image->page.y);
1149 (void) WriteBlobString(image,buffer);
1151 if ((image->page.x != 0) || (image->page.y != 0))
1153 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1154 (long) image->tile_offset.x,(long) image->tile_offset.y);
1155 (void) WriteBlobString(image,buffer);
1157 if ((GetNextImageInList(image) != (Image *) NULL) ||
1158 (GetPreviousImageInList(image) != (Image *) NULL))
1160 if (image->scene == 0)
1161 (void) FormatLocaleString(buffer,MaxTextExtent,
1162 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1163 image->iterations,(double) image->delay,(double)
1164 image->ticks_per_second);
1166 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
1167 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1168 (double) image->scene,(double) image->iterations,(double)
1169 image->delay,(double) image->ticks_per_second);
1170 (void) WriteBlobString(image,buffer);
1174 if (image->scene != 0)
1176 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
1177 (double) image->scene);
1178 (void) WriteBlobString(image,buffer);
1180 if (image->iterations != 0)
1182 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
1183 (double) image->iterations);
1184 (void) WriteBlobString(image,buffer);
1186 if (image->delay != 0)
1188 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
1189 (double) image->delay);
1190 (void) WriteBlobString(image,buffer);
1192 if (image->ticks_per_second != UndefinedTicksPerSecond)
1194 (void) FormatLocaleString(buffer,MaxTextExtent,
1195 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1196 (void) WriteBlobString(image,buffer);
1199 if (image->gravity != UndefinedGravity)
1201 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
1202 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1203 (void) WriteBlobString(image,buffer);
1205 if (image->dispose != UndefinedDispose)
1207 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
1208 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1209 (void) WriteBlobString(image,buffer);
1211 if (image->rendering_intent != UndefinedIntent)
1213 (void) FormatLocaleString(buffer,MaxTextExtent,
1214 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1215 image->rendering_intent));
1216 (void) WriteBlobString(image,buffer);
1218 if (image->gamma != 0.0)
1220 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
1222 (void) WriteBlobString(image,buffer);
1224 if (image->chromaticity.white_point.x != 0.0)
1227 Note chomaticity points.
1229 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary="
1230 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1231 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1232 image->chromaticity.green_primary.x,
1233 image->chromaticity.green_primary.y,
1234 image->chromaticity.blue_primary.x,
1235 image->chromaticity.blue_primary.y);
1236 (void) WriteBlobString(image,buffer);
1237 (void) FormatLocaleString(buffer,MaxTextExtent,
1238 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1239 image->chromaticity.white_point.y);
1240 (void) WriteBlobString(image,buffer);
1242 if (image->orientation != UndefinedOrientation)
1244 (void) FormatLocaleString(buffer,MaxTextExtent,
1245 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1246 image->orientation));
1247 (void) WriteBlobString(image,buffer);
1249 if (image->profiles != (void *) NULL)
1260 ResetImageProfileIterator(image);
1261 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1263 profile=GetImageProfile(image,name);
1264 if (profile != (StringInfo *) NULL)
1266 (void) FormatLocaleString(buffer,MaxTextExtent,
1267 "profile:%s=%.20g\n",name,(double)
1268 GetStringInfoLength(profile));
1269 (void) WriteBlobString(image,buffer);
1271 name=GetNextImageProfile(image);
1274 if (image->montage != (char *) NULL)
1276 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
1278 (void) WriteBlobString(image,buffer);
1280 ResetImagePropertyIterator(image);
1281 property=GetNextImageProperty(image);
1282 while (property != (const char *) NULL)
1284 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
1285 (void) WriteBlobString(image,buffer);
1286 value=GetImageProperty(image,property,exception);
1287 if (value != (const char *) NULL)
1289 for (i=0; i < (ssize_t) strlen(value); i++)
1290 if (isspace((int) ((unsigned char) value[i])) != 0)
1292 if (i <= (ssize_t) strlen(value))
1293 (void) WriteBlobByte(image,'{');
1294 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
1295 if (i <= (ssize_t) strlen(value))
1296 (void) WriteBlobByte(image,'}');
1298 (void) WriteBlobByte(image,'\n');
1299 property=GetNextImageProperty(image);
1301 ResetImageArtifactIterator(image);
1302 (void) WriteBlobString(image,"\f\n:\032");
1303 if (image->montage != (char *) NULL)
1306 Write montage tile directory.
1308 if (image->directory != (char *) NULL)
1309 (void) WriteBlobString(image,image->directory);
1310 (void) WriteBlobByte(image,'\0');
1312 if (image->profiles != 0)
1321 Write image profiles.
1323 ResetImageProfileIterator(image);
1324 name=GetNextImageProfile(image);
1325 while (name != (const char *) NULL)
1327 profile=GetImageProfile(image,name);
1328 (void) WriteBlob(image,GetStringInfoLength(profile),
1329 GetStringInfoDatum(profile));
1330 name=GetNextImageProfile(image);
1333 if (image->storage_class == PseudoClass)
1345 packet_size=(size_t) (3UL*depth/8UL);
1346 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1347 packet_size*sizeof(*colormap));
1348 if (colormap == (unsigned char *) NULL)
1349 return(MagickFalse);
1351 Write colormap to file.
1354 for (i=0; i < (ssize_t) image->colors; i++)
1359 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1365 pixel=ScaleQuantumToLong(image->colormap[i].red);
1366 q=PopLongPixel(MSBEndian,pixel,q);
1367 pixel=ScaleQuantumToLong(image->colormap[i].green);
1368 q=PopLongPixel(MSBEndian,pixel,q);
1369 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1370 q=PopLongPixel(MSBEndian,pixel,q);
1377 pixel=ScaleQuantumToShort(image->colormap[i].red);
1378 q=PopShortPixel(MSBEndian,pixel,q);
1379 pixel=ScaleQuantumToShort(image->colormap[i].green);
1380 q=PopShortPixel(MSBEndian,pixel,q);
1381 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1382 q=PopShortPixel(MSBEndian,pixel,q);
1390 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1391 q=PopCharPixel(pixel,q);
1392 pixel=(unsigned char) ScaleQuantumToChar(
1393 image->colormap[i].green);
1394 q=PopCharPixel(pixel,q);
1395 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1396 q=PopCharPixel(pixel,q);
1401 (void) WriteBlob(image,packet_size*image->colors,colormap);
1402 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1405 Initialize persistent pixel cache.
1407 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1409 if (status == MagickFalse)
1410 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1411 if (GetNextImageInList(image) == (Image *) NULL)
1413 image=SyncNextImageInList(image);
1414 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1416 status=image->progress_monitor(SaveImagesTag,scene,
1417 GetImageListLength(image),image->client_data);
1418 if (status == MagickFalse)
1422 } while (image_info->adjoin != MagickFalse);
1423 (void) CloseBlob(image);