2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Persistant Cache Image Format %
20 % Copyright 1999-2010 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/constitute.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/hashmap.h"
56 #include "magick/image.h"
57 #include "magick/image-private.h"
58 #include "magick/list.h"
59 #include "magick/magick.h"
60 #include "magick/memory_.h"
61 #include "magick/module.h"
62 #include "magick/monitor.h"
63 #include "magick/monitor-private.h"
64 #include "magick/option.h"
65 #include "magick/profile.h"
66 #include "magick/property.h"
67 #include "magick/quantum-private.h"
68 #include "magick/static.h"
69 #include "magick/statistic.h"
70 #include "magick/string_.h"
71 #include "magick/string-private.h"
72 #include "magick/utility.h"
77 static MagickBooleanType
78 WriteMPCImage(const ImageInfo *,Image *);
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % IsMPC() returns MagickTrue if the image format type, identified by the
92 % magick string, is an Magick Persistent Cache image.
94 % The format of the IsMPC method is:
96 % MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
98 % A description of each parameter follows:
100 % o magick: compare image format pattern against these bytes.
102 % o length: Specifies the length of the magick string.
105 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
109 if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % R e a d C A C H E I m a g e %
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % ReadMPCImage() reads an Magick Persistent Cache image file and returns
126 % it. It allocates the memory necessary for the new Image structure and
127 % returns a pointer to the new image.
129 % The format of the ReadMPCImage method is:
131 % Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
133 % Decompression code contributed by Kyle Shorter.
135 % A description of each parameter follows:
137 % o image_info: the image info.
139 % o exception: return any errors or warnings in this structure.
142 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
145 cache_filename[MaxTextExtent],
147 keyword[MaxTextExtent],
193 assert(image_info != (const ImageInfo *) NULL);
194 assert(image_info->signature == MagickSignature);
195 if (image_info->debug != MagickFalse)
196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
197 image_info->filename);
198 assert(exception != (ExceptionInfo *) NULL);
199 assert(exception->signature == MagickSignature);
200 image=AcquireImage(image_info);
201 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
202 if (status == MagickFalse)
204 image=DestroyImageList(image);
205 return((Image *) NULL);
207 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
208 AppendImageFormat("cache",cache_filename);
209 c=ReadBlobByte(image);
212 image=DestroyImage(image);
213 return((Image *) NULL);
216 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
221 Decode image header; header terminates one character beyond a ':'.
223 profiles=(LinkedListInfo *) NULL;
224 length=MaxTextExtent;
225 options=AcquireString((char *) NULL);
226 quantum_depth=MAGICKCORE_QUANTUM_DEPTH;
228 image->compression=NoCompression;
229 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
240 Read comment-- any text between { }.
242 length=MaxTextExtent;
243 comment=AcquireString((char *) NULL);
244 for (p=comment; comment != (char *) NULL; p++)
246 c=ReadBlobByte(image);
247 if ((c == EOF) || (c == (int) '}'))
249 if ((size_t) (p-comment+1) >= length)
253 comment=(char *) ResizeQuantumMemory(comment,length+
254 MaxTextExtent,sizeof(*comment));
255 if (comment == (char *) NULL)
257 p=comment+strlen(comment);
261 if (comment == (char *) NULL)
262 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
264 (void) SetImageProperty(image,"comment",comment);
265 comment=DestroyString(comment);
266 c=ReadBlobByte(image);
269 if (isalnum(c) != MagickFalse)
277 if (isspace((int) ((unsigned char) c)) != 0)
281 if ((size_t) (p-keyword) < (MaxTextExtent-1))
283 c=ReadBlobByte(image);
287 while (isspace((int) ((unsigned char) c)) != 0)
288 c=ReadBlobByte(image);
292 Get the keyword value.
294 c=ReadBlobByte(image);
295 while ((c != (int) '}') && (c != EOF))
297 if ((size_t) (p-options+1) >= length)
301 options=(char *) ResizeQuantumMemory(options,length+
302 MaxTextExtent,sizeof(*options));
303 if (options == (char *) NULL)
305 p=options+strlen(options);
307 if (options == (char *) NULL)
308 ThrowReaderException(ResourceLimitError,
309 "MemoryAllocationFailed");
311 c=ReadBlobByte(image);
313 if (isspace((int) ((unsigned char) c)) != 0)
319 (void) CopyMagickString(options,options+1,MaxTextExtent);
321 Assign a value to the specified keyword.
328 if (LocaleCompare(keyword,"background-color") == 0)
330 (void) QueryColorDatabase(options,&image->background_color,
334 if (LocaleCompare(keyword,"blue-primary") == 0)
336 flags=ParseGeometry(options,&geometry_info);
337 image->chromaticity.blue_primary.x=geometry_info.rho;
338 image->chromaticity.blue_primary.y=geometry_info.sigma;
339 if ((flags & SigmaValue) == 0)
340 image->chromaticity.blue_primary.y=
341 image->chromaticity.blue_primary.x;
344 if (LocaleCompare(keyword,"border-color") == 0)
346 (void) QueryColorDatabase(options,&image->border_color,
350 (void) SetImageProperty(image,keyword,options);
356 if (LocaleCompare(keyword,"class") == 0)
361 storage_class=ParseMagickOption(MagickClassOptions,
362 MagickFalse,options);
363 if (storage_class < 0)
365 image->storage_class=(ClassType) storage_class;
368 if (LocaleCompare(keyword,"colors") == 0)
370 image->colors=StringToUnsignedLong(options);
373 if (LocaleCompare(keyword,"colorspace") == 0)
378 colorspace=ParseMagickOption(MagickColorspaceOptions,
379 MagickFalse,options);
382 image->colorspace=(ColorspaceType) colorspace;
385 if (LocaleCompare(keyword,"compression") == 0)
390 compression=ParseMagickOption(MagickCompressOptions,
391 MagickFalse,options);
394 image->compression=(CompressionType) compression;
397 if (LocaleCompare(keyword,"columns") == 0)
399 image->columns=StringToUnsignedLong(options);
402 (void) SetImageProperty(image,keyword,options);
408 if (LocaleCompare(keyword,"delay") == 0)
410 image->delay=StringToUnsignedLong(options);
413 if (LocaleCompare(keyword,"depth") == 0)
415 image->depth=StringToUnsignedLong(options);
418 if (LocaleCompare(keyword,"dispose") == 0)
423 dispose=ParseMagickOption(MagickDisposeOptions,MagickFalse,
427 image->dispose=(DisposeType) dispose;
430 (void) SetImageProperty(image,keyword,options);
436 if (LocaleCompare(keyword,"endian") == 0)
441 endian=ParseMagickOption(MagickEndianOptions,MagickFalse,
445 image->endian=(EndianType) endian;
448 if (LocaleCompare(keyword,"error") == 0)
450 image->error.mean_error_per_pixel=StringToDouble(options);
453 (void) SetImageProperty(image,keyword,options);
459 if (LocaleCompare(keyword,"gamma") == 0)
461 image->gamma=StringToDouble(options);
464 if (LocaleCompare(keyword,"green-primary") == 0)
466 flags=ParseGeometry(options,&geometry_info);
467 image->chromaticity.green_primary.x=geometry_info.rho;
468 image->chromaticity.green_primary.y=geometry_info.sigma;
469 if ((flags & SigmaValue) == 0)
470 image->chromaticity.green_primary.y=
471 image->chromaticity.green_primary.x;
474 (void) SetImageProperty(image,keyword,options);
480 if (LocaleCompare(keyword,"id") == 0)
482 (void) CopyMagickString(id,options,MaxTextExtent);
485 if (LocaleCompare(keyword,"iterations") == 0)
487 image->iterations=StringToUnsignedLong(options);
490 (void) SetImageProperty(image,keyword,options);
496 if (LocaleCompare(keyword,"matte") == 0)
501 matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
505 image->matte=(MagickBooleanType) matte;
508 if (LocaleCompare(keyword,"matte-color") == 0)
510 (void) QueryColorDatabase(options,&image->matte_color,
514 if (LocaleCompare(keyword,"maximum-error") == 0)
516 image->error.normalized_maximum_error=StringToDouble(options);
519 if (LocaleCompare(keyword,"mean-error") == 0)
521 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);
535 if (LocaleCompare(keyword,"opaque") == 0)
540 matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
544 image->matte=(MagickBooleanType) matte;
547 if (LocaleCompare(keyword,"orientation") == 0)
552 orientation=ParseMagickOption(MagickOrientationOptions,
553 MagickFalse,options);
556 image->orientation=(OrientationType) orientation;
559 (void) SetImageProperty(image,keyword,options);
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=AcquireStringInfo((size_t) StringToLong(options));
583 (void) SetImageProfile(image,keyword+8,profile);
584 profile=DestroyStringInfo(profile);
587 (void) SetImageProperty(image,keyword,options);
593 if (LocaleCompare(keyword,"quality") == 0)
595 image->quality=StringToUnsignedLong(options);
598 if (LocaleCompare(keyword,"quantum-depth") == 0)
600 quantum_depth=StringToUnsignedLong(options);
603 (void) SetImageProperty(image,keyword,options);
609 if (LocaleCompare(keyword,"red-primary") == 0)
611 flags=ParseGeometry(options,&geometry_info);
612 image->chromaticity.red_primary.x=geometry_info.rho;
613 if ((flags & SigmaValue) != 0)
614 image->chromaticity.red_primary.y=geometry_info.sigma;
617 if (LocaleCompare(keyword,"rendering-intent") == 0)
622 rendering_intent=ParseMagickOption(MagickIntentOptions,
623 MagickFalse,options);
624 if (rendering_intent < 0)
626 image->rendering_intent=(RenderingIntent) rendering_intent;
629 if (LocaleCompare(keyword,"resolution") == 0)
631 flags=ParseGeometry(options,&geometry_info);
632 image->x_resolution=geometry_info.rho;
633 image->y_resolution=geometry_info.sigma;
634 if ((flags & SigmaValue) == 0)
635 image->y_resolution=image->x_resolution;
638 if (LocaleCompare(keyword,"rows") == 0)
640 image->rows=StringToUnsignedLong(options);
643 (void) SetImageProperty(image,keyword,options);
649 if (LocaleCompare(keyword,"scene") == 0)
651 image->scene=StringToUnsignedLong(options);
654 (void) SetImageProperty(image,keyword,options);
660 if (LocaleCompare(keyword,"ticks-per-second") == 0)
662 image->ticks_per_second=(long) StringToLong(options);
665 if (LocaleCompare(keyword,"tile-offset") == 0)
670 geometry=GetPageGeometry(options);
671 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
672 geometry=DestroyString(geometry);
674 if (LocaleCompare(keyword,"type") == 0)
679 type=ParseMagickOption(MagickTypeOptions,MagickFalse,
683 image->type=(ImageType) type;
686 (void) SetImageProperty(image,keyword,options);
692 if (LocaleCompare(keyword,"units") == 0)
697 units=ParseMagickOption(MagickResolutionOptions,MagickFalse,
701 image->units=(ResolutionType) units;
704 (void) SetImageProperty(image,keyword,options);
710 if (LocaleCompare(keyword,"white-point") == 0)
712 flags=ParseGeometry(options,&geometry_info);
713 image->chromaticity.white_point.x=geometry_info.rho;
714 image->chromaticity.white_point.y=geometry_info.sigma;
715 if ((flags & SigmaValue) == 0)
716 image->chromaticity.white_point.y=
717 image->chromaticity.white_point.x;
720 (void) SetImageProperty(image,keyword,options);
725 (void) SetImageProperty(image,keyword,options);
731 c=ReadBlobByte(image);
732 while (isspace((int) ((unsigned char) c)) != 0)
733 c=ReadBlobByte(image);
735 options=DestroyString(options);
736 (void) ReadBlobByte(image);
738 Verify that required image information is defined.
740 if ((LocaleCompare(id,"MagickCache") != 0) ||
741 (image->storage_class == UndefinedClass) ||
742 (image->compression == UndefinedCompression) || (image->columns == 0) ||
744 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
745 if (quantum_depth != MAGICKCORE_QUANTUM_DEPTH)
746 ThrowReaderException(CacheError,"InconsistentPersistentCacheDepth");
747 if (image->montage != (char *) NULL)
755 length=MaxTextExtent;
756 image->directory=AcquireString((char *) NULL);
761 if ((strlen(image->directory)+MaxTextExtent) >= length)
764 Allocate more memory for the image directory.
767 image->directory=(char *) ResizeQuantumMemory(image->directory,
768 length+MaxTextExtent,sizeof(*image->directory));
769 if (image->directory == (char *) NULL)
770 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
771 p=image->directory+strlen(image->directory);
773 c=ReadBlobByte(image);
775 } while (c != (int) '\0');
777 if (profiles != (LinkedListInfo *) NULL)
785 register unsigned char
791 ResetLinkedListIterator(profiles);
792 name=(const char *) GetNextValueInLinkedList(profiles);
793 while (name != (const char *) NULL)
795 profile=GetImageProfile(image,name);
796 if (profile != (StringInfo *) NULL)
798 p=GetStringInfoDatum(profile);
799 count=ReadBlob(image,GetStringInfoLength(profile),p);
801 name=(const char *) GetNextValueInLinkedList(profiles);
803 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
805 depth=GetImageQuantumDepth(image,MagickFalse);
806 if (image->storage_class == PseudoClass)
809 Create image colormap.
811 if (AcquireImageColormap(image,image->colors) == MagickFalse)
812 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
813 if (image->colors != 0)
822 Read image colormap from file.
824 packet_size=(size_t) (3UL*depth/8UL);
825 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
826 packet_size*sizeof(*colormap));
827 if (colormap == (unsigned char *) NULL)
828 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
829 count=ReadBlob(image,packet_size*image->colors,colormap);
830 if (count != (ssize_t) (packet_size*image->colors))
831 ThrowReaderException(CorruptImageError,
832 "InsufficientImageDataInFile");
837 ThrowReaderException(CorruptImageError,
838 "ImageDepthNotSupported");
844 for (i=0; i < (long) image->colors; i++)
846 p=PushCharPixel(p,&pixel);
847 image->colormap[i].red=ScaleCharToQuantum(pixel);
848 p=PushCharPixel(p,&pixel);
849 image->colormap[i].green=ScaleCharToQuantum(pixel);
850 p=PushCharPixel(p,&pixel);
851 image->colormap[i].blue=ScaleCharToQuantum(pixel);
860 for (i=0; i < (long) image->colors; i++)
862 p=PushShortPixel(MSBEndian,p,&pixel);
863 image->colormap[i].red=ScaleShortToQuantum(pixel);
864 p=PushShortPixel(MSBEndian,p,&pixel);
865 image->colormap[i].green=ScaleShortToQuantum(pixel);
866 p=PushShortPixel(MSBEndian,p,&pixel);
867 image->colormap[i].blue=ScaleShortToQuantum(pixel);
876 for (i=0; i < (long) image->colors; i++)
878 p=PushLongPixel(MSBEndian,p,&pixel);
879 image->colormap[i].red=ScaleLongToQuantum(pixel);
880 p=PushLongPixel(MSBEndian,p,&pixel);
881 image->colormap[i].green=ScaleLongToQuantum(pixel);
882 p=PushLongPixel(MSBEndian,p,&pixel);
883 image->colormap[i].blue=ScaleLongToQuantum(pixel);
888 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
891 if (EOFBlob(image) != MagickFalse)
893 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
897 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
898 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
901 Attach persistent pixel cache.
903 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
904 if (status == MagickFalse)
905 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
907 Proceed to next image.
911 c=ReadBlobByte(image);
912 } while ((isgraph(c) == MagickFalse) && (c != EOF));
916 Allocate next image structure.
918 AcquireNextImage(image_info,image);
919 if (GetNextImageInList(image) == (Image *) NULL)
921 image=DestroyImageList(image);
922 return((Image *) NULL);
924 image=SyncNextImageInList(image);
925 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
927 if (status == MagickFalse)
931 (void) CloseBlob(image);
932 return(GetFirstImageInList(image));
936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % R e g i s t e r M P C I m a g e %
944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 % RegisterMPCImage() adds properties for the Cache image format to
947 % the list of supported formats. The properties include the image format
948 % tag, a method to read and/or write the format, whether the format
949 % supports the saving of more than one frame to the same file or blob,
950 % whether the format supports native in-memory I/O, and a brief
951 % description of the format.
953 % The format of the RegisterMPCImage method is:
955 % unsigned long RegisterMPCImage(void)
958 ModuleExport unsigned long RegisterMPCImage(void)
963 entry=SetMagickInfo("CACHE");
964 entry->description=ConstantString("Magick Persistent Cache image format");
965 entry->module=ConstantString("CACHE");
966 entry->stealth=MagickTrue;
967 (void) RegisterMagickInfo(entry);
968 entry=SetMagickInfo("MPC");
969 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
970 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
971 entry->magick=(IsImageFormatHandler *) IsMPC;
972 entry->description=ConstantString("Magick Persistent Cache image format");
973 entry->module=ConstantString("MPC");
974 (void) RegisterMagickInfo(entry);
975 return(MagickImageCoderSignature);
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983 % U n r e g i s t e r M P C I m a g e %
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 % UnregisterMPCImage() removes format registrations made by the
990 % MPC module from the list of supported formats.
992 % The format of the UnregisterMPCImage method is:
994 % UnregisterMPCImage(void)
997 ModuleExport void UnregisterMPCImage(void)
999 (void) UnregisterMagickInfo("CACHE");
1000 (void) UnregisterMagickInfo("MPC");
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1008 % W r i t e M P C I m a g e %
1012 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1016 % The format of the WriteMPCImage method is:
1018 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image)
1020 % A description of each parameter follows:
1022 % o image_info: the image info.
1024 % o image: the image.
1027 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image)
1030 buffer[MaxTextExtent],
1031 cache_filename[MaxTextExtent];
1051 Open persistent cache.
1053 assert(image_info != (const ImageInfo *) NULL);
1054 assert(image_info->signature == MagickSignature);
1055 assert(image != (Image *) NULL);
1056 assert(image->signature == MagickSignature);
1057 if (image->debug != MagickFalse)
1058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1059 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1060 if (status == MagickFalse)
1062 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
1063 AppendImageFormat("cache",cache_filename);
1069 Write persistent cache meta-information.
1071 depth=GetImageQuantumDepth(image,MagickTrue);
1072 if ((image->storage_class == PseudoClass) &&
1073 (image->colors > (1UL << 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=%lu matte=%s\n",MagickOptionToMnemonic(
1081 MagickClassOptions,image->storage_class),image->colors,
1082 MagickOptionToMnemonic(MagickBooleanOptions,(long) image->matte));
1083 (void) WriteBlobString(image,buffer);
1084 (void) FormatMagickString(buffer,MaxTextExtent,
1085 "columns=%lu rows=%lu depth=%lu\n",image->columns,image->rows,
1087 (void) WriteBlobString(image,buffer);
1088 if (image->type != UndefinedType)
1090 (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
1091 MagickOptionToMnemonic(MagickTypeOptions,image->type));
1092 (void) WriteBlobString(image,buffer);
1094 if (image->colorspace != UndefinedColorspace)
1096 (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
1097 MagickOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1098 (void) WriteBlobString(image,buffer);
1100 if (image->endian != UndefinedEndian)
1102 (void) FormatMagickString(buffer,MaxTextExtent,"endian=%s\n",
1103 MagickOptionToMnemonic(MagickEndianOptions,image->endian));
1104 (void) WriteBlobString(image,buffer);
1106 if (image->compression != UndefinedCompression)
1108 (void) FormatMagickString(buffer,MaxTextExtent,
1109 "compression=%s quality=%lu\n",MagickOptionToMnemonic(
1110 MagickCompressOptions,image->compression),image->quality);
1111 (void) WriteBlobString(image,buffer);
1113 if (image->units != UndefinedResolution)
1115 (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n",
1116 MagickOptionToMnemonic(MagickResolutionOptions,image->units));
1117 (void) WriteBlobString(image,buffer);
1119 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1121 (void) FormatMagickString(buffer,MaxTextExtent,"resolution=%gx%g\n",
1122 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,"page=%lux%lu%+ld%+ld\n",
1128 image->page.width,image->page.height,image->page.x,image->page.y);
1129 (void) WriteBlobString(image,buffer);
1132 if ((image->page.x != 0) || (image->page.y != 0))
1134 (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
1135 image->page.x,image->page.y);
1136 (void) WriteBlobString(image,buffer);
1138 if ((image->page.x != 0) || (image->page.y != 0))
1140 (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1141 image->tile_offset.x,image->tile_offset.y);
1142 (void) WriteBlobString(image,buffer);
1144 if ((GetNextImageInList(image) != (Image *) NULL) ||
1145 (GetPreviousImageInList(image) != (Image *) NULL))
1147 if (image->scene == 0)
1148 (void) FormatMagickString(buffer,MaxTextExtent,
1149 "iterations=%lu delay=%lu ticks-per-second=%lu\n",
1150 image->iterations,image->delay,image->ticks_per_second);
1152 (void) FormatMagickString(buffer,MaxTextExtent,
1153 "scene=%lu iterations=%lu delay=%lu ticks-per-second=%lu\n",
1154 image->scene,image->iterations,image->delay,
1155 image->ticks_per_second);
1156 (void) WriteBlobString(image,buffer);
1160 if (image->scene != 0)
1162 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%lu\n",
1164 (void) WriteBlobString(image,buffer);
1166 if (image->iterations != 0)
1168 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%lu\n",
1170 (void) WriteBlobString(image,buffer);
1172 if (image->delay != 0)
1174 (void) FormatMagickString(buffer,MaxTextExtent,"delay=%lu\n",
1176 (void) WriteBlobString(image,buffer);
1178 if (image->ticks_per_second != UndefinedTicksPerSecond)
1180 (void) FormatMagickString(buffer,MaxTextExtent,
1181 "ticks-per-second=%lu\n",image->ticks_per_second);
1182 (void) WriteBlobString(image,buffer);
1185 if (image->gravity != UndefinedGravity)
1187 (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
1188 MagickOptionToMnemonic(MagickGravityOptions,image->gravity));
1189 (void) WriteBlobString(image,buffer);
1191 if (image->dispose != UndefinedDispose)
1193 (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
1194 MagickOptionToMnemonic(MagickDisposeOptions,image->dispose));
1195 (void) WriteBlobString(image,buffer);
1197 if (image->rendering_intent != UndefinedIntent)
1199 (void) FormatMagickString(buffer,MaxTextExtent,
1200 "rendering-intent=%s\n",
1201 MagickOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
1202 (void) WriteBlobString(image,buffer);
1204 if (image->gamma != 0.0)
1206 (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n",
1208 (void) WriteBlobString(image,buffer);
1210 if (image->chromaticity.white_point.x != 0.0)
1213 Note chomaticity points.
1215 (void) FormatMagickString(buffer,MaxTextExtent,
1216 "red-primary=%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1217 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1218 image->chromaticity.green_primary.x,
1219 image->chromaticity.green_primary.y,
1220 image->chromaticity.blue_primary.x,
1221 image->chromaticity.blue_primary.y);
1222 (void) WriteBlobString(image,buffer);
1223 (void) FormatMagickString(buffer,MaxTextExtent,"white-point=%g,%g\n",
1224 image->chromaticity.white_point.x,image->chromaticity.white_point.y);
1225 (void) WriteBlobString(image,buffer);
1227 if (image->orientation != UndefinedOrientation)
1229 (void) FormatMagickString(buffer,MaxTextExtent,
1230 "orientation=%s\n",MagickOptionToMnemonic(MagickOrientationOptions,
1231 image->orientation));
1232 (void) WriteBlobString(image,buffer);
1234 if (image->profiles != (void *) NULL)
1245 ResetImageProfileIterator(image);
1246 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1248 profile=GetImageProfile(image,name);
1249 if (profile != (StringInfo *) NULL)
1251 (void) FormatMagickString(buffer,MaxTextExtent,"profile:%s=%lu\n",
1252 name,(unsigned long) GetStringInfoLength(profile));
1253 (void) WriteBlobString(image,buffer);
1255 name=GetNextImageProfile(image);
1258 if (image->montage != (char *) NULL)
1260 (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n",
1262 (void) WriteBlobString(image,buffer);
1264 ResetImagePropertyIterator(image);
1265 property=GetNextImageProperty(image);
1266 while (property != (const char *) NULL)
1268 (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property);
1269 (void) WriteBlobString(image,buffer);
1270 value=GetImageProperty(image,property);
1271 if (value != (const char *) NULL)
1273 for (i=0; i < (long) strlen(value); i++)
1274 if (isspace((int) ((unsigned char) value[i])) != 0)
1276 if (i <= (long) strlen(value))
1277 (void) WriteBlobByte(image,'{');
1278 (void) WriteBlob(image,strlen(value),(unsigned char *) value);
1279 if (i <= (long) strlen(value))
1280 (void) WriteBlobByte(image,'}');
1282 (void) WriteBlobByte(image,'\n');
1283 property=GetNextImageProperty(image);
1285 ResetImageArtifactIterator(image);
1286 (void) WriteBlobString(image,"\f\n:\032");
1287 if (image->montage != (char *) NULL)
1290 Write montage tile directory.
1292 if (image->directory != (char *) NULL)
1293 (void) WriteBlobString(image,image->directory);
1294 (void) WriteBlobByte(image,'\0');
1296 if (image->profiles != 0)
1305 Write image profiles.
1307 ResetImageProfileIterator(image);
1308 name=GetNextImageProfile(image);
1309 while (name != (const char *) NULL)
1311 profile=GetImageProfile(image,name);
1312 (void) WriteBlob(image,GetStringInfoLength(profile),
1313 GetStringInfoDatum(profile));
1314 name=GetNextImageProfile(image);
1317 if (image->storage_class == PseudoClass)
1329 packet_size=(size_t) (3UL*depth/8UL);
1330 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1331 packet_size*sizeof(*colormap));
1332 if (colormap == (unsigned char *) NULL)
1333 return(MagickFalse);
1335 Write colormap to file.
1338 for (i=0; i < (long) image->colors; i++)
1343 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1349 pixel=ScaleQuantumToLong(image->colormap[i].red);
1350 q=PopLongPixel(MSBEndian,pixel,q);
1351 pixel=ScaleQuantumToLong(image->colormap[i].green);
1352 q=PopLongPixel(MSBEndian,pixel,q);
1353 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1354 q=PopLongPixel(MSBEndian,pixel,q);
1361 pixel=ScaleQuantumToShort(image->colormap[i].red);
1362 q=PopShortPixel(MSBEndian,pixel,q);
1363 pixel=ScaleQuantumToShort(image->colormap[i].green);
1364 q=PopShortPixel(MSBEndian,pixel,q);
1365 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1366 q=PopShortPixel(MSBEndian,pixel,q);
1374 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1375 q=PopCharPixel(pixel,q);
1376 pixel=(unsigned char) ScaleQuantumToChar(
1377 image->colormap[i].green);
1378 q=PopCharPixel(pixel,q);
1379 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1380 q=PopCharPixel(pixel,q);
1385 (void) WriteBlob(image,packet_size*image->colors,colormap);
1386 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1389 Initialize persistent pixel cache.
1391 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1393 if (status == MagickFalse)
1394 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1395 if (GetNextImageInList(image) == (Image *) NULL)
1397 image=SyncNextImageInList(image);
1398 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1400 status=image->progress_monitor(SaveImagesTag,scene,
1401 GetImageListLength(image),image->client_data);
1402 if (status == MagickFalse)
1406 } while (image_info->adjoin != MagickFalse);
1407 (void) CloseBlob(image);