2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2017 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 % https://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/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/linked-list.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)
402 id[MagickPathExtent],
403 keyword[MagickPathExtent],
424 #if defined(MAGICKCORE_LZMA_DELEGATE)
426 initialize_lzma = LZMA_STREAM_INIT,
472 #if defined(MAGICKCORE_ZLIB_DELEGATE)
480 assert(image_info != (const ImageInfo *) NULL);
481 assert(image_info->signature == MagickCoreSignature);
482 if (image_info->debug != MagickFalse)
483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
484 image_info->filename);
485 assert(exception != (ExceptionInfo *) NULL);
486 assert(exception->signature == MagickCoreSignature);
487 image=AcquireImage(image_info,exception);
488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
491 image=DestroyImageList(image);
492 return((Image *) NULL);
495 Decode image header; header terminates one character beyond a ':'.
497 c=ReadBlobByte(image);
499 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
501 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
507 Decode image header; header terminates one character beyond a ':'.
509 length=MagickPathExtent;
510 options=AcquireString((char *) NULL);
511 quantum_format=UndefinedQuantumFormat;
512 profiles=(LinkedListInfo *) NULL;
515 image->compression=NoCompression;
516 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
527 Read comment-- any text between { }.
529 length=MagickPathExtent;
530 comment=AcquireString((char *) NULL);
531 for (p=comment; comment != (char *) NULL; p++)
533 c=ReadBlobByte(image);
535 c=ReadBlobByte(image);
537 if ((c == EOF) || (c == (int) '}'))
539 if ((size_t) (p-comment+1) >= length)
543 comment=(char *) ResizeQuantumMemory(comment,length+
544 MagickPathExtent,sizeof(*comment));
545 if (comment == (char *) NULL)
547 p=comment+strlen(comment);
551 if (comment == (char *) NULL)
552 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
554 (void) SetImageProperty(image,"comment",comment,exception);
555 comment=DestroyString(comment);
556 c=ReadBlobByte(image);
559 if (isalnum(c) != MagickFalse)
564 length=MagickPathExtent;
570 if ((size_t) (p-keyword) < (MagickPathExtent-1))
572 c=ReadBlobByte(image);
576 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
577 c=ReadBlobByte(image);
581 Get the keyword value.
583 c=ReadBlobByte(image);
584 while ((c != (int) '}') && (c != EOF))
586 if ((size_t) (p-options+1) >= length)
590 options=(char *) ResizeQuantumMemory(options,length+
591 MagickPathExtent,sizeof(*options));
592 if (options == (char *) NULL)
594 p=options+strlen(options);
597 c=ReadBlobByte(image);
600 c=ReadBlobByte(image);
604 c=ReadBlobByte(image);
608 if (isspace((int) ((unsigned char) c)) != 0)
611 if (options == (char *) NULL)
612 ThrowReaderException(ResourceLimitError,
613 "MemoryAllocationFailed");
617 (void) CopyMagickString(options,options+1,strlen(options));
619 Assign a value to the specified keyword.
626 if (LocaleCompare(keyword,"alpha-trait") == 0)
631 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
632 MagickFalse,options);
635 image->alpha_trait=(PixelTrait) alpha_trait;
638 (void) SetImageProperty(image,keyword,options,exception);
644 if (LocaleCompare(keyword,"background-color") == 0)
646 (void) QueryColorCompliance(options,AllCompliance,
647 &image->background_color,exception);
650 if (LocaleCompare(keyword,"blue-primary") == 0)
652 flags=ParseGeometry(options,&geometry_info);
653 image->chromaticity.blue_primary.x=geometry_info.rho;
654 image->chromaticity.blue_primary.y=geometry_info.sigma;
655 if ((flags & SigmaValue) == 0)
656 image->chromaticity.blue_primary.y=
657 image->chromaticity.blue_primary.x;
660 if (LocaleCompare(keyword,"border-color") == 0)
662 (void) QueryColorCompliance(options,AllCompliance,
663 &image->border_color,exception);
666 (void) SetImageProperty(image,keyword,options,exception);
672 if (LocaleCompare(keyword,"class") == 0)
677 storage_class=ParseCommandOption(MagickClassOptions,
678 MagickFalse,options);
679 if (storage_class < 0)
681 image->storage_class=(ClassType) storage_class;
684 if (LocaleCompare(keyword,"colors") == 0)
686 colors=StringToUnsignedLong(options);
689 if (LocaleCompare(keyword,"colorspace") == 0)
694 colorspace=ParseCommandOption(MagickColorspaceOptions,
695 MagickFalse,options);
698 image->colorspace=(ColorspaceType) colorspace;
701 if (LocaleCompare(keyword,"compression") == 0)
706 compression=ParseCommandOption(MagickCompressOptions,
707 MagickFalse,options);
710 image->compression=(CompressionType) compression;
713 if (LocaleCompare(keyword,"columns") == 0)
715 image->columns=StringToUnsignedLong(options);
718 (void) SetImageProperty(image,keyword,options,exception);
724 if (LocaleCompare(keyword,"delay") == 0)
726 image->delay=StringToUnsignedLong(options);
729 if (LocaleCompare(keyword,"depth") == 0)
731 image->depth=StringToUnsignedLong(options);
734 if (LocaleCompare(keyword,"dispose") == 0)
739 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
743 image->dispose=(DisposeType) dispose;
746 (void) SetImageProperty(image,keyword,options,exception);
752 if (LocaleCompare(keyword,"endian") == 0)
757 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
761 image->endian=(EndianType) endian;
764 (void) SetImageProperty(image,keyword,options,exception);
770 if (LocaleCompare(keyword,"gamma") == 0)
772 image->gamma=StringToDouble(options,(char **) NULL);
775 if (LocaleCompare(keyword,"gravity") == 0)
780 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
784 image->gravity=(GravityType) gravity;
787 if (LocaleCompare(keyword,"green-primary") == 0)
789 flags=ParseGeometry(options,&geometry_info);
790 image->chromaticity.green_primary.x=geometry_info.rho;
791 image->chromaticity.green_primary.y=geometry_info.sigma;
792 if ((flags & SigmaValue) == 0)
793 image->chromaticity.green_primary.y=
794 image->chromaticity.green_primary.x;
797 (void) SetImageProperty(image,keyword,options,exception);
803 if (LocaleCompare(keyword,"id") == 0)
805 (void) CopyMagickString(id,options,MagickPathExtent);
808 if (LocaleCompare(keyword,"iterations") == 0)
810 image->iterations=StringToUnsignedLong(options);
813 (void) SetImageProperty(image,keyword,options,exception);
819 if (LocaleCompare(keyword,"matte") == 0)
824 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
828 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
832 if (LocaleCompare(keyword,"mattecolor") == 0)
834 (void) QueryColorCompliance(options,AllCompliance,
835 &image->matte_color,exception);
838 if (LocaleCompare(keyword,"montage") == 0)
840 (void) CloneString(&image->montage,options);
843 (void) SetImageProperty(image,keyword,options,exception);
849 if (LocaleCompare(keyword,"orientation") == 0)
854 orientation=ParseCommandOption(MagickOrientationOptions,
855 MagickFalse,options);
858 image->orientation=(OrientationType) orientation;
861 (void) SetImageProperty(image,keyword,options,exception);
867 if (LocaleCompare(keyword,"page") == 0)
872 geometry=GetPageGeometry(options);
873 (void) ParseAbsoluteGeometry(geometry,&image->page);
874 geometry=DestroyString(geometry);
877 if (LocaleCompare(keyword,"pixel-intensity") == 0)
882 intensity=ParseCommandOption(MagickPixelIntensityOptions,
883 MagickFalse,options);
886 image->intensity=(PixelIntensityMethod) intensity;
889 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
890 (LocaleNCompare(keyword,"profile-",8) == 0))
895 if (profiles == (LinkedListInfo *) NULL)
896 profiles=NewLinkedList(0);
897 (void) AppendValueToLinkedList(profiles,
898 AcquireString(keyword+8));
899 profile=BlobToStringInfo((const void *) NULL,(size_t)
900 StringToLong(options));
901 if (profile == (StringInfo *) NULL)
902 ThrowReaderException(ResourceLimitError,
903 "MemoryAllocationFailed");
904 (void) SetImageProfile(image,keyword+8,profile,exception);
905 profile=DestroyStringInfo(profile);
908 (void) SetImageProperty(image,keyword,options,exception);
914 if (LocaleCompare(keyword,"quality") == 0)
916 image->quality=StringToUnsignedLong(options);
919 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
920 (LocaleCompare(keyword,"quantum:format") == 0))
925 format=ParseCommandOption(MagickQuantumFormatOptions,
926 MagickFalse,options);
929 quantum_format=(QuantumFormatType) format;
932 (void) SetImageProperty(image,keyword,options,exception);
938 if (LocaleCompare(keyword,"red-primary") == 0)
940 flags=ParseGeometry(options,&geometry_info);
941 image->chromaticity.red_primary.x=geometry_info.rho;
942 image->chromaticity.red_primary.y=geometry_info.sigma;
943 if ((flags & SigmaValue) == 0)
944 image->chromaticity.red_primary.y=
945 image->chromaticity.red_primary.x;
948 if (LocaleCompare(keyword,"rendering-intent") == 0)
953 rendering_intent=ParseCommandOption(MagickIntentOptions,
954 MagickFalse,options);
955 if (rendering_intent < 0)
957 image->rendering_intent=(RenderingIntent) rendering_intent;
960 if (LocaleCompare(keyword,"resolution") == 0)
962 flags=ParseGeometry(options,&geometry_info);
963 image->resolution.x=geometry_info.rho;
964 image->resolution.y=geometry_info.sigma;
965 if ((flags & SigmaValue) == 0)
966 image->resolution.y=image->resolution.x;
969 if (LocaleCompare(keyword,"rows") == 0)
971 image->rows=StringToUnsignedLong(options);
974 (void) SetImageProperty(image,keyword,options,exception);
980 if (LocaleCompare(keyword,"scene") == 0)
982 image->scene=StringToUnsignedLong(options);
985 (void) SetImageProperty(image,keyword,options,exception);
991 if (LocaleCompare(keyword,"ticks-per-second") == 0)
993 image->ticks_per_second=(ssize_t) StringToLong(options);
996 if (LocaleCompare(keyword,"tile-offset") == 0)
1001 geometry=GetPageGeometry(options);
1002 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1003 geometry=DestroyString(geometry);
1006 if (LocaleCompare(keyword,"type") == 0)
1011 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1015 image->type=(ImageType) type;
1018 (void) SetImageProperty(image,keyword,options,exception);
1024 if (LocaleCompare(keyword,"units") == 0)
1029 units=ParseCommandOption(MagickResolutionOptions,
1030 MagickFalse,options);
1033 image->units=(ResolutionType) units;
1036 (void) SetImageProperty(image,keyword,options,exception);
1042 if (LocaleCompare(keyword,"version") == 0)
1044 version=StringToDouble(options,(char **) NULL);
1047 (void) SetImageProperty(image,keyword,options,exception);
1053 if (LocaleCompare(keyword,"white-point") == 0)
1055 flags=ParseGeometry(options,&geometry_info);
1056 image->chromaticity.white_point.x=geometry_info.rho;
1057 image->chromaticity.white_point.y=geometry_info.sigma;
1058 if ((flags & SigmaValue) == 0)
1059 image->chromaticity.white_point.y=
1060 image->chromaticity.white_point.x;
1063 (void) SetImageProperty(image,keyword,options,exception);
1068 (void) SetImageProperty(image,keyword,options,exception);
1074 c=ReadBlobByte(image);
1075 while (isspace((int) ((unsigned char) c)) != 0)
1076 c=ReadBlobByte(image);
1078 options=DestroyString(options);
1079 (void) ReadBlobByte(image);
1081 Verify that required image information is defined.
1083 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1084 (image->storage_class == UndefinedClass) ||
1085 (image->compression == UndefinedCompression) ||
1086 (image->colorspace == UndefinedColorspace) ||
1087 (image->columns == 0) || (image->rows == 0))
1089 if (image->previous == (Image *) NULL)
1090 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1091 DeleteImageFromList(&image);
1092 (void) ThrowMagickException(exception,GetMagickModule(),
1093 CorruptImageError,"ImproperImageHeader","`%s'",image->filename);
1096 if (image->montage != (char *) NULL)
1104 length=MagickPathExtent;
1105 image->directory=AcquireString((char *) NULL);
1110 if ((strlen(image->directory)+MagickPathExtent) >= length)
1113 Allocate more memory for the image directory.
1116 image->directory=(char *) ResizeQuantumMemory(image->directory,
1117 length+MagickPathExtent,sizeof(*image->directory));
1118 if (image->directory == (char *) NULL)
1119 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1120 p=image->directory+strlen(image->directory);
1122 c=ReadBlobByte(image);
1124 } while (c != (int) '\0');
1126 if (profiles != (LinkedListInfo *) NULL)
1135 Read image profiles.
1137 ResetLinkedListIterator(profiles);
1138 name=(const char *) GetNextValueInLinkedList(profiles);
1139 while (name != (const char *) NULL)
1141 profile=GetImageProfile(image,name);
1142 if (profile != (StringInfo *) NULL)
1144 register unsigned char
1147 p=GetStringInfoDatum(profile);
1148 count=ReadBlob(image,GetStringInfoLength(profile),p);
1151 name=(const char *) GetNextValueInLinkedList(profiles);
1153 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1155 image->depth=GetImageQuantumDepth(image,MagickFalse);
1156 if (image->storage_class == PseudoClass)
1165 Create image colormap.
1167 packet_size=(size_t) (3UL*image->depth/8UL);
1168 if ((packet_size*colors) > GetBlobSize(image))
1169 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
1170 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1171 if (status == MagickFalse)
1172 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1176 Read image colormap from file.
1178 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1179 packet_size*sizeof(*colormap));
1180 if (colormap == (unsigned char *) NULL)
1181 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1182 count=ReadBlob(image,packet_size*image->colors,colormap);
1184 switch (image->depth)
1187 ThrowReaderException(CorruptImageError,
1188 "ImageDepthNotSupported");
1194 for (i=0; i < (ssize_t) image->colors; i++)
1196 p=PushCharPixel(p,&pixel);
1197 image->colormap[i].red=ScaleCharToQuantum(pixel);
1198 p=PushCharPixel(p,&pixel);
1199 image->colormap[i].green=ScaleCharToQuantum(pixel);
1200 p=PushCharPixel(p,&pixel);
1201 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1210 for (i=0; i < (ssize_t) image->colors; i++)
1212 p=PushShortPixel(MSBEndian,p,&pixel);
1213 image->colormap[i].red=ScaleShortToQuantum(pixel);
1214 p=PushShortPixel(MSBEndian,p,&pixel);
1215 image->colormap[i].green=ScaleShortToQuantum(pixel);
1216 p=PushShortPixel(MSBEndian,p,&pixel);
1217 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1226 for (i=0; i < (ssize_t) image->colors; i++)
1228 p=PushLongPixel(MSBEndian,p,&pixel);
1229 image->colormap[i].red=ScaleLongToQuantum(pixel);
1230 p=PushLongPixel(MSBEndian,p,&pixel);
1231 image->colormap[i].green=ScaleLongToQuantum(pixel);
1232 p=PushLongPixel(MSBEndian,p,&pixel);
1233 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1238 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1241 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1242 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1244 status=SetImageExtent(image,image->columns,image->rows,exception);
1245 if (status == MagickFalse)
1246 return(DestroyImageList(image));
1248 Allocate image pixels.
1250 quantum_info=AcquireQuantumInfo(image_info,image);
1251 if (quantum_info == (QuantumInfo *) NULL)
1252 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1253 if (quantum_format != UndefinedQuantumFormat)
1255 status=SetQuantumFormat(image,quantum_info,quantum_format);
1256 if (status == MagickFalse)
1257 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1259 packet_size=(size_t) (quantum_info->depth/8);
1260 if (image->storage_class == DirectClass)
1261 packet_size=(size_t) (3*quantum_info->depth/8);
1262 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1263 packet_size=quantum_info->depth/8;
1264 if (image->alpha_trait != UndefinedPixelTrait)
1265 packet_size+=quantum_info->depth/8;
1266 if (image->colorspace == CMYKColorspace)
1267 packet_size+=quantum_info->depth/8;
1268 if (image->compression == RLECompression)
1270 compress_extent=MagickMax(MagickMax(BZipMaxExtent(packet_size*
1271 image->columns),LZMAMaxExtent(packet_size*image->columns)),
1272 ZipMaxExtent(packet_size*image->columns));
1273 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_extent,
1274 sizeof(*compress_pixels));
1275 if (compress_pixels == (unsigned char *) NULL)
1276 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1280 quantum_type=RGBQuantum;
1281 if (image->alpha_trait != UndefinedPixelTrait)
1282 quantum_type=RGBAQuantum;
1283 if (image->colorspace == CMYKColorspace)
1285 quantum_type=CMYKQuantum;
1286 if (image->alpha_trait != UndefinedPixelTrait)
1287 quantum_type=CMYKAQuantum;
1289 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1291 quantum_type=GrayQuantum;
1292 if (image->alpha_trait != UndefinedPixelTrait)
1293 quantum_type=GrayAlphaQuantum;
1295 if (image->storage_class == PseudoClass)
1297 quantum_type=IndexQuantum;
1298 if (image->alpha_trait != UndefinedPixelTrait)
1299 quantum_type=IndexAlphaQuantum;
1302 GetPixelInfo(image,&pixel);
1303 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1304 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1306 #if defined(MAGICKCORE_LZMA_DELEGATE)
1307 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1309 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1310 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1312 switch (image->compression)
1314 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1315 case BZipCompression:
1320 bzip_info.bzalloc=AcquireBZIPMemory;
1321 bzip_info.bzfree=RelinquishBZIPMemory;
1322 bzip_info.opaque=(void *) NULL;
1323 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1330 #if defined(MAGICKCORE_LZMA_DELEGATE)
1331 case LZMACompression:
1336 allocator.alloc=AcquireLZMAMemory;
1337 allocator.free=RelinquishLZMAMemory;
1338 lzma_info=initialize_lzma;
1339 lzma_info.allocator=(&allocator);
1340 code=lzma_auto_decoder(&lzma_info,-1,0);
1341 if (code != LZMA_OK)
1346 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1347 case LZWCompression:
1348 case ZipCompression:
1353 zip_info.zalloc=AcquireZIPMemory;
1354 zip_info.zfree=RelinquishZIPMemory;
1355 zip_info.opaque=(voidpf) NULL;
1356 code=inflateInit(&zip_info);
1362 case RLECompression:
1367 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1369 for (y=0; y < (ssize_t) image->rows; y++)
1377 if (status == MagickFalse)
1379 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1380 if (q == (Quantum *) NULL)
1382 switch (image->compression)
1384 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1385 case BZipCompression:
1387 bzip_info.next_out=(char *) pixels;
1388 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1394 if (bzip_info.avail_in == 0)
1396 bzip_info.next_in=(char *) compress_pixels;
1397 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1399 length=(size_t) ReadBlobMSBLong(image);
1400 if (length > compress_extent)
1402 (void) BZ2_bzDecompressEnd(&bzip_info);
1403 ThrowReaderException(CorruptImageError,
1404 "UnableToReadImageData");
1406 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1407 (unsigned char *) bzip_info.next_in);
1409 code=BZ2_bzDecompress(&bzip_info);
1415 if (code == BZ_STREAM_END)
1417 } while (bzip_info.avail_out != 0);
1418 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1419 quantum_type,pixels,exception);
1423 #if defined(MAGICKCORE_LZMA_DELEGATE)
1424 case LZMACompression:
1426 lzma_info.next_out=pixels;
1427 lzma_info.avail_out=packet_size*image->columns;
1433 if (lzma_info.avail_in == 0)
1435 lzma_info.next_in=compress_pixels;
1436 length=(size_t) ReadBlobMSBLong(image);
1437 if (length > compress_extent)
1439 lzma_end(&lzma_info);
1440 ThrowReaderException(CorruptImageError,
1441 "UnableToReadImageData");
1443 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1444 (unsigned char *) lzma_info.next_in);
1446 code=lzma_code(&lzma_info,LZMA_RUN);
1452 if (code == LZMA_STREAM_END)
1454 } while (lzma_info.avail_out != 0);
1455 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1456 quantum_type,pixels,exception);
1460 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1461 case LZWCompression:
1462 case ZipCompression:
1464 zip_info.next_out=pixels;
1465 zip_info.avail_out=(uInt) (packet_size*image->columns);
1471 if (zip_info.avail_in == 0)
1473 zip_info.next_in=compress_pixels;
1474 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1476 length=(size_t) ReadBlobMSBLong(image);
1477 if (length > compress_extent)
1479 (void) inflateEnd(&zip_info);
1480 ThrowReaderException(CorruptImageError,
1481 "UnableToReadImageData");
1483 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1486 code=inflate(&zip_info,Z_SYNC_FLUSH);
1492 if (code == Z_STREAM_END)
1494 } while (zip_info.avail_out != 0);
1495 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1496 quantum_type,pixels,exception);
1500 case RLECompression:
1502 for (x=0; x < (ssize_t) image->columns; x++)
1506 count=ReadBlob(image,packet_size,pixels);
1507 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1510 if (image->storage_class == PseudoClass)
1511 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1514 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1515 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1516 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1517 if (image->colorspace == CMYKColorspace)
1518 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1520 if (image->alpha_trait != UndefinedPixelTrait)
1521 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1522 q+=GetPixelChannels(image);
1528 count=ReadBlob(image,packet_size*image->columns,pixels);
1529 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1530 quantum_type,pixels,exception);
1534 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1537 SetQuantumImageType(image,quantum_type);
1538 switch (image->compression)
1540 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1541 case BZipCompression:
1551 offset=SeekBlob(image,-((MagickOffsetType)
1552 bzip_info.avail_in),SEEK_CUR);
1554 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1556 code=BZ2_bzDecompressEnd(&bzip_info);
1562 #if defined(MAGICKCORE_LZMA_DELEGATE)
1563 case LZMACompression:
1568 code=lzma_code(&lzma_info,LZMA_FINISH);
1569 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1571 lzma_end(&lzma_info);
1575 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1576 case LZWCompression:
1577 case ZipCompression:
1587 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1590 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1592 code=inflateEnd(&zip_info);
1593 if (code != LZMA_OK)
1601 quantum_info=DestroyQuantumInfo(quantum_info);
1602 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1603 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1605 image=DestroyImageList(image);
1606 return((Image *) NULL);
1608 if (EOFBlob(image) != MagickFalse)
1610 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1615 Proceed to next image.
1617 if (image_info->number_scenes != 0)
1618 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1622 c=ReadBlobByte(image);
1623 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1627 Allocate next image structure.
1629 AcquireNextImage(image_info,image,exception);
1630 if (GetNextImageInList(image) == (Image *) NULL)
1632 image=DestroyImageList(image);
1633 return((Image *) NULL);
1635 image=SyncNextImageInList(image);
1636 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1637 GetBlobSize(image));
1638 if (status == MagickFalse)
1642 (void) CloseBlob(image);
1643 return(GetFirstImageInList(image));
1647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1651 % R e g i s t e r M I F F I m a g e %
1655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1657 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1658 % supported formats. The properties include the image format tag, a method to
1659 % read and/or write the format, whether the format supports the saving of more
1660 % than one frame to the same file or blob, whether the format supports native
1661 % in-memory I/O, and a brief description of the format.
1663 % The format of the RegisterMIFFImage method is:
1665 % size_t RegisterMIFFImage(void)
1668 ModuleExport size_t RegisterMIFFImage(void)
1671 version[MagickPathExtent];
1677 #if defined(MagickImageCoderSignatureText)
1678 (void) CopyMagickString(version,MagickLibVersionText,MagickPathExtent);
1679 #if defined(ZLIB_VERSION)
1680 (void) ConcatenateMagickString(version," with Zlib ",MagickPathExtent);
1681 (void) ConcatenateMagickString(version,ZLIB_VERSION,MagickPathExtent);
1683 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1684 (void) ConcatenateMagickString(version," and BZlib",MagickPathExtent);
1687 entry=AcquireMagickInfo("MIFF","MIFF","Magick Image File Format");
1688 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1689 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1690 entry->magick=(IsImageFormatHandler *) IsMIFF;
1691 entry->flags|=CoderDecoderSeekableStreamFlag;
1692 if (*version != '\0')
1693 entry->version=ConstantString(version);
1694 (void) RegisterMagickInfo(entry);
1695 return(MagickImageCoderSignature);
1699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1703 % U n r e g i s t e r M I F F I m a g e %
1707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1710 % from the list of supported formats.
1712 % The format of the UnregisterMIFFImage method is:
1714 % UnregisterMIFFImage(void)
1717 ModuleExport void UnregisterMIFFImage(void)
1719 (void) UnregisterMagickInfo("MIFF");
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % W r i t e M I F F I m a g e %
1731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % WriteMIFFImage() writes a MIFF image to a file.
1735 % The format of the WriteMIFFImage method is:
1737 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1738 % Image *image,ExceptionInfo *exception)
1740 % Compression code contributed by Kyle Shorter.
1742 % A description of each parameter follows:
1744 % o image_info: the image info.
1746 % o image: the image.
1748 % o exception: return any errors or warnings in this structure.
1752 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1753 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1755 if (image->storage_class != DirectClass)
1760 value=(unsigned int) ClampToQuantum(pixel->index);
1761 switch (image->depth)
1765 *pixels++=(unsigned char) (value >> 24);
1766 *pixels++=(unsigned char) (value >> 16);
1769 *pixels++=(unsigned char) (value >> 8);
1772 *pixels++=(unsigned char) value;
1776 (void) ThrowMagickException(exception,GetMagickModule(),
1777 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1779 switch (image->depth)
1786 if (image->alpha_trait != UndefinedPixelTrait)
1788 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1789 pixels=PopLongPixel(MSBEndian,value,pixels);
1798 if (image->alpha_trait != UndefinedPixelTrait)
1800 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1801 pixels=PopShortPixel(MSBEndian,value,pixels);
1810 if (image->alpha_trait != UndefinedPixelTrait)
1812 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1814 pixels=PopCharPixel(value,pixels);
1819 (void) ThrowMagickException(exception,GetMagickModule(),
1820 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1822 *pixels++=(unsigned char) length;
1825 switch (image->depth)
1832 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1833 pixels=PopLongPixel(MSBEndian,value,pixels);
1834 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1836 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1837 pixels=PopLongPixel(MSBEndian,value,pixels);
1838 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1839 pixels=PopLongPixel(MSBEndian,value,pixels);
1841 if (image->colorspace == CMYKColorspace)
1843 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1844 pixels=PopLongPixel(MSBEndian,value,pixels);
1846 if (image->alpha_trait != UndefinedPixelTrait)
1848 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1849 pixels=PopLongPixel(MSBEndian,value,pixels);
1858 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1859 pixels=PopShortPixel(MSBEndian,value,pixels);
1860 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1862 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1863 pixels=PopShortPixel(MSBEndian,value,pixels);
1864 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1865 pixels=PopShortPixel(MSBEndian,value,pixels);
1867 if (image->colorspace == CMYKColorspace)
1869 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1870 pixels=PopShortPixel(MSBEndian,value,pixels);
1872 if (image->alpha_trait != UndefinedPixelTrait)
1874 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1875 pixels=PopShortPixel(MSBEndian,value,pixels);
1884 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1885 pixels=PopCharPixel(value,pixels);
1886 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1888 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1890 pixels=PopCharPixel(value,pixels);
1891 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1892 pixels=PopCharPixel(value,pixels);
1894 if (image->colorspace == CMYKColorspace)
1896 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1898 pixels=PopCharPixel(value,pixels);
1900 if (image->alpha_trait != UndefinedPixelTrait)
1902 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1904 pixels=PopCharPixel(value,pixels);
1909 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1910 "ImageDepthNotSupported","`%s'",image->filename);
1912 *pixels++=(unsigned char) length;
1916 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1917 Image *image,ExceptionInfo *exception)
1919 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1925 buffer[MagickPathExtent];
1934 #if defined(MAGICKCORE_LZMA_DELEGATE)
1939 initialize_lzma = LZMA_STREAM_INIT,
1974 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1980 Open output image file.
1982 assert(image_info != (const ImageInfo *) NULL);
1983 assert(image_info->signature == MagickCoreSignature);
1984 assert(image != (Image *) NULL);
1985 assert(image->signature == MagickCoreSignature);
1986 if (image->debug != MagickFalse)
1987 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1988 assert(exception != (ExceptionInfo *) NULL);
1989 assert(exception->signature == MagickCoreSignature);
1990 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1991 if (status == MagickFalse)
1997 Allocate image pixels.
1999 if ((image->storage_class == PseudoClass) &&
2000 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
2001 (void) SetImageStorageClass(image,DirectClass,exception);
2002 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
2003 image->depth <= 32 ? 32UL : 64UL;
2004 quantum_info=AcquireQuantumInfo(image_info,image);
2005 if (quantum_info == (QuantumInfo *) NULL)
2006 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2007 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2008 (quantum_info->format == UndefinedQuantumFormat) &&
2009 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
2011 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2012 if (status == MagickFalse)
2013 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2015 compression=UndefinedCompression;
2016 if (image_info->compression != UndefinedCompression)
2017 compression=image_info->compression;
2018 switch (compression)
2020 #if !defined(MAGICKCORE_LZMA_DELEGATE)
2021 case LZMACompression: compression=NoCompression; break;
2023 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
2024 case LZWCompression:
2025 case ZipCompression: compression=NoCompression; break;
2027 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
2028 case BZipCompression: compression=NoCompression; break;
2030 case RLECompression:
2032 if (quantum_info->format == FloatingPointQuantumFormat)
2033 compression=NoCompression;
2034 GetPixelInfo(image,&target);
2040 packet_size=(size_t) (quantum_info->depth/8);
2041 if (image->storage_class == DirectClass)
2042 packet_size=(size_t) (3*quantum_info->depth/8);
2043 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2044 packet_size=(size_t) (quantum_info->depth/8);
2045 if (image->alpha_trait != UndefinedPixelTrait)
2046 packet_size+=quantum_info->depth/8;
2047 if (image->colorspace == CMYKColorspace)
2048 packet_size+=quantum_info->depth/8;
2049 if (compression == RLECompression)
2051 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2052 packet_size*image->columns));
2053 if ((compression == BZipCompression) || (compression == ZipCompression))
2054 if (length != (size_t) ((unsigned int) length))
2055 compression=NoCompression;
2056 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2057 sizeof(*compress_pixels));
2058 if (compress_pixels == (unsigned char *) NULL)
2059 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2063 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2064 (void) FormatLocaleString(buffer,MagickPathExtent,
2065 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2066 MagickClassOptions,image->storage_class),(double) image->colors,
2067 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2068 image->alpha_trait));
2069 (void) WriteBlobString(image,buffer);
2070 if (image->alpha_trait != UndefinedPixelTrait)
2071 (void) WriteBlobString(image,"matte=True\n");
2072 (void) FormatLocaleString(buffer,MagickPathExtent,
2073 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
2074 (double) image->rows,(double) image->depth);
2075 (void) WriteBlobString(image,buffer);
2076 if (image->type != UndefinedType)
2078 (void) FormatLocaleString(buffer,MagickPathExtent,"type=%s\n",
2079 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2080 (void) WriteBlobString(image,buffer);
2082 if (image->colorspace != UndefinedColorspace)
2084 (void) FormatLocaleString(buffer,MagickPathExtent,"colorspace=%s\n",
2085 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2086 (void) WriteBlobString(image,buffer);
2088 if (image->intensity != UndefinedPixelIntensityMethod)
2090 (void) FormatLocaleString(buffer,MagickPathExtent,
2091 "pixel-intensity=%s\n",CommandOptionToMnemonic(
2092 MagickPixelIntensityOptions,image->intensity));
2093 (void) WriteBlobString(image,buffer);
2095 if (image->endian != UndefinedEndian)
2097 (void) FormatLocaleString(buffer,MagickPathExtent,"endian=%s\n",
2098 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2099 (void) WriteBlobString(image,buffer);
2101 if (compression != UndefinedCompression)
2103 (void) FormatLocaleString(buffer,MagickPathExtent,"compression=%s "
2104 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2105 compression),(double) image->quality);
2106 (void) WriteBlobString(image,buffer);
2108 if (image->units != UndefinedResolution)
2110 (void) FormatLocaleString(buffer,MagickPathExtent,"units=%s\n",
2111 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2112 (void) WriteBlobString(image,buffer);
2114 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2116 (void) FormatLocaleString(buffer,MagickPathExtent,
2117 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2118 (void) WriteBlobString(image,buffer);
2120 if ((image->page.width != 0) || (image->page.height != 0))
2122 (void) FormatLocaleString(buffer,MagickPathExtent,
2123 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2124 image->page.height,(double) image->page.x,(double) image->page.y);
2125 (void) WriteBlobString(image,buffer);
2128 if ((image->page.x != 0) || (image->page.y != 0))
2130 (void) FormatLocaleString(buffer,MagickPathExtent,"page=%+ld%+ld\n",
2131 (long) image->page.x,(long) image->page.y);
2132 (void) WriteBlobString(image,buffer);
2134 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2136 (void) FormatLocaleString(buffer,MagickPathExtent,
2137 "tile-offset=%+ld%+ld\n",(long) image->tile_offset.x,(long)
2138 image->tile_offset.y);
2139 (void) WriteBlobString(image,buffer);
2141 if ((GetNextImageInList(image) != (Image *) NULL) ||
2142 (GetPreviousImageInList(image) != (Image *) NULL))
2144 if (image->scene == 0)
2145 (void) FormatLocaleString(buffer,MagickPathExtent,"iterations=%.20g "
2146 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2147 (double) image->delay,(double) image->ticks_per_second);
2149 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g "
2150 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2151 image->scene,(double) image->iterations,(double) image->delay,
2152 (double) image->ticks_per_second);
2153 (void) WriteBlobString(image,buffer);
2157 if (image->scene != 0)
2159 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g\n",
2160 (double) image->scene);
2161 (void) WriteBlobString(image,buffer);
2163 if (image->iterations != 0)
2165 (void) FormatLocaleString(buffer,MagickPathExtent,
2166 "iterations=%.20g\n",(double) image->iterations);
2167 (void) WriteBlobString(image,buffer);
2169 if (image->delay != 0)
2171 (void) FormatLocaleString(buffer,MagickPathExtent,"delay=%.20g\n",
2172 (double) image->delay);
2173 (void) WriteBlobString(image,buffer);
2175 if (image->ticks_per_second != UndefinedTicksPerSecond)
2177 (void) FormatLocaleString(buffer,MagickPathExtent,
2178 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2179 (void) WriteBlobString(image,buffer);
2182 if (image->gravity != UndefinedGravity)
2184 (void) FormatLocaleString(buffer,MagickPathExtent,"gravity=%s\n",
2185 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2186 (void) WriteBlobString(image,buffer);
2188 if (image->dispose != UndefinedDispose)
2190 (void) FormatLocaleString(buffer,MagickPathExtent,"dispose=%s\n",
2191 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2192 (void) WriteBlobString(image,buffer);
2194 if (image->rendering_intent != UndefinedIntent)
2196 (void) FormatLocaleString(buffer,MagickPathExtent,
2197 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
2198 image->rendering_intent));
2199 (void) WriteBlobString(image,buffer);
2201 if (image->gamma != 0.0)
2203 (void) FormatLocaleString(buffer,MagickPathExtent,"gamma=%g\n",
2205 (void) WriteBlobString(image,buffer);
2207 if (image->chromaticity.white_point.x != 0.0)
2210 Note chomaticity points.
2212 (void) FormatLocaleString(buffer,MagickPathExtent,"red-primary=%g,"
2213 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2214 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2215 image->chromaticity.green_primary.x,
2216 image->chromaticity.green_primary.y,
2217 image->chromaticity.blue_primary.x,
2218 image->chromaticity.blue_primary.y);
2219 (void) WriteBlobString(image,buffer);
2220 (void) FormatLocaleString(buffer,MagickPathExtent,
2221 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2222 image->chromaticity.white_point.y);
2223 (void) WriteBlobString(image,buffer);
2225 if (image->orientation != UndefinedOrientation)
2227 (void) FormatLocaleString(buffer,MagickPathExtent,"orientation=%s\n",
2228 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2229 (void) WriteBlobString(image,buffer);
2231 if (image->profiles != (void *) NULL)
2240 Write image profiles.
2242 ResetImageProfileIterator(image);
2243 name=GetNextImageProfile(image);
2244 while (name != (const char *) NULL)
2246 profile=GetImageProfile(image,name);
2247 if (profile != (StringInfo *) NULL)
2249 (void) FormatLocaleString(buffer,MagickPathExtent,
2250 "profile:%s=%.20g\n",name,(double)
2251 GetStringInfoLength(profile));
2252 (void) WriteBlobString(image,buffer);
2254 name=GetNextImageProfile(image);
2257 if (image->montage != (char *) NULL)
2259 (void) FormatLocaleString(buffer,MagickPathExtent,"montage=%s\n",
2261 (void) WriteBlobString(image,buffer);
2263 if (quantum_info->format == FloatingPointQuantumFormat)
2264 (void) SetImageProperty(image,"quantum:format","floating-point",
2266 ResetImagePropertyIterator(image);
2267 property=GetNextImageProperty(image);
2268 while (property != (const char *) NULL)
2270 (void) FormatLocaleString(buffer,MagickPathExtent,"%s=",property);
2271 (void) WriteBlobString(image,buffer);
2272 value=GetImageProperty(image,property,exception);
2273 if (value != (const char *) NULL)
2278 length=strlen(value);
2279 for (i=0; i < (ssize_t) length; i++)
2280 if ((isspace((int) ((unsigned char) value[i])) != 0) ||
2283 if ((i == (ssize_t) length) && (i != 0))
2284 (void) WriteBlob(image,length,(const unsigned char *) value);
2287 (void) WriteBlobByte(image,'{');
2288 if (strchr(value,'}') == (char *) NULL)
2289 (void) WriteBlob(image,length,(const unsigned char *) value);
2291 for (i=0; i < (ssize_t) length; i++)
2293 if (value[i] == (int) '}')
2294 (void) WriteBlobByte(image,'\\');
2295 (void) WriteBlobByte(image,value[i]);
2297 (void) WriteBlobByte(image,'}');
2300 (void) WriteBlobByte(image,'\n');
2301 property=GetNextImageProperty(image);
2303 (void) WriteBlobString(image,"\f\n:\032");
2304 if (image->montage != (char *) NULL)
2307 Write montage tile directory.
2309 if (image->directory != (char *) NULL)
2310 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2312 (void) WriteBlobByte(image,'\0');
2314 if (image->profiles != (void *) NULL)
2325 ResetImageProfileIterator(image);
2326 name=GetNextImageProfile(image);
2327 while (name != (const char *) NULL)
2329 profile=GetImageProfile(image,name);
2330 (void) WriteBlob(image,GetStringInfoLength(profile),
2331 GetStringInfoDatum(profile));
2332 name=GetNextImageProfile(image);
2335 if (image->storage_class == PseudoClass)
2347 packet_size=(size_t) (3*quantum_info->depth/8);
2348 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2349 packet_size*sizeof(*colormap));
2350 if (colormap == (unsigned char *) NULL)
2351 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2353 Write colormap to file.
2356 for (i=0; i < (ssize_t) image->colors; i++)
2358 switch (quantum_info->depth)
2361 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2364 register unsigned int
2367 pixel=ScaleQuantumToLong(image->colormap[i].red);
2368 q=PopLongPixel(MSBEndian,pixel,q);
2369 pixel=ScaleQuantumToLong(image->colormap[i].green);
2370 q=PopLongPixel(MSBEndian,pixel,q);
2371 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2372 q=PopLongPixel(MSBEndian,pixel,q);
2377 register unsigned short
2380 pixel=ScaleQuantumToShort(image->colormap[i].red);
2381 q=PopShortPixel(MSBEndian,pixel,q);
2382 pixel=ScaleQuantumToShort(image->colormap[i].green);
2383 q=PopShortPixel(MSBEndian,pixel,q);
2384 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2385 q=PopShortPixel(MSBEndian,pixel,q);
2390 register unsigned char
2393 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2394 q=PopCharPixel(pixel,q);
2395 pixel=(unsigned char) ScaleQuantumToChar(
2396 image->colormap[i].green);
2397 q=PopCharPixel(pixel,q);
2398 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2399 q=PopCharPixel(pixel,q);
2404 (void) WriteBlob(image,packet_size*image->colors,colormap);
2405 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2408 Write image pixels to file.
2411 switch (compression)
2413 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2414 case BZipCompression:
2419 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2420 bzip_info.bzalloc=AcquireBZIPMemory;
2421 bzip_info.bzfree=RelinquishBZIPMemory;
2422 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2423 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)),
2424 (int) image_info->verbose,0);
2430 #if defined(MAGICKCORE_LZMA_DELEGATE)
2431 case LZMACompression:
2436 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2437 allocator.alloc=AcquireLZMAMemory;
2438 allocator.free=RelinquishLZMAMemory;
2439 lzma_info=initialize_lzma;
2440 lzma_info.allocator=&allocator;
2441 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2442 if (code != LZMA_OK)
2447 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2448 case LZWCompression:
2449 case ZipCompression:
2454 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2455 zip_info.zalloc=AcquireZIPMemory;
2456 zip_info.zfree=RelinquishZIPMemory;
2457 code=deflateInit(&zip_info,(int) (image->quality ==
2458 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2467 quantum_type=GetQuantumType(image,exception);
2468 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
2469 for (y=0; y < (ssize_t) image->rows; y++)
2471 register const Quantum
2477 if (status == MagickFalse)
2479 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2480 if (p == (const Quantum *) NULL)
2483 switch (compression)
2485 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2486 case BZipCompression:
2488 bzip_info.next_in=(char *) pixels;
2489 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2490 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2491 quantum_type,pixels,exception);
2497 bzip_info.next_out=(char *) compress_pixels;
2498 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2500 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2503 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2506 (void) WriteBlobMSBLong(image,(unsigned int) length);
2507 (void) WriteBlob(image,length,compress_pixels);
2509 } while (bzip_info.avail_in != 0);
2513 #if defined(MAGICKCORE_LZMA_DELEGATE)
2514 case LZMACompression:
2516 lzma_info.next_in=pixels;
2517 lzma_info.avail_in=packet_size*image->columns;
2518 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2519 quantum_type,pixels,exception);
2525 lzma_info.next_out=compress_pixels;
2526 lzma_info.avail_out=packet_size*image->columns;
2527 code=lzma_code(&lzma_info,LZMA_RUN);
2528 if (code != LZMA_OK)
2530 length=(size_t) (lzma_info.next_out-compress_pixels);
2533 (void) WriteBlobMSBLong(image,(unsigned int) length);
2534 (void) WriteBlob(image,length,compress_pixels);
2536 } while (lzma_info.avail_in != 0);
2540 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2541 case LZWCompression:
2542 case ZipCompression:
2544 zip_info.next_in=pixels;
2545 zip_info.avail_in=(uInt) (packet_size*image->columns);
2546 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2547 quantum_type,pixels,exception);
2553 zip_info.next_out=compress_pixels;
2554 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2555 code=deflate(&zip_info,Z_SYNC_FLUSH);
2558 length=(size_t) (zip_info.next_out-compress_pixels);
2561 (void) WriteBlobMSBLong(image,(unsigned int) length);
2562 (void) WriteBlob(image,length,compress_pixels);
2564 } while (zip_info.avail_in != 0);
2568 case RLECompression:
2571 GetPixelInfoPixel(image,p,&pixel);
2572 p+=GetPixelChannels(image);
2573 for (x=1; x < (ssize_t) image->columns; x++)
2575 GetPixelInfoPixel(image,p,&target);
2576 if ((length < 255) &&
2577 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2581 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2584 GetPixelInfoPixel(image,p,&pixel);
2585 p+=GetPixelChannels(image);
2587 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2588 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2593 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2594 quantum_type,pixels,exception);
2595 (void) WriteBlob(image,packet_size*image->columns,pixels);
2599 if (image->previous == (Image *) NULL)
2601 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2603 if (status == MagickFalse)
2607 switch (compression)
2609 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2610 case BZipCompression:
2617 if (status == MagickFalse)
2619 bzip_info.next_out=(char *) compress_pixels;
2620 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2622 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2623 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2626 (void) WriteBlobMSBLong(image,(unsigned int) length);
2627 (void) WriteBlob(image,length,compress_pixels);
2629 if (code == BZ_STREAM_END)
2632 code=BZ2_bzCompressEnd(&bzip_info);
2638 #if defined(MAGICKCORE_LZMA_DELEGATE)
2639 case LZMACompression:
2646 if (status == MagickFalse)
2648 lzma_info.next_out=compress_pixels;
2649 lzma_info.avail_out=packet_size*image->columns;
2650 code=lzma_code(&lzma_info,LZMA_FINISH);
2651 length=(size_t) (lzma_info.next_out-compress_pixels);
2654 (void) WriteBlobMSBLong(image,(unsigned int) length);
2655 (void) WriteBlob(image,length,compress_pixels);
2657 if (code == LZMA_STREAM_END)
2660 lzma_end(&lzma_info);
2664 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2665 case LZWCompression:
2666 case ZipCompression:
2673 if (status == MagickFalse)
2675 zip_info.next_out=compress_pixels;
2676 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2677 code=deflate(&zip_info,Z_FINISH);
2678 length=(size_t) (zip_info.next_out-compress_pixels);
2681 (void) WriteBlobMSBLong(image,(unsigned int) length);
2682 (void) WriteBlob(image,length,compress_pixels);
2684 if (code == Z_STREAM_END)
2687 code=deflateEnd(&zip_info);
2696 quantum_info=DestroyQuantumInfo(quantum_info);
2697 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2698 if (GetNextImageInList(image) == (Image *) NULL)
2700 image=SyncNextImageInList(image);
2701 status=SetImageProgress(image,SaveImagesTag,scene++,GetImageListLength(
2703 if (status == MagickFalse)
2705 } while (image_info->adjoin != MagickFalse);
2706 (void) CloseBlob(image);