2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2015 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 void PushRunlengthPacket(Image *image,const unsigned char *pixels,
189 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
195 if (image->storage_class == PseudoClass)
198 switch (image->depth)
202 pixel->index=ConstrainColormapIndex(image,((size_t) *p << 24) |
203 ((size_t) *(p+1) << 16) | ((size_t) *(p+2) << 8) | (size_t) *(p+3),
210 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
217 pixel->index=ConstrainColormapIndex(image,*p,exception);
222 (void) ThrowMagickException(exception,GetMagickModule(),
223 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
225 switch (image->depth)
232 if (image->alpha_trait != UndefinedPixelTrait)
234 p=PushCharPixel(p,&quantum);
235 pixel->alpha=ScaleCharToQuantum(quantum);
244 if (image->alpha_trait != UndefinedPixelTrait)
246 p=PushShortPixel(MSBEndian,p,&quantum);
247 pixel->alpha=(Quantum) (quantum >> (image->depth-
248 MAGICKCORE_QUANTUM_DEPTH));
257 if (image->alpha_trait != UndefinedPixelTrait)
259 p=PushLongPixel(MSBEndian,p,&quantum);
260 pixel->alpha=(Quantum) (quantum >> (image->depth-
261 MAGICKCORE_QUANTUM_DEPTH));
266 (void) ThrowMagickException(exception,GetMagickModule(),
267 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
269 *length=(size_t) (*p++)+1;
272 switch (image->depth)
279 p=PushCharPixel(p,&quantum);
280 pixel->red=ScaleCharToQuantum(quantum);
281 pixel->green=pixel->red;
282 pixel->blue=pixel->red;
283 if (IsGrayColorspace(image->colorspace) == MagickFalse)
285 p=PushCharPixel(p,&quantum);
286 pixel->green=ScaleCharToQuantum(quantum);
287 p=PushCharPixel(p,&quantum);
288 pixel->blue=ScaleCharToQuantum(quantum);
290 if (image->colorspace == CMYKColorspace)
292 p=PushCharPixel(p,&quantum);
293 pixel->black=ScaleCharToQuantum(quantum);
295 if (image->alpha_trait != UndefinedPixelTrait)
297 p=PushCharPixel(p,&quantum);
298 pixel->alpha=ScaleCharToQuantum(quantum);
307 p=PushShortPixel(MSBEndian,p,&quantum);
308 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
309 pixel->green=pixel->red;
310 pixel->blue=pixel->red;
311 if (IsGrayColorspace(image->colorspace) == MagickFalse)
313 p=PushShortPixel(MSBEndian,p,&quantum);
314 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
315 p=PushShortPixel(MSBEndian,p,&quantum);
316 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
318 if (image->colorspace == CMYKColorspace)
320 p=PushShortPixel(MSBEndian,p,&quantum);
321 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
323 if (image->alpha_trait != UndefinedPixelTrait)
325 p=PushShortPixel(MSBEndian,p,&quantum);
326 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
335 p=PushLongPixel(MSBEndian,p,&quantum);
336 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
337 pixel->green=pixel->red;
338 pixel->blue=pixel->red;
339 if (IsGrayColorspace(image->colorspace) == MagickFalse)
341 p=PushLongPixel(MSBEndian,p,&quantum);
342 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
343 p=PushLongPixel(MSBEndian,p,&quantum);
344 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
346 if (image->colorspace == CMYKColorspace)
348 p=PushLongPixel(MSBEndian,p,&quantum);
349 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
351 if (image->alpha_trait != UndefinedPixelTrait)
353 p=PushLongPixel(MSBEndian,p,&quantum);
354 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
359 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
360 "ImageDepthNotSupported","`%s'",image->filename);
362 *length=(size_t) (*p++)+1;
365 #if defined(MAGICKCORE_BZLIB_DELEGATE)
366 static void RelinquishBZIPMemory(void *context,void *memory)
369 memory=RelinquishMagickMemory(memory);
373 #if defined(MAGICKCORE_LZMA_DELEGATE)
374 static void RelinquishLZMAMemory(void *context,void *memory)
377 memory=RelinquishMagickMemory(memory);
381 #if defined(MAGICKCORE_ZLIB_DELEGATE)
382 static void RelinquishZIPMemory(voidpf context,voidpf memory)
385 memory=RelinquishMagickMemory(memory);
389 static Image *ReadMIFFImage(const ImageInfo *image_info,
390 ExceptionInfo *exception)
392 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
393 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
394 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
396 #if defined(MAGICKCORE_BZLIB_DELEGATE)
403 keyword[MaxTextExtent],
424 #if defined(MAGICKCORE_LZMA_DELEGATE)
426 initialize_lzma = LZMA_STREAM_INIT,
471 #if defined(MAGICKCORE_ZLIB_DELEGATE)
479 assert(image_info != (const ImageInfo *) NULL);
480 assert(image_info->signature == MagickSignature);
481 if (image_info->debug != MagickFalse)
482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
483 image_info->filename);
484 assert(exception != (ExceptionInfo *) NULL);
485 assert(exception->signature == MagickSignature);
486 image=AcquireImage(image_info,exception);
487 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
488 if (status == MagickFalse)
490 image=DestroyImageList(image);
491 return((Image *) NULL);
494 Decode image header; header terminates one character beyond a ':'.
496 c=ReadBlobByte(image);
498 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
500 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
506 Decode image header; header terminates one character beyond a ':'.
508 length=MaxTextExtent;
509 options=AcquireString((char *) NULL);
510 quantum_format=UndefinedQuantumFormat;
511 profiles=(LinkedListInfo *) NULL;
514 image->compression=NoCompression;
515 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
526 Read comment-- any text between { }.
528 length=MaxTextExtent;
529 comment=AcquireString((char *) NULL);
530 for (p=comment; comment != (char *) NULL; p++)
532 c=ReadBlobByte(image);
534 c=ReadBlobByte(image);
536 if ((c == EOF) || (c == (int) '}'))
538 if ((size_t) (p-comment+1) >= length)
542 comment=(char *) ResizeQuantumMemory(comment,length+
543 MaxTextExtent,sizeof(*comment));
544 if (comment == (char *) NULL)
546 p=comment+strlen(comment);
550 if (comment == (char *) NULL)
551 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
553 (void) SetImageProperty(image,"comment",comment,exception);
554 comment=DestroyString(comment);
555 c=ReadBlobByte(image);
558 if (isalnum(c) != MagickFalse)
568 if ((size_t) (p-keyword) < (MaxTextExtent-1))
570 c=ReadBlobByte(image);
574 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
575 c=ReadBlobByte(image);
579 Get the keyword value.
581 c=ReadBlobByte(image);
582 while ((c != (int) '}') && (c != EOF))
584 if ((size_t) (p-options+1) >= length)
588 options=(char *) ResizeQuantumMemory(options,length+
589 MaxTextExtent,sizeof(*options));
590 if (options == (char *) NULL)
592 p=options+strlen(options);
594 if (options == (char *) NULL)
595 ThrowReaderException(ResourceLimitError,
596 "MemoryAllocationFailed");
598 c=ReadBlobByte(image);
601 c=ReadBlobByte(image);
605 c=ReadBlobByte(image);
609 if (isspace((int) ((unsigned char) c)) != 0)
614 (void) CopyMagickString(options,options+1,strlen(options));
616 Assign a value to the specified keyword.
623 if (LocaleCompare(keyword,"alpha-trait") == 0)
628 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
629 MagickFalse,options);
632 image->alpha_trait=(PixelTrait) alpha_trait;
635 (void) SetImageProperty(image,keyword,options,exception);
641 if (LocaleCompare(keyword,"background-color") == 0)
643 (void) QueryColorCompliance(options,AllCompliance,
644 &image->background_color,exception);
647 if (LocaleCompare(keyword,"blue-primary") == 0)
649 flags=ParseGeometry(options,&geometry_info);
650 image->chromaticity.blue_primary.x=geometry_info.rho;
651 image->chromaticity.blue_primary.y=geometry_info.sigma;
652 if ((flags & SigmaValue) == 0)
653 image->chromaticity.blue_primary.y=
654 image->chromaticity.blue_primary.x;
657 if (LocaleCompare(keyword,"border-color") == 0)
659 (void) QueryColorCompliance(options,AllCompliance,
660 &image->border_color,exception);
663 (void) SetImageProperty(image,keyword,options,exception);
669 if (LocaleCompare(keyword,"class") == 0)
674 storage_class=ParseCommandOption(MagickClassOptions,
675 MagickFalse,options);
676 if (storage_class < 0)
678 image->storage_class=(ClassType) storage_class;
681 if (LocaleCompare(keyword,"colors") == 0)
683 colors=StringToUnsignedLong(options);
686 if (LocaleCompare(keyword,"colorspace") == 0)
691 colorspace=ParseCommandOption(MagickColorspaceOptions,
692 MagickFalse,options);
695 image->colorspace=(ColorspaceType) colorspace;
698 if (LocaleCompare(keyword,"compression") == 0)
703 compression=ParseCommandOption(MagickCompressOptions,
704 MagickFalse,options);
707 image->compression=(CompressionType) compression;
710 if (LocaleCompare(keyword,"columns") == 0)
712 image->columns=StringToUnsignedLong(options);
715 (void) SetImageProperty(image,keyword,options,exception);
721 if (LocaleCompare(keyword,"delay") == 0)
723 image->delay=StringToUnsignedLong(options);
726 if (LocaleCompare(keyword,"depth") == 0)
728 image->depth=StringToUnsignedLong(options);
731 if (LocaleCompare(keyword,"dispose") == 0)
736 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
740 image->dispose=(DisposeType) dispose;
743 (void) SetImageProperty(image,keyword,options,exception);
749 if (LocaleCompare(keyword,"endian") == 0)
754 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
758 image->endian=(EndianType) endian;
761 (void) SetImageProperty(image,keyword,options,exception);
767 if (LocaleCompare(keyword,"gamma") == 0)
769 image->gamma=StringToDouble(options,(char **) NULL);
772 if (LocaleCompare(keyword,"gravity") == 0)
777 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
781 image->gravity=(GravityType) gravity;
784 if (LocaleCompare(keyword,"green-primary") == 0)
786 flags=ParseGeometry(options,&geometry_info);
787 image->chromaticity.green_primary.x=geometry_info.rho;
788 image->chromaticity.green_primary.y=geometry_info.sigma;
789 if ((flags & SigmaValue) == 0)
790 image->chromaticity.green_primary.y=
791 image->chromaticity.green_primary.x;
794 (void) SetImageProperty(image,keyword,options,exception);
800 if (LocaleCompare(keyword,"id") == 0)
802 (void) CopyMagickString(id,options,MaxTextExtent);
805 if (LocaleCompare(keyword,"iterations") == 0)
807 image->iterations=StringToUnsignedLong(options);
810 (void) SetImageProperty(image,keyword,options,exception);
816 if (LocaleCompare(keyword,"matte") == 0)
821 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
825 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
829 if (LocaleCompare(keyword,"matte-color") == 0)
831 (void) QueryColorCompliance(options,AllCompliance,
832 &image->matte_color,exception);
835 if (LocaleCompare(keyword,"montage") == 0)
837 (void) CloneString(&image->montage,options);
840 (void) SetImageProperty(image,keyword,options,exception);
846 if (LocaleCompare(keyword,"orientation") == 0)
851 orientation=ParseCommandOption(MagickOrientationOptions,
852 MagickFalse,options);
855 image->orientation=(OrientationType) orientation;
858 (void) SetImageProperty(image,keyword,options,exception);
864 if (LocaleCompare(keyword,"page") == 0)
869 geometry=GetPageGeometry(options);
870 (void) ParseAbsoluteGeometry(geometry,&image->page);
871 geometry=DestroyString(geometry);
874 if (LocaleCompare(keyword,"pixel-intensity") == 0)
879 intensity=ParseCommandOption(MagickPixelIntensityOptions,
880 MagickFalse,options);
883 image->intensity=(PixelIntensityMethod) intensity;
886 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
887 (LocaleNCompare(keyword,"profile-",8) == 0))
892 if (profiles == (LinkedListInfo *) NULL)
893 profiles=NewLinkedList(0);
894 (void) AppendValueToLinkedList(profiles,
895 AcquireString(keyword+8));
896 profile=BlobToStringInfo((const void *) NULL,(size_t)
897 StringToLong(options));
898 if (profile == (StringInfo *) NULL)
899 ThrowReaderException(ResourceLimitError,
900 "MemoryAllocationFailed");
901 (void) SetImageProfile(image,keyword+8,profile,exception);
902 profile=DestroyStringInfo(profile);
905 (void) SetImageProperty(image,keyword,options,exception);
911 if (LocaleCompare(keyword,"quality") == 0)
913 image->quality=StringToUnsignedLong(options);
916 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
917 (LocaleCompare(keyword,"quantum:format") == 0))
922 format=ParseCommandOption(MagickQuantumFormatOptions,
923 MagickFalse,options);
926 quantum_format=(QuantumFormatType) format;
929 (void) SetImageProperty(image,keyword,options,exception);
935 if (LocaleCompare(keyword,"red-primary") == 0)
937 flags=ParseGeometry(options,&geometry_info);
938 image->chromaticity.red_primary.x=geometry_info.rho;
939 image->chromaticity.red_primary.y=geometry_info.sigma;
940 if ((flags & SigmaValue) == 0)
941 image->chromaticity.red_primary.y=
942 image->chromaticity.red_primary.x;
945 if (LocaleCompare(keyword,"rendering-intent") == 0)
950 rendering_intent=ParseCommandOption(MagickIntentOptions,
951 MagickFalse,options);
952 if (rendering_intent < 0)
954 image->rendering_intent=(RenderingIntent) rendering_intent;
957 if (LocaleCompare(keyword,"resolution") == 0)
959 flags=ParseGeometry(options,&geometry_info);
960 image->resolution.x=geometry_info.rho;
961 image->resolution.y=geometry_info.sigma;
962 if ((flags & SigmaValue) == 0)
963 image->resolution.y=image->resolution.x;
966 if (LocaleCompare(keyword,"rows") == 0)
968 image->rows=StringToUnsignedLong(options);
971 (void) SetImageProperty(image,keyword,options,exception);
977 if (LocaleCompare(keyword,"scene") == 0)
979 image->scene=StringToUnsignedLong(options);
982 (void) SetImageProperty(image,keyword,options,exception);
988 if (LocaleCompare(keyword,"ticks-per-second") == 0)
990 image->ticks_per_second=(ssize_t) StringToLong(options);
993 if (LocaleCompare(keyword,"tile-offset") == 0)
998 geometry=GetPageGeometry(options);
999 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1000 geometry=DestroyString(geometry);
1003 if (LocaleCompare(keyword,"type") == 0)
1008 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1012 image->type=(ImageType) type;
1015 (void) SetImageProperty(image,keyword,options,exception);
1021 if (LocaleCompare(keyword,"units") == 0)
1026 units=ParseCommandOption(MagickResolutionOptions,
1027 MagickFalse,options);
1030 image->units=(ResolutionType) units;
1033 (void) SetImageProperty(image,keyword,options,exception);
1039 if (LocaleCompare(keyword,"version") == 0)
1041 version=StringToDouble(options,(char **) NULL);
1044 (void) SetImageProperty(image,keyword,options,exception);
1050 if (LocaleCompare(keyword,"white-point") == 0)
1052 flags=ParseGeometry(options,&geometry_info);
1053 image->chromaticity.white_point.x=geometry_info.rho;
1054 image->chromaticity.white_point.y=geometry_info.sigma;
1055 if ((flags & SigmaValue) == 0)
1056 image->chromaticity.white_point.y=
1057 image->chromaticity.white_point.x;
1060 (void) SetImageProperty(image,keyword,options,exception);
1065 (void) SetImageProperty(image,keyword,options,exception);
1071 c=ReadBlobByte(image);
1072 while (isspace((int) ((unsigned char) c)) != 0)
1073 c=ReadBlobByte(image);
1075 options=DestroyString(options);
1076 (void) ReadBlobByte(image);
1078 Verify that required image information is defined.
1080 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1081 (image->storage_class == UndefinedClass) ||
1082 (image->columns == 0) || (image->rows == 0))
1083 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1084 if (image->montage != (char *) NULL)
1092 length=MaxTextExtent;
1093 image->directory=AcquireString((char *) NULL);
1098 if ((strlen(image->directory)+MaxTextExtent) >= length)
1101 Allocate more memory for the image directory.
1104 image->directory=(char *) ResizeQuantumMemory(image->directory,
1105 length+MaxTextExtent,sizeof(*image->directory));
1106 if (image->directory == (char *) NULL)
1107 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1108 p=image->directory+strlen(image->directory);
1110 c=ReadBlobByte(image);
1112 } while (c != (int) '\0');
1114 if (profiles != (LinkedListInfo *) NULL)
1123 Read image profiles.
1125 ResetLinkedListIterator(profiles);
1126 name=(const char *) GetNextValueInLinkedList(profiles);
1127 while (name != (const char *) NULL)
1129 profile=GetImageProfile(image,name);
1130 if (profile != (StringInfo *) NULL)
1132 register unsigned char
1135 p=GetStringInfoDatum(profile);
1136 count=ReadBlob(image,GetStringInfoLength(profile),p);
1139 name=(const char *) GetNextValueInLinkedList(profiles);
1141 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1143 image->depth=GetImageQuantumDepth(image,MagickFalse);
1144 if (image->storage_class == PseudoClass)
1147 Create image colormap.
1149 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1150 if (status == MagickFalse)
1151 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1161 Read image colormap from file.
1163 packet_size=(size_t) (3UL*image->depth/8UL);
1164 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1165 packet_size*sizeof(*colormap));
1166 if (colormap == (unsigned char *) NULL)
1167 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1168 count=ReadBlob(image,packet_size*image->colors,colormap);
1170 switch (image->depth)
1173 ThrowReaderException(CorruptImageError,
1174 "ImageDepthNotSupported");
1180 for (i=0; i < (ssize_t) image->colors; i++)
1182 p=PushCharPixel(p,&pixel);
1183 image->colormap[i].red=ScaleCharToQuantum(pixel);
1184 p=PushCharPixel(p,&pixel);
1185 image->colormap[i].green=ScaleCharToQuantum(pixel);
1186 p=PushCharPixel(p,&pixel);
1187 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1196 for (i=0; i < (ssize_t) image->colors; i++)
1198 p=PushShortPixel(MSBEndian,p,&pixel);
1199 image->colormap[i].red=ScaleShortToQuantum(pixel);
1200 p=PushShortPixel(MSBEndian,p,&pixel);
1201 image->colormap[i].green=ScaleShortToQuantum(pixel);
1202 p=PushShortPixel(MSBEndian,p,&pixel);
1203 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1212 for (i=0; i < (ssize_t) image->colors; i++)
1214 p=PushLongPixel(MSBEndian,p,&pixel);
1215 image->colormap[i].red=ScaleLongToQuantum(pixel);
1216 p=PushLongPixel(MSBEndian,p,&pixel);
1217 image->colormap[i].green=ScaleLongToQuantum(pixel);
1218 p=PushLongPixel(MSBEndian,p,&pixel);
1219 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1224 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1227 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1228 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1230 status=SetImageExtent(image,image->columns,image->rows,exception);
1231 if (status == MagickFalse)
1232 return(DestroyImageList(image));
1234 Allocate image pixels.
1236 quantum_info=AcquireQuantumInfo(image_info,image);
1237 if (quantum_info == (QuantumInfo *) NULL)
1238 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1239 if (quantum_format != UndefinedQuantumFormat)
1241 status=SetQuantumFormat(image,quantum_info,quantum_format);
1242 if (status == MagickFalse)
1243 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1245 packet_size=(size_t) (quantum_info->depth/8);
1246 if (image->storage_class == DirectClass)
1247 packet_size=(size_t) (3*quantum_info->depth/8);
1248 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1249 packet_size=quantum_info->depth/8;
1250 if (image->alpha_trait != UndefinedPixelTrait)
1251 packet_size+=quantum_info->depth/8;
1252 if (image->colorspace == CMYKColorspace)
1253 packet_size+=quantum_info->depth/8;
1254 if (image->compression == RLECompression)
1256 length=image->columns;
1257 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1258 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1260 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1261 sizeof(*compress_pixels));
1262 if (compress_pixels == (unsigned char *) NULL)
1263 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1267 quantum_type=RGBQuantum;
1268 if (image->alpha_trait != UndefinedPixelTrait)
1269 quantum_type=RGBAQuantum;
1270 if (image->colorspace == CMYKColorspace)
1272 quantum_type=CMYKQuantum;
1273 if (image->alpha_trait != UndefinedPixelTrait)
1274 quantum_type=CMYKAQuantum;
1276 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1278 quantum_type=GrayQuantum;
1279 if (image->alpha_trait != UndefinedPixelTrait)
1280 quantum_type=GrayAlphaQuantum;
1282 if (image->storage_class == PseudoClass)
1284 quantum_type=IndexQuantum;
1285 if (image->alpha_trait != UndefinedPixelTrait)
1286 quantum_type=IndexAlphaQuantum;
1289 GetPixelInfo(image,&pixel);
1290 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1291 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1293 #if defined(MAGICKCORE_LZMA_DELEGATE)
1294 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1296 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1297 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1299 switch (image->compression)
1301 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1302 case BZipCompression:
1307 bzip_info.bzalloc=AcquireBZIPMemory;
1308 bzip_info.bzfree=RelinquishBZIPMemory;
1309 bzip_info.opaque=(void *) NULL;
1310 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1317 #if defined(MAGICKCORE_LZMA_DELEGATE)
1318 case LZMACompression:
1323 allocator.alloc=AcquireLZMAMemory;
1324 allocator.free=RelinquishLZMAMemory;
1325 lzma_info=initialize_lzma;
1326 lzma_info.allocator=(&allocator);
1327 code=lzma_auto_decoder(&lzma_info,-1,0);
1328 if (code != LZMA_OK)
1333 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1334 case LZWCompression:
1335 case ZipCompression:
1340 zip_info.zalloc=AcquireZIPMemory;
1341 zip_info.zfree=RelinquishZIPMemory;
1342 zip_info.opaque=(voidpf) NULL;
1343 code=inflateInit(&zip_info);
1349 case RLECompression:
1354 pixels=GetQuantumPixels(quantum_info);
1356 for (y=0; y < (ssize_t) image->rows; y++)
1364 if (status == MagickFalse)
1366 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1367 if (q == (Quantum *) NULL)
1369 switch (image->compression)
1371 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1372 case BZipCompression:
1374 bzip_info.next_out=(char *) pixels;
1375 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1378 if (bzip_info.avail_in == 0)
1380 bzip_info.next_in=(char *) compress_pixels;
1381 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1383 length=(size_t) ReadBlobMSBLong(image);
1384 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1385 (unsigned char *) bzip_info.next_in);
1387 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1389 } while (bzip_info.avail_out != 0);
1390 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1391 quantum_type,pixels,exception);
1395 #if defined(MAGICKCORE_LZMA_DELEGATE)
1396 case LZMACompression:
1398 lzma_info.next_out=pixels;
1399 lzma_info.avail_out=packet_size*image->columns;
1405 if (lzma_info.avail_in == 0)
1407 lzma_info.next_in=compress_pixels;
1408 length=(size_t) ReadBlobMSBLong(image);
1409 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1410 (unsigned char *) lzma_info.next_in);
1412 code=lzma_code(&lzma_info,LZMA_RUN);
1418 if (code == LZMA_STREAM_END)
1420 } while (lzma_info.avail_out != 0);
1421 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1422 quantum_type,pixels,exception);
1426 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1427 case LZWCompression:
1428 case ZipCompression:
1430 zip_info.next_out=pixels;
1431 zip_info.avail_out=(uInt) (packet_size*image->columns);
1434 if (zip_info.avail_in == 0)
1436 zip_info.next_in=compress_pixels;
1437 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1439 length=(size_t) ReadBlobMSBLong(image);
1440 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1443 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1445 } while (zip_info.avail_out != 0);
1446 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1447 quantum_type,pixels,exception);
1451 case RLECompression:
1453 for (x=0; x < (ssize_t) image->columns; x++)
1457 count=ReadBlob(image,packet_size,pixels);
1458 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1461 if (image->storage_class == PseudoClass)
1462 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1465 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1466 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1467 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1468 if (image->colorspace == CMYKColorspace)
1469 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1471 if (image->alpha_trait != UndefinedPixelTrait)
1472 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1473 q+=GetPixelChannels(image);
1479 count=ReadBlob(image,packet_size*image->columns,pixels);
1480 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1481 quantum_type,pixels,exception);
1485 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1488 SetQuantumImageType(image,quantum_type);
1489 switch (image->compression)
1491 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1492 case BZipCompression:
1502 offset=SeekBlob(image,-((MagickOffsetType)
1503 bzip_info.avail_in),SEEK_CUR);
1505 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1507 code=BZ2_bzDecompressEnd(&bzip_info);
1513 #if defined(MAGICKCORE_LZMA_DELEGATE)
1514 case LZMACompression:
1519 code=lzma_code(&lzma_info,LZMA_FINISH);
1520 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1522 lzma_end(&lzma_info);
1526 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1527 case LZWCompression:
1528 case ZipCompression:
1538 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1541 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1543 code=inflateEnd(&zip_info);
1544 if (code != LZMA_OK)
1552 quantum_info=DestroyQuantumInfo(quantum_info);
1553 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1554 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1556 image=DestroyImageList(image);
1557 return((Image *) NULL);
1559 if (EOFBlob(image) != MagickFalse)
1561 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1566 Proceed to next image.
1568 if (image_info->number_scenes != 0)
1569 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1573 c=ReadBlobByte(image);
1574 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1578 Allocate next image structure.
1580 AcquireNextImage(image_info,image,exception);
1581 if (GetNextImageInList(image) == (Image *) NULL)
1583 image=DestroyImageList(image);
1584 return((Image *) NULL);
1586 image=SyncNextImageInList(image);
1587 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1588 GetBlobSize(image));
1589 if (status == MagickFalse)
1593 (void) CloseBlob(image);
1594 return(GetFirstImageInList(image));
1598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602 % R e g i s t e r M I F F I m a g e %
1606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1609 % supported formats. The properties include the image format tag, a method to
1610 % read and/or write the format, whether the format supports the saving of more
1611 % than one frame to the same file or blob, whether the format supports native
1612 % in-memory I/O, and a brief description of the format.
1614 % The format of the RegisterMIFFImage method is:
1616 % size_t RegisterMIFFImage(void)
1619 ModuleExport size_t RegisterMIFFImage(void)
1622 version[MaxTextExtent];
1628 #if defined(MagickImageCoderSignatureText)
1629 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1630 #if defined(ZLIB_VERSION)
1631 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1632 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1634 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1635 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1638 entry=SetMagickInfo("MIFF");
1639 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1640 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1641 entry->magick=(IsImageFormatHandler *) IsMIFF;
1642 entry->seekable_stream=MagickTrue;
1643 entry->description=ConstantString("Magick Image File Format");
1644 if (*version != '\0')
1645 entry->version=ConstantString(version);
1646 entry->module=ConstantString("MIFF");
1647 (void) RegisterMagickInfo(entry);
1648 return(MagickImageCoderSignature);
1652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1656 % U n r e g i s t e r M I F F I m a g e %
1660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1662 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1663 % from the list of supported formats.
1665 % The format of the UnregisterMIFFImage method is:
1667 % UnregisterMIFFImage(void)
1670 ModuleExport void UnregisterMIFFImage(void)
1672 (void) UnregisterMagickInfo("MIFF");
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1680 % W r i t e M I F F I m a g e %
1684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1686 % WriteMIFFImage() writes a MIFF image to a file.
1688 % The format of the WriteMIFFImage method is:
1690 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1691 % Image *image,ExceptionInfo *exception)
1693 % Compression code contributed by Kyle Shorter.
1695 % A description of each parameter follows:
1697 % o image_info: the image info.
1699 % o image: the image.
1701 % o exception: return any errors or warnings in this structure.
1705 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1706 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1708 if (image->storage_class != DirectClass)
1713 value=(unsigned int) ClampToQuantum(pixel->index);
1714 switch (image->depth)
1718 *pixels++=(unsigned char) (value >> 24);
1719 *pixels++=(unsigned char) (value >> 16);
1722 *pixels++=(unsigned char) (value >> 8);
1725 *pixels++=(unsigned char) value;
1729 (void) ThrowMagickException(exception,GetMagickModule(),
1730 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1732 switch (image->depth)
1739 if (image->alpha_trait != UndefinedPixelTrait)
1741 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1742 pixels=PopLongPixel(MSBEndian,value,pixels);
1751 if (image->alpha_trait != UndefinedPixelTrait)
1753 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1754 pixels=PopShortPixel(MSBEndian,value,pixels);
1763 if (image->alpha_trait != UndefinedPixelTrait)
1765 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1767 pixels=PopCharPixel(value,pixels);
1772 (void) ThrowMagickException(exception,GetMagickModule(),
1773 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1775 *pixels++=(unsigned char) length;
1778 switch (image->depth)
1785 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1786 pixels=PopLongPixel(MSBEndian,value,pixels);
1787 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1789 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1790 pixels=PopLongPixel(MSBEndian,value,pixels);
1791 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1792 pixels=PopLongPixel(MSBEndian,value,pixels);
1794 if (image->colorspace == CMYKColorspace)
1796 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1797 pixels=PopLongPixel(MSBEndian,value,pixels);
1799 if (image->alpha_trait != UndefinedPixelTrait)
1801 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1802 pixels=PopLongPixel(MSBEndian,value,pixels);
1811 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1812 pixels=PopShortPixel(MSBEndian,value,pixels);
1813 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1815 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1816 pixels=PopShortPixel(MSBEndian,value,pixels);
1817 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1818 pixels=PopShortPixel(MSBEndian,value,pixels);
1820 if (image->colorspace == CMYKColorspace)
1822 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1823 pixels=PopShortPixel(MSBEndian,value,pixels);
1825 if (image->alpha_trait != UndefinedPixelTrait)
1827 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1828 pixels=PopShortPixel(MSBEndian,value,pixels);
1837 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1838 pixels=PopCharPixel(value,pixels);
1839 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1841 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1843 pixels=PopCharPixel(value,pixels);
1844 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1845 pixels=PopCharPixel(value,pixels);
1847 if (image->colorspace == CMYKColorspace)
1849 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1851 pixels=PopCharPixel(value,pixels);
1853 if (image->alpha_trait != UndefinedPixelTrait)
1855 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1857 pixels=PopCharPixel(value,pixels);
1862 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1863 "ImageDepthNotSupported","`%s'",image->filename);
1865 *pixels++=(unsigned char) length;
1869 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1870 Image *image,ExceptionInfo *exception)
1872 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1878 buffer[MaxTextExtent];
1887 #if defined(MAGICKCORE_LZMA_DELEGATE)
1892 initialize_lzma = LZMA_STREAM_INIT,
1927 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1933 Open output image file.
1935 assert(image_info != (const ImageInfo *) NULL);
1936 assert(image_info->signature == MagickSignature);
1937 assert(image != (Image *) NULL);
1938 assert(image->signature == MagickSignature);
1939 if (image->debug != MagickFalse)
1940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1941 assert(exception != (ExceptionInfo *) NULL);
1942 assert(exception->signature == MagickSignature);
1943 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1944 if (status == MagickFalse)
1950 Allocate image pixels.
1952 if ((image->storage_class == PseudoClass) &&
1953 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1954 (void) SetImageStorageClass(image,DirectClass,exception);
1955 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1956 image->depth <= 32 ? 32UL : 64UL;
1957 if (IsImageGray(image,exception) == MagickFalse)
1960 sRGB masquerading as a grayscale image?
1962 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1963 (void) SetImageColorspace(image,sRGBColorspace,exception);
1966 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1967 (void) SetImageColorspace(image,GRAYColorspace,exception);
1968 quantum_info=AcquireQuantumInfo(image_info,image);
1969 if (quantum_info == (QuantumInfo *) NULL)
1970 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1971 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1972 (quantum_info->format == UndefinedQuantumFormat) &&
1973 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1975 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1976 if (status == MagickFalse)
1977 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1979 compression=UndefinedCompression;
1980 if (image_info->compression != UndefinedCompression)
1981 compression=image_info->compression;
1982 switch (compression)
1984 #if !defined(MAGICKCORE_LZMA_DELEGATE)
1985 case LZMACompression: compression=NoCompression; break;
1987 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1988 case LZWCompression:
1989 case ZipCompression: compression=NoCompression; break;
1991 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1992 case BZipCompression: compression=NoCompression; break;
1994 case RLECompression:
1996 if (quantum_info->format == FloatingPointQuantumFormat)
1997 compression=NoCompression;
1998 GetPixelInfo(image,&target);
2004 packet_size=(size_t) (quantum_info->depth/8);
2005 if (image->storage_class == DirectClass)
2006 packet_size=(size_t) (3*quantum_info->depth/8);
2007 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2008 packet_size=(size_t) (quantum_info->depth/8);
2009 if (image->alpha_trait != UndefinedPixelTrait)
2010 packet_size+=quantum_info->depth/8;
2011 if (image->colorspace == CMYKColorspace)
2012 packet_size+=quantum_info->depth/8;
2013 if (compression == RLECompression)
2015 length=image->columns;
2016 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2017 packet_size*image->columns));
2018 if ((compression == BZipCompression) || (compression == ZipCompression))
2019 if (length != (size_t) ((unsigned int) length))
2020 compression=NoCompression;
2021 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2022 sizeof(*compress_pixels));
2023 if (compress_pixels == (unsigned char *) NULL)
2024 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2028 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2029 (void) FormatLocaleString(buffer,MaxTextExtent,
2030 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2031 MagickClassOptions,image->storage_class),(double) image->colors,
2032 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2033 image->alpha_trait));
2034 (void) WriteBlobString(image,buffer);
2035 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2036 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2038 (void) WriteBlobString(image,buffer);
2039 if (image->type != UndefinedType)
2041 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2042 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2043 (void) WriteBlobString(image,buffer);
2045 if (image->colorspace != UndefinedColorspace)
2047 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2048 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2049 (void) WriteBlobString(image,buffer);
2051 if (image->intensity != UndefinedPixelIntensityMethod)
2053 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
2054 CommandOptionToMnemonic(MagickPixelIntensityOptions,
2056 (void) WriteBlobString(image,buffer);
2058 if (image->endian != UndefinedEndian)
2060 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
2061 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2062 (void) WriteBlobString(image,buffer);
2064 if (compression != UndefinedCompression)
2066 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2067 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2068 compression),(double) image->quality);
2069 (void) WriteBlobString(image,buffer);
2071 if (image->units != UndefinedResolution)
2073 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2074 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2075 (void) WriteBlobString(image,buffer);
2077 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2079 (void) FormatLocaleString(buffer,MaxTextExtent,
2080 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2081 (void) WriteBlobString(image,buffer);
2083 if ((image->page.width != 0) || (image->page.height != 0))
2085 (void) FormatLocaleString(buffer,MaxTextExtent,
2086 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2087 image->page.height,(double) image->page.x,(double) image->page.y);
2088 (void) WriteBlobString(image,buffer);
2091 if ((image->page.x != 0) || (image->page.y != 0))
2093 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2094 (long) image->page.x,(long) image->page.y);
2095 (void) WriteBlobString(image,buffer);
2097 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2099 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2100 (long) image->tile_offset.x,(long) image->tile_offset.y);
2101 (void) WriteBlobString(image,buffer);
2103 if ((GetNextImageInList(image) != (Image *) NULL) ||
2104 (GetPreviousImageInList(image) != (Image *) NULL))
2106 if (image->scene == 0)
2107 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2108 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2109 (double) image->delay,(double) image->ticks_per_second);
2111 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2112 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2113 image->scene,(double) image->iterations,(double) image->delay,
2114 (double) image->ticks_per_second);
2115 (void) WriteBlobString(image,buffer);
2119 if (image->scene != 0)
2121 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2122 (double) image->scene);
2123 (void) WriteBlobString(image,buffer);
2125 if (image->iterations != 0)
2127 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2128 (double) image->iterations);
2129 (void) WriteBlobString(image,buffer);
2131 if (image->delay != 0)
2133 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2134 (double) image->delay);
2135 (void) WriteBlobString(image,buffer);
2137 if (image->ticks_per_second != UndefinedTicksPerSecond)
2139 (void) FormatLocaleString(buffer,MaxTextExtent,
2140 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2141 (void) WriteBlobString(image,buffer);
2144 if (image->gravity != UndefinedGravity)
2146 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2147 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2148 (void) WriteBlobString(image,buffer);
2150 if (image->dispose != UndefinedDispose)
2152 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2153 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2154 (void) WriteBlobString(image,buffer);
2156 if (image->rendering_intent != UndefinedIntent)
2158 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2159 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2160 (void) WriteBlobString(image,buffer);
2162 if (image->gamma != 0.0)
2164 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2166 (void) WriteBlobString(image,buffer);
2168 if (image->chromaticity.white_point.x != 0.0)
2171 Note chomaticity points.
2173 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2174 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2175 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2176 image->chromaticity.green_primary.x,
2177 image->chromaticity.green_primary.y,
2178 image->chromaticity.blue_primary.x,
2179 image->chromaticity.blue_primary.y);
2180 (void) WriteBlobString(image,buffer);
2181 (void) FormatLocaleString(buffer,MaxTextExtent,
2182 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2183 image->chromaticity.white_point.y);
2184 (void) WriteBlobString(image,buffer);
2186 if (image->orientation != UndefinedOrientation)
2188 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2189 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2190 (void) WriteBlobString(image,buffer);
2192 if (image->profiles != (void *) NULL)
2201 Write image profiles.
2203 ResetImageProfileIterator(image);
2204 name=GetNextImageProfile(image);
2205 while (name != (const char *) NULL)
2207 profile=GetImageProfile(image,name);
2208 if (profile != (StringInfo *) NULL)
2210 (void) FormatLocaleString(buffer,MaxTextExtent,
2211 "profile:%s=%.20g\n",name,(double)
2212 GetStringInfoLength(profile));
2213 (void) WriteBlobString(image,buffer);
2215 name=GetNextImageProfile(image);
2218 if (image->montage != (char *) NULL)
2220 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2222 (void) WriteBlobString(image,buffer);
2224 if (quantum_info->format == FloatingPointQuantumFormat)
2225 (void) SetImageProperty(image,"quantum:format","floating-point",
2227 ResetImagePropertyIterator(image);
2228 property=GetNextImageProperty(image);
2229 while (property != (const char *) NULL)
2231 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2232 (void) WriteBlobString(image,buffer);
2233 value=GetImageProperty(image,property,exception);
2234 if (value != (const char *) NULL)
2239 length=strlen(value);
2240 for (i=0; i < (ssize_t) length; i++)
2241 if (isspace((int) ((unsigned char) value[i])) != 0)
2243 if ((i == (ssize_t) length) && (i != 0))
2244 (void) WriteBlob(image,length,(const unsigned char *) value);
2247 (void) WriteBlobByte(image,'{');
2248 if (strchr(value,'}') == (char *) NULL)
2249 (void) WriteBlob(image,length,(const unsigned char *) value);
2251 for (i=0; i < (ssize_t) length; i++)
2253 if (value[i] == (int) '}')
2254 (void) WriteBlobByte(image,'\\');
2255 (void) WriteBlobByte(image,value[i]);
2257 (void) WriteBlobByte(image,'}');
2260 (void) WriteBlobByte(image,'\n');
2261 property=GetNextImageProperty(image);
2263 (void) WriteBlobString(image,"\f\n:\032");
2264 if (image->montage != (char *) NULL)
2267 Write montage tile directory.
2269 if (image->directory != (char *) NULL)
2270 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2272 (void) WriteBlobByte(image,'\0');
2274 if (image->profiles != (void *) NULL)
2285 ResetImageProfileIterator(image);
2286 name=GetNextImageProfile(image);
2287 while (name != (const char *) NULL)
2289 profile=GetImageProfile(image,name);
2290 (void) WriteBlob(image,GetStringInfoLength(profile),
2291 GetStringInfoDatum(profile));
2292 name=GetNextImageProfile(image);
2295 if (image->storage_class == PseudoClass)
2307 packet_size=(size_t) (3*quantum_info->depth/8);
2308 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2309 packet_size*sizeof(*colormap));
2310 if (colormap == (unsigned char *) NULL)
2311 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2313 Write colormap to file.
2316 for (i=0; i < (ssize_t) image->colors; i++)
2318 switch (quantum_info->depth)
2321 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2324 register unsigned int
2327 pixel=ScaleQuantumToLong(image->colormap[i].red);
2328 q=PopLongPixel(MSBEndian,pixel,q);
2329 pixel=ScaleQuantumToLong(image->colormap[i].green);
2330 q=PopLongPixel(MSBEndian,pixel,q);
2331 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2332 q=PopLongPixel(MSBEndian,pixel,q);
2337 register unsigned short
2340 pixel=ScaleQuantumToShort(image->colormap[i].red);
2341 q=PopShortPixel(MSBEndian,pixel,q);
2342 pixel=ScaleQuantumToShort(image->colormap[i].green);
2343 q=PopShortPixel(MSBEndian,pixel,q);
2344 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2345 q=PopShortPixel(MSBEndian,pixel,q);
2350 register unsigned char
2353 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2354 q=PopCharPixel(pixel,q);
2355 pixel=(unsigned char) ScaleQuantumToChar(
2356 image->colormap[i].green);
2357 q=PopCharPixel(pixel,q);
2358 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2359 q=PopCharPixel(pixel,q);
2364 (void) WriteBlob(image,packet_size*image->colors,colormap);
2365 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2368 Write image pixels to file.
2371 switch (compression)
2373 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2374 case BZipCompression:
2379 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2380 bzip_info.bzalloc=AcquireBZIPMemory;
2381 bzip_info.bzfree=RelinquishBZIPMemory;
2382 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2383 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2384 9)),(int) image_info->verbose,0);
2390 #if defined(MAGICKCORE_LZMA_DELEGATE)
2391 case LZMACompression:
2396 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2397 allocator.alloc=AcquireLZMAMemory;
2398 allocator.free=RelinquishLZMAMemory;
2399 lzma_info=initialize_lzma;
2400 lzma_info.allocator=&allocator;
2401 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2402 if (code != LZMA_OK)
2407 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2408 case LZWCompression:
2409 case ZipCompression:
2414 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2415 zip_info.zalloc=AcquireZIPMemory;
2416 zip_info.zfree=RelinquishZIPMemory;
2417 code=deflateInit(&zip_info,(int) (image->quality ==
2418 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2427 quantum_type=GetQuantumType(image,exception);
2428 pixels=GetQuantumPixels(quantum_info);
2429 for (y=0; y < (ssize_t) image->rows; y++)
2431 register const Quantum
2437 if (status == MagickFalse)
2439 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2440 if (p == (const Quantum *) NULL)
2443 switch (compression)
2445 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2446 case BZipCompression:
2448 bzip_info.next_in=(char *) pixels;
2449 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2450 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2451 quantum_type,pixels,exception);
2457 bzip_info.next_out=(char *) compress_pixels;
2458 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2460 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2463 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2466 (void) WriteBlobMSBLong(image,(unsigned int) length);
2467 (void) WriteBlob(image,length,compress_pixels);
2469 } while (bzip_info.avail_in != 0);
2473 #if defined(MAGICKCORE_LZMA_DELEGATE)
2474 case LZMACompression:
2476 lzma_info.next_in=pixels;
2477 lzma_info.avail_in=packet_size*image->columns;
2478 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2479 quantum_type,pixels,exception);
2485 lzma_info.next_out=compress_pixels;
2486 lzma_info.avail_out=packet_size*image->columns;
2487 code=lzma_code(&lzma_info,LZMA_RUN);
2488 if (code != LZMA_OK)
2490 length=(size_t) (lzma_info.next_out-compress_pixels);
2493 (void) WriteBlobMSBLong(image,(unsigned int) length);
2494 (void) WriteBlob(image,length,compress_pixels);
2496 } while (lzma_info.avail_in != 0);
2500 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2501 case LZWCompression:
2502 case ZipCompression:
2504 zip_info.next_in=pixels;
2505 zip_info.avail_in=(uInt) (packet_size*image->columns);
2506 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2507 quantum_type,pixels,exception);
2513 zip_info.next_out=compress_pixels;
2514 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2515 code=deflate(&zip_info,Z_SYNC_FLUSH);
2518 length=(size_t) (zip_info.next_out-compress_pixels);
2521 (void) WriteBlobMSBLong(image,(unsigned int) length);
2522 (void) WriteBlob(image,length,compress_pixels);
2524 } while (zip_info.avail_in != 0);
2528 case RLECompression:
2531 GetPixelInfoPixel(image,p,&pixel);
2532 p+=GetPixelChannels(image);
2533 for (x=1; x < (ssize_t) image->columns; x++)
2535 GetPixelInfoPixel(image,p,&target);
2536 if ((length < 255) &&
2537 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2541 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2544 GetPixelInfoPixel(image,p,&pixel);
2545 p+=GetPixelChannels(image);
2547 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2548 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2553 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2554 quantum_type,pixels,exception);
2555 (void) WriteBlob(image,packet_size*image->columns,pixels);
2559 if (image->previous == (Image *) NULL)
2561 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2563 if (status == MagickFalse)
2567 switch (compression)
2569 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2570 case BZipCompression:
2577 if (status == MagickFalse)
2579 bzip_info.next_out=(char *) compress_pixels;
2580 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2582 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2583 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2586 (void) WriteBlobMSBLong(image,(unsigned int) length);
2587 (void) WriteBlob(image,length,compress_pixels);
2589 if (code == BZ_STREAM_END)
2592 code=BZ2_bzCompressEnd(&bzip_info);
2598 #if defined(MAGICKCORE_LZMA_DELEGATE)
2599 case LZMACompression:
2606 if (status == MagickFalse)
2608 lzma_info.next_out=compress_pixels;
2609 lzma_info.avail_out=packet_size*image->columns;
2610 code=lzma_code(&lzma_info,LZMA_FINISH);
2611 length=(size_t) (lzma_info.next_out-compress_pixels);
2614 (void) WriteBlobMSBLong(image,(unsigned int) length);
2615 (void) WriteBlob(image,length,compress_pixels);
2617 if (code == LZMA_STREAM_END)
2620 lzma_end(&lzma_info);
2624 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2625 case LZWCompression:
2626 case ZipCompression:
2633 if (status == MagickFalse)
2635 zip_info.next_out=compress_pixels;
2636 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2637 code=deflate(&zip_info,Z_FINISH);
2638 length=(size_t) (zip_info.next_out-compress_pixels);
2641 (void) WriteBlobMSBLong(image,(unsigned int) length);
2642 (void) WriteBlob(image,length,compress_pixels);
2644 if (code == Z_STREAM_END)
2647 code=deflateEnd(&zip_info);
2656 quantum_info=DestroyQuantumInfo(quantum_info);
2657 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2658 if (GetNextImageInList(image) == (Image *) NULL)
2660 image=SyncNextImageInList(image);
2661 status=SetImageProgress(image,SaveImagesTag,scene++,
2662 GetImageListLength(image));
2663 if (status == MagickFalse)
2665 } while (image_info->adjoin != MagickFalse);
2666 (void) CloseBlob(image);