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 "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/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/hashmap.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/profile.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/static.h"
72 #include "MagickCore/statistic.h"
73 #include "MagickCore/string_.h"
74 #include "MagickCore/string-private.h"
75 #if defined(MAGICKCORE_BZLIB_DELEGATE)
78 #if defined(MAGICKCORE_LZMA_DELEGATE)
81 #if defined(MAGICKCORE_ZLIB_DELEGATE)
95 static MagickBooleanType
96 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % IsMIFF() returns MagickTrue if the image format type, identified by the
110 % magick string, is MIFF.
112 % The format of the IsMIFF method is:
114 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
116 % A description of each parameter follows:
118 % o magick: compare image format pattern against these bytes.
120 % o length: Specifies the length of the magick string.
123 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
127 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 % R e a d M I F F I m a g e %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
144 % memory necessary for the new Image structure and returns a pointer to the
147 % The format of the ReadMIFFImage method is:
149 % Image *ReadMIFFImage(const ImageInfo *image_info,
150 % ExceptionInfo *exception)
152 % Decompression code contributed by Kyle Shorter.
154 % A description of each parameter follows:
156 % o image_info: the image info.
158 % o exception: return any errors or warnings in this structure.
162 #if defined(MAGICKCORE_BZLIB_DELEGATE)
163 static void *AcquireBZIPMemory(void *context,int items,int size)
166 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
170 #if defined(MAGICKCORE_LZMA_DELEGATE)
171 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
174 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
178 #if defined(MAGICKCORE_ZLIB_DELEGATE)
179 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
183 return((voidpf) AcquireQuantumMemory(items,size));
187 static inline size_t MagickMax(const size_t x,const size_t y)
194 static inline size_t MagickMin(const size_t x,const size_t y)
201 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
202 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
208 if (image->storage_class == PseudoClass)
211 switch (image->depth)
215 pixel->index=ConstrainColormapIndex(image,
216 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3),exception);
222 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
229 pixel->index=ConstrainColormapIndex(image,*p,exception);
234 (void) ThrowMagickException(exception,GetMagickModule(),
235 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
237 switch (image->depth)
244 if (image->matte != MagickFalse)
246 p=PushCharPixel(p,&quantum);
247 pixel->alpha=ScaleCharToQuantum(quantum);
256 if (image->matte != MagickFalse)
258 p=PushShortPixel(MSBEndian,p,&quantum);
259 pixel->alpha=(Quantum) (quantum >> (image->depth-
260 MAGICKCORE_QUANTUM_DEPTH));
269 if (image->matte != MagickFalse)
271 p=PushLongPixel(MSBEndian,p,&quantum);
272 pixel->alpha=(Quantum) (quantum >> (image->depth-
273 MAGICKCORE_QUANTUM_DEPTH));
278 (void) ThrowMagickException(exception,GetMagickModule(),
279 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
281 *length=(size_t) (*p++)+1;
284 switch (image->depth)
291 p=PushCharPixel(p,&quantum);
292 pixel->red=ScaleCharToQuantum(quantum);
293 p=PushCharPixel(p,&quantum);
294 pixel->green=ScaleCharToQuantum(quantum);
295 p=PushCharPixel(p,&quantum);
296 pixel->blue=ScaleCharToQuantum(quantum);
297 if (image->colorspace == CMYKColorspace)
299 p=PushCharPixel(p,&quantum);
300 pixel->black=ScaleCharToQuantum(quantum);
302 if (image->matte != MagickFalse)
304 p=PushCharPixel(p,&quantum);
305 pixel->alpha=ScaleCharToQuantum(quantum);
314 p=PushShortPixel(MSBEndian,p,&quantum);
315 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
316 p=PushShortPixel(MSBEndian,p,&quantum);
317 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
318 p=PushShortPixel(MSBEndian,p,&quantum);
319 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
320 if (image->colorspace == CMYKColorspace)
322 p=PushShortPixel(MSBEndian,p,&quantum);
323 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
325 if (image->matte != MagickFalse)
327 p=PushShortPixel(MSBEndian,p,&quantum);
328 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
337 p=PushLongPixel(MSBEndian,p,&quantum);
338 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
339 p=PushLongPixel(MSBEndian,p,&quantum);
340 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
341 p=PushLongPixel(MSBEndian,p,&quantum);
342 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
343 if (image->colorspace == CMYKColorspace)
345 p=PushLongPixel(MSBEndian,p,&quantum);
346 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
348 if (image->matte != MagickFalse)
350 p=PushLongPixel(MSBEndian,p,&quantum);
351 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
356 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
357 "ImageDepthNotSupported","`%s'",image->filename);
359 *length=(size_t) (*p++)+1;
362 #if defined(MAGICKCORE_BZLIB_DELEGATE)
363 static void RelinquishBZIPMemory(void *context,void *memory)
366 memory=RelinquishMagickMemory(memory);
370 #if defined(MAGICKCORE_LZMA_DELEGATE)
371 static void RelinquishLZMAMemory(void *context,void *memory)
374 memory=RelinquishMagickMemory(memory);
378 #if defined(MAGICKCORE_ZLIB_DELEGATE)
379 static void RelinquishZIPMemory(voidpf context,voidpf memory)
382 memory=RelinquishMagickMemory(memory);
386 static Image *ReadMIFFImage(const ImageInfo *image_info,
387 ExceptionInfo *exception)
389 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
390 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
391 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
393 #if defined(MAGICKCORE_BZLIB_DELEGATE)
400 keyword[MaxTextExtent],
419 #if defined(MAGICKCORE_LZMA_DELEGATE)
421 initialize_lzma = LZMA_STREAM_INIT,
469 #if defined(MAGICKCORE_ZLIB_DELEGATE)
477 assert(image_info != (const ImageInfo *) NULL);
478 assert(image_info->signature == MagickSignature);
479 if (image_info->debug != MagickFalse)
480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
481 image_info->filename);
482 assert(exception != (ExceptionInfo *) NULL);
483 assert(exception->signature == MagickSignature);
484 image=AcquireImage(image_info,exception);
485 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
486 if (status == MagickFalse)
488 image=DestroyImageList(image);
489 return((Image *) NULL);
492 Decode image header; header terminates one character beyond a ':'.
494 c=ReadBlobByte(image);
496 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
499 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
504 Decode image header; header terminates one character beyond a ':'.
506 length=MaxTextExtent;
507 options=AcquireString((char *) NULL);
508 quantum_format=UndefinedQuantumFormat;
509 profiles=(LinkedListInfo *) NULL;
512 image->compression=NoCompression;
513 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
524 Read comment-- any text between { }.
526 length=MaxTextExtent;
527 comment=AcquireString((char *) NULL);
528 for (p=comment; comment != (char *) NULL; p++)
530 c=ReadBlobByte(image);
531 if ((c == EOF) || (c == (int) '}'))
533 if ((size_t) (p-comment+1) >= length)
537 comment=(char *) ResizeQuantumMemory(comment,length+
538 MaxTextExtent,sizeof(*comment));
539 if (comment == (char *) NULL)
541 p=comment+strlen(comment);
545 if (comment == (char *) NULL)
546 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
548 (void) SetImageProperty(image,"comment",comment,exception);
549 comment=DestroyString(comment);
550 c=ReadBlobByte(image);
553 if (isalnum(c) != MagickFalse)
563 if ((size_t) (p-keyword) < (MaxTextExtent-1))
565 c=ReadBlobByte(image);
569 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
570 c=ReadBlobByte(image);
574 Get the keyword value.
576 c=ReadBlobByte(image);
577 while ((c != (int) '}') && (c != EOF))
579 if ((size_t) (p-options+1) >= length)
583 options=(char *) ResizeQuantumMemory(options,length+
584 MaxTextExtent,sizeof(*options));
585 if (options == (char *) NULL)
587 p=options+strlen(options);
589 if (options == (char *) NULL)
590 ThrowReaderException(ResourceLimitError,
591 "MemoryAllocationFailed");
593 c=ReadBlobByte(image);
595 if (isspace((int) ((unsigned char) c)) != 0)
601 (void) CopyMagickString(options,options+1,MaxTextExtent);
603 Assign a value to the specified keyword.
610 if (LocaleCompare(keyword,"background-color") == 0)
612 (void) QueryColorCompliance(options,AllCompliance,
613 &image->background_color,exception);
616 if (LocaleCompare(keyword,"blue-primary") == 0)
618 flags=ParseGeometry(options,&geometry_info);
619 image->chromaticity.blue_primary.x=geometry_info.rho;
620 image->chromaticity.blue_primary.y=geometry_info.sigma;
621 if ((flags & SigmaValue) == 0)
622 image->chromaticity.blue_primary.y=
623 image->chromaticity.blue_primary.x;
626 if (LocaleCompare(keyword,"border-color") == 0)
628 (void) QueryColorCompliance(options,AllCompliance,
629 &image->border_color,exception);
632 (void) SetImageProperty(image,keyword,options,exception);
638 if (LocaleCompare(keyword,"class") == 0)
643 storage_class=ParseCommandOption(MagickClassOptions,
644 MagickFalse,options);
645 if (storage_class < 0)
647 image->storage_class=(ClassType) storage_class;
650 if (LocaleCompare(keyword,"colors") == 0)
652 colors=StringToUnsignedLong(options);
655 if (LocaleCompare(keyword,"colorspace") == 0)
660 colorspace=ParseCommandOption(MagickColorspaceOptions,
661 MagickFalse,options);
664 image->colorspace=(ColorspaceType) colorspace;
667 if (LocaleCompare(keyword,"compression") == 0)
672 compression=ParseCommandOption(MagickCompressOptions,
673 MagickFalse,options);
676 image->compression=(CompressionType) compression;
679 if (LocaleCompare(keyword,"columns") == 0)
681 image->columns=StringToUnsignedLong(options);
684 (void) SetImageProperty(image,keyword,options,exception);
690 if (LocaleCompare(keyword,"delay") == 0)
692 image->delay=StringToUnsignedLong(options);
695 if (LocaleCompare(keyword,"depth") == 0)
697 image->depth=StringToUnsignedLong(options);
700 if (LocaleCompare(keyword,"dispose") == 0)
705 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
709 image->dispose=(DisposeType) dispose;
712 (void) SetImageProperty(image,keyword,options,exception);
718 if (LocaleCompare(keyword,"endian") == 0)
723 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
727 image->endian=(EndianType) endian;
730 (void) SetImageProperty(image,keyword,options,exception);
736 if (LocaleCompare(keyword,"gamma") == 0)
738 image->gamma=InterpretLocaleValue(options,(char **) NULL);
741 if (LocaleCompare(keyword,"gravity") == 0)
746 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
750 image->gravity=(GravityType) gravity;
753 if (LocaleCompare(keyword,"green-primary") == 0)
755 flags=ParseGeometry(options,&geometry_info);
756 image->chromaticity.green_primary.x=geometry_info.rho;
757 image->chromaticity.green_primary.y=geometry_info.sigma;
758 if ((flags & SigmaValue) == 0)
759 image->chromaticity.green_primary.y=
760 image->chromaticity.green_primary.x;
763 (void) SetImageProperty(image,keyword,options,exception);
769 if (LocaleCompare(keyword,"id") == 0)
771 (void) CopyMagickString(id,options,MaxTextExtent);
774 if (LocaleCompare(keyword,"iterations") == 0)
776 image->iterations=StringToUnsignedLong(options);
779 (void) SetImageProperty(image,keyword,options,exception);
785 if (LocaleCompare(keyword,"matte") == 0)
790 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
794 image->matte=(MagickBooleanType) matte;
797 if (LocaleCompare(keyword,"matte-color") == 0)
799 (void) QueryColorCompliance(options,AllCompliance,
800 &image->matte_color,exception);
803 if (LocaleCompare(keyword,"montage") == 0)
805 (void) CloneString(&image->montage,options);
808 (void) SetImageProperty(image,keyword,options,exception);
814 if (LocaleCompare(keyword,"opaque") == 0)
819 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
823 image->matte=(MagickBooleanType) matte;
826 if (LocaleCompare(keyword,"orientation") == 0)
831 orientation=ParseCommandOption(MagickOrientationOptions,
832 MagickFalse,options);
835 image->orientation=(OrientationType) orientation;
838 (void) SetImageProperty(image,keyword,options,exception);
844 if (LocaleCompare(keyword,"page") == 0)
849 geometry=GetPageGeometry(options);
850 (void) ParseAbsoluteGeometry(geometry,&image->page);
851 geometry=DestroyString(geometry);
854 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
855 (LocaleNCompare(keyword,"profile-",8) == 0))
860 if (profiles == (LinkedListInfo *) NULL)
861 profiles=NewLinkedList(0);
862 (void) AppendValueToLinkedList(profiles,
863 AcquireString(keyword+8));
864 profile=BlobToStringInfo((const void *) NULL,(size_t)
865 StringToLong(options));
866 if (profile == (StringInfo *) NULL)
867 ThrowReaderException(ResourceLimitError,
868 "MemoryAllocationFailed");
869 (void) SetImageProfile(image,keyword+8,profile,exception);
870 profile=DestroyStringInfo(profile);
873 (void) SetImageProperty(image,keyword,options,exception);
879 if (LocaleCompare(keyword,"quality") == 0)
881 image->quality=StringToUnsignedLong(options);
884 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
885 (LocaleCompare(keyword,"quantum:format") == 0))
890 format=ParseCommandOption(MagickQuantumFormatOptions,
891 MagickFalse,options);
894 quantum_format=(QuantumFormatType) format;
897 (void) SetImageProperty(image,keyword,options,exception);
903 if (LocaleCompare(keyword,"red-primary") == 0)
905 flags=ParseGeometry(options,&geometry_info);
906 image->chromaticity.red_primary.x=geometry_info.rho;
907 image->chromaticity.red_primary.y=geometry_info.sigma;
908 if ((flags & SigmaValue) == 0)
909 image->chromaticity.red_primary.y=
910 image->chromaticity.red_primary.x;
913 if (LocaleCompare(keyword,"rendering-intent") == 0)
918 rendering_intent=ParseCommandOption(MagickIntentOptions,
919 MagickFalse,options);
920 if (rendering_intent < 0)
922 image->rendering_intent=(RenderingIntent) rendering_intent;
925 if (LocaleCompare(keyword,"resolution") == 0)
927 flags=ParseGeometry(options,&geometry_info);
928 image->x_resolution=geometry_info.rho;
929 image->y_resolution=geometry_info.sigma;
930 if ((flags & SigmaValue) == 0)
931 image->y_resolution=image->x_resolution;
934 if (LocaleCompare(keyword,"rows") == 0)
936 image->rows=StringToUnsignedLong(options);
939 (void) SetImageProperty(image,keyword,options,exception);
945 if (LocaleCompare(keyword,"scene") == 0)
947 image->scene=StringToUnsignedLong(options);
950 (void) SetImageProperty(image,keyword,options,exception);
956 if (LocaleCompare(keyword,"ticks-per-second") == 0)
958 image->ticks_per_second=(ssize_t) StringToLong(options);
961 if (LocaleCompare(keyword,"tile-offset") == 0)
966 geometry=GetPageGeometry(options);
967 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
968 geometry=DestroyString(geometry);
971 if (LocaleCompare(keyword,"type") == 0)
976 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
980 image->type=(ImageType) type;
983 (void) SetImageProperty(image,keyword,options,exception);
989 if (LocaleCompare(keyword,"units") == 0)
994 units=ParseCommandOption(MagickResolutionOptions,
995 MagickFalse,options);
998 image->units=(ResolutionType) units;
1001 (void) SetImageProperty(image,keyword,options,exception);
1007 if (LocaleCompare(keyword,"version") == 0)
1009 version=InterpretLocaleValue(options,(char **) NULL);
1012 (void) SetImageProperty(image,keyword,options,exception);
1018 if (LocaleCompare(keyword,"white-point") == 0)
1020 flags=ParseGeometry(options,&geometry_info);
1021 image->chromaticity.white_point.x=geometry_info.rho;
1022 image->chromaticity.white_point.y=geometry_info.rho;
1023 if ((flags & SigmaValue) != 0)
1024 image->chromaticity.white_point.y=
1025 image->chromaticity.white_point.x;
1028 (void) SetImageProperty(image,keyword,options,exception);
1033 (void) SetImageProperty(image,keyword,options,exception);
1039 c=ReadBlobByte(image);
1040 while (isspace((int) ((unsigned char) c)) != 0)
1041 c=ReadBlobByte(image);
1043 options=DestroyString(options);
1044 (void) ReadBlobByte(image);
1046 Verify that required image information is defined.
1048 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1049 (image->storage_class == UndefinedClass) ||
1050 (image->columns == 0) || (image->rows == 0))
1051 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1052 if (image->montage != (char *) NULL)
1060 length=MaxTextExtent;
1061 image->directory=AcquireString((char *) NULL);
1066 if ((strlen(image->directory)+MaxTextExtent) >= length)
1069 Allocate more memory for the image directory.
1072 image->directory=(char *) ResizeQuantumMemory(image->directory,
1073 length+MaxTextExtent,sizeof(*image->directory));
1074 if (image->directory == (char *) NULL)
1075 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1076 p=image->directory+strlen(image->directory);
1078 c=ReadBlobByte(image);
1080 } while (c != (int) '\0');
1082 if (profiles != (LinkedListInfo *) NULL)
1091 Read image profiles.
1093 ResetLinkedListIterator(profiles);
1094 name=(const char *) GetNextValueInLinkedList(profiles);
1095 while (name != (const char *) NULL)
1097 profile=GetImageProfile(image,name);
1098 if (profile != (StringInfo *) NULL)
1100 register unsigned char
1103 p=GetStringInfoDatum(profile);
1104 count=ReadBlob(image,GetStringInfoLength(profile),p);
1107 name=(const char *) GetNextValueInLinkedList(profiles);
1109 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1111 image->depth=GetImageQuantumDepth(image,MagickFalse);
1112 if (image->storage_class == PseudoClass)
1115 Create image colormap.
1117 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1118 if (status == MagickFalse)
1119 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1129 Read image colormap from file.
1131 packet_size=(size_t) (3UL*image->depth/8UL);
1132 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1133 packet_size*sizeof(*colormap));
1134 if (colormap == (unsigned char *) NULL)
1135 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1136 count=ReadBlob(image,packet_size*image->colors,colormap);
1138 switch (image->depth)
1141 ThrowReaderException(CorruptImageError,
1142 "ImageDepthNotSupported");
1148 for (i=0; i < (ssize_t) image->colors; i++)
1150 p=PushCharPixel(p,&pixel);
1151 image->colormap[i].red=ScaleCharToQuantum(pixel);
1152 p=PushCharPixel(p,&pixel);
1153 image->colormap[i].green=ScaleCharToQuantum(pixel);
1154 p=PushCharPixel(p,&pixel);
1155 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1164 for (i=0; i < (ssize_t) image->colors; i++)
1166 p=PushShortPixel(MSBEndian,p,&pixel);
1167 image->colormap[i].red=ScaleShortToQuantum(pixel);
1168 p=PushShortPixel(MSBEndian,p,&pixel);
1169 image->colormap[i].green=ScaleShortToQuantum(pixel);
1170 p=PushShortPixel(MSBEndian,p,&pixel);
1171 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1180 for (i=0; i < (ssize_t) image->colors; i++)
1182 p=PushLongPixel(MSBEndian,p,&pixel);
1183 image->colormap[i].red=ScaleLongToQuantum(pixel);
1184 p=PushLongPixel(MSBEndian,p,&pixel);
1185 image->colormap[i].green=ScaleLongToQuantum(pixel);
1186 p=PushLongPixel(MSBEndian,p,&pixel);
1187 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1192 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1195 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1196 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1199 Allocate image pixels.
1201 quantum_info=AcquireQuantumInfo(image_info,image);
1202 if (quantum_info == (QuantumInfo *) NULL)
1203 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1204 if (quantum_format != UndefinedQuantumFormat)
1206 status=SetQuantumFormat(image,quantum_info,quantum_format);
1207 if (status == MagickFalse)
1208 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1210 packet_size=(size_t) (quantum_info->depth/8);
1211 if (image->storage_class == DirectClass)
1212 packet_size=(size_t) (3*quantum_info->depth/8);
1213 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1214 (image->type == GrayscaleMatteType))
1215 packet_size=quantum_info->depth/8;
1216 if (image->matte != MagickFalse)
1217 packet_size+=quantum_info->depth/8;
1218 if (image->colorspace == CMYKColorspace)
1219 packet_size+=quantum_info->depth/8;
1220 if (image->compression == RLECompression)
1222 length=image->columns;
1223 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1224 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1226 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1227 sizeof(*compress_pixels));
1228 if (compress_pixels == (unsigned char *) NULL)
1229 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1233 quantum_type=RGBQuantum;
1234 if (image->matte != MagickFalse)
1235 quantum_type=RGBAQuantum;
1236 if (image->colorspace == CMYKColorspace)
1238 quantum_type=CMYKQuantum;
1239 if (image->matte != MagickFalse)
1240 quantum_type=CMYKAQuantum;
1242 if (image->storage_class == PseudoClass)
1244 quantum_type=IndexQuantum;
1245 if (image->matte != MagickFalse)
1246 quantum_type=IndexAlphaQuantum;
1248 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1249 (image->type == GrayscaleMatteType))
1251 quantum_type=GrayQuantum;
1252 if (image->matte != MagickFalse)
1253 quantum_type=GrayAlphaQuantum;
1256 switch (image->compression)
1258 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1259 case BZipCompression:
1261 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1262 bzip_info.bzalloc=AcquireBZIPMemory;
1263 bzip_info.bzfree=RelinquishBZIPMemory;
1264 bzip_info.opaque=(void *) NULL;
1265 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1272 #if defined(MAGICKCORE_LZMA_DELEGATE)
1273 case LZMACompression:
1275 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1276 allocator.alloc=AcquireLZMAMemory;
1277 allocator.free=RelinquishLZMAMemory;
1278 lzma_info=initialize_lzma;
1279 lzma_info.allocator=(&allocator);
1280 code=lzma_auto_decoder(&lzma_info,-1,0);
1281 if (code != LZMA_OK)
1286 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1287 case LZWCompression:
1288 case ZipCompression:
1290 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1291 zip_info.zalloc=AcquireZIPMemory;
1292 zip_info.zfree=RelinquishZIPMemory;
1293 zip_info.opaque=(voidpf) NULL;
1294 code=inflateInit(&zip_info);
1300 case RLECompression:
1302 GetPixelInfo(image,&pixel);
1308 pixels=GetQuantumPixels(quantum_info);
1310 for (y=0; y < (ssize_t) image->rows; y++)
1318 if (status == MagickFalse)
1320 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1321 if (q == (Quantum *) NULL)
1323 switch (image->compression)
1325 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1326 case BZipCompression:
1328 bzip_info.next_out=(char *) pixels;
1329 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1332 if (bzip_info.avail_in == 0)
1334 bzip_info.next_in=(char *) compress_pixels;
1335 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1337 length=(size_t) ReadBlobMSBLong(image);
1338 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1339 (unsigned char *) bzip_info.next_in);
1341 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1343 } while (bzip_info.avail_out != 0);
1344 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1345 quantum_type,pixels,exception);
1349 #if defined(MAGICKCORE_LZMA_DELEGATE)
1350 case LZMACompression:
1352 lzma_info.next_out=pixels;
1353 lzma_info.avail_out=packet_size*image->columns;
1356 if (lzma_info.avail_in == 0)
1358 lzma_info.next_in=compress_pixels;
1359 length=(size_t) ReadBlobMSBLong(image);
1360 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1361 (unsigned char *) lzma_info.next_in);
1363 code=lzma_code(&lzma_info,LZMA_RUN);
1369 if (code == LZMA_STREAM_END)
1371 } while (lzma_info.avail_out != 0);
1372 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1373 quantum_type,pixels,exception);
1377 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1378 case LZWCompression:
1379 case ZipCompression:
1381 zip_info.next_out=pixels;
1382 zip_info.avail_out=(uInt) (packet_size*image->columns);
1385 if (zip_info.avail_in == 0)
1387 zip_info.next_in=compress_pixels;
1388 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1390 length=(size_t) ReadBlobMSBLong(image);
1391 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1394 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1396 } while (zip_info.avail_out != 0);
1397 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1398 quantum_type,pixels,exception);
1402 case RLECompression:
1404 for (x=0; x < (ssize_t) image->columns; x++)
1408 count=ReadBlob(image,packet_size,pixels);
1409 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1412 if (image->storage_class == PseudoClass)
1413 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1416 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1417 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1418 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1419 if (image->colorspace == CMYKColorspace)
1420 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1422 if (image->matte != MagickFalse)
1423 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1424 q+=GetPixelChannels(image);
1430 count=ReadBlob(image,packet_size*image->columns,pixels);
1431 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1432 quantum_type,pixels,exception);
1436 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1439 SetQuantumImageType(image,quantum_type);
1440 switch (image->compression)
1442 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1443 case BZipCompression:
1450 offset=SeekBlob(image,-((MagickOffsetType)
1451 bzip_info.avail_in),SEEK_CUR);
1453 ThrowReaderException(CorruptImageError,
1454 "ImproperImageHeader");
1456 code=BZ2_bzDecompressEnd(&bzip_info);
1462 #if defined(MAGICKCORE_LZMA_DELEGATE)
1463 case LZMACompression:
1465 code=lzma_code(&lzma_info,LZMA_FINISH);
1466 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1468 lzma_end(&lzma_info);
1472 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1473 case LZWCompression:
1474 case ZipCompression:
1481 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1484 ThrowReaderException(CorruptImageError,
1485 "ImproperImageHeader");
1487 code=inflateEnd(&zip_info);
1488 if (code != LZMA_OK)
1496 quantum_info=DestroyQuantumInfo(quantum_info);
1497 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1498 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1500 image=DestroyImageList(image);
1501 return((Image *) NULL);
1503 if (EOFBlob(image) != MagickFalse)
1505 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1510 Proceed to next image.
1512 if (image_info->number_scenes != 0)
1513 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1517 c=ReadBlobByte(image);
1518 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1522 Allocate next image structure.
1524 AcquireNextImage(image_info,image,exception);
1525 if (GetNextImageInList(image) == (Image *) NULL)
1527 image=DestroyImageList(image);
1528 return((Image *) NULL);
1530 image=SyncNextImageInList(image);
1531 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1532 GetBlobSize(image));
1533 if (status == MagickFalse)
1537 (void) CloseBlob(image);
1538 return(GetFirstImageInList(image));
1542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546 % R e g i s t e r M I F F I m a g e %
1550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1552 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1553 % supported formats. The properties include the image format tag, a method to
1554 % read and/or write the format, whether the format supports the saving of more
1555 % than one frame to the same file or blob, whether the format supports native
1556 % in-memory I/O, and a brief description of the format.
1558 % The format of the RegisterMIFFImage method is:
1560 % size_t RegisterMIFFImage(void)
1563 ModuleExport size_t RegisterMIFFImage(void)
1566 version[MaxTextExtent];
1572 #if defined(MagickImageCoderSignatureText)
1573 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1574 #if defined(ZLIB_VERSION)
1575 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1576 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1578 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1579 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1582 entry=SetMagickInfo("MIFF");
1583 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1584 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1585 entry->magick=(IsImageFormatHandler *) IsMIFF;
1586 entry->seekable_stream=MagickTrue;
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,
1635 % Image *image,ExceptionInfo *exception)
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.
1645 % o exception: return any errors or warnings in this structure.
1649 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1650 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1652 if (image->storage_class != DirectClass)
1654 switch (image->depth)
1658 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1659 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1662 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1665 *pixels++=(unsigned char) pixel->index;
1669 (void) ThrowMagickException(exception,GetMagickModule(),
1670 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1672 switch (image->depth)
1679 if (image->matte != MagickFalse)
1681 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1682 pixels=PopLongPixel(MSBEndian,value,pixels);
1691 if (image->matte != MagickFalse)
1693 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1694 pixels=PopShortPixel(MSBEndian,value,pixels);
1703 if (image->matte != MagickFalse)
1705 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1707 pixels=PopCharPixel(value,pixels);
1712 (void) ThrowMagickException(exception,GetMagickModule(),
1713 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1715 *pixels++=(unsigned char) length;
1718 switch (image->depth)
1725 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1726 pixels=PopLongPixel(MSBEndian,value,pixels);
1727 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1728 pixels=PopLongPixel(MSBEndian,value,pixels);
1729 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1730 pixels=PopLongPixel(MSBEndian,value,pixels);
1731 if (image->colorspace == CMYKColorspace)
1733 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1734 pixels=PopLongPixel(MSBEndian,value,pixels);
1736 if (image->matte != MagickFalse)
1738 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1739 pixels=PopLongPixel(MSBEndian,value,pixels);
1748 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1749 pixels=PopShortPixel(MSBEndian,value,pixels);
1750 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1751 pixels=PopShortPixel(MSBEndian,value,pixels);
1752 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1753 pixels=PopShortPixel(MSBEndian,value,pixels);
1754 if (image->colorspace == CMYKColorspace)
1756 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1757 pixels=PopShortPixel(MSBEndian,value,pixels);
1759 if (image->matte != MagickFalse)
1761 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1762 pixels=PopShortPixel(MSBEndian,value,pixels);
1771 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1772 pixels=PopCharPixel(value,pixels);
1773 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->green));
1774 pixels=PopCharPixel(value,pixels);
1775 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1776 pixels=PopCharPixel(value,pixels);
1777 if (image->colorspace == CMYKColorspace)
1779 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1781 pixels=PopCharPixel(value,pixels);
1783 if (image->matte != MagickFalse)
1785 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1787 pixels=PopCharPixel(value,pixels);
1792 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1793 "ImageDepthNotSupported","`%s'",image->filename);
1795 *pixels++=(unsigned char) length;
1799 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1800 Image *image,ExceptionInfo *exception)
1802 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1808 buffer[MaxTextExtent];
1820 #if defined(MAGICKCORE_LZMA_DELEGATE)
1825 initialize_lzma = LZMA_STREAM_INIT,
1860 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1866 Open output image file.
1868 assert(image_info != (const ImageInfo *) NULL);
1869 assert(image_info->signature == MagickSignature);
1870 assert(image != (Image *) NULL);
1871 assert(image->signature == MagickSignature);
1872 if (image->debug != MagickFalse)
1873 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1874 assert(exception != (ExceptionInfo *) NULL);
1875 assert(exception->signature == MagickSignature);
1876 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1877 if (status == MagickFalse)
1884 Allocate image pixels.
1886 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1887 image->depth <= 32 ? 32UL : 64UL;
1888 quantum_info=AcquireQuantumInfo(image_info,image);
1889 if (quantum_info == (QuantumInfo *) NULL)
1890 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1891 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1892 (quantum_info->format == UndefinedQuantumFormat) &&
1893 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1895 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1896 if (status == MagickFalse)
1897 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1899 if ((image->storage_class == PseudoClass) &&
1900 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1901 (void) SetImageStorageClass(image,DirectClass,exception);
1902 if (IsImageGray(image,exception) != MagickFalse)
1904 (void) SetImageStorageClass(image,DirectClass,exception);
1905 (void) SetImageColorspace(image,GRAYColorspace,exception);
1907 compression=image->compression;
1908 if (image_info->compression != UndefinedCompression)
1909 compression=image_info->compression;
1910 switch (compression)
1912 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1913 case LZMACompression: compression=NoCompression; break;
1915 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1916 case LZWCompression:
1917 case ZipCompression: compression=NoCompression; break;
1919 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1920 case BZipCompression: compression=NoCompression; break;
1922 case RLECompression:
1924 if (quantum_info->format == FloatingPointQuantumFormat)
1925 compression=NoCompression;
1926 GetPixelInfo(image,&target);
1932 packet_size=(size_t) (quantum_info->depth/8);
1933 if (image->storage_class == DirectClass)
1934 packet_size=(size_t) (3*quantum_info->depth/8);
1935 if (IsImageGray(image,exception) != MagickFalse)
1936 packet_size=(size_t) (quantum_info->depth/8);
1937 if (image->matte != MagickFalse)
1938 packet_size+=quantum_info->depth/8;
1939 if (image->colorspace == CMYKColorspace)
1940 packet_size+=quantum_info->depth/8;
1941 if (compression == RLECompression)
1943 length=image->columns;
1944 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1945 packet_size*image->columns));
1946 if ((compression == BZipCompression) || (compression == ZipCompression))
1947 if (length != (size_t) ((unsigned int) length))
1948 compression=NoCompression;
1949 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1950 sizeof(*compress_pixels));
1951 if (compress_pixels == (unsigned char *) NULL)
1952 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1956 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1957 (void) FormatLocaleString(buffer,MaxTextExtent,
1958 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1959 MagickClassOptions,image->storage_class),(double) image->colors,
1960 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1961 (void) WriteBlobString(image,buffer);
1962 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1963 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1965 (void) WriteBlobString(image,buffer);
1966 if (image->type != UndefinedType)
1968 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1969 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1970 (void) WriteBlobString(image,buffer);
1972 if (image->colorspace != UndefinedColorspace)
1974 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1975 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1976 (void) WriteBlobString(image,buffer);
1978 if (compression != UndefinedCompression)
1980 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
1981 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
1982 compression),(double) image->quality);
1983 (void) WriteBlobString(image,buffer);
1985 if (image->units != UndefinedResolution)
1987 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
1988 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1989 (void) WriteBlobString(image,buffer);
1991 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1993 (void) FormatLocaleString(buffer,MaxTextExtent,
1994 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
1995 (void) WriteBlobString(image,buffer);
1997 if ((image->page.width != 0) || (image->page.height != 0))
1999 (void) FormatLocaleString(buffer,MaxTextExtent,
2000 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2001 image->page.height,(double) image->page.x,(double) image->page.y);
2002 (void) WriteBlobString(image,buffer);
2005 if ((image->page.x != 0) || (image->page.y != 0))
2007 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2008 (long) image->page.x,(long) image->page.y);
2009 (void) WriteBlobString(image,buffer);
2011 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2013 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2014 (long) image->tile_offset.x,(long) image->tile_offset.y);
2015 (void) WriteBlobString(image,buffer);
2017 if ((GetNextImageInList(image) != (Image *) NULL) ||
2018 (GetPreviousImageInList(image) != (Image *) NULL))
2020 if (image->scene == 0)
2021 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2022 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2023 (double) image->delay,(double) image->ticks_per_second);
2025 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2026 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2027 image->scene,(double) image->iterations,(double) image->delay,
2028 (double) image->ticks_per_second);
2029 (void) WriteBlobString(image,buffer);
2033 if (image->scene != 0)
2035 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2036 (double) image->scene);
2037 (void) WriteBlobString(image,buffer);
2039 if (image->iterations != 0)
2041 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2042 (double) image->iterations);
2043 (void) WriteBlobString(image,buffer);
2045 if (image->delay != 0)
2047 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2048 (double) image->delay);
2049 (void) WriteBlobString(image,buffer);
2051 if (image->ticks_per_second != UndefinedTicksPerSecond)
2053 (void) FormatLocaleString(buffer,MaxTextExtent,
2054 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2055 (void) WriteBlobString(image,buffer);
2058 if (image->gravity != UndefinedGravity)
2060 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2061 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2062 (void) WriteBlobString(image,buffer);
2064 if (image->dispose != UndefinedDispose)
2066 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2067 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2068 (void) WriteBlobString(image,buffer);
2070 if (image->rendering_intent != UndefinedIntent)
2072 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2073 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2074 (void) WriteBlobString(image,buffer);
2076 if (image->gamma != 0.0)
2078 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2080 (void) WriteBlobString(image,buffer);
2082 if (image->chromaticity.white_point.x != 0.0)
2085 Note chomaticity points.
2087 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2088 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2089 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2090 image->chromaticity.green_primary.x,
2091 image->chromaticity.green_primary.y,
2092 image->chromaticity.blue_primary.x,
2093 image->chromaticity.blue_primary.y);
2094 (void) WriteBlobString(image,buffer);
2095 (void) FormatLocaleString(buffer,MaxTextExtent,
2096 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2097 image->chromaticity.white_point.y);
2098 (void) WriteBlobString(image,buffer);
2100 if (image->orientation != UndefinedOrientation)
2102 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2103 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2104 (void) WriteBlobString(image,buffer);
2106 if (image->profiles != (void *) NULL)
2115 Write image profiles.
2117 ResetImageProfileIterator(image);
2118 name=GetNextImageProfile(image);
2119 while (name != (const char *) NULL)
2121 profile=GetImageProfile(image,name);
2122 if (profile != (StringInfo *) NULL)
2124 (void) FormatLocaleString(buffer,MaxTextExtent,
2125 "profile:%s=%.20g\n",name,(double)
2126 GetStringInfoLength(profile));
2127 (void) WriteBlobString(image,buffer);
2129 name=GetNextImageProfile(image);
2132 if (image->montage != (char *) NULL)
2134 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2136 (void) WriteBlobString(image,buffer);
2138 if (quantum_info->format == FloatingPointQuantumFormat)
2139 (void) SetImageProperty(image,"quantum:format","floating-point",
2141 ResetImagePropertyIterator(image);
2142 property=GetNextImageProperty(image);
2143 while (property != (const char *) NULL)
2145 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2146 (void) WriteBlobString(image,buffer);
2147 value=GetImageProperty(image,property,exception);
2148 if (value != (const char *) NULL)
2150 for (i=0; i < (ssize_t) strlen(value); i++)
2151 if (isspace((int) ((unsigned char) value[i])) != 0)
2153 if (i <= (ssize_t) strlen(value))
2154 (void) WriteBlobByte(image,'{');
2155 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2156 if (i <= (ssize_t) strlen(value))
2157 (void) WriteBlobByte(image,'}');
2159 (void) WriteBlobByte(image,'\n');
2160 property=GetNextImageProperty(image);
2162 (void) WriteBlobString(image,"\f\n:\032");
2163 if (image->montage != (char *) NULL)
2166 Write montage tile directory.
2168 if (image->directory != (char *) NULL)
2169 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2171 (void) WriteBlobByte(image,'\0');
2173 if (image->profiles != (void *) NULL)
2184 ResetImageProfileIterator(image);
2185 name=GetNextImageProfile(image);
2186 while (name != (const char *) NULL)
2188 profile=GetImageProfile(image,name);
2189 (void) WriteBlob(image,GetStringInfoLength(profile),
2190 GetStringInfoDatum(profile));
2191 name=GetNextImageProfile(image);
2194 if (image->storage_class == PseudoClass)
2206 packet_size=(size_t) (3*quantum_info->depth/8);
2207 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2208 packet_size*sizeof(*colormap));
2209 if (colormap == (unsigned char *) NULL)
2210 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2212 Write colormap to file.
2215 for (i=0; i < (ssize_t) image->colors; i++)
2217 switch (quantum_info->depth)
2220 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2223 register unsigned int
2226 pixel=ScaleQuantumToLong(image->colormap[i].red);
2227 q=PopLongPixel(MSBEndian,pixel,q);
2228 pixel=ScaleQuantumToLong(image->colormap[i].green);
2229 q=PopLongPixel(MSBEndian,pixel,q);
2230 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2231 q=PopLongPixel(MSBEndian,pixel,q);
2236 register unsigned short
2239 pixel=ScaleQuantumToShort(image->colormap[i].red);
2240 q=PopShortPixel(MSBEndian,pixel,q);
2241 pixel=ScaleQuantumToShort(image->colormap[i].green);
2242 q=PopShortPixel(MSBEndian,pixel,q);
2243 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2244 q=PopShortPixel(MSBEndian,pixel,q);
2249 register unsigned char
2252 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2253 q=PopCharPixel(pixel,q);
2254 pixel=(unsigned char) ScaleQuantumToChar(
2255 image->colormap[i].green);
2256 q=PopCharPixel(pixel,q);
2257 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2258 q=PopCharPixel(pixel,q);
2263 (void) WriteBlob(image,packet_size*image->colors,colormap);
2264 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2267 Write image pixels to file.
2270 switch (compression)
2272 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2273 case BZipCompression:
2275 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2276 bzip_info.bzalloc=AcquireBZIPMemory;
2277 bzip_info.bzfree=RelinquishBZIPMemory;
2278 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2279 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2280 9)),(int) image_info->verbose,0);
2286 #if defined(MAGICKCORE_LZMA_DELEGATE)
2287 case LZMACompression:
2289 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2290 allocator.alloc=AcquireLZMAMemory;
2291 allocator.free=RelinquishLZMAMemory;
2292 lzma_info=initialize_lzma;
2293 lzma_info.allocator=&allocator;
2294 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2295 if (code != LZMA_OK)
2300 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2301 case LZWCompression:
2302 case ZipCompression:
2304 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2305 zip_info.zalloc=AcquireZIPMemory;
2306 zip_info.zfree=RelinquishZIPMemory;
2307 code=deflateInit(&zip_info,(int) (image->quality ==
2308 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2317 quantum_type=GetQuantumType(image,exception);
2318 pixels=GetQuantumPixels(quantum_info);
2319 for (y=0; y < (ssize_t) image->rows; y++)
2321 register const Quantum
2327 if (status == MagickFalse)
2329 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2330 if (p == (const Quantum *) NULL)
2333 switch (compression)
2335 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2336 case BZipCompression:
2338 bzip_info.next_in=(char *) pixels;
2339 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2340 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2341 quantum_type,pixels,exception);
2344 bzip_info.next_out=(char *) compress_pixels;
2345 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2347 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2350 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2353 (void) WriteBlobMSBLong(image,(unsigned int) length);
2354 (void) WriteBlob(image,length,compress_pixels);
2356 } while (bzip_info.avail_in != 0);
2360 #if defined(MAGICKCORE_LZMA_DELEGATE)
2361 case LZMACompression:
2363 lzma_info.next_in=pixels;
2364 lzma_info.avail_in=packet_size*image->columns;
2365 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2366 quantum_type,pixels,exception);
2369 lzma_info.next_out=compress_pixels;
2370 lzma_info.avail_out=packet_size*image->columns;
2371 code=lzma_code(&lzma_info,LZMA_RUN);
2372 if (code != LZMA_OK)
2374 length=(size_t) (lzma_info.next_out-compress_pixels);
2377 (void) WriteBlobMSBLong(image,(unsigned int) length);
2378 (void) WriteBlob(image,length,compress_pixels);
2380 } while (lzma_info.avail_in != 0);
2384 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2385 case LZWCompression:
2386 case ZipCompression:
2388 zip_info.next_in=pixels;
2389 zip_info.avail_in=(uInt) (packet_size*image->columns);
2390 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2391 quantum_type,pixels,exception);
2394 zip_info.next_out=compress_pixels;
2395 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2396 code=deflate(&zip_info,Z_SYNC_FLUSH);
2399 length=(size_t) (zip_info.next_out-compress_pixels);
2402 (void) WriteBlobMSBLong(image,(unsigned int) length);
2403 (void) WriteBlob(image,length,compress_pixels);
2405 } while (zip_info.avail_in != 0);
2409 case RLECompression:
2412 SetPixelInfo(image,p,&pixel);
2413 p+=GetPixelChannels(image);
2414 for (x=1; x < (ssize_t) image->columns; x++)
2416 SetPixelInfo(image,p,&target);
2417 if ((length < 255) &&
2418 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2422 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2425 SetPixelInfo(image,p,&pixel);
2426 p+=GetPixelChannels(image);
2428 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2429 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2434 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2435 quantum_type,pixels,exception);
2436 (void) WriteBlob(image,packet_size*image->columns,pixels);
2440 if (image->previous == (Image *) NULL)
2442 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2444 if (status == MagickFalse)
2448 switch (compression)
2450 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2451 case BZipCompression:
2455 if (status == MagickFalse)
2457 bzip_info.next_out=(char *) compress_pixels;
2458 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2460 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2461 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2464 (void) WriteBlobMSBLong(image,(unsigned int) length);
2465 (void) WriteBlob(image,length,compress_pixels);
2467 if (code == BZ_STREAM_END)
2470 code=BZ2_bzCompressEnd(&bzip_info);
2476 #if defined(MAGICKCORE_LZMA_DELEGATE)
2477 case LZMACompression:
2481 if (status == MagickFalse)
2483 lzma_info.next_out=compress_pixels;
2484 lzma_info.avail_out=packet_size*image->columns;
2485 code=lzma_code(&lzma_info,LZMA_FINISH);
2486 length=(size_t) (lzma_info.next_out-compress_pixels);
2489 (void) WriteBlobMSBLong(image,(unsigned int) length);
2490 (void) WriteBlob(image,length,compress_pixels);
2492 if (code == LZMA_STREAM_END)
2495 lzma_end(&lzma_info);
2499 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2500 case LZWCompression:
2501 case ZipCompression:
2505 if (status == MagickFalse)
2507 zip_info.next_out=compress_pixels;
2508 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2509 code=deflate(&zip_info,Z_FINISH);
2510 length=(size_t) (zip_info.next_out-compress_pixels);
2513 (void) WriteBlobMSBLong(image,(unsigned int) length);
2514 (void) WriteBlob(image,length,compress_pixels);
2516 if (code == Z_STREAM_END)
2519 code=deflateEnd(&zip_info);
2528 quantum_info=DestroyQuantumInfo(quantum_info);
2529 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2530 if (GetNextImageInList(image) == (Image *) NULL)
2532 image=SyncNextImageInList(image);
2533 status=SetImageProgress(image,SaveImagesTag,scene++,
2534 GetImageListLength(image));
2535 if (status == MagickFalse)
2537 } while (image_info->adjoin != MagickFalse);
2538 (void) CloseBlob(image);