2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colormap.h"
50 #include "magick/colormap-private.h"
51 #include "magick/colorspace.h"
52 #include "magick/constitute.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/hashmap.h"
56 #include "magick/geometry.h"
57 #include "magick/image.h"
58 #include "magick/image-private.h"
59 #include "magick/list.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/module.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/option.h"
66 #include "magick/pixel.h"
67 #include "magick/profile.h"
68 #include "magick/property.h"
69 #include "magick/quantum-private.h"
70 #include "magick/static.h"
71 #include "magick/statistic.h"
72 #include "magick/string_.h"
73 #include "magick/string-private.h"
74 #if defined(MAGICKCORE_BZLIB_DELEGATE)
77 #if defined(MAGICKCORE_LZMA_DELEGATE)
80 #if defined(MAGICKCORE_ZLIB_DELEGATE)
94 static MagickBooleanType
95 WriteMIFFImage(const ImageInfo *,Image *);
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % IsMIFF() returns MagickTrue if the image format type, identified by the
109 % magick string, is MIFF.
111 % The format of the IsMIFF method is:
113 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
115 % A description of each parameter follows:
117 % o magick: compare image format pattern against these bytes.
119 % o length: Specifies the length of the magick string.
122 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
126 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 % R e a d M I F F I m a g e %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
143 % memory necessary for the new Image structure and returns a pointer to the
146 % The format of the ReadMIFFImage method is:
148 % Image *ReadMIFFImage(const ImageInfo *image_info,
149 % ExceptionInfo *exception)
151 % Decompression code contributed by Kyle Shorter.
153 % A description of each parameter follows:
155 % o image_info: the image info.
157 % o exception: return any errors or warnings in this structure.
161 #if defined(MAGICKCORE_BZLIB_DELEGATE)
162 static void *AcquireBZIPMemory(void *context,int items,int size)
165 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
169 #if defined(MAGICKCORE_LZMA_DELEGATE)
170 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
173 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
177 #if defined(MAGICKCORE_ZLIB_DELEGATE)
178 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
182 return((voidpf) AcquireQuantumMemory(items,size));
186 static inline size_t MagickMax(const size_t x,const size_t y)
193 static inline size_t MagickMin(const size_t x,const size_t y)
200 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
201 size_t *length,PixelPacket *pixel,IndexPacket *index)
207 if (image->storage_class == PseudoClass)
209 *index=(IndexPacket) 0;
210 switch (image->depth)
214 *index=ConstrainColormapIndex(image,
215 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3));
221 *index=ConstrainColormapIndex(image,(*p << 8) | *(p+1));
227 *index=ConstrainColormapIndex(image,*p);
232 (void) ThrowMagickException(&image->exception,GetMagickModule(),
233 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
235 *pixel=image->colormap[(ssize_t) *index];
236 switch (image->depth)
243 if (image->matte != MagickFalse)
245 p=PushCharPixel(p,&quantum);
246 pixel->opacity=ScaleCharToQuantum(quantum);
255 if (image->matte != MagickFalse)
257 p=PushShortPixel(MSBEndian,p,&quantum);
258 pixel->opacity=(Quantum) (quantum >> (image->depth-
259 MAGICKCORE_QUANTUM_DEPTH));
268 if (image->matte != MagickFalse)
270 p=PushLongPixel(MSBEndian,p,&quantum);
271 pixel->opacity=(Quantum) (quantum >> (image->depth-
272 MAGICKCORE_QUANTUM_DEPTH));
277 (void) ThrowMagickException(&image->exception,GetMagickModule(),
278 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
280 *length=(size_t) (*p++)+1;
283 switch (image->depth)
290 p=PushCharPixel(p,&quantum);
291 pixel->red=ScaleCharToQuantum(quantum);
292 p=PushCharPixel(p,&quantum);
293 pixel->green=ScaleCharToQuantum(quantum);
294 p=PushCharPixel(p,&quantum);
295 pixel->blue=ScaleCharToQuantum(quantum);
296 if (image->matte != MagickFalse)
298 p=PushCharPixel(p,&quantum);
299 pixel->opacity=ScaleCharToQuantum(quantum);
301 if (image->colorspace == CMYKColorspace)
303 p=PushCharPixel(p,&quantum);
304 *index=ScaleCharToQuantum(quantum);
313 p=PushShortPixel(MSBEndian,p,&quantum);
314 pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH));
315 p=PushShortPixel(MSBEndian,p,&quantum);
316 pixel->green=(Quantum) (quantum >> (image->depth-
317 MAGICKCORE_QUANTUM_DEPTH));
318 p=PushShortPixel(MSBEndian,p,&quantum);
319 pixel->blue=(Quantum) (quantum >> (image->depth-
320 MAGICKCORE_QUANTUM_DEPTH));
321 if (image->matte != MagickFalse)
323 p=PushShortPixel(MSBEndian,p,&quantum);
324 pixel->opacity=(Quantum) (quantum >> (image->depth-
325 MAGICKCORE_QUANTUM_DEPTH));
327 if (image->colorspace == CMYKColorspace)
329 p=PushShortPixel(MSBEndian,p,&quantum);
330 *index=(IndexPacket) (quantum >> (image->depth-
331 MAGICKCORE_QUANTUM_DEPTH));
340 p=PushLongPixel(MSBEndian,p,&quantum);
341 pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH));
342 p=PushLongPixel(MSBEndian,p,&quantum);
343 pixel->green=(Quantum) (quantum >> (image->depth-
344 MAGICKCORE_QUANTUM_DEPTH));
345 p=PushLongPixel(MSBEndian,p,&quantum);
346 pixel->blue=(Quantum) (quantum >> (image->depth-
347 MAGICKCORE_QUANTUM_DEPTH));
348 if (image->matte != MagickFalse)
350 p=PushLongPixel(MSBEndian,p,&quantum);
351 pixel->opacity=(Quantum) (quantum >> (image->depth-
352 MAGICKCORE_QUANTUM_DEPTH));
354 if (image->colorspace == CMYKColorspace)
356 p=PushLongPixel(MSBEndian,p,&quantum);
357 *index=(IndexPacket) (quantum >> (image->depth-
358 MAGICKCORE_QUANTUM_DEPTH));
363 (void) ThrowMagickException(&image->exception,GetMagickModule(),
364 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
366 *length=(size_t) (*p++)+1;
369 #if defined(MAGICKCORE_BZLIB_DELEGATE)
370 static void RelinquishBZIPMemory(void *context,void *memory)
373 memory=RelinquishMagickMemory(memory);
377 #if defined(MAGICKCORE_LZMA_DELEGATE)
378 static void RelinquishLZMAMemory(void *context,void *memory)
381 memory=RelinquishMagickMemory(memory);
385 #if defined(MAGICKCORE_ZLIB_DELEGATE)
386 static void RelinquishZIPMemory(voidpf context,voidpf memory)
389 memory=RelinquishMagickMemory(memory);
393 static Image *ReadMIFFImage(const ImageInfo *image_info,
394 ExceptionInfo *exception)
396 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
397 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
398 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
400 #if defined(MAGICKCORE_BZLIB_DELEGATE)
407 keyword[MaxTextExtent],
429 #if defined(MAGICKCORE_LZMA_DELEGATE)
431 initialize_lzma = LZMA_STREAM_INIT,
479 #if defined(MAGICKCORE_ZLIB_DELEGATE)
487 assert(image_info != (const ImageInfo *) NULL);
488 assert(image_info->signature == MagickSignature);
489 if (image_info->debug != MagickFalse)
490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
491 image_info->filename);
492 assert(exception != (ExceptionInfo *) NULL);
493 assert(exception->signature == MagickSignature);
494 image=AcquireImage(image_info);
495 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
496 if (status == MagickFalse)
498 image=DestroyImageList(image);
499 return((Image *) NULL);
502 Decode image header; header terminates one character beyond a ':'.
504 c=ReadBlobByte(image);
506 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
509 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
514 Decode image header; header terminates one character beyond a ':'.
516 length=MaxTextExtent;
517 options=AcquireString((char *) NULL);
518 quantum_format=UndefinedQuantumFormat;
519 profiles=(LinkedListInfo *) NULL;
522 image->compression=NoCompression;
523 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
534 Read comment-- any text between { }.
536 length=MaxTextExtent;
537 comment=AcquireString((char *) NULL);
538 for (p=comment; comment != (char *) NULL; p++)
540 c=ReadBlobByte(image);
541 if ((c == EOF) || (c == (int) '}'))
543 if ((size_t) (p-comment+1) >= length)
547 comment=(char *) ResizeQuantumMemory(comment,length+
548 MaxTextExtent,sizeof(*comment));
549 if (comment == (char *) NULL)
551 p=comment+strlen(comment);
555 if (comment == (char *) NULL)
556 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
558 (void) SetImageProperty(image,"comment",comment);
559 comment=DestroyString(comment);
560 c=ReadBlobByte(image);
563 if (isalnum(c) != MagickFalse)
571 if (isspace((int) ((unsigned char) c)) != 0)
575 if ((size_t) (p-keyword) < (MaxTextExtent-1))
577 c=ReadBlobByte(image);
581 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
582 c=ReadBlobByte(image);
586 Get the keyword value.
588 c=ReadBlobByte(image);
589 while ((c != (int) '}') && (c != EOF))
591 if ((size_t) (p-options+1) >= length)
595 options=(char *) ResizeQuantumMemory(options,length+
596 MaxTextExtent,sizeof(*options));
597 if (options == (char *) NULL)
599 p=options+strlen(options);
601 if (options == (char *) NULL)
602 ThrowReaderException(ResourceLimitError,
603 "MemoryAllocationFailed");
605 c=ReadBlobByte(image);
607 if (isspace((int) ((unsigned char) c)) != 0)
613 (void) CopyMagickString(options,options+1,MaxTextExtent);
615 Assign a value to the specified keyword.
622 if (LocaleCompare(keyword,"background-color") == 0)
624 (void) QueryColorDatabase(options,&image->background_color,
628 if (LocaleCompare(keyword,"blue-primary") == 0)
630 flags=ParseGeometry(options,&geometry_info);
631 image->chromaticity.blue_primary.x=geometry_info.rho;
632 image->chromaticity.blue_primary.y=geometry_info.sigma;
633 if ((flags & SigmaValue) == 0)
634 image->chromaticity.blue_primary.y=
635 image->chromaticity.blue_primary.x;
638 if (LocaleCompare(keyword,"border-color") == 0)
640 (void) QueryColorDatabase(options,&image->border_color,
644 (void) SetImageProperty(image,keyword,options);
650 if (LocaleCompare(keyword,"class") == 0)
655 storage_class=ParseMagickOption(MagickClassOptions,
656 MagickFalse,options);
657 if (storage_class < 0)
659 image->storage_class=(ClassType) storage_class;
662 if (LocaleCompare(keyword,"colors") == 0)
664 colors=StringToUnsignedLong(options);
667 if (LocaleCompare(keyword,"colorspace") == 0)
672 colorspace=ParseMagickOption(MagickColorspaceOptions,
673 MagickFalse,options);
676 image->colorspace=(ColorspaceType) colorspace;
679 if (LocaleCompare(keyword,"compression") == 0)
684 compression=ParseMagickOption(MagickCompressOptions,
685 MagickFalse,options);
688 image->compression=(CompressionType) compression;
691 if (LocaleCompare(keyword,"columns") == 0)
693 image->columns=StringToUnsignedLong(options);
696 (void) SetImageProperty(image,keyword,options);
702 if (LocaleCompare(keyword,"delay") == 0)
704 image->delay=StringToUnsignedLong(options);
707 if (LocaleCompare(keyword,"depth") == 0)
709 image->depth=StringToUnsignedLong(options);
712 if (LocaleCompare(keyword,"dispose") == 0)
717 dispose=ParseMagickOption(MagickDisposeOptions,MagickFalse,
721 image->dispose=(DisposeType) dispose;
724 (void) SetImageProperty(image,keyword,options);
730 if (LocaleCompare(keyword,"endian") == 0)
735 endian=ParseMagickOption(MagickEndianOptions,MagickFalse,
739 image->endian=(EndianType) endian;
742 (void) SetImageProperty(image,keyword,options);
748 if (LocaleCompare(keyword,"gamma") == 0)
750 image->gamma=StringToDouble(options);
753 if (LocaleCompare(keyword,"gravity") == 0)
758 gravity=ParseMagickOption(MagickGravityOptions,MagickFalse,
762 image->gravity=(GravityType) gravity;
765 if (LocaleCompare(keyword,"green-primary") == 0)
767 flags=ParseGeometry(options,&geometry_info);
768 image->chromaticity.green_primary.x=geometry_info.rho;
769 image->chromaticity.green_primary.y=geometry_info.sigma;
770 if ((flags & SigmaValue) == 0)
771 image->chromaticity.green_primary.y=
772 image->chromaticity.green_primary.x;
775 (void) SetImageProperty(image,keyword,options);
781 if (LocaleCompare(keyword,"id") == 0)
783 (void) CopyMagickString(id,options,MaxTextExtent);
786 if (LocaleCompare(keyword,"iterations") == 0)
788 image->iterations=StringToUnsignedLong(options);
791 (void) SetImageProperty(image,keyword,options);
797 if (LocaleCompare(keyword,"matte") == 0)
802 matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
806 image->matte=(MagickBooleanType) matte;
809 if (LocaleCompare(keyword,"matte-color") == 0)
811 (void) QueryColorDatabase(options,&image->matte_color,
815 if (LocaleCompare(keyword,"montage") == 0)
817 (void) CloneString(&image->montage,options);
820 (void) SetImageProperty(image,keyword,options);
826 if (LocaleCompare(keyword,"opaque") == 0)
831 matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
835 image->matte=(MagickBooleanType) matte;
838 if (LocaleCompare(keyword,"orientation") == 0)
843 orientation=ParseMagickOption(MagickOrientationOptions,
844 MagickFalse,options);
847 image->orientation=(OrientationType) orientation;
850 (void) SetImageProperty(image,keyword,options);
856 if (LocaleCompare(keyword,"page") == 0)
861 geometry=GetPageGeometry(options);
862 (void) ParseAbsoluteGeometry(geometry,&image->page);
863 geometry=DestroyString(geometry);
866 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
867 (LocaleNCompare(keyword,"profile-",8) == 0))
872 if (profiles == (LinkedListInfo *) NULL)
873 profiles=NewLinkedList(0);
874 (void) AppendValueToLinkedList(profiles,
875 AcquireString(keyword+8));
876 profile=AcquireStringInfo((size_t) StringToLong(options));
877 (void) SetImageProfile(image,keyword+8,profile);
878 profile=DestroyStringInfo(profile);
881 (void) SetImageProperty(image,keyword,options);
887 if (LocaleCompare(keyword,"quality") == 0)
889 image->quality=StringToUnsignedLong(options);
892 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
893 (LocaleCompare(keyword,"quantum:format") == 0))
898 format=ParseMagickOption(MagickQuantumFormatOptions,
899 MagickFalse,options);
902 quantum_format=(QuantumFormatType) format;
905 (void) SetImageProperty(image,keyword,options);
911 if (LocaleCompare(keyword,"red-primary") == 0)
913 flags=ParseGeometry(options,&geometry_info);
914 image->chromaticity.red_primary.x=geometry_info.rho;
915 image->chromaticity.red_primary.y=geometry_info.sigma;
916 if ((flags & SigmaValue) == 0)
917 image->chromaticity.red_primary.y=
918 image->chromaticity.red_primary.x;
921 if (LocaleCompare(keyword,"rendering-intent") == 0)
926 rendering_intent=ParseMagickOption(MagickIntentOptions,
927 MagickFalse,options);
928 if (rendering_intent < 0)
930 image->rendering_intent=(RenderingIntent) rendering_intent;
933 if (LocaleCompare(keyword,"resolution") == 0)
935 flags=ParseGeometry(options,&geometry_info);
936 image->x_resolution=geometry_info.rho;
937 image->y_resolution=geometry_info.sigma;
938 if ((flags & SigmaValue) == 0)
939 image->y_resolution=image->x_resolution;
942 if (LocaleCompare(keyword,"rows") == 0)
944 image->rows=StringToUnsignedLong(options);
947 (void) SetImageProperty(image,keyword,options);
953 if (LocaleCompare(keyword,"scene") == 0)
955 image->scene=StringToUnsignedLong(options);
958 (void) SetImageProperty(image,keyword,options);
964 if (LocaleCompare(keyword,"ticks-per-second") == 0)
966 image->ticks_per_second=(ssize_t) StringToLong(options);
969 if (LocaleCompare(keyword,"tile-offset") == 0)
974 geometry=GetPageGeometry(options);
975 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
976 geometry=DestroyString(geometry);
979 if (LocaleCompare(keyword,"type") == 0)
984 type=ParseMagickOption(MagickTypeOptions,MagickFalse,
988 image->type=(ImageType) type;
991 (void) SetImageProperty(image,keyword,options);
997 if (LocaleCompare(keyword,"units") == 0)
1002 units=ParseMagickOption(MagickResolutionOptions,MagickFalse,
1006 image->units=(ResolutionType) units;
1009 (void) SetImageProperty(image,keyword,options);
1015 if (LocaleCompare(keyword,"version") == 0)
1017 version=StringToDouble(options);
1020 (void) SetImageProperty(image,keyword,options);
1026 if (LocaleCompare(keyword,"white-point") == 0)
1028 flags=ParseGeometry(options,&geometry_info);
1029 image->chromaticity.white_point.x=geometry_info.rho;
1030 image->chromaticity.white_point.y=geometry_info.rho;
1031 if ((flags & SigmaValue) != 0)
1032 image->chromaticity.white_point.y=
1033 image->chromaticity.white_point.x;
1036 (void) SetImageProperty(image,keyword,options);
1041 (void) SetImageProperty(image,keyword,options);
1047 c=ReadBlobByte(image);
1048 while (isspace((int) ((unsigned char) c)) != 0)
1049 c=ReadBlobByte(image);
1051 options=DestroyString(options);
1052 (void) ReadBlobByte(image);
1054 Verify that required image information is defined.
1056 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1057 (image->storage_class == UndefinedClass) ||
1058 (image->columns == 0) || (image->rows == 0))
1059 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1060 if (image->montage != (char *) NULL)
1068 length=MaxTextExtent;
1069 image->directory=AcquireString((char *) NULL);
1074 if ((strlen(image->directory)+MaxTextExtent) >= length)
1077 Allocate more memory for the image directory.
1080 image->directory=(char *) ResizeQuantumMemory(image->directory,
1081 length+MaxTextExtent,sizeof(*image->directory));
1082 if (image->directory == (char *) NULL)
1083 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1084 p=image->directory+strlen(image->directory);
1086 c=ReadBlobByte(image);
1088 } while (c != (int) '\0');
1090 if (profiles != (LinkedListInfo *) NULL)
1099 Read image profiles.
1101 ResetLinkedListIterator(profiles);
1102 name=(const char *) GetNextValueInLinkedList(profiles);
1103 while (name != (const char *) NULL)
1105 profile=GetImageProfile(image,name);
1106 if (profile != (StringInfo *) NULL)
1108 register unsigned char
1111 p=GetStringInfoDatum(profile);
1112 count=ReadBlob(image,GetStringInfoLength(profile),p);
1114 name=(const char *) GetNextValueInLinkedList(profiles);
1116 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1118 image->depth=GetImageQuantumDepth(image,MagickFalse);
1119 if (image->storage_class == PseudoClass)
1122 Create image colormap.
1124 status=AcquireImageColormap(image,colors != 0 ? colors : 256);
1125 if (status == MagickFalse)
1126 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1136 Read image colormap from file.
1138 packet_size=(size_t) (3UL*image->depth/8UL);
1139 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1140 packet_size*sizeof(*colormap));
1141 if (colormap == (unsigned char *) NULL)
1142 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1143 count=ReadBlob(image,packet_size*image->colors,colormap);
1145 switch (image->depth)
1148 ThrowReaderException(CorruptImageError,
1149 "ImageDepthNotSupported");
1155 for (i=0; i < (ssize_t) image->colors; i++)
1157 p=PushCharPixel(p,&pixel);
1158 image->colormap[i].red=ScaleCharToQuantum(pixel);
1159 p=PushCharPixel(p,&pixel);
1160 image->colormap[i].green=ScaleCharToQuantum(pixel);
1161 p=PushCharPixel(p,&pixel);
1162 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1171 for (i=0; i < (ssize_t) image->colors; i++)
1173 p=PushShortPixel(MSBEndian,p,&pixel);
1174 image->colormap[i].red=ScaleShortToQuantum(pixel);
1175 p=PushShortPixel(MSBEndian,p,&pixel);
1176 image->colormap[i].green=ScaleShortToQuantum(pixel);
1177 p=PushShortPixel(MSBEndian,p,&pixel);
1178 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1187 for (i=0; i < (ssize_t) image->colors; i++)
1189 p=PushLongPixel(MSBEndian,p,&pixel);
1190 image->colormap[i].red=ScaleLongToQuantum(pixel);
1191 p=PushLongPixel(MSBEndian,p,&pixel);
1192 image->colormap[i].green=ScaleLongToQuantum(pixel);
1193 p=PushLongPixel(MSBEndian,p,&pixel);
1194 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1199 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1202 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1203 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1206 Allocate image pixels.
1208 quantum_info=AcquireQuantumInfo(image_info,image);
1209 if (quantum_info == (QuantumInfo *) NULL)
1210 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1211 if (quantum_format != UndefinedQuantumFormat)
1213 status=SetQuantumFormat(image,quantum_info,quantum_format);
1214 if (status == MagickFalse)
1215 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1217 packet_size=(size_t) (quantum_info->depth/8);
1218 if (image->storage_class == DirectClass)
1219 packet_size=(size_t) (3*quantum_info->depth/8);
1220 if (image->matte != MagickFalse)
1221 packet_size+=quantum_info->depth/8;
1222 if (image->colorspace == CMYKColorspace)
1223 packet_size+=quantum_info->depth/8;
1224 if (image->compression == RLECompression)
1226 length=image->columns;
1227 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1228 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1230 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1231 sizeof(*compress_pixels));
1232 if (compress_pixels == (unsigned char *) NULL)
1233 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1237 quantum_type=RGBQuantum;
1238 if (image->matte != MagickFalse)
1239 quantum_type=RGBAQuantum;
1240 if (image->colorspace == CMYKColorspace)
1242 quantum_type=CMYKQuantum;
1243 if (image->matte != MagickFalse)
1244 quantum_type=CMYKAQuantum;
1246 if (image->storage_class == PseudoClass)
1248 quantum_type=IndexQuantum;
1249 if (image->matte != MagickFalse)
1250 quantum_type=IndexAlphaQuantum;
1252 if (image->colorspace == GRAYColorspace)
1254 quantum_type=GrayQuantum;
1255 if (image->matte != MagickFalse)
1256 quantum_type=GrayAlphaQuantum;
1259 switch (image->compression)
1261 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1262 case BZipCompression:
1264 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1265 bzip_info.bzalloc=AcquireBZIPMemory;
1266 bzip_info.bzfree=RelinquishBZIPMemory;
1267 bzip_info.opaque=(void *) NULL;
1268 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1275 #if defined(MAGICKCORE_LZMA_DELEGATE)
1276 case LZMACompression:
1278 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1279 allocator.alloc=AcquireLZMAMemory;
1280 allocator.free=RelinquishLZMAMemory;
1281 lzma_info=initialize_lzma;
1282 lzma_info.allocator=(&allocator);
1283 code=lzma_auto_decoder(&lzma_info,-1,0);
1284 if (code != LZMA_OK)
1289 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1290 case LZWCompression:
1291 case ZipCompression:
1293 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1294 zip_info.zalloc=AcquireZIPMemory;
1295 zip_info.zfree=RelinquishZIPMemory;
1296 zip_info.opaque=(voidpf) NULL;
1297 code=inflateInit(&zip_info);
1303 case RLECompression:
1305 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1306 pixel.opacity=(Quantum) TransparentOpacity;
1307 index=(IndexPacket) 0;
1313 pixels=GetQuantumPixels(quantum_info);
1314 index=(IndexPacket) 0;
1316 for (y=0; y < (ssize_t) image->rows; y++)
1318 register IndexPacket
1324 register PixelPacket
1327 if (status == MagickFalse)
1329 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1330 if (q == (PixelPacket *) NULL)
1332 indexes=GetAuthenticIndexQueue(image);
1333 switch (image->compression)
1335 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1336 case BZipCompression:
1338 bzip_info.next_out=(char *) pixels;
1339 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1342 if (bzip_info.avail_in == 0)
1344 bzip_info.next_in=(char *) compress_pixels;
1345 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1347 length=(size_t) ReadBlobMSBLong(image);
1348 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1349 (unsigned char *) bzip_info.next_in);
1351 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1353 } while (bzip_info.avail_out != 0);
1354 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1355 quantum_type,pixels,exception);
1359 #if defined(MAGICKCORE_LZMA_DELEGATE)
1360 case LZMACompression:
1362 lzma_info.next_out=pixels;
1363 lzma_info.avail_out=packet_size*image->columns;
1366 if (lzma_info.avail_in == 0)
1368 lzma_info.next_in=compress_pixels;
1369 length=(size_t) ReadBlobMSBLong(image);
1370 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1371 (unsigned char *) lzma_info.next_in);
1373 code=lzma_code(&lzma_info,LZMA_RUN);
1379 if (code == LZMA_STREAM_END)
1381 } while (lzma_info.avail_out != 0);
1382 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1383 quantum_type,pixels,exception);
1387 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1388 case LZWCompression:
1389 case ZipCompression:
1391 zip_info.next_out=pixels;
1392 zip_info.avail_out=(uInt) (packet_size*image->columns);
1395 if (zip_info.avail_in == 0)
1397 zip_info.next_in=compress_pixels;
1398 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1400 length=(size_t) ReadBlobMSBLong(image);
1401 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1404 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1406 } while (zip_info.avail_out != 0);
1407 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1408 quantum_type,pixels,exception);
1412 case RLECompression:
1414 for (x=0; x < (ssize_t) image->columns; x++)
1418 count=ReadBlob(image,packet_size,pixels);
1419 PushRunlengthPacket(image,pixels,&length,&pixel,&index);
1422 if ((image->storage_class == PseudoClass) ||
1423 (image->colorspace == CMYKColorspace))
1431 count=ReadBlob(image,packet_size*image->columns,pixels);
1432 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1433 quantum_type,pixels,exception);
1437 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1440 SetQuantumImageType(image,quantum_type);
1441 switch (image->compression)
1443 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1444 case BZipCompression:
1451 offset=SeekBlob(image,-((MagickOffsetType)
1452 bzip_info.avail_in),SEEK_CUR);
1454 ThrowReaderException(CorruptImageError,
1455 "ImproperImageHeader");
1457 code=BZ2_bzDecompressEnd(&bzip_info);
1463 #if defined(MAGICKCORE_LZMA_DELEGATE)
1464 case LZMACompression:
1466 status=lzma_code(&lzma_info,LZMA_FINISH);
1467 if ((status != LZMA_STREAM_END) && (status != LZMA_OK))
1469 lzma_end(&lzma_info);
1473 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1474 case LZWCompression:
1475 case ZipCompression:
1482 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1485 ThrowReaderException(CorruptImageError,
1486 "ImproperImageHeader");
1488 code=inflateEnd(&zip_info);
1489 if (code != LZMA_OK)
1497 quantum_info=DestroyQuantumInfo(quantum_info);
1498 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1499 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1501 image=DestroyImageList(image);
1502 return((Image *) NULL);
1504 if (EOFBlob(image) != MagickFalse)
1506 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1511 Proceed to next image.
1513 if (image_info->number_scenes != 0)
1514 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1518 c=ReadBlobByte(image);
1519 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1523 Allocate next image structure.
1525 AcquireNextImage(image_info,image);
1526 if (GetNextImageInList(image) == (Image *) NULL)
1528 image=DestroyImageList(image);
1529 return((Image *) NULL);
1531 image=SyncNextImageInList(image);
1532 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1533 GetBlobSize(image));
1534 if (status == MagickFalse)
1538 (void) CloseBlob(image);
1539 return(GetFirstImageInList(image));
1543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1547 % R e g i s t e r M I F F I m a g e %
1551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1553 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1554 % supported formats. The properties include the image format tag, a method to
1555 % read and/or write the format, whether the format supports the saving of more
1556 % than one frame to the same file or blob, whether the format supports native
1557 % in-memory I/O, and a brief description of the format.
1559 % The format of the RegisterMIFFImage method is:
1561 % size_t RegisterMIFFImage(void)
1564 ModuleExport size_t RegisterMIFFImage(void)
1567 version[MaxTextExtent];
1573 #if defined(MagickImageCoderSignatureText)
1574 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1575 #if defined(ZLIB_VERSION)
1576 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1577 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1579 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1580 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1583 entry=SetMagickInfo("MIFF");
1584 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1585 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1586 entry->magick=(IsImageFormatHandler *) IsMIFF;
1587 entry->description=ConstantString("Magick Image File Format");
1588 if (*version != '\0')
1589 entry->version=ConstantString(version);
1590 entry->module=ConstantString("MIFF");
1591 (void) RegisterMagickInfo(entry);
1592 return(MagickImageCoderSignature);
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 % U n r e g i s t e r M I F F I m a g e %
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1607 % from the list of supported formats.
1609 % The format of the UnregisterMIFFImage method is:
1611 % UnregisterMIFFImage(void)
1614 ModuleExport void UnregisterMIFFImage(void)
1616 (void) UnregisterMagickInfo("MIFF");
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % W r i t e M I F F I m a g e %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % WriteMIFFImage() writes a MIFF image to a file.
1632 % The format of the WriteMIFFImage method is:
1634 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1637 % Compression code contributed by Kyle Shorter.
1639 % A description of each parameter follows:
1641 % o image_info: the image info.
1643 % o image: the image.
1647 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1648 size_t length,PixelPacket pixel,IndexPacket index)
1650 if (image->storage_class != DirectClass)
1652 switch (image->depth)
1656 *pixels++=(unsigned char) ((size_t) index >> 24);
1657 *pixels++=(unsigned char) ((size_t) index >> 16);
1660 *pixels++=(unsigned char) ((size_t) index >> 8);
1663 *pixels++=(unsigned char) index;
1667 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1668 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1670 switch (image->depth)
1677 if (image->matte != MagickFalse)
1679 value=ScaleQuantumToLong(pixel.opacity);
1680 pixels=PopLongPixel(MSBEndian,value,pixels);
1689 if (image->matte != MagickFalse)
1691 value=ScaleQuantumToShort(pixel.opacity);
1692 pixels=PopShortPixel(MSBEndian,value,pixels);
1701 if (image->matte != MagickFalse)
1703 value=(unsigned char) ScaleQuantumToChar(pixel.opacity);
1704 pixels=PopCharPixel(value,pixels);
1709 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1710 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1712 *pixels++=(unsigned char) length;
1715 switch (image->depth)
1722 value=ScaleQuantumToLong(pixel.red);
1723 pixels=PopLongPixel(MSBEndian,value,pixels);
1724 value=ScaleQuantumToLong(pixel.green);
1725 pixels=PopLongPixel(MSBEndian,value,pixels);
1726 value=ScaleQuantumToLong(pixel.blue);
1727 pixels=PopLongPixel(MSBEndian,value,pixels);
1728 if (image->matte != MagickFalse)
1730 value=ScaleQuantumToLong(pixel.opacity);
1731 pixels=PopLongPixel(MSBEndian,value,pixels);
1733 if (image->colorspace == CMYKColorspace)
1735 value=ScaleQuantumToLong(index);
1736 pixels=PopLongPixel(MSBEndian,value,pixels);
1745 value=ScaleQuantumToShort(pixel.red);
1746 pixels=PopShortPixel(MSBEndian,value,pixels);
1747 value=ScaleQuantumToShort(pixel.green);
1748 pixels=PopShortPixel(MSBEndian,value,pixels);
1749 value=ScaleQuantumToShort(pixel.blue);
1750 pixels=PopShortPixel(MSBEndian,value,pixels);
1751 if (image->matte != MagickFalse)
1753 value=ScaleQuantumToShort(pixel.opacity);
1754 pixels=PopShortPixel(MSBEndian,value,pixels);
1756 if (image->colorspace == CMYKColorspace)
1758 value=ScaleQuantumToShort(index);
1759 pixels=PopShortPixel(MSBEndian,value,pixels);
1768 value=(unsigned char) ScaleQuantumToChar(pixel.red);
1769 pixels=PopCharPixel(value,pixels);
1770 value=(unsigned char) ScaleQuantumToChar(pixel.green);
1771 pixels=PopCharPixel(value,pixels);
1772 value=(unsigned char) ScaleQuantumToChar(pixel.blue);
1773 pixels=PopCharPixel(value,pixels);
1774 if (image->matte != MagickFalse)
1776 value=(unsigned char) ScaleQuantumToChar(pixel.opacity);
1777 pixels=PopCharPixel(value,pixels);
1779 if (image->colorspace == CMYKColorspace)
1781 value=(unsigned char) ScaleQuantumToChar(index);
1782 pixels=PopCharPixel(value,pixels);
1787 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1788 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1790 *pixels++=(unsigned char) length;
1794 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1797 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1803 buffer[MaxTextExtent];
1818 #if defined(MAGICKCORE_LZMA_DELEGATE)
1823 initialize_lzma = LZMA_STREAM_INIT,
1857 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1863 Open output image file.
1865 assert(image_info != (const ImageInfo *) NULL);
1866 assert(image_info->signature == MagickSignature);
1867 assert(image != (Image *) NULL);
1868 assert(image->signature == MagickSignature);
1869 if (image->debug != MagickFalse)
1870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1871 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1872 if (status == MagickFalse)
1879 Allocate image pixels.
1881 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1882 image->depth <= 32 ? 32UL : 64UL;
1883 quantum_info=AcquireQuantumInfo(image_info,image);
1884 if (quantum_info == (QuantumInfo *) NULL)
1885 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1886 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1887 (quantum_info->format == UndefinedQuantumFormat) &&
1888 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
1890 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1891 if (status == MagickFalse)
1892 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1894 if ((image->storage_class == PseudoClass) &&
1895 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1896 (void) SetImageStorageClass(image,DirectClass);
1897 if (IsGrayImage(image,&image->exception) != MagickFalse)
1899 image->storage_class=DirectClass;
1900 (void) SetImageColorspace(image,GRAYColorspace);
1902 compression=image->compression;
1903 if (image_info->compression != UndefinedCompression)
1904 compression=image_info->compression;
1905 switch (compression)
1907 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1908 case LZMACompression: compression=NoCompression; break;
1910 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1911 case LZWCompression:
1912 case ZipCompression: compression=NoCompression; break;
1914 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1915 case BZipCompression: compression=NoCompression; break;
1917 case RLECompression:
1919 if (quantum_info->format == FloatingPointQuantumFormat)
1920 compression=NoCompression;
1926 packet_size=(size_t) (quantum_info->depth/8);
1927 if (image->storage_class == DirectClass)
1928 packet_size=(size_t) (3*quantum_info->depth/8);
1929 if (image->matte != MagickFalse)
1930 packet_size+=quantum_info->depth/8;
1931 if (image->colorspace == CMYKColorspace)
1932 packet_size+=quantum_info->depth/8;
1933 if (compression == RLECompression)
1935 length=image->columns;
1936 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1937 packet_size*image->columns));
1938 if ((compression == BZipCompression) || (compression == ZipCompression))
1939 if (length != (size_t) ((unsigned int) length))
1940 compression=NoCompression;
1941 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1942 sizeof(*compress_pixels));
1943 if (compress_pixels == (unsigned char *) NULL)
1944 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1948 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1949 (void) FormatMagickString(buffer,MaxTextExtent,
1950 "class=%s colors=%.20g matte=%s\n",MagickOptionToMnemonic(
1951 MagickClassOptions,image->storage_class),(double) image->colors,
1952 MagickOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1953 (void) WriteBlobString(image,buffer);
1954 (void) FormatMagickString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1955 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1957 (void) WriteBlobString(image,buffer);
1958 if (image->type != UndefinedType)
1960 (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
1961 MagickOptionToMnemonic(MagickTypeOptions,image->type));
1962 (void) WriteBlobString(image,buffer);
1964 if (image->colorspace != UndefinedColorspace)
1966 (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
1967 MagickOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1968 (void) WriteBlobString(image,buffer);
1970 if (compression != UndefinedCompression)
1972 (void) FormatMagickString(buffer,MaxTextExtent,"compression=%s "
1973 "quality=%.20g\n",MagickOptionToMnemonic(MagickCompressOptions,
1974 compression),(double) image->quality);
1975 (void) WriteBlobString(image,buffer);
1977 if (image->units != UndefinedResolution)
1979 (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n",
1980 MagickOptionToMnemonic(MagickResolutionOptions,image->units));
1981 (void) WriteBlobString(image,buffer);
1983 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1985 (void) FormatMagickString(buffer,MaxTextExtent,
1986 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
1987 (void) WriteBlobString(image,buffer);
1989 if ((image->page.width != 0) || (image->page.height != 0))
1991 (void) FormatMagickString(buffer,MaxTextExtent,
1992 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1993 image->page.height,(double) image->page.x,(double) image->page.y);
1994 (void) WriteBlobString(image,buffer);
1997 if ((image->page.x != 0) || (image->page.y != 0))
1999 (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2000 (long) image->page.x,(long) image->page.y);
2001 (void) WriteBlobString(image,buffer);
2003 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2005 (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2006 (long) image->tile_offset.x,(long) image->tile_offset.y);
2007 (void) WriteBlobString(image,buffer);
2009 if ((GetNextImageInList(image) != (Image *) NULL) ||
2010 (GetPreviousImageInList(image) != (Image *) NULL))
2012 if (image->scene == 0)
2013 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g "
2014 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2015 (double) image->delay,(double) image->ticks_per_second);
2017 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g "
2018 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2019 image->scene,(double) image->iterations,(double) image->delay,
2020 (double) image->ticks_per_second);
2021 (void) WriteBlobString(image,buffer);
2025 if (image->scene != 0)
2027 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g\n",
2028 (double) image->scene);
2029 (void) WriteBlobString(image,buffer);
2031 if (image->iterations != 0)
2033 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g\n",
2034 (double) image->iterations);
2035 (void) WriteBlobString(image,buffer);
2037 if (image->delay != 0)
2039 (void) FormatMagickString(buffer,MaxTextExtent,"delay=%.20g\n",
2040 (double) image->delay);
2041 (void) WriteBlobString(image,buffer);
2043 if (image->ticks_per_second != UndefinedTicksPerSecond)
2045 (void) FormatMagickString(buffer,MaxTextExtent,
2046 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2047 (void) WriteBlobString(image,buffer);
2050 if (image->gravity != UndefinedGravity)
2052 (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
2053 MagickOptionToMnemonic(MagickGravityOptions,image->gravity));
2054 (void) WriteBlobString(image,buffer);
2056 if (image->dispose != UndefinedDispose)
2058 (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
2059 MagickOptionToMnemonic(MagickDisposeOptions,image->dispose));
2060 (void) WriteBlobString(image,buffer);
2062 if (image->rendering_intent != UndefinedIntent)
2064 (void) FormatMagickString(buffer,MaxTextExtent,
2065 "rendering-intent=%s\n",
2066 MagickOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2067 (void) WriteBlobString(image,buffer);
2069 if (image->gamma != 0.0)
2071 (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n",
2073 (void) WriteBlobString(image,buffer);
2075 if (image->chromaticity.white_point.x != 0.0)
2078 Note chomaticity points.
2080 (void) FormatMagickString(buffer,MaxTextExtent,"red-primary=%g,"
2081 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2082 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2083 image->chromaticity.green_primary.x,
2084 image->chromaticity.green_primary.y,
2085 image->chromaticity.blue_primary.x,
2086 image->chromaticity.blue_primary.y);
2087 (void) WriteBlobString(image,buffer);
2088 (void) FormatMagickString(buffer,MaxTextExtent,
2089 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2090 image->chromaticity.white_point.y);
2091 (void) WriteBlobString(image,buffer);
2093 if (image->orientation != UndefinedOrientation)
2095 (void) FormatMagickString(buffer,MaxTextExtent,"orientation=%s\n",
2096 MagickOptionToMnemonic(MagickOrientationOptions,image->orientation));
2097 (void) WriteBlobString(image,buffer);
2099 if (image->profiles != (void *) NULL)
2108 Write image profiles.
2110 ResetImageProfileIterator(image);
2111 name=GetNextImageProfile(image);
2112 while (name != (const char *) NULL)
2114 profile=GetImageProfile(image,name);
2115 if (profile != (StringInfo *) NULL)
2117 (void) FormatMagickString(buffer,MaxTextExtent,
2118 "profile:%s=%.20g\n",name,(double)
2119 GetStringInfoLength(profile));
2120 (void) WriteBlobString(image,buffer);
2122 name=GetNextImageProfile(image);
2125 if (image->montage != (char *) NULL)
2127 (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n",
2129 (void) WriteBlobString(image,buffer);
2131 if (quantum_info->format == FloatingPointQuantumFormat)
2132 (void) SetImageProperty(image,"quantum:format","floating-point");
2133 ResetImagePropertyIterator(image);
2134 property=GetNextImageProperty(image);
2135 while (property != (const char *) NULL)
2137 (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property);
2138 (void) WriteBlobString(image,buffer);
2139 value=GetImageProperty(image,property);
2140 if (value != (const char *) NULL)
2142 for (i=0; i < (ssize_t) strlen(value); i++)
2143 if (isspace((int) ((unsigned char) value[i])) != 0)
2145 if (i <= (ssize_t) strlen(value))
2146 (void) WriteBlobByte(image,'{');
2147 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2148 if (i <= (ssize_t) strlen(value))
2149 (void) WriteBlobByte(image,'}');
2151 (void) WriteBlobByte(image,'\n');
2152 property=GetNextImageProperty(image);
2154 (void) WriteBlobString(image,"\f\n:\032");
2155 if (image->montage != (char *) NULL)
2158 Write montage tile directory.
2160 if (image->directory != (char *) NULL)
2161 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2163 (void) WriteBlobByte(image,'\0');
2165 if (image->profiles != (void *) NULL)
2176 ResetImageProfileIterator(image);
2177 name=GetNextImageProfile(image);
2178 while (name != (const char *) NULL)
2180 profile=GetImageProfile(image,name);
2181 (void) WriteBlob(image,GetStringInfoLength(profile),
2182 GetStringInfoDatum(profile));
2183 name=GetNextImageProfile(image);
2186 if (image->storage_class == PseudoClass)
2198 packet_size=(size_t) (3*quantum_info->depth/8);
2199 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2200 packet_size*sizeof(*colormap));
2201 if (colormap == (unsigned char *) NULL)
2202 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2204 Write colormap to file.
2207 for (i=0; i < (ssize_t) image->colors; i++)
2209 switch (quantum_info->depth)
2212 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2215 register unsigned int
2218 pixel=ScaleQuantumToLong(image->colormap[i].red);
2219 q=PopLongPixel(MSBEndian,pixel,q);
2220 pixel=ScaleQuantumToLong(image->colormap[i].green);
2221 q=PopLongPixel(MSBEndian,pixel,q);
2222 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2223 q=PopLongPixel(MSBEndian,pixel,q);
2228 register unsigned short
2231 pixel=ScaleQuantumToShort(image->colormap[i].red);
2232 q=PopShortPixel(MSBEndian,pixel,q);
2233 pixel=ScaleQuantumToShort(image->colormap[i].green);
2234 q=PopShortPixel(MSBEndian,pixel,q);
2235 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2236 q=PopShortPixel(MSBEndian,pixel,q);
2241 register unsigned char
2244 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2245 q=PopCharPixel(pixel,q);
2246 pixel=(unsigned char) ScaleQuantumToChar(
2247 image->colormap[i].green);
2248 q=PopCharPixel(pixel,q);
2249 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2250 q=PopCharPixel(pixel,q);
2255 (void) WriteBlob(image,packet_size*image->colors,colormap);
2256 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2259 Write image pixels to file.
2262 switch (compression)
2264 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2265 case BZipCompression:
2267 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2268 bzip_info.bzalloc=AcquireBZIPMemory;
2269 bzip_info.bzfree=RelinquishBZIPMemory;
2270 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2271 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2272 9)),(int) image_info->verbose,0);
2278 #if defined(MAGICKCORE_LZMA_DELEGATE)
2279 case LZMACompression:
2281 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2282 allocator.alloc=AcquireLZMAMemory;
2283 allocator.free=RelinquishLZMAMemory;
2284 lzma_info=initialize_lzma;
2285 lzma_info.allocator=&allocator;
2286 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2287 if (code != LZMA_OK)
2292 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2293 case LZWCompression:
2294 case ZipCompression:
2296 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2297 zip_info.zalloc=AcquireZIPMemory;
2298 zip_info.zfree=RelinquishZIPMemory;
2299 code=deflateInit(&zip_info,(int) (image->quality ==
2300 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2309 quantum_type=GetQuantumType(image,&image->exception);
2310 pixels=GetQuantumPixels(quantum_info);
2311 for (y=0; y < (ssize_t) image->rows; y++)
2313 register const IndexPacket
2316 register const PixelPacket
2322 if (status == MagickFalse)
2324 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2325 if (p == (const PixelPacket *) NULL)
2327 indexes=GetVirtualIndexQueue(image);
2329 switch (compression)
2331 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2332 case BZipCompression:
2334 bzip_info.next_in=(char *) pixels;
2335 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2336 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2337 quantum_info,quantum_type,pixels,&image->exception);
2340 bzip_info.next_out=(char *) compress_pixels;
2341 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2343 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2346 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2349 (void) WriteBlobMSBLong(image,(unsigned int) length);
2350 (void) WriteBlob(image,length,compress_pixels);
2352 } while (bzip_info.avail_in != 0);
2356 #if defined(MAGICKCORE_LZMA_DELEGATE)
2357 case LZMACompression:
2359 lzma_info.next_in=pixels;
2360 lzma_info.avail_in=packet_size*image->columns;
2361 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2362 quantum_info,quantum_type,pixels,&image->exception);
2365 lzma_info.next_out=compress_pixels;
2366 lzma_info.avail_out=packet_size*image->columns;
2367 code=lzma_code(&lzma_info,LZMA_RUN);
2368 if (code != LZMA_OK)
2370 length=(size_t) (lzma_info.next_out-compress_pixels);
2373 (void) WriteBlobMSBLong(image,(unsigned int) length);
2374 (void) WriteBlob(image,length,compress_pixels);
2376 } while (lzma_info.avail_in != 0);
2380 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2381 case LZWCompression:
2382 case ZipCompression:
2384 zip_info.next_in=pixels;
2385 zip_info.avail_in=(uInt) (packet_size*image->columns);
2386 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2387 quantum_info,quantum_type,pixels,&image->exception);
2390 zip_info.next_out=compress_pixels;
2391 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2392 code=deflate(&zip_info,Z_SYNC_FLUSH);
2395 length=(size_t) (zip_info.next_out-compress_pixels);
2398 (void) WriteBlobMSBLong(image,(unsigned int) length);
2399 (void) WriteBlob(image,length,compress_pixels);
2401 } while (zip_info.avail_in != 0);
2405 case RLECompression:
2408 index=(IndexPacket) 0;
2409 if (indexes != (IndexPacket *) NULL)
2412 for (x=0; x < (ssize_t) image->columns; x++)
2414 if ((length < 255) && (x < (ssize_t) (image->columns-1)) &&
2415 (IsColorEqual(p,&pixel) != MagickFalse) &&
2416 ((image->matte == MagickFalse) ||
2417 (p->opacity == pixel.opacity)) &&
2418 ((indexes == (IndexPacket *) NULL) || (index == indexes[x])))
2423 q=PopRunlengthPacket(image,q,length,pixel,index);
2427 if (indexes != (IndexPacket *) NULL)
2431 q=PopRunlengthPacket(image,q,length,pixel,index);
2432 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2437 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2438 quantum_info,quantum_type,pixels,&image->exception);
2439 (void) WriteBlob(image,packet_size*image->columns,pixels);
2443 if (image->previous == (Image *) NULL)
2445 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2447 if (status == MagickFalse)
2451 switch (compression)
2453 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2454 case BZipCompression:
2458 if (status == MagickFalse)
2460 bzip_info.next_out=(char *) compress_pixels;
2461 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2463 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2466 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2469 (void) WriteBlobMSBLong(image,(unsigned int) length);
2470 (void) WriteBlob(image,length,compress_pixels);
2472 if (code == BZ_STREAM_END)
2475 code=BZ2_bzCompressEnd(&bzip_info);
2481 #if defined(MAGICKCORE_LZMA_DELEGATE)
2482 case LZMACompression:
2486 if (status == MagickFalse)
2488 lzma_info.next_out=compress_pixels;
2489 lzma_info.avail_out=packet_size*image->columns;
2490 code=lzma_code(&lzma_info,LZMA_FINISH);
2491 if (code != LZMA_OK)
2493 length=(size_t) (lzma_info.next_out-compress_pixels);
2496 (void) WriteBlobMSBLong(image,(unsigned int) length);
2497 (void) WriteBlob(image,length,compress_pixels);
2499 if (code == LZMA_STREAM_END)
2502 lzma_end(&lzma_info);
2506 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2507 case LZWCompression:
2508 case ZipCompression:
2512 if (status == MagickFalse)
2514 zip_info.next_out=compress_pixels;
2515 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
2517 code=deflate(&zip_info,Z_FINISH);
2520 length=(size_t) (zip_info.next_out-compress_pixels);
2523 (void) WriteBlobMSBLong(image,(unsigned int) length);
2524 (void) WriteBlob(image,length,compress_pixels);
2526 if (code == Z_STREAM_END)
2529 code=deflateEnd(&zip_info);
2538 quantum_info=DestroyQuantumInfo(quantum_info);
2539 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2540 if (GetNextImageInList(image) == (Image *) NULL)
2542 image=SyncNextImageInList(image);
2543 status=SetImageProgress(image,SaveImagesTag,scene++,
2544 GetImageListLength(image));
2545 if (status == MagickFalse)
2547 } while (image_info->adjoin != MagickFalse);
2548 (void) CloseBlob(image);