2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Magick Persistant Cache Image Format %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/color.h"
50 #include "MagickCore/color-private.h"
51 #include "MagickCore/colormap.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/hashmap.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/statistic.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/string-private.h"
73 #include "MagickCore/utility.h"
74 #include "MagickCore/version-private.h"
79 static MagickBooleanType
80 WriteMPCImage(const ImageInfo *,Image *,ExceptionInfo *);
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % IsMPC() returns MagickTrue if the image format type, identified by the
94 % magick string, is an Magick Persistent Cache image.
96 % The format of the IsMPC method is:
98 % MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
100 % A description of each parameter follows:
102 % o magick: compare image format pattern against these bytes.
104 % o length: Specifies the length of the magick string.
107 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
111 if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % R e a d C A C H E I m a g e %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % ReadMPCImage() reads an Magick Persistent Cache image file and returns
128 % it. It allocates the memory necessary for the new Image structure and
129 % returns a pointer to the new image.
131 % The format of the ReadMPCImage method is:
133 % Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
135 % Decompression code contributed by Kyle Shorter.
137 % A description of each parameter follows:
139 % o image_info: the image info.
141 % o exception: return any errors or warnings in this structure.
144 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
147 cache_filename[MaxTextExtent],
149 keyword[MaxTextExtent],
195 assert(image_info != (const ImageInfo *) NULL);
196 assert(image_info->signature == MagickSignature);
197 if (image_info->debug != MagickFalse)
198 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
199 image_info->filename);
200 assert(exception != (ExceptionInfo *) NULL);
201 assert(exception->signature == MagickSignature);
202 image=AcquireImage(image_info,exception);
203 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
204 if (status == MagickFalse)
206 image=DestroyImageList(image);
207 return((Image *) NULL);
209 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
210 AppendImageFormat("cache",cache_filename);
211 c=ReadBlobByte(image);
214 image=DestroyImage(image);
215 return((Image *) NULL);
218 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
223 Decode image header; header terminates one character beyond a ':'.
225 profiles=(LinkedListInfo *) NULL;
226 length=MaxTextExtent;
227 options=AcquireString((char *) NULL);
228 signature=GetMagickSignature((const StringInfo *) NULL);
230 image->compression=NoCompression;
231 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
242 Read comment-- any text between { }.
244 length=MaxTextExtent;
245 comment=AcquireString((char *) NULL);
246 for (p=comment; comment != (char *) NULL; p++)
248 c=ReadBlobByte(image);
250 c=ReadBlobByte(image);
252 if ((c == EOF) || (c == (int) '}'))
254 if ((size_t) (p-comment+1) >= length)
258 comment=(char *) ResizeQuantumMemory(comment,length+
259 MaxTextExtent,sizeof(*comment));
260 if (comment == (char *) NULL)
262 p=comment+strlen(comment);
266 if (comment == (char *) NULL)
267 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
269 (void) SetImageProperty(image,"comment",comment,exception);
270 comment=DestroyString(comment);
271 c=ReadBlobByte(image);
274 if (isalnum(c) != MagickFalse)
284 if ((size_t) (p-keyword) < (MaxTextExtent-1))
286 c=ReadBlobByte(image);
290 while (isspace((int) ((unsigned char) c)) != 0)
291 c=ReadBlobByte(image);
295 Get the keyword value.
297 c=ReadBlobByte(image);
298 while ((c != (int) '}') && (c != EOF))
300 if ((size_t) (p-options+1) >= length)
304 options=(char *) ResizeQuantumMemory(options,length+
305 MaxTextExtent,sizeof(*options));
306 if (options == (char *) NULL)
308 p=options+strlen(options);
310 if (options == (char *) NULL)
311 ThrowReaderException(ResourceLimitError,
312 "MemoryAllocationFailed");
314 c=ReadBlobByte(image);
317 c=ReadBlobByte(image);
321 c=ReadBlobByte(image);
325 if (isspace((int) ((unsigned char) c)) != 0)
331 (void) CopyMagickString(options,options+1,strlen(options));
333 Assign a value to the specified keyword.
340 if (LocaleCompare(keyword,"alpha-trait") == 0)
345 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
346 MagickFalse,options);
349 image->alpha_trait=(PixelTrait) alpha_trait;
352 (void) SetImageProperty(image,keyword,options,exception);
358 if (LocaleCompare(keyword,"background-color") == 0)
360 (void) QueryColorCompliance(options,AllCompliance,
361 &image->background_color,exception);
364 if (LocaleCompare(keyword,"blue-primary") == 0)
366 flags=ParseGeometry(options,&geometry_info);
367 image->chromaticity.blue_primary.x=geometry_info.rho;
368 image->chromaticity.blue_primary.y=geometry_info.sigma;
369 if ((flags & SigmaValue) == 0)
370 image->chromaticity.blue_primary.y=
371 image->chromaticity.blue_primary.x;
374 if (LocaleCompare(keyword,"border-color") == 0)
376 (void) QueryColorCompliance(options,AllCompliance,
377 &image->border_color,exception);
380 (void) SetImageProperty(image,keyword,options,exception);
386 if (LocaleCompare(keyword,"class") == 0)
391 storage_class=ParseCommandOption(MagickClassOptions,
392 MagickFalse,options);
393 if (storage_class < 0)
395 image->storage_class=(ClassType) storage_class;
398 if (LocaleCompare(keyword,"colors") == 0)
400 image->colors=StringToUnsignedLong(options);
403 if (LocaleCompare(keyword,"colorspace") == 0)
408 colorspace=ParseCommandOption(MagickColorspaceOptions,
409 MagickFalse,options);
412 (void) SetImageColorspace(image,(ColorspaceType) colorspace, exception);
415 if (LocaleCompare(keyword,"compression") == 0)
420 compression=ParseCommandOption(MagickCompressOptions,
421 MagickFalse,options);
424 image->compression=(CompressionType) compression;
427 if (LocaleCompare(keyword,"columns") == 0)
429 image->columns=StringToUnsignedLong(options);
432 (void) SetImageProperty(image,keyword,options,exception);
438 if (LocaleCompare(keyword,"delay") == 0)
440 image->delay=StringToUnsignedLong(options);
443 if (LocaleCompare(keyword,"depth") == 0)
445 image->depth=StringToUnsignedLong(options);
448 if (LocaleCompare(keyword,"dispose") == 0)
453 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
457 image->dispose=(DisposeType) dispose;
460 (void) SetImageProperty(image,keyword,options,exception);
466 if (LocaleCompare(keyword,"endian") == 0)
471 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
475 image->endian=(EndianType) endian;
478 if (LocaleCompare(keyword,"error") == 0)
480 image->error.mean_error_per_pixel=StringToDouble(options,
484 (void) SetImageProperty(image,keyword,options,exception);
490 if (LocaleCompare(keyword,"gamma") == 0)
492 image->gamma=StringToDouble(options,(char **) NULL);
495 if (LocaleCompare(keyword,"green-primary") == 0)
497 flags=ParseGeometry(options,&geometry_info);
498 image->chromaticity.green_primary.x=geometry_info.rho;
499 image->chromaticity.green_primary.y=geometry_info.sigma;
500 if ((flags & SigmaValue) == 0)
501 image->chromaticity.green_primary.y=
502 image->chromaticity.green_primary.x;
505 (void) SetImageProperty(image,keyword,options,exception);
511 if (LocaleCompare(keyword,"id") == 0)
513 (void) CopyMagickString(id,options,MaxTextExtent);
516 if (LocaleCompare(keyword,"iterations") == 0)
518 image->iterations=StringToUnsignedLong(options);
521 (void) SetImageProperty(image,keyword,options,exception);
527 if (LocaleCompare(keyword,"matte-color") == 0)
529 (void) QueryColorCompliance(options,AllCompliance,
530 &image->matte_color,exception);
533 if (LocaleCompare(keyword,"maximum-error") == 0)
535 image->error.normalized_maximum_error=StringToDouble(
536 options,(char **) NULL);
539 if (LocaleCompare(keyword,"mean-error") == 0)
541 image->error.normalized_mean_error=StringToDouble(options,
545 if (LocaleCompare(keyword,"montage") == 0)
547 (void) CloneString(&image->montage,options);
550 (void) SetImageProperty(image,keyword,options,exception);
556 if (LocaleCompare(keyword,"orientation") == 0)
561 orientation=ParseCommandOption(MagickOrientationOptions,
562 MagickFalse,options);
565 image->orientation=(OrientationType) orientation;
568 (void) SetImageProperty(image,keyword,options,exception);
574 if (LocaleCompare(keyword,"page") == 0)
579 geometry=GetPageGeometry(options);
580 (void) ParseAbsoluteGeometry(geometry,&image->page);
581 geometry=DestroyString(geometry);
584 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
585 (LocaleNCompare(keyword,"profile-",8) == 0))
587 if (profiles == (LinkedListInfo *) NULL)
588 profiles=NewLinkedList(0);
589 (void) AppendValueToLinkedList(profiles,
590 AcquireString(keyword+8));
591 profile=BlobToStringInfo((const void *) NULL,(size_t)
592 StringToLong(options));
593 if (profile == (StringInfo *) NULL)
594 ThrowReaderException(ResourceLimitError,
595 "MemoryAllocationFailed");
596 (void) SetImageProfile(image,keyword+8,profile,exception);
597 profile=DestroyStringInfo(profile);
600 (void) SetImageProperty(image,keyword,options,exception);
606 if (LocaleCompare(keyword,"quality") == 0)
608 image->quality=StringToUnsignedLong(options);
611 (void) SetImageProperty(image,keyword,options,exception);
617 if (LocaleCompare(keyword,"red-primary") == 0)
619 flags=ParseGeometry(options,&geometry_info);
620 image->chromaticity.red_primary.x=geometry_info.rho;
621 if ((flags & SigmaValue) != 0)
622 image->chromaticity.red_primary.y=geometry_info.sigma;
625 if (LocaleCompare(keyword,"rendering-intent") == 0)
630 rendering_intent=ParseCommandOption(MagickIntentOptions,
631 MagickFalse,options);
632 if (rendering_intent < 0)
634 image->rendering_intent=(RenderingIntent) rendering_intent;
637 if (LocaleCompare(keyword,"resolution") == 0)
639 flags=ParseGeometry(options,&geometry_info);
640 image->resolution.x=geometry_info.rho;
641 image->resolution.y=geometry_info.sigma;
642 if ((flags & SigmaValue) == 0)
643 image->resolution.y=image->resolution.x;
646 if (LocaleCompare(keyword,"rows") == 0)
648 image->rows=StringToUnsignedLong(options);
651 (void) SetImageProperty(image,keyword,options,exception);
657 if (LocaleCompare(keyword,"scene") == 0)
659 image->scene=StringToUnsignedLong(options);
662 if (LocaleCompare(keyword,"signature") == 0)
664 signature=(unsigned int) StringToUnsignedLong(options);
667 (void) SetImageProperty(image,keyword,options,exception);
673 if (LocaleCompare(keyword,"ticks-per-second") == 0)
675 image->ticks_per_second=(ssize_t) StringToLong(options);
678 if (LocaleCompare(keyword,"tile-offset") == 0)
683 geometry=GetPageGeometry(options);
684 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
685 geometry=DestroyString(geometry);
687 if (LocaleCompare(keyword,"type") == 0)
692 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
696 image->type=(ImageType) type;
699 (void) SetImageProperty(image,keyword,options,exception);
705 if (LocaleCompare(keyword,"units") == 0)
710 units=ParseCommandOption(MagickResolutionOptions,
711 MagickFalse,options);
714 image->units=(ResolutionType) units;
717 (void) SetImageProperty(image,keyword,options,exception);
723 if (LocaleCompare(keyword,"white-point") == 0)
725 flags=ParseGeometry(options,&geometry_info);
726 image->chromaticity.white_point.x=geometry_info.rho;
727 image->chromaticity.white_point.y=geometry_info.sigma;
728 if ((flags & SigmaValue) == 0)
729 image->chromaticity.white_point.y=
730 image->chromaticity.white_point.x;
733 (void) SetImageProperty(image,keyword,options,exception);
738 (void) SetImageProperty(image,keyword,options,exception);
744 c=ReadBlobByte(image);
745 while (isspace((int) ((unsigned char) c)) != 0)
746 c=ReadBlobByte(image);
748 options=DestroyString(options);
749 (void) ReadBlobByte(image);
751 Verify that required image information is defined.
753 if ((LocaleCompare(id,"MagickCache") != 0) ||
754 (image->storage_class == UndefinedClass) ||
755 (image->compression == UndefinedCompression) || (image->columns == 0) ||
757 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
758 if (signature != GetMagickSignature((const StringInfo *) NULL))
759 ThrowReaderException(CacheError,"IncompatibleAPI");
760 if (image->montage != (char *) NULL)
768 length=MaxTextExtent;
769 image->directory=AcquireString((char *) NULL);
774 if ((strlen(image->directory)+MaxTextExtent) >= length)
777 Allocate more memory for the image directory.
780 image->directory=(char *) ResizeQuantumMemory(image->directory,
781 length+MaxTextExtent,sizeof(*image->directory));
782 if (image->directory == (char *) NULL)
783 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
784 p=image->directory+strlen(image->directory);
786 c=ReadBlobByte(image);
788 } while (c != (int) '\0');
790 if (profiles != (LinkedListInfo *) NULL)
798 register unsigned char
804 ResetLinkedListIterator(profiles);
805 name=(const char *) GetNextValueInLinkedList(profiles);
806 while (name != (const char *) NULL)
808 profile=GetImageProfile(image,name);
809 if (profile != (StringInfo *) NULL)
811 p=GetStringInfoDatum(profile);
812 count=ReadBlob(image,GetStringInfoLength(profile),p);
814 name=(const char *) GetNextValueInLinkedList(profiles);
816 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
818 depth=GetImageQuantumDepth(image,MagickFalse);
819 if (image->storage_class == PseudoClass)
822 Create image colormap.
824 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
825 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
826 if (image->colors != 0)
835 Read image colormap from file.
837 packet_size=(size_t) (3UL*depth/8UL);
838 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
839 packet_size*sizeof(*colormap));
840 if (colormap == (unsigned char *) NULL)
841 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
842 count=ReadBlob(image,packet_size*image->colors,colormap);
843 if (count != (ssize_t) (packet_size*image->colors))
844 ThrowReaderException(CorruptImageError,
845 "InsufficientImageDataInFile");
850 ThrowReaderException(CorruptImageError,
851 "ImageDepthNotSupported");
857 for (i=0; i < (ssize_t) image->colors; i++)
859 p=PushCharPixel(p,&pixel);
860 image->colormap[i].red=ScaleCharToQuantum(pixel);
861 p=PushCharPixel(p,&pixel);
862 image->colormap[i].green=ScaleCharToQuantum(pixel);
863 p=PushCharPixel(p,&pixel);
864 image->colormap[i].blue=ScaleCharToQuantum(pixel);
873 for (i=0; i < (ssize_t) image->colors; i++)
875 p=PushShortPixel(MSBEndian,p,&pixel);
876 image->colormap[i].red=ScaleShortToQuantum(pixel);
877 p=PushShortPixel(MSBEndian,p,&pixel);
878 image->colormap[i].green=ScaleShortToQuantum(pixel);
879 p=PushShortPixel(MSBEndian,p,&pixel);
880 image->colormap[i].blue=ScaleShortToQuantum(pixel);
889 for (i=0; i < (ssize_t) image->colors; i++)
891 p=PushLongPixel(MSBEndian,p,&pixel);
892 image->colormap[i].red=ScaleLongToQuantum(pixel);
893 p=PushLongPixel(MSBEndian,p,&pixel);
894 image->colormap[i].green=ScaleLongToQuantum(pixel);
895 p=PushLongPixel(MSBEndian,p,&pixel);
896 image->colormap[i].blue=ScaleLongToQuantum(pixel);
901 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
904 if (EOFBlob(image) != MagickFalse)
906 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
910 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
911 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
914 Attach persistent pixel cache.
916 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
917 if (status == MagickFalse)
918 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
920 Proceed to next image.
924 c=ReadBlobByte(image);
925 } while ((isgraph(c) == MagickFalse) && (c != EOF));
929 Allocate next image structure.
931 AcquireNextImage(image_info,image,exception);
932 if (GetNextImageInList(image) == (Image *) NULL)
934 image=DestroyImageList(image);
935 return((Image *) NULL);
937 image=SyncNextImageInList(image);
938 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
940 if (status == MagickFalse)
944 (void) CloseBlob(image);
945 return(GetFirstImageInList(image));
949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 % R e g i s t e r M P C I m a g e %
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959 % RegisterMPCImage() adds properties for the Cache image format to
960 % the list of supported formats. The properties include the image format
961 % tag, a method to read and/or write the format, whether the format
962 % supports the saving of more than one frame to the same file or blob,
963 % whether the format supports native in-memory I/O, and a brief
964 % description of the format.
966 % The format of the RegisterMPCImage method is:
968 % size_t RegisterMPCImage(void)
971 ModuleExport size_t RegisterMPCImage(void)
976 entry=SetMagickInfo("CACHE");
977 entry->description=ConstantString("Magick Persistent Cache image format");
978 entry->module=ConstantString("CACHE");
979 entry->stealth=MagickTrue;
980 (void) RegisterMagickInfo(entry);
981 entry=SetMagickInfo("MPC");
982 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
983 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
984 entry->magick=(IsImageFormatHandler *) IsMPC;
985 entry->description=ConstantString("Magick Persistent Cache image format");
986 entry->module=ConstantString("MPC");
987 (void) RegisterMagickInfo(entry);
988 return(MagickImageCoderSignature);
992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996 % U n r e g i s t e r M P C I m a g e %
1000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1002 % UnregisterMPCImage() removes format registrations made by the
1003 % MPC module from the list of supported formats.
1005 % The format of the UnregisterMPCImage method is:
1007 % UnregisterMPCImage(void)
1010 ModuleExport void UnregisterMPCImage(void)
1012 (void) UnregisterMagickInfo("CACHE");
1013 (void) UnregisterMagickInfo("MPC");
1017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1021 % W r i t e M P C I m a g e %
1025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1029 % The format of the WriteMPCImage method is:
1031 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1032 % Image *image,ExceptionInfo *exception)
1034 % A description of each parameter follows:
1036 % o image_info: the image info.
1038 % o image: the image.
1040 % o exception: return any errors or warnings in this structure.
1043 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1044 ExceptionInfo *exception)
1047 buffer[MaxTextExtent],
1048 cache_filename[MaxTextExtent];
1069 Open persistent cache.
1071 assert(image_info != (const ImageInfo *) NULL);
1072 assert(image_info->signature == MagickSignature);
1073 assert(image != (Image *) NULL);
1074 assert(image->signature == MagickSignature);
1075 if (image->debug != MagickFalse)
1076 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1077 assert(exception != (ExceptionInfo *) NULL);
1078 assert(exception->signature == MagickSignature);
1079 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1080 if (status == MagickFalse)
1082 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
1083 AppendImageFormat("cache",cache_filename);
1090 Write persistent cache meta-information.
1092 depth=GetImageQuantumDepth(image,MagickTrue);
1093 if ((image->storage_class == PseudoClass) &&
1094 (image->colors > (one << depth)))
1095 image->storage_class=DirectClass;
1096 (void) WriteBlobString(image,"id=MagickCache\n");
1097 (void) FormatLocaleString(buffer,MaxTextExtent,"signature=%u\n",
1098 GetMagickSignature((const StringInfo *) NULL));
1099 (void) WriteBlobString(image,buffer);
1100 (void) FormatLocaleString(buffer,MaxTextExtent,
1101 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
1102 MagickClassOptions,image->storage_class),(double) image->colors,
1103 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
1104 image->alpha_trait));
1105 (void) WriteBlobString(image,buffer);
1106 (void) FormatLocaleString(buffer,MaxTextExtent,
1107 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
1108 (double) image->rows,(double) image->depth);
1109 (void) WriteBlobString(image,buffer);
1110 if (image->type != UndefinedType)
1112 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1113 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1114 (void) WriteBlobString(image,buffer);
1116 if (image->colorspace != UndefinedColorspace)
1118 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1119 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1120 (void) WriteBlobString(image,buffer);
1122 if (image->endian != UndefinedEndian)
1124 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
1125 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1126 (void) WriteBlobString(image,buffer);
1128 if (image->compression != UndefinedCompression)
1130 (void) FormatLocaleString(buffer,MaxTextExtent,
1131 "compression=%s quality=%.20g\n",CommandOptionToMnemonic(
1132 MagickCompressOptions,image->compression),(double) image->quality);
1133 (void) WriteBlobString(image,buffer);
1135 if (image->units != UndefinedResolution)
1137 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
1138 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1139 (void) WriteBlobString(image,buffer);
1141 if ((image->resolution.x != 0) || (image->resolution.y != 0))
1143 (void) FormatLocaleString(buffer,MaxTextExtent,
1144 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1145 (void) WriteBlobString(image,buffer);
1147 if ((image->page.width != 0) || (image->page.height != 0))
1149 (void) FormatLocaleString(buffer,MaxTextExtent,
1150 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1151 image->page.height,(double) image->page.x,(double) image->page.y);
1152 (void) WriteBlobString(image,buffer);
1155 if ((image->page.x != 0) || (image->page.y != 0))
1157 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
1158 (long) image->page.x,(long) image->page.y);
1159 (void) WriteBlobString(image,buffer);
1161 if ((image->page.x != 0) || (image->page.y != 0))
1163 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1164 (long) image->tile_offset.x,(long) image->tile_offset.y);
1165 (void) WriteBlobString(image,buffer);
1167 if ((GetNextImageInList(image) != (Image *) NULL) ||
1168 (GetPreviousImageInList(image) != (Image *) NULL))
1170 if (image->scene == 0)
1171 (void) FormatLocaleString(buffer,MaxTextExtent,
1172 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1173 image->iterations,(double) image->delay,(double)
1174 image->ticks_per_second);
1176 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
1177 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1178 (double) image->scene,(double) image->iterations,(double)
1179 image->delay,(double) image->ticks_per_second);
1180 (void) WriteBlobString(image,buffer);
1184 if (image->scene != 0)
1186 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
1187 (double) image->scene);
1188 (void) WriteBlobString(image,buffer);
1190 if (image->iterations != 0)
1192 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
1193 (double) image->iterations);
1194 (void) WriteBlobString(image,buffer);
1196 if (image->delay != 0)
1198 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
1199 (double) image->delay);
1200 (void) WriteBlobString(image,buffer);
1202 if (image->ticks_per_second != UndefinedTicksPerSecond)
1204 (void) FormatLocaleString(buffer,MaxTextExtent,
1205 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1206 (void) WriteBlobString(image,buffer);
1209 if (image->gravity != UndefinedGravity)
1211 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
1212 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1213 (void) WriteBlobString(image,buffer);
1215 if (image->dispose != UndefinedDispose)
1217 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
1218 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1219 (void) WriteBlobString(image,buffer);
1221 if (image->rendering_intent != UndefinedIntent)
1223 (void) FormatLocaleString(buffer,MaxTextExtent,
1224 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1225 image->rendering_intent));
1226 (void) WriteBlobString(image,buffer);
1228 if (image->gamma != 0.0)
1230 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
1232 (void) WriteBlobString(image,buffer);
1234 if (image->chromaticity.white_point.x != 0.0)
1237 Note chomaticity points.
1239 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary="
1240 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1241 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1242 image->chromaticity.green_primary.x,
1243 image->chromaticity.green_primary.y,
1244 image->chromaticity.blue_primary.x,
1245 image->chromaticity.blue_primary.y);
1246 (void) WriteBlobString(image,buffer);
1247 (void) FormatLocaleString(buffer,MaxTextExtent,
1248 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1249 image->chromaticity.white_point.y);
1250 (void) WriteBlobString(image,buffer);
1252 if (image->orientation != UndefinedOrientation)
1254 (void) FormatLocaleString(buffer,MaxTextExtent,
1255 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1256 image->orientation));
1257 (void) WriteBlobString(image,buffer);
1259 if (image->profiles != (void *) NULL)
1270 ResetImageProfileIterator(image);
1271 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1273 profile=GetImageProfile(image,name);
1274 if (profile != (StringInfo *) NULL)
1276 (void) FormatLocaleString(buffer,MaxTextExtent,
1277 "profile:%s=%.20g\n",name,(double)
1278 GetStringInfoLength(profile));
1279 (void) WriteBlobString(image,buffer);
1281 name=GetNextImageProfile(image);
1284 if (image->montage != (char *) NULL)
1286 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
1288 (void) WriteBlobString(image,buffer);
1290 ResetImagePropertyIterator(image);
1291 property=GetNextImageProperty(image);
1292 while (property != (const char *) NULL)
1294 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
1295 (void) WriteBlobString(image,buffer);
1296 value=GetImageProperty(image,property,exception);
1297 if (value != (const char *) NULL)
1302 length=strlen(value);
1303 for (i=0; i < (ssize_t) length; i++)
1304 if (isspace((int) ((unsigned char) value[i])) != 0)
1306 if (i == (ssize_t) length)
1307 (void) WriteBlob(image,length,(const unsigned char *) value);
1310 (void) WriteBlobByte(image,'{');
1311 if (strchr(value,'}') == (char *) NULL)
1312 (void) WriteBlob(image,length,(const unsigned char *) value);
1314 for (i=0; i < (ssize_t) length; i++)
1316 if (value[i] == (int) '}')
1317 (void) WriteBlobByte(image,'\\');
1318 (void) WriteBlobByte(image,value[i]);
1320 (void) WriteBlobByte(image,'}');
1323 (void) WriteBlobByte(image,'\n');
1324 property=GetNextImageProperty(image);
1326 ResetImageArtifactIterator(image);
1327 (void) WriteBlobString(image,"\f\n:\032");
1328 if (image->montage != (char *) NULL)
1331 Write montage tile directory.
1333 if (image->directory != (char *) NULL)
1334 (void) WriteBlobString(image,image->directory);
1335 (void) WriteBlobByte(image,'\0');
1337 if (image->profiles != 0)
1346 Write image profiles.
1348 ResetImageProfileIterator(image);
1349 name=GetNextImageProfile(image);
1350 while (name != (const char *) NULL)
1352 profile=GetImageProfile(image,name);
1353 (void) WriteBlob(image,GetStringInfoLength(profile),
1354 GetStringInfoDatum(profile));
1355 name=GetNextImageProfile(image);
1358 if (image->storage_class == PseudoClass)
1370 packet_size=(size_t) (3UL*depth/8UL);
1371 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1372 packet_size*sizeof(*colormap));
1373 if (colormap == (unsigned char *) NULL)
1374 return(MagickFalse);
1376 Write colormap to file.
1379 for (i=0; i < (ssize_t) image->colors; i++)
1384 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1390 pixel=ScaleQuantumToLong(image->colormap[i].red);
1391 q=PopLongPixel(MSBEndian,pixel,q);
1392 pixel=ScaleQuantumToLong(image->colormap[i].green);
1393 q=PopLongPixel(MSBEndian,pixel,q);
1394 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1395 q=PopLongPixel(MSBEndian,pixel,q);
1402 pixel=ScaleQuantumToShort(image->colormap[i].red);
1403 q=PopShortPixel(MSBEndian,pixel,q);
1404 pixel=ScaleQuantumToShort(image->colormap[i].green);
1405 q=PopShortPixel(MSBEndian,pixel,q);
1406 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1407 q=PopShortPixel(MSBEndian,pixel,q);
1415 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1416 q=PopCharPixel(pixel,q);
1417 pixel=(unsigned char) ScaleQuantumToChar(
1418 image->colormap[i].green);
1419 q=PopCharPixel(pixel,q);
1420 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1421 q=PopCharPixel(pixel,q);
1426 (void) WriteBlob(image,packet_size*image->colors,colormap);
1427 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1430 Initialize persistent pixel cache.
1432 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1434 if (status == MagickFalse)
1435 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1436 if (GetNextImageInList(image) == (Image *) NULL)
1438 image=SyncNextImageInList(image);
1439 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1441 status=image->progress_monitor(SaveImagesTag,scene,
1442 GetImageListLength(image),image->client_data);
1443 if (status == MagickFalse)
1447 } while (image_info->adjoin != MagickFalse);
1448 (void) CloseBlob(image);