2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/hashmap.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/profile.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/static.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #if defined(MAGICKCORE_BZLIB_DELEGATE)
79 #if defined(MAGICKCORE_LZMA_DELEGATE)
82 #if defined(MAGICKCORE_ZLIB_DELEGATE)
96 static MagickBooleanType
97 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % IsMIFF() returns MagickTrue if the image format type, identified by the
111 % magick string, is MIFF.
113 % The format of the IsMIFF method is:
115 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
117 % A description of each parameter follows:
119 % o magick: compare image format pattern against these bytes.
121 % o length: Specifies the length of the magick string.
124 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
128 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % R e a d M I F F I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
145 % memory necessary for the new Image structure and returns a pointer to the
148 % The format of the ReadMIFFImage method is:
150 % Image *ReadMIFFImage(const ImageInfo *image_info,
151 % ExceptionInfo *exception)
153 % Decompression code contributed by Kyle Shorter.
155 % A description of each parameter follows:
157 % o image_info: the image info.
159 % o exception: return any errors or warnings in this structure.
163 #if defined(MAGICKCORE_BZLIB_DELEGATE)
164 static void *AcquireBZIPMemory(void *context,int items,int size)
167 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
171 #if defined(MAGICKCORE_LZMA_DELEGATE)
172 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
175 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
179 #if defined(MAGICKCORE_ZLIB_DELEGATE)
180 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
184 return((voidpf) AcquireQuantumMemory(items,size));
188 static inline size_t MagickMax(const size_t x,const size_t y)
195 static inline size_t MagickMin(const size_t x,const size_t y)
202 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
203 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
209 if (image->storage_class == PseudoClass)
212 switch (image->depth)
216 pixel->index=ConstrainColormapIndex(image,
217 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3),exception);
223 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
230 pixel->index=ConstrainColormapIndex(image,*p,exception);
235 (void) ThrowMagickException(exception,GetMagickModule(),
236 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
238 switch (image->depth)
245 if (image->alpha_trait == BlendPixelTrait)
247 p=PushCharPixel(p,&quantum);
248 pixel->alpha=ScaleCharToQuantum(quantum);
257 if (image->alpha_trait == BlendPixelTrait)
259 p=PushShortPixel(MSBEndian,p,&quantum);
260 pixel->alpha=(Quantum) (quantum >> (image->depth-
261 MAGICKCORE_QUANTUM_DEPTH));
270 if (image->alpha_trait == BlendPixelTrait)
272 p=PushLongPixel(MSBEndian,p,&quantum);
273 pixel->alpha=(Quantum) (quantum >> (image->depth-
274 MAGICKCORE_QUANTUM_DEPTH));
279 (void) ThrowMagickException(exception,GetMagickModule(),
280 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
282 *length=(size_t) (*p++)+1;
285 switch (image->depth)
292 p=PushCharPixel(p,&quantum);
293 pixel->red=ScaleCharToQuantum(quantum);
294 pixel->green=pixel->red;
295 pixel->blue=pixel->red;
296 if (IsGrayColorspace(image->colorspace) == MagickFalse)
298 p=PushCharPixel(p,&quantum);
299 pixel->green=ScaleCharToQuantum(quantum);
300 p=PushCharPixel(p,&quantum);
301 pixel->blue=ScaleCharToQuantum(quantum);
303 if (image->colorspace == CMYKColorspace)
305 p=PushCharPixel(p,&quantum);
306 pixel->black=ScaleCharToQuantum(quantum);
308 if (image->alpha_trait == BlendPixelTrait)
310 p=PushCharPixel(p,&quantum);
311 pixel->alpha=ScaleCharToQuantum(quantum);
320 p=PushShortPixel(MSBEndian,p,&quantum);
321 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
322 pixel->green=pixel->red;
323 pixel->blue=pixel->red;
324 if (IsGrayColorspace(image->colorspace) == MagickFalse)
326 p=PushShortPixel(MSBEndian,p,&quantum);
327 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
328 p=PushShortPixel(MSBEndian,p,&quantum);
329 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
331 if (image->colorspace == CMYKColorspace)
333 p=PushShortPixel(MSBEndian,p,&quantum);
334 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
336 if (image->alpha_trait == BlendPixelTrait)
338 p=PushShortPixel(MSBEndian,p,&quantum);
339 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
348 p=PushLongPixel(MSBEndian,p,&quantum);
349 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
350 pixel->green=pixel->red;
351 pixel->blue=pixel->red;
352 if (IsGrayColorspace(image->colorspace) == MagickFalse)
354 p=PushLongPixel(MSBEndian,p,&quantum);
355 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
356 p=PushLongPixel(MSBEndian,p,&quantum);
357 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
359 if (image->colorspace == CMYKColorspace)
361 p=PushLongPixel(MSBEndian,p,&quantum);
362 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
364 if (image->alpha_trait == BlendPixelTrait)
366 p=PushLongPixel(MSBEndian,p,&quantum);
367 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
372 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
373 "ImageDepthNotSupported","`%s'",image->filename);
375 *length=(size_t) (*p++)+1;
378 #if defined(MAGICKCORE_BZLIB_DELEGATE)
379 static void RelinquishBZIPMemory(void *context,void *memory)
382 memory=RelinquishMagickMemory(memory);
386 #if defined(MAGICKCORE_LZMA_DELEGATE)
387 static void RelinquishLZMAMemory(void *context,void *memory)
390 memory=RelinquishMagickMemory(memory);
394 #if defined(MAGICKCORE_ZLIB_DELEGATE)
395 static void RelinquishZIPMemory(voidpf context,voidpf memory)
398 memory=RelinquishMagickMemory(memory);
402 static Image *ReadMIFFImage(const ImageInfo *image_info,
403 ExceptionInfo *exception)
405 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
406 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
407 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
409 #if defined(MAGICKCORE_BZLIB_DELEGATE)
416 keyword[MaxTextExtent],
437 #if defined(MAGICKCORE_LZMA_DELEGATE)
439 initialize_lzma = LZMA_STREAM_INIT,
484 #if defined(MAGICKCORE_ZLIB_DELEGATE)
492 assert(image_info != (const ImageInfo *) NULL);
493 assert(image_info->signature == MagickSignature);
494 if (image_info->debug != MagickFalse)
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
496 image_info->filename);
497 assert(exception != (ExceptionInfo *) NULL);
498 assert(exception->signature == MagickSignature);
499 image=AcquireImage(image_info,exception);
500 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
501 if (status == MagickFalse)
503 image=DestroyImageList(image);
504 return((Image *) NULL);
507 Decode image header; header terminates one character beyond a ':'.
509 c=ReadBlobByte(image);
511 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
513 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
519 Decode image header; header terminates one character beyond a ':'.
521 length=MaxTextExtent;
522 options=AcquireString((char *) NULL);
523 quantum_format=UndefinedQuantumFormat;
524 profiles=(LinkedListInfo *) NULL;
527 image->compression=NoCompression;
528 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
539 Read comment-- any text between { }.
541 length=MaxTextExtent;
542 comment=AcquireString((char *) NULL);
543 for (p=comment; comment != (char *) NULL; p++)
545 c=ReadBlobByte(image);
547 c=ReadBlobByte(image);
549 if ((c == EOF) || (c == (int) '}'))
551 if ((size_t) (p-comment+1) >= length)
555 comment=(char *) ResizeQuantumMemory(comment,length+
556 MaxTextExtent,sizeof(*comment));
557 if (comment == (char *) NULL)
559 p=comment+strlen(comment);
563 if (comment == (char *) NULL)
564 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
566 (void) SetImageProperty(image,"comment",comment,exception);
567 comment=DestroyString(comment);
568 c=ReadBlobByte(image);
571 if (isalnum(c) != MagickFalse)
581 if ((size_t) (p-keyword) < (MaxTextExtent-1))
583 c=ReadBlobByte(image);
587 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
588 c=ReadBlobByte(image);
592 Get the keyword value.
594 c=ReadBlobByte(image);
595 while ((c != (int) '}') && (c != EOF))
597 if ((size_t) (p-options+1) >= length)
601 options=(char *) ResizeQuantumMemory(options,length+
602 MaxTextExtent,sizeof(*options));
603 if (options == (char *) NULL)
605 p=options+strlen(options);
607 if (options == (char *) NULL)
608 ThrowReaderException(ResourceLimitError,
609 "MemoryAllocationFailed");
611 c=ReadBlobByte(image);
614 c=ReadBlobByte(image);
618 c=ReadBlobByte(image);
622 if (isspace((int) ((unsigned char) c)) != 0)
627 (void) CopyMagickString(options,options+1,strlen(options));
629 Assign a value to the specified keyword.
636 if (LocaleCompare(keyword,"alpha-trait") == 0)
641 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
642 MagickFalse,options);
645 image->alpha_trait=(PixelTrait) alpha_trait;
648 (void) SetImageProperty(image,keyword,options,exception);
654 if (LocaleCompare(keyword,"background-color") == 0)
656 (void) QueryColorCompliance(options,AllCompliance,
657 &image->background_color,exception);
660 if (LocaleCompare(keyword,"blue-primary") == 0)
662 flags=ParseGeometry(options,&geometry_info);
663 image->chromaticity.blue_primary.x=geometry_info.rho;
664 image->chromaticity.blue_primary.y=geometry_info.sigma;
665 if ((flags & SigmaValue) == 0)
666 image->chromaticity.blue_primary.y=
667 image->chromaticity.blue_primary.x;
670 if (LocaleCompare(keyword,"border-color") == 0)
672 (void) QueryColorCompliance(options,AllCompliance,
673 &image->border_color,exception);
676 (void) SetImageProperty(image,keyword,options,exception);
682 if (LocaleCompare(keyword,"class") == 0)
687 storage_class=ParseCommandOption(MagickClassOptions,
688 MagickFalse,options);
689 if (storage_class < 0)
691 image->storage_class=(ClassType) storage_class;
694 if (LocaleCompare(keyword,"colors") == 0)
696 colors=StringToUnsignedLong(options);
699 if (LocaleCompare(keyword,"colorspace") == 0)
704 colorspace=ParseCommandOption(MagickColorspaceOptions,
705 MagickFalse,options);
708 image->colorspace=(ColorspaceType) colorspace;
711 if (LocaleCompare(keyword,"compression") == 0)
716 compression=ParseCommandOption(MagickCompressOptions,
717 MagickFalse,options);
720 image->compression=(CompressionType) compression;
723 if (LocaleCompare(keyword,"columns") == 0)
725 image->columns=StringToUnsignedLong(options);
728 (void) SetImageProperty(image,keyword,options,exception);
734 if (LocaleCompare(keyword,"delay") == 0)
736 image->delay=StringToUnsignedLong(options);
739 if (LocaleCompare(keyword,"depth") == 0)
741 image->depth=StringToUnsignedLong(options);
744 if (LocaleCompare(keyword,"dispose") == 0)
749 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
753 image->dispose=(DisposeType) dispose;
756 (void) SetImageProperty(image,keyword,options,exception);
762 if (LocaleCompare(keyword,"endian") == 0)
767 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
771 image->endian=(EndianType) endian;
774 (void) SetImageProperty(image,keyword,options,exception);
780 if (LocaleCompare(keyword,"gamma") == 0)
782 image->gamma=StringToDouble(options,(char **) NULL);
785 if (LocaleCompare(keyword,"gravity") == 0)
790 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
794 image->gravity=(GravityType) gravity;
797 if (LocaleCompare(keyword,"green-primary") == 0)
799 flags=ParseGeometry(options,&geometry_info);
800 image->chromaticity.green_primary.x=geometry_info.rho;
801 image->chromaticity.green_primary.y=geometry_info.sigma;
802 if ((flags & SigmaValue) == 0)
803 image->chromaticity.green_primary.y=
804 image->chromaticity.green_primary.x;
807 (void) SetImageProperty(image,keyword,options,exception);
813 if (LocaleCompare(keyword,"id") == 0)
815 (void) CopyMagickString(id,options,MaxTextExtent);
818 if (LocaleCompare(keyword,"iterations") == 0)
820 image->iterations=StringToUnsignedLong(options);
823 (void) SetImageProperty(image,keyword,options,exception);
829 if (LocaleCompare(keyword,"matte") == 0)
834 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
838 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
842 if (LocaleCompare(keyword,"matte-color") == 0)
844 (void) QueryColorCompliance(options,AllCompliance,
845 &image->matte_color,exception);
848 if (LocaleCompare(keyword,"montage") == 0)
850 (void) CloneString(&image->montage,options);
853 (void) SetImageProperty(image,keyword,options,exception);
859 if (LocaleCompare(keyword,"orientation") == 0)
864 orientation=ParseCommandOption(MagickOrientationOptions,
865 MagickFalse,options);
868 image->orientation=(OrientationType) orientation;
871 (void) SetImageProperty(image,keyword,options,exception);
877 if (LocaleCompare(keyword,"page") == 0)
882 geometry=GetPageGeometry(options);
883 (void) ParseAbsoluteGeometry(geometry,&image->page);
884 geometry=DestroyString(geometry);
887 if (LocaleCompare(keyword,"pixel-intensity") == 0)
892 intensity=ParseCommandOption(MagickPixelIntensityOptions,
893 MagickFalse,options);
896 image->intensity=(PixelIntensityMethod) intensity;
899 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
900 (LocaleNCompare(keyword,"profile-",8) == 0))
905 if (profiles == (LinkedListInfo *) NULL)
906 profiles=NewLinkedList(0);
907 (void) AppendValueToLinkedList(profiles,
908 AcquireString(keyword+8));
909 profile=BlobToStringInfo((const void *) NULL,(size_t)
910 StringToLong(options));
911 if (profile == (StringInfo *) NULL)
912 ThrowReaderException(ResourceLimitError,
913 "MemoryAllocationFailed");
914 (void) SetImageProfile(image,keyword+8,profile,exception);
915 profile=DestroyStringInfo(profile);
918 (void) SetImageProperty(image,keyword,options,exception);
924 if (LocaleCompare(keyword,"quality") == 0)
926 image->quality=StringToUnsignedLong(options);
929 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
930 (LocaleCompare(keyword,"quantum:format") == 0))
935 format=ParseCommandOption(MagickQuantumFormatOptions,
936 MagickFalse,options);
939 quantum_format=(QuantumFormatType) format;
942 (void) SetImageProperty(image,keyword,options,exception);
948 if (LocaleCompare(keyword,"red-primary") == 0)
950 flags=ParseGeometry(options,&geometry_info);
951 image->chromaticity.red_primary.x=geometry_info.rho;
952 image->chromaticity.red_primary.y=geometry_info.sigma;
953 if ((flags & SigmaValue) == 0)
954 image->chromaticity.red_primary.y=
955 image->chromaticity.red_primary.x;
958 if (LocaleCompare(keyword,"rendering-intent") == 0)
963 rendering_intent=ParseCommandOption(MagickIntentOptions,
964 MagickFalse,options);
965 if (rendering_intent < 0)
967 image->rendering_intent=(RenderingIntent) rendering_intent;
970 if (LocaleCompare(keyword,"resolution") == 0)
972 flags=ParseGeometry(options,&geometry_info);
973 image->resolution.x=geometry_info.rho;
974 image->resolution.y=geometry_info.sigma;
975 if ((flags & SigmaValue) == 0)
976 image->resolution.y=image->resolution.x;
979 if (LocaleCompare(keyword,"rows") == 0)
981 image->rows=StringToUnsignedLong(options);
984 (void) SetImageProperty(image,keyword,options,exception);
990 if (LocaleCompare(keyword,"scene") == 0)
992 image->scene=StringToUnsignedLong(options);
995 (void) SetImageProperty(image,keyword,options,exception);
1001 if (LocaleCompare(keyword,"ticks-per-second") == 0)
1003 image->ticks_per_second=(ssize_t) StringToLong(options);
1006 if (LocaleCompare(keyword,"tile-offset") == 0)
1011 geometry=GetPageGeometry(options);
1012 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1013 geometry=DestroyString(geometry);
1016 if (LocaleCompare(keyword,"type") == 0)
1021 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1025 image->type=(ImageType) type;
1028 (void) SetImageProperty(image,keyword,options,exception);
1034 if (LocaleCompare(keyword,"units") == 0)
1039 units=ParseCommandOption(MagickResolutionOptions,
1040 MagickFalse,options);
1043 image->units=(ResolutionType) units;
1046 (void) SetImageProperty(image,keyword,options,exception);
1052 if (LocaleCompare(keyword,"version") == 0)
1054 version=StringToDouble(options,(char **) NULL);
1057 (void) SetImageProperty(image,keyword,options,exception);
1063 if (LocaleCompare(keyword,"white-point") == 0)
1065 flags=ParseGeometry(options,&geometry_info);
1066 image->chromaticity.white_point.x=geometry_info.rho;
1067 image->chromaticity.white_point.y=geometry_info.sigma;
1068 if ((flags & SigmaValue) == 0)
1069 image->chromaticity.white_point.y=
1070 image->chromaticity.white_point.x;
1073 (void) SetImageProperty(image,keyword,options,exception);
1078 (void) SetImageProperty(image,keyword,options,exception);
1084 c=ReadBlobByte(image);
1085 while (isspace((int) ((unsigned char) c)) != 0)
1086 c=ReadBlobByte(image);
1088 options=DestroyString(options);
1089 (void) ReadBlobByte(image);
1091 Verify that required image information is defined.
1093 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1094 (image->storage_class == UndefinedClass) ||
1095 (image->columns == 0) || (image->rows == 0))
1096 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1097 if (image->montage != (char *) NULL)
1105 length=MaxTextExtent;
1106 image->directory=AcquireString((char *) NULL);
1111 if ((strlen(image->directory)+MaxTextExtent) >= length)
1114 Allocate more memory for the image directory.
1117 image->directory=(char *) ResizeQuantumMemory(image->directory,
1118 length+MaxTextExtent,sizeof(*image->directory));
1119 if (image->directory == (char *) NULL)
1120 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1121 p=image->directory+strlen(image->directory);
1123 c=ReadBlobByte(image);
1125 } while (c != (int) '\0');
1127 if (profiles != (LinkedListInfo *) NULL)
1136 Read image profiles.
1138 ResetLinkedListIterator(profiles);
1139 name=(const char *) GetNextValueInLinkedList(profiles);
1140 while (name != (const char *) NULL)
1142 profile=GetImageProfile(image,name);
1143 if (profile != (StringInfo *) NULL)
1145 register unsigned char
1148 p=GetStringInfoDatum(profile);
1149 count=ReadBlob(image,GetStringInfoLength(profile),p);
1152 name=(const char *) GetNextValueInLinkedList(profiles);
1154 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1156 image->depth=GetImageQuantumDepth(image,MagickFalse);
1157 if (image->storage_class == PseudoClass)
1160 Create image colormap.
1162 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1163 if (status == MagickFalse)
1164 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1174 Read image colormap from file.
1176 packet_size=(size_t) (3UL*image->depth/8UL);
1177 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1178 packet_size*sizeof(*colormap));
1179 if (colormap == (unsigned char *) NULL)
1180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1181 count=ReadBlob(image,packet_size*image->colors,colormap);
1183 switch (image->depth)
1186 ThrowReaderException(CorruptImageError,
1187 "ImageDepthNotSupported");
1193 for (i=0; i < (ssize_t) image->colors; i++)
1195 p=PushCharPixel(p,&pixel);
1196 image->colormap[i].red=ScaleCharToQuantum(pixel);
1197 p=PushCharPixel(p,&pixel);
1198 image->colormap[i].green=ScaleCharToQuantum(pixel);
1199 p=PushCharPixel(p,&pixel);
1200 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1209 for (i=0; i < (ssize_t) image->colors; i++)
1211 p=PushShortPixel(MSBEndian,p,&pixel);
1212 image->colormap[i].red=ScaleShortToQuantum(pixel);
1213 p=PushShortPixel(MSBEndian,p,&pixel);
1214 image->colormap[i].green=ScaleShortToQuantum(pixel);
1215 p=PushShortPixel(MSBEndian,p,&pixel);
1216 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1225 for (i=0; i < (ssize_t) image->colors; i++)
1227 p=PushLongPixel(MSBEndian,p,&pixel);
1228 image->colormap[i].red=ScaleLongToQuantum(pixel);
1229 p=PushLongPixel(MSBEndian,p,&pixel);
1230 image->colormap[i].green=ScaleLongToQuantum(pixel);
1231 p=PushLongPixel(MSBEndian,p,&pixel);
1232 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1237 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1240 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1241 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1244 Allocate image pixels.
1246 quantum_info=AcquireQuantumInfo(image_info,image);
1247 if (quantum_info == (QuantumInfo *) NULL)
1248 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1249 if (quantum_format != UndefinedQuantumFormat)
1251 status=SetQuantumFormat(image,quantum_info,quantum_format);
1252 if (status == MagickFalse)
1253 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1255 packet_size=(size_t) (quantum_info->depth/8);
1256 if (image->storage_class == DirectClass)
1257 packet_size=(size_t) (3*quantum_info->depth/8);
1258 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1259 packet_size=quantum_info->depth/8;
1260 if (image->alpha_trait == BlendPixelTrait)
1261 packet_size+=quantum_info->depth/8;
1262 if (image->colorspace == CMYKColorspace)
1263 packet_size+=quantum_info->depth/8;
1264 if (image->compression == RLECompression)
1266 length=image->columns;
1267 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1268 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1270 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1271 sizeof(*compress_pixels));
1272 if (compress_pixels == (unsigned char *) NULL)
1273 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1277 quantum_type=RGBQuantum;
1278 if (image->alpha_trait == BlendPixelTrait)
1279 quantum_type=RGBAQuantum;
1280 if (image->colorspace == CMYKColorspace)
1282 quantum_type=CMYKQuantum;
1283 if (image->alpha_trait == BlendPixelTrait)
1284 quantum_type=CMYKAQuantum;
1286 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1288 quantum_type=GrayQuantum;
1289 if (image->alpha_trait == BlendPixelTrait)
1290 quantum_type=GrayAlphaQuantum;
1292 if (image->storage_class == PseudoClass)
1294 quantum_type=IndexQuantum;
1295 if (image->alpha_trait == BlendPixelTrait)
1296 quantum_type=IndexAlphaQuantum;
1299 switch (image->compression)
1301 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1302 case BZipCompression:
1307 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1308 bzip_info.bzalloc=AcquireBZIPMemory;
1309 bzip_info.bzfree=RelinquishBZIPMemory;
1310 bzip_info.opaque=(void *) NULL;
1311 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1318 #if defined(MAGICKCORE_LZMA_DELEGATE)
1319 case LZMACompression:
1324 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1325 allocator.alloc=AcquireLZMAMemory;
1326 allocator.free=RelinquishLZMAMemory;
1327 lzma_info=initialize_lzma;
1328 lzma_info.allocator=(&allocator);
1329 code=lzma_auto_decoder(&lzma_info,-1,0);
1330 if (code != LZMA_OK)
1335 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1336 case LZWCompression:
1337 case ZipCompression:
1342 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1343 zip_info.zalloc=AcquireZIPMemory;
1344 zip_info.zfree=RelinquishZIPMemory;
1345 zip_info.opaque=(voidpf) NULL;
1346 code=inflateInit(&zip_info);
1352 case RLECompression:
1354 GetPixelInfo(image,&pixel);
1360 pixels=GetQuantumPixels(quantum_info);
1362 for (y=0; y < (ssize_t) image->rows; y++)
1370 if (status == MagickFalse)
1372 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1373 if (q == (Quantum *) NULL)
1375 switch (image->compression)
1377 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1378 case BZipCompression:
1380 bzip_info.next_out=(char *) pixels;
1381 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1384 if (bzip_info.avail_in == 0)
1386 bzip_info.next_in=(char *) compress_pixels;
1387 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1389 length=(size_t) ReadBlobMSBLong(image);
1390 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1391 (unsigned char *) bzip_info.next_in);
1393 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1395 } while (bzip_info.avail_out != 0);
1396 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1397 quantum_type,pixels,exception);
1401 #if defined(MAGICKCORE_LZMA_DELEGATE)
1402 case LZMACompression:
1404 lzma_info.next_out=pixels;
1405 lzma_info.avail_out=packet_size*image->columns;
1411 if (lzma_info.avail_in == 0)
1413 lzma_info.next_in=compress_pixels;
1414 length=(size_t) ReadBlobMSBLong(image);
1415 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1416 (unsigned char *) lzma_info.next_in);
1418 code=lzma_code(&lzma_info,LZMA_RUN);
1424 if (code == LZMA_STREAM_END)
1426 } while (lzma_info.avail_out != 0);
1427 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1428 quantum_type,pixels,exception);
1432 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1433 case LZWCompression:
1434 case ZipCompression:
1436 zip_info.next_out=pixels;
1437 zip_info.avail_out=(uInt) (packet_size*image->columns);
1440 if (zip_info.avail_in == 0)
1442 zip_info.next_in=compress_pixels;
1443 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1445 length=(size_t) ReadBlobMSBLong(image);
1446 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1449 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1451 } while (zip_info.avail_out != 0);
1452 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1453 quantum_type,pixels,exception);
1457 case RLECompression:
1459 for (x=0; x < (ssize_t) image->columns; x++)
1463 count=ReadBlob(image,packet_size,pixels);
1464 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1467 if (image->storage_class == PseudoClass)
1468 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1471 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1472 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1473 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1474 if (image->colorspace == CMYKColorspace)
1475 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1477 if (image->alpha_trait == BlendPixelTrait)
1478 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1479 q+=GetPixelChannels(image);
1485 count=ReadBlob(image,packet_size*image->columns,pixels);
1486 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1487 quantum_type,pixels,exception);
1491 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1494 SetQuantumImageType(image,quantum_type);
1495 switch (image->compression)
1497 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1498 case BZipCompression:
1508 offset=SeekBlob(image,-((MagickOffsetType)
1509 bzip_info.avail_in),SEEK_CUR);
1511 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1513 code=BZ2_bzDecompressEnd(&bzip_info);
1519 #if defined(MAGICKCORE_LZMA_DELEGATE)
1520 case LZMACompression:
1525 code=lzma_code(&lzma_info,LZMA_FINISH);
1526 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1528 lzma_end(&lzma_info);
1532 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1533 case LZWCompression:
1534 case ZipCompression:
1544 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1547 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1549 code=inflateEnd(&zip_info);
1550 if (code != LZMA_OK)
1558 quantum_info=DestroyQuantumInfo(quantum_info);
1559 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1560 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1562 image=DestroyImageList(image);
1563 return((Image *) NULL);
1565 if (EOFBlob(image) != MagickFalse)
1567 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1572 Proceed to next image.
1574 if (image_info->number_scenes != 0)
1575 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1579 c=ReadBlobByte(image);
1580 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1584 Allocate next image structure.
1586 AcquireNextImage(image_info,image,exception);
1587 if (GetNextImageInList(image) == (Image *) NULL)
1589 image=DestroyImageList(image);
1590 return((Image *) NULL);
1592 image=SyncNextImageInList(image);
1593 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1594 GetBlobSize(image));
1595 if (status == MagickFalse)
1599 (void) CloseBlob(image);
1600 return(GetFirstImageInList(image));
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608 % R e g i s t e r M I F F I m a g e %
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1614 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1615 % supported formats. The properties include the image format tag, a method to
1616 % read and/or write the format, whether the format supports the saving of more
1617 % than one frame to the same file or blob, whether the format supports native
1618 % in-memory I/O, and a brief description of the format.
1620 % The format of the RegisterMIFFImage method is:
1622 % size_t RegisterMIFFImage(void)
1625 ModuleExport size_t RegisterMIFFImage(void)
1628 version[MaxTextExtent];
1634 #if defined(MagickImageCoderSignatureText)
1635 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1636 #if defined(ZLIB_VERSION)
1637 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1638 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1640 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1641 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1644 entry=SetMagickInfo("MIFF");
1645 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1646 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1647 entry->magick=(IsImageFormatHandler *) IsMIFF;
1648 entry->seekable_stream=MagickTrue;
1649 entry->description=ConstantString("Magick Image File Format");
1650 if (*version != '\0')
1651 entry->version=ConstantString(version);
1652 entry->module=ConstantString("MIFF");
1653 (void) RegisterMagickInfo(entry);
1654 return(MagickImageCoderSignature);
1658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1662 % U n r e g i s t e r M I F F I m a g e %
1666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1668 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1669 % from the list of supported formats.
1671 % The format of the UnregisterMIFFImage method is:
1673 % UnregisterMIFFImage(void)
1676 ModuleExport void UnregisterMIFFImage(void)
1678 (void) UnregisterMagickInfo("MIFF");
1682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686 % W r i t e M I F F I m a g e %
1690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1692 % WriteMIFFImage() writes a MIFF image to a file.
1694 % The format of the WriteMIFFImage method is:
1696 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1697 % Image *image,ExceptionInfo *exception)
1699 % Compression code contributed by Kyle Shorter.
1701 % A description of each parameter follows:
1703 % o image_info: the image info.
1705 % o image: the image.
1707 % o exception: return any errors or warnings in this structure.
1711 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1712 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1714 if (image->storage_class != DirectClass)
1716 switch (image->depth)
1720 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1721 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1724 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1727 *pixels++=(unsigned char) pixel->index;
1731 (void) ThrowMagickException(exception,GetMagickModule(),
1732 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1734 switch (image->depth)
1741 if (image->alpha_trait == BlendPixelTrait)
1743 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1744 pixels=PopLongPixel(MSBEndian,value,pixels);
1753 if (image->alpha_trait == BlendPixelTrait)
1755 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1756 pixels=PopShortPixel(MSBEndian,value,pixels);
1765 if (image->alpha_trait == BlendPixelTrait)
1767 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1769 pixels=PopCharPixel(value,pixels);
1774 (void) ThrowMagickException(exception,GetMagickModule(),
1775 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1777 *pixels++=(unsigned char) length;
1780 switch (image->depth)
1787 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1788 pixels=PopLongPixel(MSBEndian,value,pixels);
1789 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1791 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1792 pixels=PopLongPixel(MSBEndian,value,pixels);
1793 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1794 pixels=PopLongPixel(MSBEndian,value,pixels);
1796 if (image->colorspace == CMYKColorspace)
1798 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1799 pixels=PopLongPixel(MSBEndian,value,pixels);
1801 if (image->alpha_trait == BlendPixelTrait)
1803 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1804 pixels=PopLongPixel(MSBEndian,value,pixels);
1813 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1814 pixels=PopShortPixel(MSBEndian,value,pixels);
1815 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1817 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1818 pixels=PopShortPixel(MSBEndian,value,pixels);
1819 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1820 pixels=PopShortPixel(MSBEndian,value,pixels);
1822 if (image->colorspace == CMYKColorspace)
1824 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1825 pixels=PopShortPixel(MSBEndian,value,pixels);
1827 if (image->alpha_trait == BlendPixelTrait)
1829 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1830 pixels=PopShortPixel(MSBEndian,value,pixels);
1839 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1840 pixels=PopCharPixel(value,pixels);
1841 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1843 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1845 pixels=PopCharPixel(value,pixels);
1846 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1847 pixels=PopCharPixel(value,pixels);
1849 if (image->colorspace == CMYKColorspace)
1851 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1853 pixels=PopCharPixel(value,pixels);
1855 if (image->alpha_trait == BlendPixelTrait)
1857 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1859 pixels=PopCharPixel(value,pixels);
1864 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1865 "ImageDepthNotSupported","`%s'",image->filename);
1867 *pixels++=(unsigned char) length;
1871 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1872 Image *image,ExceptionInfo *exception)
1874 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1880 buffer[MaxTextExtent];
1889 #if defined(MAGICKCORE_LZMA_DELEGATE)
1894 initialize_lzma = LZMA_STREAM_INIT,
1929 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1935 Open output image file.
1937 assert(image_info != (const ImageInfo *) NULL);
1938 assert(image_info->signature == MagickSignature);
1939 assert(image != (Image *) NULL);
1940 assert(image->signature == MagickSignature);
1941 if (image->debug != MagickFalse)
1942 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1943 assert(exception != (ExceptionInfo *) NULL);
1944 assert(exception->signature == MagickSignature);
1945 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1946 if (status == MagickFalse)
1952 Allocate image pixels.
1954 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1955 image->depth <= 32 ? 32UL : 64UL;
1956 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1957 image->depth <= 32 ? 32UL : 64UL;
1958 quantum_info=AcquireQuantumInfo(image_info,image);
1959 if (quantum_info == (QuantumInfo *) NULL)
1960 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1961 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1962 (quantum_info->format == UndefinedQuantumFormat) &&
1963 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1965 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1966 if (status == MagickFalse)
1967 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1969 compression=image->compression;
1970 if (image_info->compression != UndefinedCompression)
1971 compression=image_info->compression;
1972 switch (compression)
1974 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1975 case LZMACompression: compression=NoCompression; break;
1977 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1978 case LZWCompression:
1979 case ZipCompression: compression=NoCompression; break;
1981 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1982 case BZipCompression: compression=NoCompression; break;
1984 case RLECompression:
1986 if (quantum_info->format == FloatingPointQuantumFormat)
1987 compression=NoCompression;
1988 GetPixelInfo(image,&target);
1994 packet_size=(size_t) (quantum_info->depth/8);
1995 if (image->storage_class == DirectClass)
1996 packet_size=(size_t) (3*quantum_info->depth/8);
1997 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1998 packet_size=(size_t) (quantum_info->depth/8);
1999 if (image->alpha_trait == BlendPixelTrait)
2000 packet_size+=quantum_info->depth/8;
2001 if (image->colorspace == CMYKColorspace)
2002 packet_size+=quantum_info->depth/8;
2003 if (compression == RLECompression)
2005 length=image->columns;
2006 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2007 packet_size*image->columns));
2008 if ((compression == BZipCompression) || (compression == ZipCompression))
2009 if (length != (size_t) ((unsigned int) length))
2010 compression=NoCompression;
2011 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2012 sizeof(*compress_pixels));
2013 if (compress_pixels == (unsigned char *) NULL)
2014 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2018 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2019 (void) FormatLocaleString(buffer,MaxTextExtent,
2020 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2021 MagickClassOptions,image->storage_class),(double) image->colors,
2022 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2023 image->alpha_trait));
2024 (void) WriteBlobString(image,buffer);
2025 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2026 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2028 (void) WriteBlobString(image,buffer);
2029 if (image->type != UndefinedType)
2031 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2032 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2033 (void) WriteBlobString(image,buffer);
2035 if (image->colorspace != UndefinedColorspace)
2037 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2038 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2039 (void) WriteBlobString(image,buffer);
2041 if (image->intensity != UndefinedPixelIntensityMethod)
2043 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
2044 CommandOptionToMnemonic(MagickPixelIntensityOptions,
2046 (void) WriteBlobString(image,buffer);
2048 if (image->endian != UndefinedEndian)
2050 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
2051 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2052 (void) WriteBlobString(image,buffer);
2054 if (compression != UndefinedCompression)
2056 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2057 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2058 compression),(double) image->quality);
2059 (void) WriteBlobString(image,buffer);
2061 if (image->units != UndefinedResolution)
2063 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2064 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2065 (void) WriteBlobString(image,buffer);
2067 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2069 (void) FormatLocaleString(buffer,MaxTextExtent,
2070 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2071 (void) WriteBlobString(image,buffer);
2073 if ((image->page.width != 0) || (image->page.height != 0))
2075 (void) FormatLocaleString(buffer,MaxTextExtent,
2076 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2077 image->page.height,(double) image->page.x,(double) image->page.y);
2078 (void) WriteBlobString(image,buffer);
2081 if ((image->page.x != 0) || (image->page.y != 0))
2083 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2084 (long) image->page.x,(long) image->page.y);
2085 (void) WriteBlobString(image,buffer);
2087 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2089 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2090 (long) image->tile_offset.x,(long) image->tile_offset.y);
2091 (void) WriteBlobString(image,buffer);
2093 if ((GetNextImageInList(image) != (Image *) NULL) ||
2094 (GetPreviousImageInList(image) != (Image *) NULL))
2096 if (image->scene == 0)
2097 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2098 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2099 (double) image->delay,(double) image->ticks_per_second);
2101 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2102 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2103 image->scene,(double) image->iterations,(double) image->delay,
2104 (double) image->ticks_per_second);
2105 (void) WriteBlobString(image,buffer);
2109 if (image->scene != 0)
2111 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2112 (double) image->scene);
2113 (void) WriteBlobString(image,buffer);
2115 if (image->iterations != 0)
2117 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2118 (double) image->iterations);
2119 (void) WriteBlobString(image,buffer);
2121 if (image->delay != 0)
2123 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2124 (double) image->delay);
2125 (void) WriteBlobString(image,buffer);
2127 if (image->ticks_per_second != UndefinedTicksPerSecond)
2129 (void) FormatLocaleString(buffer,MaxTextExtent,
2130 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2131 (void) WriteBlobString(image,buffer);
2134 if (image->gravity != UndefinedGravity)
2136 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2137 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2138 (void) WriteBlobString(image,buffer);
2140 if (image->dispose != UndefinedDispose)
2142 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2143 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2144 (void) WriteBlobString(image,buffer);
2146 if (image->rendering_intent != UndefinedIntent)
2148 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2149 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2150 (void) WriteBlobString(image,buffer);
2152 if (image->gamma != 0.0)
2154 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2156 (void) WriteBlobString(image,buffer);
2158 if (image->chromaticity.white_point.x != 0.0)
2161 Note chomaticity points.
2163 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2164 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2165 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2166 image->chromaticity.green_primary.x,
2167 image->chromaticity.green_primary.y,
2168 image->chromaticity.blue_primary.x,
2169 image->chromaticity.blue_primary.y);
2170 (void) WriteBlobString(image,buffer);
2171 (void) FormatLocaleString(buffer,MaxTextExtent,
2172 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2173 image->chromaticity.white_point.y);
2174 (void) WriteBlobString(image,buffer);
2176 if (image->orientation != UndefinedOrientation)
2178 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2179 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2180 (void) WriteBlobString(image,buffer);
2182 if (image->profiles != (void *) NULL)
2191 Write image profiles.
2193 ResetImageProfileIterator(image);
2194 name=GetNextImageProfile(image);
2195 while (name != (const char *) NULL)
2197 profile=GetImageProfile(image,name);
2198 if (profile != (StringInfo *) NULL)
2200 (void) FormatLocaleString(buffer,MaxTextExtent,
2201 "profile:%s=%.20g\n",name,(double)
2202 GetStringInfoLength(profile));
2203 (void) WriteBlobString(image,buffer);
2205 name=GetNextImageProfile(image);
2208 if (image->montage != (char *) NULL)
2210 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2212 (void) WriteBlobString(image,buffer);
2214 if (quantum_info->format == FloatingPointQuantumFormat)
2215 (void) SetImageProperty(image,"quantum:format","floating-point",
2217 ResetImagePropertyIterator(image);
2218 property=GetNextImageProperty(image);
2219 while (property != (const char *) NULL)
2221 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2222 (void) WriteBlobString(image,buffer);
2223 value=GetImageProperty(image,property,exception);
2224 if (value != (const char *) NULL)
2229 length=strlen(value);
2230 for (i=0; i < (ssize_t) length; i++)
2231 if (isspace((int) ((unsigned char) value[i])) != 0)
2233 if (i == (ssize_t) length)
2234 (void) WriteBlob(image,length,(const unsigned char *) value);
2237 (void) WriteBlobByte(image,'{');
2238 if (strchr(value,'}') == (char *) NULL)
2239 (void) WriteBlob(image,length,(const unsigned char *) value);
2241 for (i=0; i < (ssize_t) length; i++)
2243 if (value[i] == (int) '}')
2244 (void) WriteBlobByte(image,'\\');
2245 (void) WriteBlobByte(image,value[i]);
2247 (void) WriteBlobByte(image,'}');
2250 (void) WriteBlobByte(image,'\n');
2251 property=GetNextImageProperty(image);
2253 (void) WriteBlobString(image,"\f\n:\032");
2254 if (image->montage != (char *) NULL)
2257 Write montage tile directory.
2259 if (image->directory != (char *) NULL)
2260 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2262 (void) WriteBlobByte(image,'\0');
2264 if (image->profiles != (void *) NULL)
2275 ResetImageProfileIterator(image);
2276 name=GetNextImageProfile(image);
2277 while (name != (const char *) NULL)
2279 profile=GetImageProfile(image,name);
2280 (void) WriteBlob(image,GetStringInfoLength(profile),
2281 GetStringInfoDatum(profile));
2282 name=GetNextImageProfile(image);
2285 if (image->storage_class == PseudoClass)
2297 packet_size=(size_t) (3*quantum_info->depth/8);
2298 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2299 packet_size*sizeof(*colormap));
2300 if (colormap == (unsigned char *) NULL)
2301 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2303 Write colormap to file.
2306 for (i=0; i < (ssize_t) image->colors; i++)
2308 switch (quantum_info->depth)
2311 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2314 register unsigned int
2317 pixel=ScaleQuantumToLong(image->colormap[i].red);
2318 q=PopLongPixel(MSBEndian,pixel,q);
2319 pixel=ScaleQuantumToLong(image->colormap[i].green);
2320 q=PopLongPixel(MSBEndian,pixel,q);
2321 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2322 q=PopLongPixel(MSBEndian,pixel,q);
2327 register unsigned short
2330 pixel=ScaleQuantumToShort(image->colormap[i].red);
2331 q=PopShortPixel(MSBEndian,pixel,q);
2332 pixel=ScaleQuantumToShort(image->colormap[i].green);
2333 q=PopShortPixel(MSBEndian,pixel,q);
2334 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2335 q=PopShortPixel(MSBEndian,pixel,q);
2340 register unsigned char
2343 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2344 q=PopCharPixel(pixel,q);
2345 pixel=(unsigned char) ScaleQuantumToChar(
2346 image->colormap[i].green);
2347 q=PopCharPixel(pixel,q);
2348 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2349 q=PopCharPixel(pixel,q);
2354 (void) WriteBlob(image,packet_size*image->colors,colormap);
2355 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2358 Write image pixels to file.
2361 switch (compression)
2363 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2364 case BZipCompression:
2369 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2370 bzip_info.bzalloc=AcquireBZIPMemory;
2371 bzip_info.bzfree=RelinquishBZIPMemory;
2372 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2373 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2374 9)),(int) image_info->verbose,0);
2380 #if defined(MAGICKCORE_LZMA_DELEGATE)
2381 case LZMACompression:
2386 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2387 allocator.alloc=AcquireLZMAMemory;
2388 allocator.free=RelinquishLZMAMemory;
2389 lzma_info=initialize_lzma;
2390 lzma_info.allocator=&allocator;
2391 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2392 if (code != LZMA_OK)
2397 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2398 case LZWCompression:
2399 case ZipCompression:
2404 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2405 zip_info.zalloc=AcquireZIPMemory;
2406 zip_info.zfree=RelinquishZIPMemory;
2407 code=deflateInit(&zip_info,(int) (image->quality ==
2408 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2417 quantum_type=GetQuantumType(image,exception);
2418 pixels=GetQuantumPixels(quantum_info);
2419 for (y=0; y < (ssize_t) image->rows; y++)
2421 register const Quantum
2427 if (status == MagickFalse)
2429 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2430 if (p == (const Quantum *) NULL)
2433 switch (compression)
2435 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2436 case BZipCompression:
2438 bzip_info.next_in=(char *) pixels;
2439 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2440 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2441 quantum_type,pixels,exception);
2447 bzip_info.next_out=(char *) compress_pixels;
2448 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2450 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2453 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2456 (void) WriteBlobMSBLong(image,(unsigned int) length);
2457 (void) WriteBlob(image,length,compress_pixels);
2459 } while (bzip_info.avail_in != 0);
2463 #if defined(MAGICKCORE_LZMA_DELEGATE)
2464 case LZMACompression:
2466 lzma_info.next_in=pixels;
2467 lzma_info.avail_in=packet_size*image->columns;
2468 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2469 quantum_type,pixels,exception);
2475 lzma_info.next_out=compress_pixels;
2476 lzma_info.avail_out=packet_size*image->columns;
2477 code=lzma_code(&lzma_info,LZMA_RUN);
2478 if (code != LZMA_OK)
2480 length=(size_t) (lzma_info.next_out-compress_pixels);
2483 (void) WriteBlobMSBLong(image,(unsigned int) length);
2484 (void) WriteBlob(image,length,compress_pixels);
2486 } while (lzma_info.avail_in != 0);
2490 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2491 case LZWCompression:
2492 case ZipCompression:
2494 zip_info.next_in=pixels;
2495 zip_info.avail_in=(uInt) (packet_size*image->columns);
2496 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2497 quantum_type,pixels,exception);
2503 zip_info.next_out=compress_pixels;
2504 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2505 code=deflate(&zip_info,Z_SYNC_FLUSH);
2508 length=(size_t) (zip_info.next_out-compress_pixels);
2511 (void) WriteBlobMSBLong(image,(unsigned int) length);
2512 (void) WriteBlob(image,length,compress_pixels);
2514 } while (zip_info.avail_in != 0);
2518 case RLECompression:
2521 GetPixelInfoPixel(image,p,&pixel);
2522 p+=GetPixelChannels(image);
2523 for (x=1; x < (ssize_t) image->columns; x++)
2525 GetPixelInfoPixel(image,p,&target);
2526 if ((length < 255) &&
2527 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2531 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2534 GetPixelInfoPixel(image,p,&pixel);
2535 p+=GetPixelChannels(image);
2537 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2538 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2543 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2544 quantum_type,pixels,exception);
2545 (void) WriteBlob(image,packet_size*image->columns,pixels);
2549 if (image->previous == (Image *) NULL)
2551 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2553 if (status == MagickFalse)
2557 switch (compression)
2559 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2560 case BZipCompression:
2567 if (status == MagickFalse)
2569 bzip_info.next_out=(char *) compress_pixels;
2570 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2572 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2573 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2576 (void) WriteBlobMSBLong(image,(unsigned int) length);
2577 (void) WriteBlob(image,length,compress_pixels);
2579 if (code == BZ_STREAM_END)
2582 code=BZ2_bzCompressEnd(&bzip_info);
2588 #if defined(MAGICKCORE_LZMA_DELEGATE)
2589 case LZMACompression:
2596 if (status == MagickFalse)
2598 lzma_info.next_out=compress_pixels;
2599 lzma_info.avail_out=packet_size*image->columns;
2600 code=lzma_code(&lzma_info,LZMA_FINISH);
2601 length=(size_t) (lzma_info.next_out-compress_pixels);
2604 (void) WriteBlobMSBLong(image,(unsigned int) length);
2605 (void) WriteBlob(image,length,compress_pixels);
2607 if (code == LZMA_STREAM_END)
2610 lzma_end(&lzma_info);
2614 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2615 case LZWCompression:
2616 case ZipCompression:
2623 if (status == MagickFalse)
2625 zip_info.next_out=compress_pixels;
2626 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2627 code=deflate(&zip_info,Z_FINISH);
2628 length=(size_t) (zip_info.next_out-compress_pixels);
2631 (void) WriteBlobMSBLong(image,(unsigned int) length);
2632 (void) WriteBlob(image,length,compress_pixels);
2634 if (code == Z_STREAM_END)
2637 code=deflateEnd(&zip_info);
2646 quantum_info=DestroyQuantumInfo(quantum_info);
2647 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2648 if (GetNextImageInList(image) == (Image *) NULL)
2650 image=SyncNextImageInList(image);
2651 status=SetImageProgress(image,SaveImagesTag,scene++,
2652 GetImageListLength(image));
2653 if (status == MagickFalse)
2655 } while (image_info->adjoin != MagickFalse);
2656 (void) CloseBlob(image);