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"
78 static MagickBooleanType
79 WriteMPCImage(const ImageInfo *,Image *,ExceptionInfo *);
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % IsMPC() returns MagickTrue if the image format type, identified by the
93 % magick string, is an Magick Persistent Cache image.
95 % The format of the IsMPC method is:
97 % MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
99 % A description of each parameter follows:
101 % o magick: compare image format pattern against these bytes.
103 % o length: Specifies the length of the magick string.
106 static MagickBooleanType IsMPC(const unsigned char *magick,const size_t length)
110 if (LocaleNCompare((const char *) magick,"id=MagickCache",14) == 0)
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % R e a d C A C H E I m a g e %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % ReadMPCImage() reads an Magick Persistent Cache image file and returns
127 % it. It allocates the memory necessary for the new Image structure and
128 % returns a pointer to the new image.
130 % The format of the ReadMPCImage method is:
132 % Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
134 % Decompression code contributed by Kyle Shorter.
136 % A description of each parameter follows:
138 % o image_info: the image info.
140 % o exception: return any errors or warnings in this structure.
143 static Image *ReadMPCImage(const ImageInfo *image_info,ExceptionInfo *exception)
146 cache_filename[MaxTextExtent],
148 keyword[MaxTextExtent],
192 assert(image_info != (const ImageInfo *) NULL);
193 assert(image_info->signature == MagickSignature);
194 if (image_info->debug != MagickFalse)
195 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
196 image_info->filename);
197 assert(exception != (ExceptionInfo *) NULL);
198 assert(exception->signature == MagickSignature);
199 image=AcquireImage(image_info,exception);
200 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
201 if (status == MagickFalse)
203 image=DestroyImageList(image);
204 return((Image *) NULL);
206 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
207 AppendImageFormat("cache",cache_filename);
208 c=ReadBlobByte(image);
211 image=DestroyImage(image);
212 return((Image *) NULL);
215 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
220 Decode image header; header terminates one character beyond a ':'.
222 profiles=(LinkedListInfo *) NULL;
223 length=MaxTextExtent;
224 options=AcquireString((char *) NULL);
225 quantum_depth=MAGICKCORE_QUANTUM_DEPTH;
227 image->compression=NoCompression;
228 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
239 Read comment-- any text between { }.
241 length=MaxTextExtent;
242 comment=AcquireString((char *) NULL);
243 for (p=comment; comment != (char *) NULL; p++)
245 c=ReadBlobByte(image);
247 c=ReadBlobByte(image);
249 if ((c == EOF) || (c == (int) '}'))
251 if ((size_t) (p-comment+1) >= length)
255 comment=(char *) ResizeQuantumMemory(comment,length+
256 MaxTextExtent,sizeof(*comment));
257 if (comment == (char *) NULL)
259 p=comment+strlen(comment);
263 if (comment == (char *) NULL)
264 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
266 (void) SetImageProperty(image,"comment",comment,exception);
267 comment=DestroyString(comment);
268 c=ReadBlobByte(image);
271 if (isalnum(c) != MagickFalse)
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);
314 c=ReadBlobByte(image);
318 c=ReadBlobByte(image);
322 if (isspace((int) ((unsigned char) c)) != 0)
328 (void) CopyMagickString(options,options+1,strlen(options));
330 Assign a value to the specified keyword.
337 if (LocaleCompare(keyword,"alpha-trait") == 0)
342 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
343 MagickFalse,options);
346 image->alpha_trait=(PixelTrait) alpha_trait;
349 (void) SetImageProperty(image,keyword,options,exception);
355 if (LocaleCompare(keyword,"background-color") == 0)
357 (void) QueryColorCompliance(options,AllCompliance,
358 &image->background_color,exception);
361 if (LocaleCompare(keyword,"blue-primary") == 0)
363 flags=ParseGeometry(options,&geometry_info);
364 image->chromaticity.blue_primary.x=geometry_info.rho;
365 image->chromaticity.blue_primary.y=geometry_info.sigma;
366 if ((flags & SigmaValue) == 0)
367 image->chromaticity.blue_primary.y=
368 image->chromaticity.blue_primary.x;
371 if (LocaleCompare(keyword,"border-color") == 0)
373 (void) QueryColorCompliance(options,AllCompliance,
374 &image->border_color,exception);
377 (void) SetImageProperty(image,keyword,options,exception);
383 if (LocaleCompare(keyword,"class") == 0)
388 storage_class=ParseCommandOption(MagickClassOptions,
389 MagickFalse,options);
390 if (storage_class < 0)
392 image->storage_class=(ClassType) storage_class;
395 if (LocaleCompare(keyword,"colors") == 0)
397 image->colors=StringToUnsignedLong(options);
400 if (LocaleCompare(keyword,"colorspace") == 0)
405 colorspace=ParseCommandOption(MagickColorspaceOptions,
406 MagickFalse,options);
409 (void) SetImageColorspace(image,(ColorspaceType) colorspace, exception);
412 if (LocaleCompare(keyword,"compression") == 0)
417 compression=ParseCommandOption(MagickCompressOptions,
418 MagickFalse,options);
421 image->compression=(CompressionType) compression;
424 if (LocaleCompare(keyword,"columns") == 0)
426 image->columns=StringToUnsignedLong(options);
429 (void) SetImageProperty(image,keyword,options,exception);
435 if (LocaleCompare(keyword,"delay") == 0)
437 image->delay=StringToUnsignedLong(options);
440 if (LocaleCompare(keyword,"depth") == 0)
442 image->depth=StringToUnsignedLong(options);
445 if (LocaleCompare(keyword,"dispose") == 0)
450 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
454 image->dispose=(DisposeType) dispose;
457 (void) SetImageProperty(image,keyword,options,exception);
463 if (LocaleCompare(keyword,"endian") == 0)
468 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
472 image->endian=(EndianType) endian;
475 if (LocaleCompare(keyword,"error") == 0)
477 image->error.mean_error_per_pixel=StringToDouble(options,
481 (void) SetImageProperty(image,keyword,options,exception);
487 if (LocaleCompare(keyword,"gamma") == 0)
489 image->gamma=StringToDouble(options,(char **) NULL);
492 if (LocaleCompare(keyword,"green-primary") == 0)
494 flags=ParseGeometry(options,&geometry_info);
495 image->chromaticity.green_primary.x=geometry_info.rho;
496 image->chromaticity.green_primary.y=geometry_info.sigma;
497 if ((flags & SigmaValue) == 0)
498 image->chromaticity.green_primary.y=
499 image->chromaticity.green_primary.x;
502 (void) SetImageProperty(image,keyword,options,exception);
508 if (LocaleCompare(keyword,"id") == 0)
510 (void) CopyMagickString(id,options,MaxTextExtent);
513 if (LocaleCompare(keyword,"iterations") == 0)
515 image->iterations=StringToUnsignedLong(options);
518 (void) SetImageProperty(image,keyword,options,exception);
524 if (LocaleCompare(keyword,"matte-color") == 0)
526 (void) QueryColorCompliance(options,AllCompliance,
527 &image->matte_color,exception);
530 if (LocaleCompare(keyword,"maximum-error") == 0)
532 image->error.normalized_maximum_error=StringToDouble(
533 options,(char **) NULL);
536 if (LocaleCompare(keyword,"mean-error") == 0)
538 image->error.normalized_mean_error=StringToDouble(options,
542 if (LocaleCompare(keyword,"montage") == 0)
544 (void) CloneString(&image->montage,options);
547 (void) SetImageProperty(image,keyword,options,exception);
553 if (LocaleCompare(keyword,"orientation") == 0)
558 orientation=ParseCommandOption(MagickOrientationOptions,
559 MagickFalse,options);
562 image->orientation=(OrientationType) orientation;
565 (void) SetImageProperty(image,keyword,options,exception);
571 if (LocaleCompare(keyword,"page") == 0)
576 geometry=GetPageGeometry(options);
577 (void) ParseAbsoluteGeometry(geometry,&image->page);
578 geometry=DestroyString(geometry);
581 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
582 (LocaleNCompare(keyword,"profile-",8) == 0))
584 if (profiles == (LinkedListInfo *) NULL)
585 profiles=NewLinkedList(0);
586 (void) AppendValueToLinkedList(profiles,
587 AcquireString(keyword+8));
588 profile=BlobToStringInfo((const void *) NULL,(size_t)
589 StringToLong(options));
590 if (profile == (StringInfo *) NULL)
591 ThrowReaderException(ResourceLimitError,
592 "MemoryAllocationFailed");
593 (void) SetImageProfile(image,keyword+8,profile,exception);
594 profile=DestroyStringInfo(profile);
597 (void) SetImageProperty(image,keyword,options,exception);
603 if (LocaleCompare(keyword,"quality") == 0)
605 image->quality=StringToUnsignedLong(options);
608 if (LocaleCompare(keyword,"quantum-depth") == 0)
610 quantum_depth=StringToUnsignedLong(options);
613 (void) SetImageProperty(image,keyword,options,exception);
619 if (LocaleCompare(keyword,"red-primary") == 0)
621 flags=ParseGeometry(options,&geometry_info);
622 image->chromaticity.red_primary.x=geometry_info.rho;
623 if ((flags & SigmaValue) != 0)
624 image->chromaticity.red_primary.y=geometry_info.sigma;
627 if (LocaleCompare(keyword,"rendering-intent") == 0)
632 rendering_intent=ParseCommandOption(MagickIntentOptions,
633 MagickFalse,options);
634 if (rendering_intent < 0)
636 image->rendering_intent=(RenderingIntent) rendering_intent;
639 if (LocaleCompare(keyword,"resolution") == 0)
641 flags=ParseGeometry(options,&geometry_info);
642 image->resolution.x=geometry_info.rho;
643 image->resolution.y=geometry_info.sigma;
644 if ((flags & SigmaValue) == 0)
645 image->resolution.y=image->resolution.x;
648 if (LocaleCompare(keyword,"rows") == 0)
650 image->rows=StringToUnsignedLong(options);
653 (void) SetImageProperty(image,keyword,options,exception);
659 if (LocaleCompare(keyword,"scene") == 0)
661 image->scene=StringToUnsignedLong(options);
664 (void) SetImageProperty(image,keyword,options,exception);
670 if (LocaleCompare(keyword,"ticks-per-second") == 0)
672 image->ticks_per_second=(ssize_t) StringToLong(options);
675 if (LocaleCompare(keyword,"tile-offset") == 0)
680 geometry=GetPageGeometry(options);
681 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
682 geometry=DestroyString(geometry);
684 if (LocaleCompare(keyword,"type") == 0)
689 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
693 image->type=(ImageType) type;
696 (void) SetImageProperty(image,keyword,options,exception);
702 if (LocaleCompare(keyword,"units") == 0)
707 units=ParseCommandOption(MagickResolutionOptions,
708 MagickFalse,options);
711 image->units=(ResolutionType) units;
714 (void) SetImageProperty(image,keyword,options,exception);
720 if (LocaleCompare(keyword,"white-point") == 0)
722 flags=ParseGeometry(options,&geometry_info);
723 image->chromaticity.white_point.x=geometry_info.rho;
724 image->chromaticity.white_point.y=geometry_info.sigma;
725 if ((flags & SigmaValue) == 0)
726 image->chromaticity.white_point.y=
727 image->chromaticity.white_point.x;
730 (void) SetImageProperty(image,keyword,options,exception);
735 (void) SetImageProperty(image,keyword,options,exception);
741 c=ReadBlobByte(image);
742 while (isspace((int) ((unsigned char) c)) != 0)
743 c=ReadBlobByte(image);
745 options=DestroyString(options);
746 (void) ReadBlobByte(image);
748 Verify that required image information is defined.
750 if ((LocaleCompare(id,"MagickCache") != 0) ||
751 (image->storage_class == UndefinedClass) ||
752 (image->compression == UndefinedCompression) || (image->columns == 0) ||
754 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
755 if (quantum_depth != MAGICKCORE_QUANTUM_DEPTH)
756 ThrowReaderException(CacheError,"InconsistentPersistentCacheDepth");
757 if (image->montage != (char *) NULL)
765 length=MaxTextExtent;
766 image->directory=AcquireString((char *) NULL);
771 if ((strlen(image->directory)+MaxTextExtent) >= length)
774 Allocate more memory for the image directory.
777 image->directory=(char *) ResizeQuantumMemory(image->directory,
778 length+MaxTextExtent,sizeof(*image->directory));
779 if (image->directory == (char *) NULL)
780 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
781 p=image->directory+strlen(image->directory);
783 c=ReadBlobByte(image);
785 } while (c != (int) '\0');
787 if (profiles != (LinkedListInfo *) NULL)
795 register unsigned char
801 ResetLinkedListIterator(profiles);
802 name=(const char *) GetNextValueInLinkedList(profiles);
803 while (name != (const char *) NULL)
805 profile=GetImageProfile(image,name);
806 if (profile != (StringInfo *) NULL)
808 p=GetStringInfoDatum(profile);
809 count=ReadBlob(image,GetStringInfoLength(profile),p);
811 name=(const char *) GetNextValueInLinkedList(profiles);
813 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
815 depth=GetImageQuantumDepth(image,MagickFalse);
816 if (image->storage_class == PseudoClass)
819 Create image colormap.
821 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
822 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
823 if (image->colors != 0)
832 Read image colormap from file.
834 packet_size=(size_t) (3UL*depth/8UL);
835 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
836 packet_size*sizeof(*colormap));
837 if (colormap == (unsigned char *) NULL)
838 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
839 count=ReadBlob(image,packet_size*image->colors,colormap);
840 if (count != (ssize_t) (packet_size*image->colors))
841 ThrowReaderException(CorruptImageError,
842 "InsufficientImageDataInFile");
847 ThrowReaderException(CorruptImageError,
848 "ImageDepthNotSupported");
854 for (i=0; i < (ssize_t) image->colors; i++)
856 p=PushCharPixel(p,&pixel);
857 image->colormap[i].red=ScaleCharToQuantum(pixel);
858 p=PushCharPixel(p,&pixel);
859 image->colormap[i].green=ScaleCharToQuantum(pixel);
860 p=PushCharPixel(p,&pixel);
861 image->colormap[i].blue=ScaleCharToQuantum(pixel);
870 for (i=0; i < (ssize_t) image->colors; i++)
872 p=PushShortPixel(MSBEndian,p,&pixel);
873 image->colormap[i].red=ScaleShortToQuantum(pixel);
874 p=PushShortPixel(MSBEndian,p,&pixel);
875 image->colormap[i].green=ScaleShortToQuantum(pixel);
876 p=PushShortPixel(MSBEndian,p,&pixel);
877 image->colormap[i].blue=ScaleShortToQuantum(pixel);
886 for (i=0; i < (ssize_t) image->colors; i++)
888 p=PushLongPixel(MSBEndian,p,&pixel);
889 image->colormap[i].red=ScaleLongToQuantum(pixel);
890 p=PushLongPixel(MSBEndian,p,&pixel);
891 image->colormap[i].green=ScaleLongToQuantum(pixel);
892 p=PushLongPixel(MSBEndian,p,&pixel);
893 image->colormap[i].blue=ScaleLongToQuantum(pixel);
898 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
901 if (EOFBlob(image) != MagickFalse)
903 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
907 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
908 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
911 Attach persistent pixel cache.
913 status=PersistPixelCache(image,cache_filename,MagickTrue,&offset,exception);
914 if (status == MagickFalse)
915 ThrowReaderException(CacheError,"UnableToPersistPixelCache");
917 Proceed to next image.
921 c=ReadBlobByte(image);
922 } while ((isgraph(c) == MagickFalse) && (c != EOF));
926 Allocate next image structure.
928 AcquireNextImage(image_info,image,exception);
929 if (GetNextImageInList(image) == (Image *) NULL)
931 image=DestroyImageList(image);
932 return((Image *) NULL);
934 image=SyncNextImageInList(image);
935 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
937 if (status == MagickFalse)
941 (void) CloseBlob(image);
942 return(GetFirstImageInList(image));
946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 % R e g i s t e r M P C I m a g e %
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 % RegisterMPCImage() adds properties for the Cache image format to
957 % the list of supported formats. The properties include the image format
958 % tag, a method to read and/or write the format, whether the format
959 % supports the saving of more than one frame to the same file or blob,
960 % whether the format supports native in-memory I/O, and a brief
961 % description of the format.
963 % The format of the RegisterMPCImage method is:
965 % size_t RegisterMPCImage(void)
968 ModuleExport size_t RegisterMPCImage(void)
973 entry=SetMagickInfo("CACHE");
974 entry->description=ConstantString("Magick Persistent Cache image format");
975 entry->module=ConstantString("CACHE");
976 entry->stealth=MagickTrue;
977 (void) RegisterMagickInfo(entry);
978 entry=SetMagickInfo("MPC");
979 entry->decoder=(DecodeImageHandler *) ReadMPCImage;
980 entry->encoder=(EncodeImageHandler *) WriteMPCImage;
981 entry->magick=(IsImageFormatHandler *) IsMPC;
982 entry->description=ConstantString("Magick Persistent Cache image format");
983 entry->module=ConstantString("MPC");
984 (void) RegisterMagickInfo(entry);
985 return(MagickImageCoderSignature);
989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % U n r e g i s t e r M P C I m a g e %
997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
999 % UnregisterMPCImage() removes format registrations made by the
1000 % MPC module from the list of supported formats.
1002 % The format of the UnregisterMPCImage method is:
1004 % UnregisterMPCImage(void)
1007 ModuleExport void UnregisterMPCImage(void)
1009 (void) UnregisterMagickInfo("CACHE");
1010 (void) UnregisterMagickInfo("MPC");
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1018 % W r i t e M P C I m a g e %
1022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1024 % WriteMPCImage() writes an Magick Persistent Cache image to a file.
1026 % The format of the WriteMPCImage method is:
1028 % MagickBooleanType WriteMPCImage(const ImageInfo *image_info,
1029 % Image *image,ExceptionInfo *exception)
1031 % A description of each parameter follows:
1033 % o image_info: the image info.
1035 % o image: the image.
1037 % o exception: return any errors or warnings in this structure.
1040 static MagickBooleanType WriteMPCImage(const ImageInfo *image_info,Image *image,
1041 ExceptionInfo *exception)
1044 buffer[MaxTextExtent],
1045 cache_filename[MaxTextExtent];
1066 Open persistent cache.
1068 assert(image_info != (const ImageInfo *) NULL);
1069 assert(image_info->signature == MagickSignature);
1070 assert(image != (Image *) NULL);
1071 assert(image->signature == MagickSignature);
1072 if (image->debug != MagickFalse)
1073 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1074 assert(exception != (ExceptionInfo *) NULL);
1075 assert(exception->signature == MagickSignature);
1076 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1077 if (status == MagickFalse)
1079 (void) CopyMagickString(cache_filename,image->filename,MaxTextExtent);
1080 AppendImageFormat("cache",cache_filename);
1087 Write persistent cache meta-information.
1089 depth=GetImageQuantumDepth(image,MagickTrue);
1090 if ((image->storage_class == PseudoClass) &&
1091 (image->colors > (one << depth)))
1092 image->storage_class=DirectClass;
1093 (void) WriteBlobString(image,"id=MagickCache\n");
1094 (void) FormatLocaleString(buffer,MaxTextExtent,"quantum-depth=%d\n",
1095 MAGICKCORE_QUANTUM_DEPTH);
1096 (void) WriteBlobString(image,buffer);
1097 (void) FormatLocaleString(buffer,MaxTextExtent,
1098 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
1099 MagickClassOptions,image->storage_class),(double) image->colors,
1100 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
1101 image->alpha_trait));
1102 (void) WriteBlobString(image,buffer);
1103 (void) FormatLocaleString(buffer,MaxTextExtent,
1104 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
1105 (double) image->rows,(double) image->depth);
1106 (void) WriteBlobString(image,buffer);
1107 if (image->type != UndefinedType)
1109 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1110 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1111 (void) WriteBlobString(image,buffer);
1113 if (image->colorspace != UndefinedColorspace)
1115 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1116 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1117 (void) WriteBlobString(image,buffer);
1119 if (image->endian != UndefinedEndian)
1121 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
1122 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
1123 (void) WriteBlobString(image,buffer);
1125 if (image->compression != UndefinedCompression)
1127 (void) FormatLocaleString(buffer,MaxTextExtent,
1128 "compression=%s quality=%.20g\n",CommandOptionToMnemonic(
1129 MagickCompressOptions,image->compression),(double) image->quality);
1130 (void) WriteBlobString(image,buffer);
1132 if (image->units != UndefinedResolution)
1134 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
1135 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1136 (void) WriteBlobString(image,buffer);
1138 if ((image->resolution.x != 0) || (image->resolution.y != 0))
1140 (void) FormatLocaleString(buffer,MaxTextExtent,
1141 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
1142 (void) WriteBlobString(image,buffer);
1144 if ((image->page.width != 0) || (image->page.height != 0))
1146 (void) FormatLocaleString(buffer,MaxTextExtent,
1147 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1148 image->page.height,(double) image->page.x,(double) image->page.y);
1149 (void) WriteBlobString(image,buffer);
1152 if ((image->page.x != 0) || (image->page.y != 0))
1154 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
1155 (long) image->page.x,(long) image->page.y);
1156 (void) WriteBlobString(image,buffer);
1158 if ((image->page.x != 0) || (image->page.y != 0))
1160 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1161 (long) image->tile_offset.x,(long) image->tile_offset.y);
1162 (void) WriteBlobString(image,buffer);
1164 if ((GetNextImageInList(image) != (Image *) NULL) ||
1165 (GetPreviousImageInList(image) != (Image *) NULL))
1167 if (image->scene == 0)
1168 (void) FormatLocaleString(buffer,MaxTextExtent,
1169 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
1170 image->iterations,(double) image->delay,(double)
1171 image->ticks_per_second);
1173 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
1174 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",
1175 (double) image->scene,(double) image->iterations,(double)
1176 image->delay,(double) image->ticks_per_second);
1177 (void) WriteBlobString(image,buffer);
1181 if (image->scene != 0)
1183 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
1184 (double) image->scene);
1185 (void) WriteBlobString(image,buffer);
1187 if (image->iterations != 0)
1189 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
1190 (double) image->iterations);
1191 (void) WriteBlobString(image,buffer);
1193 if (image->delay != 0)
1195 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
1196 (double) image->delay);
1197 (void) WriteBlobString(image,buffer);
1199 if (image->ticks_per_second != UndefinedTicksPerSecond)
1201 (void) FormatLocaleString(buffer,MaxTextExtent,
1202 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
1203 (void) WriteBlobString(image,buffer);
1206 if (image->gravity != UndefinedGravity)
1208 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
1209 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
1210 (void) WriteBlobString(image,buffer);
1212 if (image->dispose != UndefinedDispose)
1214 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
1215 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
1216 (void) WriteBlobString(image,buffer);
1218 if (image->rendering_intent != UndefinedIntent)
1220 (void) FormatLocaleString(buffer,MaxTextExtent,
1221 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
1222 image->rendering_intent));
1223 (void) WriteBlobString(image,buffer);
1225 if (image->gamma != 0.0)
1227 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
1229 (void) WriteBlobString(image,buffer);
1231 if (image->chromaticity.white_point.x != 0.0)
1234 Note chomaticity points.
1236 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary="
1237 "%g,%g green-primary=%g,%g blue-primary=%g,%g\n",
1238 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
1239 image->chromaticity.green_primary.x,
1240 image->chromaticity.green_primary.y,
1241 image->chromaticity.blue_primary.x,
1242 image->chromaticity.blue_primary.y);
1243 (void) WriteBlobString(image,buffer);
1244 (void) FormatLocaleString(buffer,MaxTextExtent,
1245 "white-point=%g,%g\n",image->chromaticity.white_point.x,
1246 image->chromaticity.white_point.y);
1247 (void) WriteBlobString(image,buffer);
1249 if (image->orientation != UndefinedOrientation)
1251 (void) FormatLocaleString(buffer,MaxTextExtent,
1252 "orientation=%s\n",CommandOptionToMnemonic(MagickOrientationOptions,
1253 image->orientation));
1254 (void) WriteBlobString(image,buffer);
1256 if (image->profiles != (void *) NULL)
1267 ResetImageProfileIterator(image);
1268 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
1270 profile=GetImageProfile(image,name);
1271 if (profile != (StringInfo *) NULL)
1273 (void) FormatLocaleString(buffer,MaxTextExtent,
1274 "profile:%s=%.20g\n",name,(double)
1275 GetStringInfoLength(profile));
1276 (void) WriteBlobString(image,buffer);
1278 name=GetNextImageProfile(image);
1281 if (image->montage != (char *) NULL)
1283 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
1285 (void) WriteBlobString(image,buffer);
1287 ResetImagePropertyIterator(image);
1288 property=GetNextImageProperty(image);
1289 while (property != (const char *) NULL)
1291 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
1292 (void) WriteBlobString(image,buffer);
1293 value=GetImageProperty(image,property,exception);
1294 if (value != (const char *) NULL)
1299 length=strlen(value);
1300 for (i=0; i < (ssize_t) length; i++)
1301 if (isspace((int) ((unsigned char) value[i])) != 0)
1303 if (i == (ssize_t) length)
1304 (void) WriteBlob(image,length,(const unsigned char *) value);
1307 (void) WriteBlobByte(image,'{');
1308 if (strchr(value,'}') == (char *) NULL)
1309 (void) WriteBlob(image,length,(const unsigned char *) value);
1311 for (i=0; i < (ssize_t) length; i++)
1313 if (value[i] == (int) '}')
1314 (void) WriteBlobByte(image,'\\');
1315 (void) WriteBlobByte(image,value[i]);
1317 (void) WriteBlobByte(image,'}');
1320 (void) WriteBlobByte(image,'\n');
1321 property=GetNextImageProperty(image);
1323 ResetImageArtifactIterator(image);
1324 (void) WriteBlobString(image,"\f\n:\032");
1325 if (image->montage != (char *) NULL)
1328 Write montage tile directory.
1330 if (image->directory != (char *) NULL)
1331 (void) WriteBlobString(image,image->directory);
1332 (void) WriteBlobByte(image,'\0');
1334 if (image->profiles != 0)
1343 Write image profiles.
1345 ResetImageProfileIterator(image);
1346 name=GetNextImageProfile(image);
1347 while (name != (const char *) NULL)
1349 profile=GetImageProfile(image,name);
1350 (void) WriteBlob(image,GetStringInfoLength(profile),
1351 GetStringInfoDatum(profile));
1352 name=GetNextImageProfile(image);
1355 if (image->storage_class == PseudoClass)
1367 packet_size=(size_t) (3UL*depth/8UL);
1368 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1369 packet_size*sizeof(*colormap));
1370 if (colormap == (unsigned char *) NULL)
1371 return(MagickFalse);
1373 Write colormap to file.
1376 for (i=0; i < (ssize_t) image->colors; i++)
1381 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
1387 pixel=ScaleQuantumToLong(image->colormap[i].red);
1388 q=PopLongPixel(MSBEndian,pixel,q);
1389 pixel=ScaleQuantumToLong(image->colormap[i].green);
1390 q=PopLongPixel(MSBEndian,pixel,q);
1391 pixel=ScaleQuantumToLong(image->colormap[i].blue);
1392 q=PopLongPixel(MSBEndian,pixel,q);
1399 pixel=ScaleQuantumToShort(image->colormap[i].red);
1400 q=PopShortPixel(MSBEndian,pixel,q);
1401 pixel=ScaleQuantumToShort(image->colormap[i].green);
1402 q=PopShortPixel(MSBEndian,pixel,q);
1403 pixel=ScaleQuantumToShort(image->colormap[i].blue);
1404 q=PopShortPixel(MSBEndian,pixel,q);
1412 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
1413 q=PopCharPixel(pixel,q);
1414 pixel=(unsigned char) ScaleQuantumToChar(
1415 image->colormap[i].green);
1416 q=PopCharPixel(pixel,q);
1417 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
1418 q=PopCharPixel(pixel,q);
1423 (void) WriteBlob(image,packet_size*image->colors,colormap);
1424 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1427 Initialize persistent pixel cache.
1429 status=PersistPixelCache(image,cache_filename,MagickFalse,&offset,
1431 if (status == MagickFalse)
1432 ThrowWriterException(CacheError,"UnableToPersistPixelCache");
1433 if (GetNextImageInList(image) == (Image *) NULL)
1435 image=SyncNextImageInList(image);
1436 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1438 status=image->progress_monitor(SaveImagesTag,scene,
1439 GetImageListLength(image),image->client_data);
1440 if (status == MagickFalse)
1444 } while (image_info->adjoin != MagickFalse);
1445 (void) CloseBlob(image);