2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/hashmap.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/profile.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/static.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #if defined(MAGICKCORE_BZLIB_DELEGATE)
79 #if defined(MAGICKCORE_LZMA_DELEGATE)
82 #if defined(MAGICKCORE_ZLIB_DELEGATE)
96 static MagickBooleanType
97 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % IsMIFF() returns MagickTrue if the image format type, identified by the
111 % magick string, is MIFF.
113 % The format of the IsMIFF method is:
115 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
117 % A description of each parameter follows:
119 % o magick: compare image format pattern against these bytes.
121 % o length: Specifies the length of the magick string.
124 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
128 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % R e a d M I F F I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
145 % memory necessary for the new Image structure and returns a pointer to the
148 % The format of the ReadMIFFImage method is:
150 % Image *ReadMIFFImage(const ImageInfo *image_info,
151 % ExceptionInfo *exception)
153 % Decompression code contributed by Kyle Shorter.
155 % A description of each parameter follows:
157 % o image_info: the image info.
159 % o exception: return any errors or warnings in this structure.
163 #if defined(MAGICKCORE_BZLIB_DELEGATE)
164 static void *AcquireBZIPMemory(void *context,int items,int size)
167 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
171 #if defined(MAGICKCORE_LZMA_DELEGATE)
172 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
175 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
179 #if defined(MAGICKCORE_ZLIB_DELEGATE)
180 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
184 return((voidpf) AcquireQuantumMemory(items,size));
188 static inline size_t MagickMax(const size_t x,const size_t y)
195 static inline size_t MagickMin(const size_t x,const size_t y)
202 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
203 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
209 if (image->storage_class == PseudoClass)
212 switch (image->depth)
216 pixel->index=ConstrainColormapIndex(image,
217 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3),exception);
223 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
230 pixel->index=ConstrainColormapIndex(image,*p,exception);
235 (void) ThrowMagickException(exception,GetMagickModule(),
236 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
238 switch (image->depth)
245 if (image->matte != MagickFalse)
247 p=PushCharPixel(p,&quantum);
248 pixel->alpha=ScaleCharToQuantum(quantum);
257 if (image->matte != MagickFalse)
259 p=PushShortPixel(MSBEndian,p,&quantum);
260 pixel->alpha=(Quantum) (quantum >> (image->depth-
261 MAGICKCORE_QUANTUM_DEPTH));
270 if (image->matte != MagickFalse)
272 p=PushLongPixel(MSBEndian,p,&quantum);
273 pixel->alpha=(Quantum) (quantum >> (image->depth-
274 MAGICKCORE_QUANTUM_DEPTH));
279 (void) ThrowMagickException(exception,GetMagickModule(),
280 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
282 *length=(size_t) (*p++)+1;
285 switch (image->depth)
292 p=PushCharPixel(p,&quantum);
293 pixel->red=ScaleCharToQuantum(quantum);
294 pixel->green=pixel->red;
295 pixel->blue=pixel->red;
296 if (IsGrayColorspace(image->colorspace) == MagickFalse)
298 p=PushCharPixel(p,&quantum);
299 pixel->green=ScaleCharToQuantum(quantum);
300 p=PushCharPixel(p,&quantum);
301 pixel->blue=ScaleCharToQuantum(quantum);
303 if (image->colorspace == CMYKColorspace)
305 p=PushCharPixel(p,&quantum);
306 pixel->black=ScaleCharToQuantum(quantum);
308 if (image->matte != MagickFalse)
310 p=PushCharPixel(p,&quantum);
311 pixel->alpha=ScaleCharToQuantum(quantum);
320 p=PushShortPixel(MSBEndian,p,&quantum);
321 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
322 pixel->green=pixel->red;
323 pixel->blue=pixel->red;
324 if (IsGrayColorspace(image->colorspace) == MagickFalse)
326 p=PushShortPixel(MSBEndian,p,&quantum);
327 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
328 p=PushShortPixel(MSBEndian,p,&quantum);
329 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
331 if (image->colorspace == CMYKColorspace)
333 p=PushShortPixel(MSBEndian,p,&quantum);
334 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
336 if (image->matte != MagickFalse)
338 p=PushShortPixel(MSBEndian,p,&quantum);
339 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
348 p=PushLongPixel(MSBEndian,p,&quantum);
349 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
350 pixel->green=pixel->red;
351 pixel->blue=pixel->red;
352 if (IsGrayColorspace(image->colorspace) == MagickFalse)
354 p=PushLongPixel(MSBEndian,p,&quantum);
355 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
356 p=PushLongPixel(MSBEndian,p,&quantum);
357 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
359 if (image->colorspace == CMYKColorspace)
361 p=PushLongPixel(MSBEndian,p,&quantum);
362 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
364 if (image->matte != MagickFalse)
366 p=PushLongPixel(MSBEndian,p,&quantum);
367 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
372 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
373 "ImageDepthNotSupported","`%s'",image->filename);
375 *length=(size_t) (*p++)+1;
378 #if defined(MAGICKCORE_BZLIB_DELEGATE)
379 static void RelinquishBZIPMemory(void *context,void *memory)
382 memory=RelinquishMagickMemory(memory);
386 #if defined(MAGICKCORE_LZMA_DELEGATE)
387 static void RelinquishLZMAMemory(void *context,void *memory)
390 memory=RelinquishMagickMemory(memory);
394 #if defined(MAGICKCORE_ZLIB_DELEGATE)
395 static void RelinquishZIPMemory(voidpf context,voidpf memory)
398 memory=RelinquishMagickMemory(memory);
402 static Image *ReadMIFFImage(const ImageInfo *image_info,
403 ExceptionInfo *exception)
405 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
406 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
407 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
409 #if defined(MAGICKCORE_BZLIB_DELEGATE)
416 keyword[MaxTextExtent],
434 #if defined(MAGICKCORE_LZMA_DELEGATE)
436 initialize_lzma = LZMA_STREAM_INIT,
484 #if defined(MAGICKCORE_ZLIB_DELEGATE)
492 assert(image_info != (const ImageInfo *) NULL);
493 assert(image_info->signature == MagickSignature);
494 if (image_info->debug != MagickFalse)
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
496 image_info->filename);
497 assert(exception != (ExceptionInfo *) NULL);
498 assert(exception->signature == MagickSignature);
499 image=AcquireImage(image_info,exception);
500 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
501 if (status == MagickFalse)
503 image=DestroyImageList(image);
504 return((Image *) NULL);
507 Decode image header; header terminates one character beyond a ':'.
509 c=ReadBlobByte(image);
511 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
513 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
519 Decode image header; header terminates one character beyond a ':'.
521 length=MaxTextExtent;
522 options=AcquireString((char *) NULL);
523 quantum_format=UndefinedQuantumFormat;
524 profiles=(LinkedListInfo *) NULL;
527 image->compression=NoCompression;
528 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
539 Read comment-- any text between { }.
541 length=MaxTextExtent;
542 comment=AcquireString((char *) NULL);
543 for (p=comment; comment != (char *) NULL; p++)
545 c=ReadBlobByte(image);
547 c=ReadBlobByte(image);
549 if ((c == EOF) || (c == (int) '}'))
551 if ((size_t) (p-comment+1) >= length)
555 comment=(char *) ResizeQuantumMemory(comment,length+
556 MaxTextExtent,sizeof(*comment));
557 if (comment == (char *) NULL)
559 p=comment+strlen(comment);
563 if (comment == (char *) NULL)
564 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
566 (void) SetImageProperty(image,"comment",comment,exception);
567 comment=DestroyString(comment);
568 c=ReadBlobByte(image);
571 if (isalnum(c) != MagickFalse)
581 if ((size_t) (p-keyword) < (MaxTextExtent-1))
583 c=ReadBlobByte(image);
587 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
588 c=ReadBlobByte(image);
592 Get the keyword value.
594 c=ReadBlobByte(image);
595 while ((c != (int) '}') && (c != EOF))
597 if ((size_t) (p-options+1) >= length)
601 options=(char *) ResizeQuantumMemory(options,length+
602 MaxTextExtent,sizeof(*options));
603 if (options == (char *) NULL)
605 p=options+strlen(options);
607 if (options == (char *) NULL)
608 ThrowReaderException(ResourceLimitError,
609 "MemoryAllocationFailed");
611 c=ReadBlobByte(image);
614 c=ReadBlobByte(image);
618 c=ReadBlobByte(image);
622 if (isspace((int) ((unsigned char) c)) != 0)
628 (void) CopyMagickString(options,options+1,MaxTextExtent);
630 Assign a value to the specified keyword.
637 if (LocaleCompare(keyword,"background-color") == 0)
639 (void) QueryColorCompliance(options,AllCompliance,
640 &image->background_color,exception);
643 if (LocaleCompare(keyword,"blue-primary") == 0)
645 flags=ParseGeometry(options,&geometry_info);
646 image->chromaticity.blue_primary.x=geometry_info.rho;
647 image->chromaticity.blue_primary.y=geometry_info.sigma;
648 if ((flags & SigmaValue) == 0)
649 image->chromaticity.blue_primary.y=
650 image->chromaticity.blue_primary.x;
653 if (LocaleCompare(keyword,"border-color") == 0)
655 (void) QueryColorCompliance(options,AllCompliance,
656 &image->border_color,exception);
659 (void) SetImageProperty(image,keyword,options,exception);
665 if (LocaleCompare(keyword,"class") == 0)
670 storage_class=ParseCommandOption(MagickClassOptions,
671 MagickFalse,options);
672 if (storage_class < 0)
674 image->storage_class=(ClassType) storage_class;
677 if (LocaleCompare(keyword,"colors") == 0)
679 colors=StringToUnsignedLong(options);
682 if (LocaleCompare(keyword,"colorspace") == 0)
687 colorspace=ParseCommandOption(MagickColorspaceOptions,
688 MagickFalse,options);
691 image->colorspace=(ColorspaceType) colorspace;
694 if (LocaleCompare(keyword,"compression") == 0)
699 compression=ParseCommandOption(MagickCompressOptions,
700 MagickFalse,options);
703 image->compression=(CompressionType) compression;
706 if (LocaleCompare(keyword,"columns") == 0)
708 image->columns=StringToUnsignedLong(options);
711 (void) SetImageProperty(image,keyword,options,exception);
717 if (LocaleCompare(keyword,"delay") == 0)
719 image->delay=StringToUnsignedLong(options);
722 if (LocaleCompare(keyword,"depth") == 0)
724 image->depth=StringToUnsignedLong(options);
727 if (LocaleCompare(keyword,"dispose") == 0)
732 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
736 image->dispose=(DisposeType) dispose;
739 (void) SetImageProperty(image,keyword,options,exception);
745 if (LocaleCompare(keyword,"endian") == 0)
750 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
754 image->endian=(EndianType) endian;
757 (void) SetImageProperty(image,keyword,options,exception);
763 if (LocaleCompare(keyword,"gamma") == 0)
765 image->gamma=StringToDouble(options,(char **) NULL);
768 if (LocaleCompare(keyword,"gravity") == 0)
773 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
777 image->gravity=(GravityType) gravity;
780 if (LocaleCompare(keyword,"green-primary") == 0)
782 flags=ParseGeometry(options,&geometry_info);
783 image->chromaticity.green_primary.x=geometry_info.rho;
784 image->chromaticity.green_primary.y=geometry_info.sigma;
785 if ((flags & SigmaValue) == 0)
786 image->chromaticity.green_primary.y=
787 image->chromaticity.green_primary.x;
790 (void) SetImageProperty(image,keyword,options,exception);
796 if (LocaleCompare(keyword,"id") == 0)
798 (void) CopyMagickString(id,options,MaxTextExtent);
801 if (LocaleCompare(keyword,"iterations") == 0)
803 image->iterations=StringToUnsignedLong(options);
806 (void) SetImageProperty(image,keyword,options,exception);
812 if (LocaleCompare(keyword,"matte") == 0)
817 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
821 image->matte=(MagickBooleanType) matte;
824 if (LocaleCompare(keyword,"matte-color") == 0)
826 (void) QueryColorCompliance(options,AllCompliance,
827 &image->matte_color,exception);
830 if (LocaleCompare(keyword,"montage") == 0)
832 (void) CloneString(&image->montage,options);
835 (void) SetImageProperty(image,keyword,options,exception);
841 if (LocaleCompare(keyword,"opaque") == 0)
846 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
850 image->matte=(MagickBooleanType) matte;
853 if (LocaleCompare(keyword,"orientation") == 0)
858 orientation=ParseCommandOption(MagickOrientationOptions,
859 MagickFalse,options);
862 image->orientation=(OrientationType) orientation;
865 (void) SetImageProperty(image,keyword,options,exception);
871 if (LocaleCompare(keyword,"page") == 0)
876 geometry=GetPageGeometry(options);
877 (void) ParseAbsoluteGeometry(geometry,&image->page);
878 geometry=DestroyString(geometry);
881 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
882 (LocaleNCompare(keyword,"profile-",8) == 0))
887 if (profiles == (LinkedListInfo *) NULL)
888 profiles=NewLinkedList(0);
889 (void) AppendValueToLinkedList(profiles,
890 AcquireString(keyword+8));
891 profile=BlobToStringInfo((const void *) NULL,(size_t)
892 StringToLong(options));
893 if (profile == (StringInfo *) NULL)
894 ThrowReaderException(ResourceLimitError,
895 "MemoryAllocationFailed");
896 (void) SetImageProfile(image,keyword+8,profile,exception);
897 profile=DestroyStringInfo(profile);
900 (void) SetImageProperty(image,keyword,options,exception);
906 if (LocaleCompare(keyword,"quality") == 0)
908 image->quality=StringToUnsignedLong(options);
911 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
912 (LocaleCompare(keyword,"quantum:format") == 0))
917 format=ParseCommandOption(MagickQuantumFormatOptions,
918 MagickFalse,options);
921 quantum_format=(QuantumFormatType) format;
924 (void) SetImageProperty(image,keyword,options,exception);
930 if (LocaleCompare(keyword,"red-primary") == 0)
932 flags=ParseGeometry(options,&geometry_info);
933 image->chromaticity.red_primary.x=geometry_info.rho;
934 image->chromaticity.red_primary.y=geometry_info.sigma;
935 if ((flags & SigmaValue) == 0)
936 image->chromaticity.red_primary.y=
937 image->chromaticity.red_primary.x;
940 if (LocaleCompare(keyword,"rendering-intent") == 0)
945 rendering_intent=ParseCommandOption(MagickIntentOptions,
946 MagickFalse,options);
947 if (rendering_intent < 0)
949 image->rendering_intent=(RenderingIntent) rendering_intent;
952 if (LocaleCompare(keyword,"resolution") == 0)
954 flags=ParseGeometry(options,&geometry_info);
955 image->resolution.x=geometry_info.rho;
956 image->resolution.y=geometry_info.sigma;
957 if ((flags & SigmaValue) == 0)
958 image->resolution.y=image->resolution.x;
961 if (LocaleCompare(keyword,"rows") == 0)
963 image->rows=StringToUnsignedLong(options);
966 (void) SetImageProperty(image,keyword,options,exception);
972 if (LocaleCompare(keyword,"scene") == 0)
974 image->scene=StringToUnsignedLong(options);
977 (void) SetImageProperty(image,keyword,options,exception);
983 if (LocaleCompare(keyword,"ticks-per-second") == 0)
985 image->ticks_per_second=(ssize_t) StringToLong(options);
988 if (LocaleCompare(keyword,"tile-offset") == 0)
993 geometry=GetPageGeometry(options);
994 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
995 geometry=DestroyString(geometry);
998 if (LocaleCompare(keyword,"type") == 0)
1003 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1007 image->type=(ImageType) type;
1010 (void) SetImageProperty(image,keyword,options,exception);
1016 if (LocaleCompare(keyword,"units") == 0)
1021 units=ParseCommandOption(MagickResolutionOptions,
1022 MagickFalse,options);
1025 image->units=(ResolutionType) units;
1028 (void) SetImageProperty(image,keyword,options,exception);
1034 if (LocaleCompare(keyword,"version") == 0)
1036 version=StringToDouble(options,(char **) NULL);
1039 (void) SetImageProperty(image,keyword,options,exception);
1045 if (LocaleCompare(keyword,"white-point") == 0)
1047 flags=ParseGeometry(options,&geometry_info);
1048 image->chromaticity.white_point.x=geometry_info.rho;
1049 image->chromaticity.white_point.y=geometry_info.rho;
1050 if ((flags & SigmaValue) != 0)
1051 image->chromaticity.white_point.y=
1052 image->chromaticity.white_point.x;
1055 (void) SetImageProperty(image,keyword,options,exception);
1060 (void) SetImageProperty(image,keyword,options,exception);
1066 c=ReadBlobByte(image);
1067 while (isspace((int) ((unsigned char) c)) != 0)
1068 c=ReadBlobByte(image);
1070 options=DestroyString(options);
1071 (void) ReadBlobByte(image);
1073 Verify that required image information is defined.
1075 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1076 (image->storage_class == UndefinedClass) ||
1077 (image->columns == 0) || (image->rows == 0))
1078 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1079 if (image->montage != (char *) NULL)
1087 length=MaxTextExtent;
1088 image->directory=AcquireString((char *) NULL);
1093 if ((strlen(image->directory)+MaxTextExtent) >= length)
1096 Allocate more memory for the image directory.
1099 image->directory=(char *) ResizeQuantumMemory(image->directory,
1100 length+MaxTextExtent,sizeof(*image->directory));
1101 if (image->directory == (char *) NULL)
1102 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1103 p=image->directory+strlen(image->directory);
1105 c=ReadBlobByte(image);
1107 } while (c != (int) '\0');
1109 if (profiles != (LinkedListInfo *) NULL)
1118 Read image profiles.
1120 ResetLinkedListIterator(profiles);
1121 name=(const char *) GetNextValueInLinkedList(profiles);
1122 while (name != (const char *) NULL)
1124 profile=GetImageProfile(image,name);
1125 if (profile != (StringInfo *) NULL)
1127 register unsigned char
1130 p=GetStringInfoDatum(profile);
1131 count=ReadBlob(image,GetStringInfoLength(profile),p);
1134 name=(const char *) GetNextValueInLinkedList(profiles);
1136 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1138 image->depth=GetImageQuantumDepth(image,MagickFalse);
1139 if (image->storage_class == PseudoClass)
1142 Create image colormap.
1144 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1145 if (status == MagickFalse)
1146 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1156 Read image colormap from file.
1158 packet_size=(size_t) (3UL*image->depth/8UL);
1159 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1160 packet_size*sizeof(*colormap));
1161 if (colormap == (unsigned char *) NULL)
1162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1163 count=ReadBlob(image,packet_size*image->colors,colormap);
1165 switch (image->depth)
1168 ThrowReaderException(CorruptImageError,
1169 "ImageDepthNotSupported");
1175 for (i=0; i < (ssize_t) image->colors; i++)
1177 p=PushCharPixel(p,&pixel);
1178 image->colormap[i].red=ScaleCharToQuantum(pixel);
1179 p=PushCharPixel(p,&pixel);
1180 image->colormap[i].green=ScaleCharToQuantum(pixel);
1181 p=PushCharPixel(p,&pixel);
1182 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1191 for (i=0; i < (ssize_t) image->colors; i++)
1193 p=PushShortPixel(MSBEndian,p,&pixel);
1194 image->colormap[i].red=ScaleShortToQuantum(pixel);
1195 p=PushShortPixel(MSBEndian,p,&pixel);
1196 image->colormap[i].green=ScaleShortToQuantum(pixel);
1197 p=PushShortPixel(MSBEndian,p,&pixel);
1198 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1207 for (i=0; i < (ssize_t) image->colors; i++)
1209 p=PushLongPixel(MSBEndian,p,&pixel);
1210 image->colormap[i].red=ScaleLongToQuantum(pixel);
1211 p=PushLongPixel(MSBEndian,p,&pixel);
1212 image->colormap[i].green=ScaleLongToQuantum(pixel);
1213 p=PushLongPixel(MSBEndian,p,&pixel);
1214 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1219 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1222 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1223 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1226 Allocate image pixels.
1228 quantum_info=AcquireQuantumInfo(image_info,image);
1229 if (quantum_info == (QuantumInfo *) NULL)
1230 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1231 if (quantum_format != UndefinedQuantumFormat)
1233 status=SetQuantumFormat(image,quantum_info,quantum_format);
1234 if (status == MagickFalse)
1235 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1237 packet_size=(size_t) (quantum_info->depth/8);
1238 if (image->storage_class == DirectClass)
1239 packet_size=(size_t) (3*quantum_info->depth/8);
1240 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1241 packet_size=quantum_info->depth/8;
1242 if (image->matte != MagickFalse)
1243 packet_size+=quantum_info->depth/8;
1244 if (image->colorspace == CMYKColorspace)
1245 packet_size+=quantum_info->depth/8;
1246 if (image->compression == RLECompression)
1248 length=image->columns;
1249 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1250 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1252 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1253 sizeof(*compress_pixels));
1254 if (compress_pixels == (unsigned char *) NULL)
1255 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1259 quantum_type=RGBQuantum;
1260 if (image->matte != MagickFalse)
1261 quantum_type=RGBAQuantum;
1262 if (image->colorspace == CMYKColorspace)
1264 quantum_type=CMYKQuantum;
1265 if (image->matte != MagickFalse)
1266 quantum_type=CMYKAQuantum;
1268 if (image->storage_class == PseudoClass)
1270 quantum_type=IndexQuantum;
1271 if (image->matte != MagickFalse)
1272 quantum_type=IndexAlphaQuantum;
1274 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1276 quantum_type=GrayQuantum;
1277 if (image->matte != MagickFalse)
1278 quantum_type=GrayAlphaQuantum;
1281 switch (image->compression)
1283 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1284 case BZipCompression:
1289 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1290 bzip_info.bzalloc=AcquireBZIPMemory;
1291 bzip_info.bzfree=RelinquishBZIPMemory;
1292 bzip_info.opaque=(void *) NULL;
1293 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1300 #if defined(MAGICKCORE_LZMA_DELEGATE)
1301 case LZMACompression:
1306 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1307 allocator.alloc=AcquireLZMAMemory;
1308 allocator.free=RelinquishLZMAMemory;
1309 lzma_info=initialize_lzma;
1310 lzma_info.allocator=(&allocator);
1311 code=lzma_auto_decoder(&lzma_info,-1,0);
1312 if (code != LZMA_OK)
1317 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1318 case LZWCompression:
1319 case ZipCompression:
1324 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1325 zip_info.zalloc=AcquireZIPMemory;
1326 zip_info.zfree=RelinquishZIPMemory;
1327 zip_info.opaque=(voidpf) NULL;
1328 code=inflateInit(&zip_info);
1334 case RLECompression:
1336 GetPixelInfo(image,&pixel);
1342 pixels=GetQuantumPixels(quantum_info);
1344 for (y=0; y < (ssize_t) image->rows; y++)
1352 if (status == MagickFalse)
1354 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1355 if (q == (Quantum *) NULL)
1357 switch (image->compression)
1359 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1360 case BZipCompression:
1362 bzip_info.next_out=(char *) pixels;
1363 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1366 if (bzip_info.avail_in == 0)
1368 bzip_info.next_in=(char *) compress_pixels;
1369 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1371 length=(size_t) ReadBlobMSBLong(image);
1372 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1373 (unsigned char *) bzip_info.next_in);
1375 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1377 } while (bzip_info.avail_out != 0);
1378 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1379 quantum_type,pixels,exception);
1383 #if defined(MAGICKCORE_LZMA_DELEGATE)
1384 case LZMACompression:
1386 lzma_info.next_out=pixels;
1387 lzma_info.avail_out=packet_size*image->columns;
1393 if (lzma_info.avail_in == 0)
1395 lzma_info.next_in=compress_pixels;
1396 length=(size_t) ReadBlobMSBLong(image);
1397 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1398 (unsigned char *) lzma_info.next_in);
1400 code=lzma_code(&lzma_info,LZMA_RUN);
1406 if (code == LZMA_STREAM_END)
1408 } while (lzma_info.avail_out != 0);
1409 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1410 quantum_type,pixels,exception);
1414 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1415 case LZWCompression:
1416 case ZipCompression:
1418 zip_info.next_out=pixels;
1419 zip_info.avail_out=(uInt) (packet_size*image->columns);
1422 if (zip_info.avail_in == 0)
1424 zip_info.next_in=compress_pixels;
1425 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1427 length=(size_t) ReadBlobMSBLong(image);
1428 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1431 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1433 } while (zip_info.avail_out != 0);
1434 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1435 quantum_type,pixels,exception);
1439 case RLECompression:
1441 for (x=0; x < (ssize_t) image->columns; x++)
1445 count=ReadBlob(image,packet_size,pixels);
1446 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1449 if (image->storage_class == PseudoClass)
1450 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1453 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1454 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1455 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1456 if (image->colorspace == CMYKColorspace)
1457 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1459 if (image->matte != MagickFalse)
1460 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1461 q+=GetPixelChannels(image);
1467 count=ReadBlob(image,packet_size*image->columns,pixels);
1468 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1469 quantum_type,pixels,exception);
1473 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1476 SetQuantumImageType(image,quantum_type);
1477 switch (image->compression)
1479 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1480 case BZipCompression:
1490 offset=SeekBlob(image,-((MagickOffsetType)
1491 bzip_info.avail_in),SEEK_CUR);
1493 ThrowReaderException(CorruptImageError,
1494 "ImproperImageHeader");
1496 code=BZ2_bzDecompressEnd(&bzip_info);
1502 #if defined(MAGICKCORE_LZMA_DELEGATE)
1503 case LZMACompression:
1508 code=lzma_code(&lzma_info,LZMA_FINISH);
1509 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1511 lzma_end(&lzma_info);
1515 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1516 case LZWCompression:
1517 case ZipCompression:
1527 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1530 ThrowReaderException(CorruptImageError,
1531 "ImproperImageHeader");
1533 code=inflateEnd(&zip_info);
1534 if (code != LZMA_OK)
1542 quantum_info=DestroyQuantumInfo(quantum_info);
1543 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1544 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1546 image=DestroyImageList(image);
1547 return((Image *) NULL);
1549 if (EOFBlob(image) != MagickFalse)
1551 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1556 Proceed to next image.
1558 if (image_info->number_scenes != 0)
1559 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1563 c=ReadBlobByte(image);
1564 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1568 Allocate next image structure.
1570 AcquireNextImage(image_info,image,exception);
1571 if (GetNextImageInList(image) == (Image *) NULL)
1573 image=DestroyImageList(image);
1574 return((Image *) NULL);
1576 image=SyncNextImageInList(image);
1577 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1578 GetBlobSize(image));
1579 if (status == MagickFalse)
1583 (void) CloseBlob(image);
1584 return(GetFirstImageInList(image));
1588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1592 % R e g i s t e r M I F F I m a g e %
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1599 % supported formats. The properties include the image format tag, a method to
1600 % read and/or write the format, whether the format supports the saving of more
1601 % than one frame to the same file or blob, whether the format supports native
1602 % in-memory I/O, and a brief description of the format.
1604 % The format of the RegisterMIFFImage method is:
1606 % size_t RegisterMIFFImage(void)
1609 ModuleExport size_t RegisterMIFFImage(void)
1612 version[MaxTextExtent];
1618 #if defined(MagickImageCoderSignatureText)
1619 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1620 #if defined(ZLIB_VERSION)
1621 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1622 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1624 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1625 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1628 entry=SetMagickInfo("MIFF");
1629 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1630 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1631 entry->magick=(IsImageFormatHandler *) IsMIFF;
1632 entry->seekable_stream=MagickTrue;
1633 entry->description=ConstantString("Magick Image File Format");
1634 if (*version != '\0')
1635 entry->version=ConstantString(version);
1636 entry->module=ConstantString("MIFF");
1637 (void) RegisterMagickInfo(entry);
1638 return(MagickImageCoderSignature);
1642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1646 % U n r e g i s t e r M I F F I m a g e %
1650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1652 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1653 % from the list of supported formats.
1655 % The format of the UnregisterMIFFImage method is:
1657 % UnregisterMIFFImage(void)
1660 ModuleExport void UnregisterMIFFImage(void)
1662 (void) UnregisterMagickInfo("MIFF");
1666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1670 % W r i t e M I F F I m a g e %
1674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 % WriteMIFFImage() writes a MIFF image to a file.
1678 % The format of the WriteMIFFImage method is:
1680 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1681 % Image *image,ExceptionInfo *exception)
1683 % Compression code contributed by Kyle Shorter.
1685 % A description of each parameter follows:
1687 % o image_info: the image info.
1689 % o image: the image.
1691 % o exception: return any errors or warnings in this structure.
1695 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1696 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1698 if (image->storage_class != DirectClass)
1700 switch (image->depth)
1704 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1705 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1708 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1711 *pixels++=(unsigned char) pixel->index;
1715 (void) ThrowMagickException(exception,GetMagickModule(),
1716 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1718 switch (image->depth)
1725 if (image->matte != MagickFalse)
1727 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1728 pixels=PopLongPixel(MSBEndian,value,pixels);
1737 if (image->matte != MagickFalse)
1739 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1740 pixels=PopShortPixel(MSBEndian,value,pixels);
1749 if (image->matte != MagickFalse)
1751 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1753 pixels=PopCharPixel(value,pixels);
1758 (void) ThrowMagickException(exception,GetMagickModule(),
1759 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1761 *pixels++=(unsigned char) length;
1764 switch (image->depth)
1771 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1772 pixels=PopLongPixel(MSBEndian,value,pixels);
1773 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1775 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1776 pixels=PopLongPixel(MSBEndian,value,pixels);
1777 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1778 pixels=PopLongPixel(MSBEndian,value,pixels);
1780 if (image->colorspace == CMYKColorspace)
1782 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1783 pixels=PopLongPixel(MSBEndian,value,pixels);
1785 if (image->matte != MagickFalse)
1787 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1788 pixels=PopLongPixel(MSBEndian,value,pixels);
1797 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1798 pixels=PopShortPixel(MSBEndian,value,pixels);
1799 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1801 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1802 pixels=PopShortPixel(MSBEndian,value,pixels);
1803 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1804 pixels=PopShortPixel(MSBEndian,value,pixels);
1806 if (image->colorspace == CMYKColorspace)
1808 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1809 pixels=PopShortPixel(MSBEndian,value,pixels);
1811 if (image->matte != MagickFalse)
1813 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1814 pixels=PopShortPixel(MSBEndian,value,pixels);
1823 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1824 pixels=PopCharPixel(value,pixels);
1825 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1827 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1829 pixels=PopCharPixel(value,pixels);
1830 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1831 pixels=PopCharPixel(value,pixels);
1833 if (image->colorspace == CMYKColorspace)
1835 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1837 pixels=PopCharPixel(value,pixels);
1839 if (image->matte != MagickFalse)
1841 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1843 pixels=PopCharPixel(value,pixels);
1848 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1849 "ImageDepthNotSupported","`%s'",image->filename);
1851 *pixels++=(unsigned char) length;
1855 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1856 Image *image,ExceptionInfo *exception)
1858 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1864 buffer[MaxTextExtent];
1873 #if defined(MAGICKCORE_LZMA_DELEGATE)
1878 initialize_lzma = LZMA_STREAM_INIT,
1913 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1919 Open output image file.
1921 assert(image_info != (const ImageInfo *) NULL);
1922 assert(image_info->signature == MagickSignature);
1923 assert(image != (Image *) NULL);
1924 assert(image->signature == MagickSignature);
1925 if (image->debug != MagickFalse)
1926 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1927 assert(exception != (ExceptionInfo *) NULL);
1928 assert(exception->signature == MagickSignature);
1929 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1930 if (status == MagickFalse)
1936 Allocate image pixels.
1938 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1939 image->depth <= 32 ? 32UL : 64UL;
1940 quantum_info=AcquireQuantumInfo(image_info,image);
1941 if (quantum_info == (QuantumInfo *) NULL)
1942 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1943 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1944 (quantum_info->format == UndefinedQuantumFormat) &&
1945 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1947 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1948 if (status == MagickFalse)
1949 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1951 if ((image->storage_class == PseudoClass) &&
1952 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1953 (void) SetImageStorageClass(image,DirectClass,exception);
1954 if (IsImageGray(image,exception) != MagickFalse)
1955 (void) SetImageColorspace(image,GRAYColorspace,exception);
1956 compression=image->compression;
1957 if (image_info->compression != UndefinedCompression)
1958 compression=image_info->compression;
1959 switch (compression)
1961 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1962 case LZMACompression: compression=NoCompression; break;
1964 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1965 case LZWCompression:
1966 case ZipCompression: compression=NoCompression; break;
1968 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1969 case BZipCompression: compression=NoCompression; break;
1971 case RLECompression:
1973 if (quantum_info->format == FloatingPointQuantumFormat)
1974 compression=NoCompression;
1975 GetPixelInfo(image,&target);
1981 packet_size=(size_t) (quantum_info->depth/8);
1982 if (image->storage_class == DirectClass)
1983 packet_size=(size_t) (3*quantum_info->depth/8);
1984 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1985 packet_size=(size_t) (quantum_info->depth/8);
1986 if (image->matte != MagickFalse)
1987 packet_size+=quantum_info->depth/8;
1988 if (image->colorspace == CMYKColorspace)
1989 packet_size+=quantum_info->depth/8;
1990 if (compression == RLECompression)
1992 length=image->columns;
1993 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1994 packet_size*image->columns));
1995 if ((compression == BZipCompression) || (compression == ZipCompression))
1996 if (length != (size_t) ((unsigned int) length))
1997 compression=NoCompression;
1998 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1999 sizeof(*compress_pixels));
2000 if (compress_pixels == (unsigned char *) NULL)
2001 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2005 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2006 (void) FormatLocaleString(buffer,MaxTextExtent,
2007 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
2008 MagickClassOptions,image->storage_class),(double) image->colors,
2009 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
2010 (void) WriteBlobString(image,buffer);
2011 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2012 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2014 (void) WriteBlobString(image,buffer);
2015 if (image->type != UndefinedType)
2017 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2018 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2019 (void) WriteBlobString(image,buffer);
2021 if (image->colorspace != UndefinedColorspace)
2023 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2024 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2025 (void) WriteBlobString(image,buffer);
2027 if (compression != UndefinedCompression)
2029 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2030 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2031 compression),(double) image->quality);
2032 (void) WriteBlobString(image,buffer);
2034 if (image->units != UndefinedResolution)
2036 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2037 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2038 (void) WriteBlobString(image,buffer);
2040 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2042 (void) FormatLocaleString(buffer,MaxTextExtent,
2043 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2044 (void) WriteBlobString(image,buffer);
2046 if ((image->page.width != 0) || (image->page.height != 0))
2048 (void) FormatLocaleString(buffer,MaxTextExtent,
2049 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2050 image->page.height,(double) image->page.x,(double) image->page.y);
2051 (void) WriteBlobString(image,buffer);
2054 if ((image->page.x != 0) || (image->page.y != 0))
2056 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2057 (long) image->page.x,(long) image->page.y);
2058 (void) WriteBlobString(image,buffer);
2060 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2062 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2063 (long) image->tile_offset.x,(long) image->tile_offset.y);
2064 (void) WriteBlobString(image,buffer);
2066 if ((GetNextImageInList(image) != (Image *) NULL) ||
2067 (GetPreviousImageInList(image) != (Image *) NULL))
2069 if (image->scene == 0)
2070 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2071 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2072 (double) image->delay,(double) image->ticks_per_second);
2074 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2075 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2076 image->scene,(double) image->iterations,(double) image->delay,
2077 (double) image->ticks_per_second);
2078 (void) WriteBlobString(image,buffer);
2082 if (image->scene != 0)
2084 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2085 (double) image->scene);
2086 (void) WriteBlobString(image,buffer);
2088 if (image->iterations != 0)
2090 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2091 (double) image->iterations);
2092 (void) WriteBlobString(image,buffer);
2094 if (image->delay != 0)
2096 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2097 (double) image->delay);
2098 (void) WriteBlobString(image,buffer);
2100 if (image->ticks_per_second != UndefinedTicksPerSecond)
2102 (void) FormatLocaleString(buffer,MaxTextExtent,
2103 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2104 (void) WriteBlobString(image,buffer);
2107 if (image->gravity != UndefinedGravity)
2109 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2110 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2111 (void) WriteBlobString(image,buffer);
2113 if (image->dispose != UndefinedDispose)
2115 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2116 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2117 (void) WriteBlobString(image,buffer);
2119 if (image->rendering_intent != UndefinedIntent)
2121 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2122 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2123 (void) WriteBlobString(image,buffer);
2125 if (image->gamma != 0.0)
2127 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2129 (void) WriteBlobString(image,buffer);
2131 if (image->chromaticity.white_point.x != 0.0)
2134 Note chomaticity points.
2136 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2137 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2138 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2139 image->chromaticity.green_primary.x,
2140 image->chromaticity.green_primary.y,
2141 image->chromaticity.blue_primary.x,
2142 image->chromaticity.blue_primary.y);
2143 (void) WriteBlobString(image,buffer);
2144 (void) FormatLocaleString(buffer,MaxTextExtent,
2145 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2146 image->chromaticity.white_point.y);
2147 (void) WriteBlobString(image,buffer);
2149 if (image->orientation != UndefinedOrientation)
2151 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2152 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2153 (void) WriteBlobString(image,buffer);
2155 if (image->profiles != (void *) NULL)
2164 Write image profiles.
2166 ResetImageProfileIterator(image);
2167 name=GetNextImageProfile(image);
2168 while (name != (const char *) NULL)
2170 profile=GetImageProfile(image,name);
2171 if (profile != (StringInfo *) NULL)
2173 (void) FormatLocaleString(buffer,MaxTextExtent,
2174 "profile:%s=%.20g\n",name,(double)
2175 GetStringInfoLength(profile));
2176 (void) WriteBlobString(image,buffer);
2178 name=GetNextImageProfile(image);
2181 if (image->montage != (char *) NULL)
2183 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2185 (void) WriteBlobString(image,buffer);
2187 if (quantum_info->format == FloatingPointQuantumFormat)
2188 (void) SetImageProperty(image,"quantum:format","floating-point",
2190 ResetImagePropertyIterator(image);
2191 property=GetNextImageProperty(image);
2192 while (property != (const char *) NULL)
2194 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2195 (void) WriteBlobString(image,buffer);
2196 value=GetImageProperty(image,property,exception);
2197 if (value != (const char *) NULL)
2199 for (i=0; i < (ssize_t) strlen(value); i++)
2200 if (isspace((int) ((unsigned char) value[i])) != 0)
2202 if (i == (ssize_t) strlen(value))
2203 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2206 (void) WriteBlobByte(image,'{');
2207 for (i=0; i < (ssize_t) strlen(value); i++)
2209 if (value[i] == (int) '}')
2210 (void) WriteBlobByte(image,'\\');
2211 (void) WriteBlobByte(image,value[i]);
2213 (void) WriteBlobByte(image,'}');
2216 (void) WriteBlobByte(image,'\n');
2217 property=GetNextImageProperty(image);
2219 (void) WriteBlobString(image,"\f\n:\032");
2220 if (image->montage != (char *) NULL)
2223 Write montage tile directory.
2225 if (image->directory != (char *) NULL)
2226 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2228 (void) WriteBlobByte(image,'\0');
2230 if (image->profiles != (void *) NULL)
2241 ResetImageProfileIterator(image);
2242 name=GetNextImageProfile(image);
2243 while (name != (const char *) NULL)
2245 profile=GetImageProfile(image,name);
2246 (void) WriteBlob(image,GetStringInfoLength(profile),
2247 GetStringInfoDatum(profile));
2248 name=GetNextImageProfile(image);
2251 if (image->storage_class == PseudoClass)
2263 packet_size=(size_t) (3*quantum_info->depth/8);
2264 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2265 packet_size*sizeof(*colormap));
2266 if (colormap == (unsigned char *) NULL)
2267 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2269 Write colormap to file.
2272 for (i=0; i < (ssize_t) image->colors; i++)
2274 switch (quantum_info->depth)
2277 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2280 register unsigned int
2283 pixel=ScaleQuantumToLong(image->colormap[i].red);
2284 q=PopLongPixel(MSBEndian,pixel,q);
2285 pixel=ScaleQuantumToLong(image->colormap[i].green);
2286 q=PopLongPixel(MSBEndian,pixel,q);
2287 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2288 q=PopLongPixel(MSBEndian,pixel,q);
2293 register unsigned short
2296 pixel=ScaleQuantumToShort(image->colormap[i].red);
2297 q=PopShortPixel(MSBEndian,pixel,q);
2298 pixel=ScaleQuantumToShort(image->colormap[i].green);
2299 q=PopShortPixel(MSBEndian,pixel,q);
2300 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2301 q=PopShortPixel(MSBEndian,pixel,q);
2306 register unsigned char
2309 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2310 q=PopCharPixel(pixel,q);
2311 pixel=(unsigned char) ScaleQuantumToChar(
2312 image->colormap[i].green);
2313 q=PopCharPixel(pixel,q);
2314 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2315 q=PopCharPixel(pixel,q);
2320 (void) WriteBlob(image,packet_size*image->colors,colormap);
2321 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2324 Write image pixels to file.
2327 switch (compression)
2329 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2330 case BZipCompression:
2335 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2336 bzip_info.bzalloc=AcquireBZIPMemory;
2337 bzip_info.bzfree=RelinquishBZIPMemory;
2338 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2339 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2340 9)),(int) image_info->verbose,0);
2346 #if defined(MAGICKCORE_LZMA_DELEGATE)
2347 case LZMACompression:
2352 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2353 allocator.alloc=AcquireLZMAMemory;
2354 allocator.free=RelinquishLZMAMemory;
2355 lzma_info=initialize_lzma;
2356 lzma_info.allocator=&allocator;
2357 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2358 if (code != LZMA_OK)
2363 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2364 case LZWCompression:
2365 case ZipCompression:
2370 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2371 zip_info.zalloc=AcquireZIPMemory;
2372 zip_info.zfree=RelinquishZIPMemory;
2373 code=deflateInit(&zip_info,(int) (image->quality ==
2374 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2383 quantum_type=GetQuantumType(image,exception);
2384 pixels=GetQuantumPixels(quantum_info);
2385 for (y=0; y < (ssize_t) image->rows; y++)
2387 register const Quantum
2393 if (status == MagickFalse)
2395 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2396 if (p == (const Quantum *) NULL)
2399 switch (compression)
2401 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2402 case BZipCompression:
2404 bzip_info.next_in=(char *) pixels;
2405 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2406 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2407 quantum_type,pixels,exception);
2413 bzip_info.next_out=(char *) compress_pixels;
2414 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2416 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2419 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2422 (void) WriteBlobMSBLong(image,(unsigned int) length);
2423 (void) WriteBlob(image,length,compress_pixels);
2425 } while (bzip_info.avail_in != 0);
2429 #if defined(MAGICKCORE_LZMA_DELEGATE)
2430 case LZMACompression:
2432 lzma_info.next_in=pixels;
2433 lzma_info.avail_in=packet_size*image->columns;
2434 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2435 quantum_type,pixels,exception);
2441 lzma_info.next_out=compress_pixels;
2442 lzma_info.avail_out=packet_size*image->columns;
2443 code=lzma_code(&lzma_info,LZMA_RUN);
2444 if (code != LZMA_OK)
2446 length=(size_t) (lzma_info.next_out-compress_pixels);
2449 (void) WriteBlobMSBLong(image,(unsigned int) length);
2450 (void) WriteBlob(image,length,compress_pixels);
2452 } while (lzma_info.avail_in != 0);
2456 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2457 case LZWCompression:
2458 case ZipCompression:
2460 zip_info.next_in=pixels;
2461 zip_info.avail_in=(uInt) (packet_size*image->columns);
2462 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2463 quantum_type,pixels,exception);
2469 zip_info.next_out=compress_pixels;
2470 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2471 code=deflate(&zip_info,Z_SYNC_FLUSH);
2474 length=(size_t) (zip_info.next_out-compress_pixels);
2477 (void) WriteBlobMSBLong(image,(unsigned int) length);
2478 (void) WriteBlob(image,length,compress_pixels);
2480 } while (zip_info.avail_in != 0);
2484 case RLECompression:
2487 GetPixelInfoPixel(image,p,&pixel);
2488 p+=GetPixelChannels(image);
2489 for (x=1; x < (ssize_t) image->columns; x++)
2491 GetPixelInfoPixel(image,p,&target);
2492 if ((length < 255) &&
2493 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2497 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2500 GetPixelInfoPixel(image,p,&pixel);
2501 p+=GetPixelChannels(image);
2503 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2504 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2509 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2510 quantum_type,pixels,exception);
2511 (void) WriteBlob(image,packet_size*image->columns,pixels);
2515 if (image->previous == (Image *) NULL)
2517 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2519 if (status == MagickFalse)
2523 switch (compression)
2525 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2526 case BZipCompression:
2533 if (status == MagickFalse)
2535 bzip_info.next_out=(char *) compress_pixels;
2536 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2538 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2539 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2542 (void) WriteBlobMSBLong(image,(unsigned int) length);
2543 (void) WriteBlob(image,length,compress_pixels);
2545 if (code == BZ_STREAM_END)
2548 code=BZ2_bzCompressEnd(&bzip_info);
2554 #if defined(MAGICKCORE_LZMA_DELEGATE)
2555 case LZMACompression:
2562 if (status == MagickFalse)
2564 lzma_info.next_out=compress_pixels;
2565 lzma_info.avail_out=packet_size*image->columns;
2566 code=lzma_code(&lzma_info,LZMA_FINISH);
2567 length=(size_t) (lzma_info.next_out-compress_pixels);
2570 (void) WriteBlobMSBLong(image,(unsigned int) length);
2571 (void) WriteBlob(image,length,compress_pixels);
2573 if (code == LZMA_STREAM_END)
2576 lzma_end(&lzma_info);
2580 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2581 case LZWCompression:
2582 case ZipCompression:
2589 if (status == MagickFalse)
2591 zip_info.next_out=compress_pixels;
2592 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2593 code=deflate(&zip_info,Z_FINISH);
2594 length=(size_t) (zip_info.next_out-compress_pixels);
2597 (void) WriteBlobMSBLong(image,(unsigned int) length);
2598 (void) WriteBlob(image,length,compress_pixels);
2600 if (code == Z_STREAM_END)
2603 code=deflateEnd(&zip_info);
2612 quantum_info=DestroyQuantumInfo(quantum_info);
2613 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2614 if (GetNextImageInList(image) == (Image *) NULL)
2616 image=SyncNextImageInList(image);
2617 status=SetImageProgress(image,SaveImagesTag,scene++,
2618 GetImageListLength(image));
2619 if (status == MagickFalse)
2621 } while (image_info->adjoin != MagickFalse);
2622 (void) CloseBlob(image);