2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Persistant Cache Image Format %
20 % Copyright 1999-2011 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 "magick/studio.h"
44 #include "magick/artifact.h"
45 #include "magick/attribute.h"
46 #include "magick/blob.h"
47 #include "magick/blob-private.h"
48 #include "magick/cache.h"
49 #include "magick/color.h"
50 #include "magick/color-private.h"
51 #include "magick/colormap.h"
52 #include "magick/constitute.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/geometry.h"
56 #include "magick/hashmap.h"
57 #include "magick/image.h"
58 #include "magick/image-private.h"
59 #include "magick/list.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/module.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/option.h"
66 #include "magick/profile.h"
67 #include "magick/property.h"
68 #include "magick/quantum-private.h"
69 #include "magick/static.h"
70 #include "magick/statistic.h"
71 #include "magick/string_.h"
72 #include "magick/string-private.h"
73 #include "magick/utility.h"
78 static MagickBooleanType
79 WriteMPCImage(const ImageInfo *,Image *);
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);
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);
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) QueryColorDatabase(options,&image->background_color,
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) QueryColorDatabase(options,&image->border_color,
347 (void) SetImageProperty(image,keyword,options);
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);
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);
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);
450 (void) SetImageProperty(image,keyword,options);
456 if (LocaleCompare(keyword,"gamma") == 0)
458 image->gamma=StringToDouble(options);
461 if (LocaleCompare(keyword,"green-primary") == 0)
463 flags=ParseGeometry(options,&geometry_info);
464 image->chromaticity.green_primary.x=geometry_info.rho;
465 image->chromaticity.green_primary.y=geometry_info.sigma;
466 if ((flags & SigmaValue) == 0)
467 image->chromaticity.green_primary.y=
468 image->chromaticity.green_primary.x;
471 (void) SetImageProperty(image,keyword,options);
477 if (LocaleCompare(keyword,"id") == 0)
479 (void) CopyMagickString(id,options,MaxTextExtent);
482 if (LocaleCompare(keyword,"iterations") == 0)
484 image->iterations=StringToUnsignedLong(options);
487 (void) SetImageProperty(image,keyword,options);
493 if (LocaleCompare(keyword,"matte") == 0)
498 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
502 image->matte=(MagickBooleanType) matte;
505 if (LocaleCompare(keyword,"matte-color") == 0)
507 (void) QueryColorDatabase(options,&image->matte_color,
511 if (LocaleCompare(keyword,"maximum-error") == 0)
513 image->error.normalized_maximum_error=
514 StringToDouble(options);
517 if (LocaleCompare(keyword,"mean-error") == 0)
519 image->error.normalized_mean_error=StringToDouble(options);
522 if (LocaleCompare(keyword,"montage") == 0)
524 (void) CloneString(&image->montage,options);
527 (void) SetImageProperty(image,keyword,options);
533 if (LocaleCompare(keyword,"opaque") == 0)
538 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
542 image->matte=(MagickBooleanType) matte;
545 if (LocaleCompare(keyword,"orientation") == 0)
550 orientation=ParseCommandOption(MagickOrientationOptions,
551 MagickFalse,options);
554 image->orientation=(OrientationType) orientation;
557 (void) SetImageProperty(image,keyword,options);
563 if (LocaleCompare(keyword,"page") == 0)
568 geometry=GetPageGeometry(options);
569 (void) ParseAbsoluteGeometry(geometry,&image->page);
570 geometry=DestroyString(geometry);
573 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
574 (LocaleNCompare(keyword,"profile-",8) == 0))
576 if (profiles == (LinkedListInfo *) NULL)
577 profiles=NewLinkedList(0);
578 (void) AppendValueToLinkedList(profiles,
579 AcquireString(keyword+8));
580 profile=AcquireStringInfo((size_t) StringToLong(options));
581 (void) SetImageProfile(image,keyword+8,profile);
582 profile=DestroyStringInfo(profile);
585 (void) SetImageProperty(image,keyword,options);
591 if (LocaleCompare(keyword,"quality") == 0)
593 image->quality=StringToUnsignedLong(options);
596 if (LocaleCompare(keyword,"quantum-depth") == 0)
598 quantum_depth=StringToUnsignedLong(options);
601 (void) SetImageProperty(image,keyword,options);
607 if (LocaleCompare(keyword,"red-primary") == 0)
609 flags=ParseGeometry(options,&geometry_info);
610 image->chromaticity.red_primary.x=geometry_info.rho;
611 if ((flags & SigmaValue) != 0)
612 image->chromaticity.red_primary.y=geometry_info.sigma;
615 if (LocaleCompare(keyword,"rendering-intent") == 0)
620 rendering_intent=ParseCommandOption(MagickIntentOptions,
621 MagickFalse,options);
622 if (rendering_intent < 0)
624 image->rendering_intent=(RenderingIntent) rendering_intent;
627 if (LocaleCompare(keyword,"resolution") == 0)
629 flags=ParseGeometry(options,&geometry_info);
630 image->x_resolution=geometry_info.rho;
631 image->y_resolution=geometry_info.sigma;
632 if ((flags & SigmaValue) == 0)
633 image->y_resolution=image->x_resolution;
636 if (LocaleCompare(keyword,"rows") == 0)
638 image->rows=StringToUnsignedLong(options);
641 (void) SetImageProperty(image,keyword,options);
647 if (LocaleCompare(keyword,"scene") == 0)
649 image->scene=StringToUnsignedLong(options);
652 (void) SetImageProperty(image,keyword,options);
658 if (LocaleCompare(keyword,"ticks-per-second") == 0)
660 image->ticks_per_second=(ssize_t) StringToLong(options);
663 if (LocaleCompare(keyword,"tile-offset") == 0)
668 geometry=GetPageGeometry(options);
669 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
670 geometry=DestroyString(geometry);
672 if (LocaleCompare(keyword,"type") == 0)
677 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
681 image->type=(ImageType) type;
684 (void) SetImageProperty(image,keyword,options);
690 if (LocaleCompare(keyword,"units") == 0)
695 units=ParseCommandOption(MagickResolutionOptions,MagickFalse,
699 image->units=(ResolutionType) units;
702 (void) SetImageProperty(image,keyword,options);
708 if (LocaleCompare(keyword,"white-point") == 0)
710 flags=ParseGeometry(options,&geometry_info);
711 image->chromaticity.white_point.x=geometry_info.rho;
712 image->chromaticity.white_point.y=geometry_info.sigma;
713 if ((flags & SigmaValue) == 0)
714 image->chromaticity.white_point.y=
715 image->chromaticity.white_point.x;
718 (void) SetImageProperty(image,keyword,options);
723 (void) SetImageProperty(image,keyword,options);
729 c=ReadBlobByte(image);
730 while (isspace((int) ((unsigned char) c)) != 0)
731 c=ReadBlobByte(image);
733 options=DestroyString(options);
734 (void) ReadBlobByte(image);
736 Verify that required image information is defined.
738 if ((LocaleCompare(id,"MagickCache") != 0) ||
739 (image->storage_class == UndefinedClass) ||
740 (image->compression == UndefinedCompression) || (image->columns == 0) ||
742 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
743 if (quantum_depth != MAGICKCORE_QUANTUM_DEPTH)
744 ThrowReaderException(CacheError,"InconsistentPersistentCacheDepth");
745 if (image->montage != (char *) NULL)
753 length=MaxTextExtent;
754 image->directory=AcquireString((char *) NULL);
759 if ((strlen(image->directory)+MaxTextExtent) >= length)
762 Allocate more memory for the image directory.
765 image->directory=(char *) ResizeQuantumMemory(image->directory,
766 length+MaxTextExtent,sizeof(*image->directory));
767 if (image->directory == (char *) NULL)
768 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
769 p=image->directory+strlen(image->directory);
771 c=ReadBlobByte(image);
773 } while (c != (int) '\0');
775 if (profiles != (LinkedListInfo *) NULL)
783 register unsigned char
789 ResetLinkedListIterator(profiles);
790 name=(const char *) GetNextValueInLinkedList(profiles);
791 while (name != (const char *) NULL)
793 profile=GetImageProfile(image,name);
794 if (profile != (StringInfo *) NULL)
796 p=GetStringInfoDatum(profile);
797 count=ReadBlob(image,GetStringInfoLength(profile),p);
799 name=(const char *) GetNextValueInLinkedList(profiles);
801 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
803 depth=GetImageQuantumDepth(image,MagickFalse);
804 if (image->storage_class == PseudoClass)
807 Create image colormap.
809 if (AcquireImageColormap(image,image->colors) == MagickFalse)
810 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
811 if (image->colors != 0)
820 Read image colormap from file.
822 packet_size=(size_t) (3UL*depth/8UL);
823 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
824 packet_size*sizeof(*colormap));
825 if (colormap == (unsigned char *) NULL)
826 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
827 count=ReadBlob(image,packet_size*image->colors,colormap);
828 if (count != (ssize_t) (packet_size*image->colors))
829 ThrowReaderException(CorruptImageError,
830 "InsufficientImageDataInFile");
835 ThrowReaderException(CorruptImageError,
836 "ImageDepthNotSupported");
842 for (i=0; i < (ssize_t) image->colors; i++)
844 p=PushCharPixel(p,&pixel);
845 image->colormap[i].red=ScaleCharToQuantum(pixel);
846 p=PushCharPixel(p,&pixel);
847 image->colormap[i].green=ScaleCharToQuantum(pixel);
848 p=PushCharPixel(p,&pixel);
849 image->colormap[i].blue=ScaleCharToQuantum(pixel);
858 for (i=0; i < (ssize_t) image->colors; i++)
860 p=PushShortPixel(MSBEndian,p,&pixel);
861 image->colormap[i].red=ScaleShortToQuantum(pixel);
862 p=PushShortPixel(MSBEndian,p,&pixel);
863 image->colormap[i].green=ScaleShortToQuantum(pixel);
864 p=PushShortPixel(MSBEndian,p,&pixel);
865 image->colormap[i].blue=ScaleShortToQuantum(pixel);
874 for (i=0; i < (ssize_t) image->colors; i++)
876 p=PushLongPixel(MSBEndian,p,&pixel);
877 image->colormap[i].red=ScaleLongToQuantum(pixel);
878 p=PushLongPixel(MSBEndian,p,&pixel);
879 image->colormap[i].green=ScaleLongToQuantum(pixel);
880 p=PushLongPixel(MSBEndian,p,&pixel);
881 image->colormap[i].blue=ScaleLongToQuantum(pixel);
886 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
889 if (EOFBlob(image) != MagickFalse)
891 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
895 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
896 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
899 Attach persistent pixel cache.
901 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
902 if (status == MagickFalse)
903 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
905 Proceed to next image.
909 c=ReadBlobByte(image);
910 } while ((isgraph(c) == MagickFalse) && (c != EOF));
914 Allocate next image structure.
916 AcquireNextImage(image_info,image);
917 if (GetNextImageInList(image) == (Image *) NULL)
919 image=DestroyImageList(image);
920 return((Image *) NULL);
922 image=SyncNextImageInList(image);
923 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
925 if (status == MagickFalse)
929 (void) CloseBlob(image);
930 return(GetFirstImageInList(image));
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938 % R e g i s t e r M P C I m a g e %
942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 % RegisterMPCImage() adds properties for the Cache image format to
945 % the list of supported formats. The properties include the image format
946 % tag, a method to read and/or write the format, whether the format
947 % supports the saving of more than one frame to the same file or blob,
948 % whether the format supports native in-memory I/O, and a brief
949 % description of the format.
951 % The format of the RegisterMPCImage method is:
953 % size_t RegisterMPCImage(void)
956 ModuleExport size_t RegisterMPCImage(void)
961 entry=SetMagickInfo("CACHE");
962 entry->description=ConstantString("Magick Persistent Cache image format");
963 entry->module=ConstantString("CACHE");
964 entry->stealth=MagickTrue;
965 (void) RegisterMagickInfo(entry);
966 entry=SetMagickInfo("MPC");
967 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
968 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
969 entry->magick=(IsImageFormatHandler *) IsMPC;
970 entry->description=ConstantString("Magick Persistent Cache image format");
971 entry->module=ConstantString("MPC");
972 (void) RegisterMagickInfo(entry);
973 return(MagickImageCoderSignature);
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981 % U n r e g i s t e r M P C I m a g e %
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 % UnregisterMPCImage() removes format registrations made by the
988 % MPC module from the list of supported formats.
990 % The format of the UnregisterMPCImage method is:
992 % UnregisterMPCImage(void)
995 ModuleExport void UnregisterMPCImage(void)
997 (void) UnregisterMagickInfo("CACHE");
998 (void) UnregisterMagickInfo("MPC");
1002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006 % W r i t e M P C I m a g e %
1010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1014 % The format of the WriteMPCImage method is:
1016 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image)
1018 % A description of each parameter follows:
1020 % o image_info: the image info.
1022 % o image: the image.
1025 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image)
1028 buffer[MaxTextExtent],
1029 cache_filename[MaxTextExtent];
1050 Open persistent cache.
1052 assert(image_info != (const ImageInfo *) NULL);
1053 assert(image_info->signature == MagickSignature);
1054 assert(image != (Image *) NULL);
1055 assert(image->signature == MagickSignature);
1056 if (image->debug != MagickFalse)
1057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1058 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1059 if (status == MagickFalse)
1061 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
1062 AppendImageFormat("cache",cache_filename);
1069 Write persistent cache meta-information.
1071 depth=GetImageQuantumDepth(image,MagickTrue);
1072 if ((image->storage_class == PseudoClass) &&
1073 (image->colors > (one << depth)))
1074 image->storage_class=DirectClass;
1075 (void) WriteBlobString(image,"id=MagickCache\n");
1076 (void) FormatMagickString(buffer,MaxTextExtent,"quantum-depth=%d\n",
1077 MAGICKCORE_QUANTUM_DEPTH);
1078 (void) WriteBlobString(image,buffer);
1079 (void) FormatMagickString(buffer,MaxTextExtent,
1080 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1081 MagickClassOptions,image->storage_class),(double) image->colors,
1082 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1083 (void) WriteBlobString(image,buffer);
1084 (void) FormatMagickString(buffer,MaxTextExtent,
1085 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
1086 (double) image->rows,(double) image->depth);
1087 (void) WriteBlobString(image,buffer);
1088 if (image->type != UndefinedType)
1090 (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
1091 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1092 (void) WriteBlobString(image,buffer);
1094 if (image->colorspace != UndefinedColorspace)
1096 (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
1097 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1098 (void) WriteBlobString(image,buffer);
1100 if (image->endian != UndefinedEndian)
1102 (void) FormatMagickString(buffer,MaxTextExtent,"endian=%s\n",
1103 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1104 (void) WriteBlobString(image,buffer);
1106 if (image->compression != UndefinedCompression)
1108 (void) FormatMagickString(buffer,MaxTextExtent,
1109 "compression=%s quality=%.20g\n",CommandOptionToMnemonic(
1110 MagickCompressOptions,image->compression),(double) image->quality);
1111 (void) WriteBlobString(image,buffer);
1113 if (image->units != UndefinedResolution)
1115 (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n",
1116 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1117 (void) WriteBlobString(image,buffer);
1119 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1121 (void) FormatMagickString(buffer,MaxTextExtent,
1122 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
1123 (void) WriteBlobString(image,buffer);
1125 if ((image->page.width != 0) || (image->page.height != 0))
1127 (void) FormatMagickString(buffer,MaxTextExtent,
1128 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1129 image->page.height,(double) image->page.x,(double) image->page.y);
1130 (void) WriteBlobString(image,buffer);
1133 if ((image->page.x != 0) || (image->page.y != 0))
1135 (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
1136 (long) image->page.x,(long) image->page.y);
1137 (void) WriteBlobString(image,buffer);
1139 if ((image->page.x != 0) || (image->page.y != 0))
1141 (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1142 (long) image->tile_offset.x,(long) image->tile_offset.y);
1143 (void) WriteBlobString(image,buffer);
1145 if ((GetNextImageInList(image) != (Image *) NULL) ||
1146 (GetPreviousImageInList(image) != (Image *) NULL))
1148 if (image->scene == 0)
1149 (void) FormatMagickString(buffer,MaxTextExtent,
1150 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1151 image->iterations,(double) image->delay,(double)
1152 image->ticks_per_second);
1154 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g "
1155 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1156 (double) image->scene,(double) image->iterations,(double)
1157 image->delay,(double) image->ticks_per_second);
1158 (void) WriteBlobString(image,buffer);
1162 if (image->scene != 0)
1164 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g\n",
1165 (double) image->scene);
1166 (void) WriteBlobString(image,buffer);
1168 if (image->iterations != 0)
1170 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g\n",
1171 (double) image->iterations);
1172 (void) WriteBlobString(image,buffer);
1174 if (image->delay != 0)
1176 (void) FormatMagickString(buffer,MaxTextExtent,"delay=%.20g\n",
1177 (double) image->delay);
1178 (void) WriteBlobString(image,buffer);
1180 if (image->ticks_per_second != UndefinedTicksPerSecond)
1182 (void) FormatMagickString(buffer,MaxTextExtent,
1183 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1184 (void) WriteBlobString(image,buffer);
1187 if (image->gravity != UndefinedGravity)
1189 (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
1190 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1191 (void) WriteBlobString(image,buffer);
1193 if (image->dispose != UndefinedDispose)
1195 (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
1196 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1197 (void) WriteBlobString(image,buffer);
1199 if (image->rendering_intent != UndefinedIntent)
1201 (void) FormatMagickString(buffer,MaxTextExtent,
1202 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1203 image->rendering_intent));
1204 (void) WriteBlobString(image,buffer);
1206 if (image->gamma != 0.0)
1208 (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n",
1210 (void) WriteBlobString(image,buffer);
1212 if (image->chromaticity.white_point.x != 0.0)
1215 Note chomaticity points.
1217 (void) FormatMagickString(buffer,MaxTextExtent,"red-primary="
1218 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1219 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1220 image->chromaticity.green_primary.x,
1221 image->chromaticity.green_primary.y,
1222 image->chromaticity.blue_primary.x,
1223 image->chromaticity.blue_primary.y);
1224 (void) WriteBlobString(image,buffer);
1225 (void) FormatMagickString(buffer,MaxTextExtent,
1226 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1227 image->chromaticity.white_point.y);
1228 (void) WriteBlobString(image,buffer);
1230 if (image->orientation != UndefinedOrientation)
1232 (void) FormatMagickString(buffer,MaxTextExtent,
1233 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1234 image->orientation));
1235 (void) WriteBlobString(image,buffer);
1237 if (image->profiles != (void *) NULL)
1248 ResetImageProfileIterator(image);
1249 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1251 profile=GetImageProfile(image,name);
1252 if (profile != (StringInfo *) NULL)
1254 (void) FormatMagickString(buffer,MaxTextExtent,
1255 "profile:%s=%.20g\n",name,(double)
1256 GetStringInfoLength(profile));
1257 (void) WriteBlobString(image,buffer);
1259 name=GetNextImageProfile(image);
1262 if (image->montage != (char *) NULL)
1264 (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n",
1266 (void) WriteBlobString(image,buffer);
1268 ResetImagePropertyIterator(image);
1269 property=GetNextImageProperty(image);
1270 while (property != (const char *) NULL)
1272 (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property);
1273 (void) WriteBlobString(image,buffer);
1274 value=GetImageProperty(image,property);
1275 if (value != (const char *) NULL)
1277 for (i=0; i < (ssize_t) strlen(value); i++)
1278 if (isspace((int) ((unsigned char) value[i])) != 0)
1280 if (i <= (ssize_t) strlen(value))
1281 (void) WriteBlobByte(image,'{');
1282 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
1283 if (i <= (ssize_t) strlen(value))
1284 (void) WriteBlobByte(image,'}');
1286 (void) WriteBlobByte(image,'\n');
1287 property=GetNextImageProperty(image);
1289 ResetImageArtifactIterator(image);
1290 (void) WriteBlobString(image,"\f\n:\032");
1291 if (image->montage != (char *) NULL)
1294 Write montage tile directory.
1296 if (image->directory != (char *) NULL)
1297 (void) WriteBlobString(image,image->directory);
1298 (void) WriteBlobByte(image,'\0');
1300 if (image->profiles != 0)
1309 Write image profiles.
1311 ResetImageProfileIterator(image);
1312 name=GetNextImageProfile(image);
1313 while (name != (const char *) NULL)
1315 profile=GetImageProfile(image,name);
1316 (void) WriteBlob(image,GetStringInfoLength(profile),
1317 GetStringInfoDatum(profile));
1318 name=GetNextImageProfile(image);
1321 if (image->storage_class == PseudoClass)
1333 packet_size=(size_t) (3UL*depth/8UL);
1334 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1335 packet_size*sizeof(*colormap));
1336 if (colormap == (unsigned char *) NULL)
1337 return(MagickFalse);
1339 Write colormap to file.
1342 for (i=0; i < (ssize_t) image->colors; i++)
1347 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1353 pixel=ScaleQuantumToLong(image->colormap[i].red);
1354 q=PopLongPixel(MSBEndian,pixel,q);
1355 pixel=ScaleQuantumToLong(image->colormap[i].green);
1356 q=PopLongPixel(MSBEndian,pixel,q);
1357 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1358 q=PopLongPixel(MSBEndian,pixel,q);
1365 pixel=ScaleQuantumToShort(image->colormap[i].red);
1366 q=PopShortPixel(MSBEndian,pixel,q);
1367 pixel=ScaleQuantumToShort(image->colormap[i].green);
1368 q=PopShortPixel(MSBEndian,pixel,q);
1369 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1370 q=PopShortPixel(MSBEndian,pixel,q);
1378 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1379 q=PopCharPixel(pixel,q);
1380 pixel=(unsigned char) ScaleQuantumToChar(
1381 image->colormap[i].green);
1382 q=PopCharPixel(pixel,q);
1383 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1384 q=PopCharPixel(pixel,q);
1389 (void) WriteBlob(image,packet_size*image->colors,colormap);
1390 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1393 Initialize persistent pixel cache.
1395 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1397 if (status == MagickFalse)
1398 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1399 if (GetNextImageInList(image) == (Image *) NULL)
1401 image=SyncNextImageInList(image);
1402 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1404 status=image->progress_monitor(SaveImagesTag,scene,
1405 GetImageListLength(image),image->client_data);
1406 if (status == MagickFalse)
1410 } while (image_info->adjoin != MagickFalse);
1411 (void) CloseBlob(image);