2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/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],
418 #if defined(MAGICKCORE_LZMA_DELEGATE)
420 initialize_lzma = LZMA_STREAM_INIT,
468 #if defined(MAGICKCORE_ZLIB_DELEGATE)
476 assert(image_info != (const ImageInfo *) NULL);
477 assert(image_info->signature == MagickSignature);
478 if (image_info->debug != MagickFalse)
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
480 image_info->filename);
481 assert(exception != (ExceptionInfo *) NULL);
482 assert(exception->signature == MagickSignature);
483 image=AcquireImage(image_info,exception);
484 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
485 if (status == MagickFalse)
487 image=DestroyImageList(image);
488 return((Image *) NULL);
491 Decode image header; header terminates one character beyond a ':'.
493 c=ReadBlobByte(image);
495 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
497 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
503 Decode image header; header terminates one character beyond a ':'.
505 length=MaxTextExtent;
506 options=AcquireString((char *) NULL);
507 quantum_format=UndefinedQuantumFormat;
508 profiles=(LinkedListInfo *) NULL;
511 image->compression=NoCompression;
512 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
523 Read comment-- any text between { }.
525 length=MaxTextExtent;
526 comment=AcquireString((char *) NULL);
527 for (p=comment; comment != (char *) NULL; p++)
529 c=ReadBlobByte(image);
530 if ((c == EOF) || (c == (int) '}'))
532 if ((size_t) (p-comment+1) >= length)
536 comment=(char *) ResizeQuantumMemory(comment,length+
537 MaxTextExtent,sizeof(*comment));
538 if (comment == (char *) NULL)
540 p=comment+strlen(comment);
544 if (comment == (char *) NULL)
545 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
547 (void) SetImageProperty(image,"comment",comment,exception);
548 comment=DestroyString(comment);
549 c=ReadBlobByte(image);
552 if (isalnum(c) != MagickFalse)
562 if ((size_t) (p-keyword) < (MaxTextExtent-1))
564 c=ReadBlobByte(image);
568 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
569 c=ReadBlobByte(image);
573 Get the keyword value.
575 c=ReadBlobByte(image);
576 while ((c != (int) '}') && (c != EOF))
578 if ((size_t) (p-options+1) >= length)
582 options=(char *) ResizeQuantumMemory(options,length+
583 MaxTextExtent,sizeof(*options));
584 if (options == (char *) NULL)
586 p=options+strlen(options);
588 if (options == (char *) NULL)
589 ThrowReaderException(ResourceLimitError,
590 "MemoryAllocationFailed");
592 c=ReadBlobByte(image);
594 if (isspace((int) ((unsigned char) c)) != 0)
600 (void) CopyMagickString(options,options+1,MaxTextExtent);
602 Assign a value to the specified keyword.
609 if (LocaleCompare(keyword,"background-color") == 0)
611 (void) QueryColorCompliance(options,AllCompliance,
612 &image->background_color,exception);
615 if (LocaleCompare(keyword,"blue-primary") == 0)
617 flags=ParseGeometry(options,&geometry_info);
618 image->chromaticity.blue_primary.x=geometry_info.rho;
619 image->chromaticity.blue_primary.y=geometry_info.sigma;
620 if ((flags & SigmaValue) == 0)
621 image->chromaticity.blue_primary.y=
622 image->chromaticity.blue_primary.x;
625 if (LocaleCompare(keyword,"border-color") == 0)
627 (void) QueryColorCompliance(options,AllCompliance,
628 &image->border_color,exception);
631 (void) SetImageProperty(image,keyword,options,exception);
637 if (LocaleCompare(keyword,"class") == 0)
642 storage_class=ParseCommandOption(MagickClassOptions,
643 MagickFalse,options);
644 if (storage_class < 0)
646 image->storage_class=(ClassType) storage_class;
649 if (LocaleCompare(keyword,"colors") == 0)
651 colors=StringToUnsignedLong(options);
654 if (LocaleCompare(keyword,"colorspace") == 0)
659 colorspace=ParseCommandOption(MagickColorspaceOptions,
660 MagickFalse,options);
663 image->colorspace=(ColorspaceType) colorspace;
666 if (LocaleCompare(keyword,"compression") == 0)
671 compression=ParseCommandOption(MagickCompressOptions,
672 MagickFalse,options);
675 image->compression=(CompressionType) compression;
678 if (LocaleCompare(keyword,"columns") == 0)
680 image->columns=StringToUnsignedLong(options);
683 (void) SetImageProperty(image,keyword,options,exception);
689 if (LocaleCompare(keyword,"delay") == 0)
691 image->delay=StringToUnsignedLong(options);
694 if (LocaleCompare(keyword,"depth") == 0)
696 image->depth=StringToUnsignedLong(options);
699 if (LocaleCompare(keyword,"dispose") == 0)
704 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
708 image->dispose=(DisposeType) dispose;
711 (void) SetImageProperty(image,keyword,options,exception);
717 if (LocaleCompare(keyword,"endian") == 0)
722 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
726 image->endian=(EndianType) endian;
729 (void) SetImageProperty(image,keyword,options,exception);
735 if (LocaleCompare(keyword,"gamma") == 0)
737 image->gamma=StringToDouble(options,(char **) NULL);
740 if (LocaleCompare(keyword,"gravity") == 0)
745 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
749 image->gravity=(GravityType) gravity;
752 if (LocaleCompare(keyword,"green-primary") == 0)
754 flags=ParseGeometry(options,&geometry_info);
755 image->chromaticity.green_primary.x=geometry_info.rho;
756 image->chromaticity.green_primary.y=geometry_info.sigma;
757 if ((flags & SigmaValue) == 0)
758 image->chromaticity.green_primary.y=
759 image->chromaticity.green_primary.x;
762 (void) SetImageProperty(image,keyword,options,exception);
768 if (LocaleCompare(keyword,"id") == 0)
770 (void) CopyMagickString(id,options,MaxTextExtent);
773 if (LocaleCompare(keyword,"iterations") == 0)
775 image->iterations=StringToUnsignedLong(options);
778 (void) SetImageProperty(image,keyword,options,exception);
784 if (LocaleCompare(keyword,"matte") == 0)
789 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
793 image->matte=(MagickBooleanType) matte;
796 if (LocaleCompare(keyword,"matte-color") == 0)
798 (void) QueryColorCompliance(options,AllCompliance,
799 &image->matte_color,exception);
802 if (LocaleCompare(keyword,"montage") == 0)
804 (void) CloneString(&image->montage,options);
807 (void) SetImageProperty(image,keyword,options,exception);
813 if (LocaleCompare(keyword,"opaque") == 0)
818 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
822 image->matte=(MagickBooleanType) matte;
825 if (LocaleCompare(keyword,"orientation") == 0)
830 orientation=ParseCommandOption(MagickOrientationOptions,
831 MagickFalse,options);
834 image->orientation=(OrientationType) orientation;
837 (void) SetImageProperty(image,keyword,options,exception);
843 if (LocaleCompare(keyword,"page") == 0)
848 geometry=GetPageGeometry(options);
849 (void) ParseAbsoluteGeometry(geometry,&image->page);
850 geometry=DestroyString(geometry);
853 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
854 (LocaleNCompare(keyword,"profile-",8) == 0))
859 if (profiles == (LinkedListInfo *) NULL)
860 profiles=NewLinkedList(0);
861 (void) AppendValueToLinkedList(profiles,
862 AcquireString(keyword+8));
863 profile=BlobToStringInfo((const void *) NULL,(size_t)
864 StringToLong(options));
865 if (profile == (StringInfo *) NULL)
866 ThrowReaderException(ResourceLimitError,
867 "MemoryAllocationFailed");
868 (void) SetImageProfile(image,keyword+8,profile,exception);
869 profile=DestroyStringInfo(profile);
872 (void) SetImageProperty(image,keyword,options,exception);
878 if (LocaleCompare(keyword,"quality") == 0)
880 image->quality=StringToUnsignedLong(options);
883 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
884 (LocaleCompare(keyword,"quantum:format") == 0))
889 format=ParseCommandOption(MagickQuantumFormatOptions,
890 MagickFalse,options);
893 quantum_format=(QuantumFormatType) format;
896 (void) SetImageProperty(image,keyword,options,exception);
902 if (LocaleCompare(keyword,"red-primary") == 0)
904 flags=ParseGeometry(options,&geometry_info);
905 image->chromaticity.red_primary.x=geometry_info.rho;
906 image->chromaticity.red_primary.y=geometry_info.sigma;
907 if ((flags & SigmaValue) == 0)
908 image->chromaticity.red_primary.y=
909 image->chromaticity.red_primary.x;
912 if (LocaleCompare(keyword,"rendering-intent") == 0)
917 rendering_intent=ParseCommandOption(MagickIntentOptions,
918 MagickFalse,options);
919 if (rendering_intent < 0)
921 image->rendering_intent=(RenderingIntent) rendering_intent;
924 if (LocaleCompare(keyword,"resolution") == 0)
926 flags=ParseGeometry(options,&geometry_info);
927 image->resolution.x=geometry_info.rho;
928 image->resolution.y=geometry_info.sigma;
929 if ((flags & SigmaValue) == 0)
930 image->resolution.y=image->resolution.x;
933 if (LocaleCompare(keyword,"rows") == 0)
935 image->rows=StringToUnsignedLong(options);
938 (void) SetImageProperty(image,keyword,options,exception);
944 if (LocaleCompare(keyword,"scene") == 0)
946 image->scene=StringToUnsignedLong(options);
949 (void) SetImageProperty(image,keyword,options,exception);
955 if (LocaleCompare(keyword,"ticks-per-second") == 0)
957 image->ticks_per_second=(ssize_t) StringToLong(options);
960 if (LocaleCompare(keyword,"tile-offset") == 0)
965 geometry=GetPageGeometry(options);
966 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
967 geometry=DestroyString(geometry);
970 if (LocaleCompare(keyword,"type") == 0)
975 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
979 image->type=(ImageType) type;
982 (void) SetImageProperty(image,keyword,options,exception);
988 if (LocaleCompare(keyword,"units") == 0)
993 units=ParseCommandOption(MagickResolutionOptions,
994 MagickFalse,options);
997 image->units=(ResolutionType) units;
1000 (void) SetImageProperty(image,keyword,options,exception);
1006 if (LocaleCompare(keyword,"version") == 0)
1008 version=StringToDouble(options,(char **) NULL);
1011 (void) SetImageProperty(image,keyword,options,exception);
1017 if (LocaleCompare(keyword,"white-point") == 0)
1019 flags=ParseGeometry(options,&geometry_info);
1020 image->chromaticity.white_point.x=geometry_info.rho;
1021 image->chromaticity.white_point.y=geometry_info.rho;
1022 if ((flags & SigmaValue) != 0)
1023 image->chromaticity.white_point.y=
1024 image->chromaticity.white_point.x;
1027 (void) SetImageProperty(image,keyword,options,exception);
1032 (void) SetImageProperty(image,keyword,options,exception);
1038 c=ReadBlobByte(image);
1039 while (isspace((int) ((unsigned char) c)) != 0)
1040 c=ReadBlobByte(image);
1042 options=DestroyString(options);
1043 (void) ReadBlobByte(image);
1045 Verify that required image information is defined.
1047 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1048 (image->storage_class == UndefinedClass) ||
1049 (image->columns == 0) || (image->rows == 0))
1050 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1051 if (image->montage != (char *) NULL)
1059 length=MaxTextExtent;
1060 image->directory=AcquireString((char *) NULL);
1065 if ((strlen(image->directory)+MaxTextExtent) >= length)
1068 Allocate more memory for the image directory.
1071 image->directory=(char *) ResizeQuantumMemory(image->directory,
1072 length+MaxTextExtent,sizeof(*image->directory));
1073 if (image->directory == (char *) NULL)
1074 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1075 p=image->directory+strlen(image->directory);
1077 c=ReadBlobByte(image);
1079 } while (c != (int) '\0');
1081 if (profiles != (LinkedListInfo *) NULL)
1090 Read image profiles.
1092 ResetLinkedListIterator(profiles);
1093 name=(const char *) GetNextValueInLinkedList(profiles);
1094 while (name != (const char *) NULL)
1096 profile=GetImageProfile(image,name);
1097 if (profile != (StringInfo *) NULL)
1099 register unsigned char
1102 p=GetStringInfoDatum(profile);
1103 count=ReadBlob(image,GetStringInfoLength(profile),p);
1106 name=(const char *) GetNextValueInLinkedList(profiles);
1108 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1110 image->depth=GetImageQuantumDepth(image,MagickFalse);
1111 if (image->storage_class == PseudoClass)
1114 Create image colormap.
1116 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1117 if (status == MagickFalse)
1118 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1128 Read image colormap from file.
1130 packet_size=(size_t) (3UL*image->depth/8UL);
1131 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1132 packet_size*sizeof(*colormap));
1133 if (colormap == (unsigned char *) NULL)
1134 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1135 count=ReadBlob(image,packet_size*image->colors,colormap);
1137 switch (image->depth)
1140 ThrowReaderException(CorruptImageError,
1141 "ImageDepthNotSupported");
1147 for (i=0; i < (ssize_t) image->colors; i++)
1149 p=PushCharPixel(p,&pixel);
1150 image->colormap[i].red=ScaleCharToQuantum(pixel);
1151 p=PushCharPixel(p,&pixel);
1152 image->colormap[i].green=ScaleCharToQuantum(pixel);
1153 p=PushCharPixel(p,&pixel);
1154 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1163 for (i=0; i < (ssize_t) image->colors; i++)
1165 p=PushShortPixel(MSBEndian,p,&pixel);
1166 image->colormap[i].red=ScaleShortToQuantum(pixel);
1167 p=PushShortPixel(MSBEndian,p,&pixel);
1168 image->colormap[i].green=ScaleShortToQuantum(pixel);
1169 p=PushShortPixel(MSBEndian,p,&pixel);
1170 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1179 for (i=0; i < (ssize_t) image->colors; i++)
1181 p=PushLongPixel(MSBEndian,p,&pixel);
1182 image->colormap[i].red=ScaleLongToQuantum(pixel);
1183 p=PushLongPixel(MSBEndian,p,&pixel);
1184 image->colormap[i].green=ScaleLongToQuantum(pixel);
1185 p=PushLongPixel(MSBEndian,p,&pixel);
1186 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1191 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1194 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1195 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1198 Allocate image pixels.
1200 quantum_info=AcquireQuantumInfo(image_info,image);
1201 if (quantum_info == (QuantumInfo *) NULL)
1202 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1203 if (quantum_format != UndefinedQuantumFormat)
1205 status=SetQuantumFormat(image,quantum_info,quantum_format);
1206 if (status == MagickFalse)
1207 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1209 packet_size=(size_t) (quantum_info->depth/8);
1210 if (image->storage_class == DirectClass)
1211 packet_size=(size_t) (3*quantum_info->depth/8);
1212 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1213 (image->type == GrayscaleMatteType))
1214 packet_size=quantum_info->depth/8;
1215 if (image->matte != MagickFalse)
1216 packet_size+=quantum_info->depth/8;
1217 if (image->colorspace == CMYKColorspace)
1218 packet_size+=quantum_info->depth/8;
1219 if (image->compression == RLECompression)
1221 length=image->columns;
1222 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1223 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1225 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1226 sizeof(*compress_pixels));
1227 if (compress_pixels == (unsigned char *) NULL)
1228 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1232 quantum_type=RGBQuantum;
1233 if (image->matte != MagickFalse)
1234 quantum_type=RGBAQuantum;
1235 if (image->colorspace == CMYKColorspace)
1237 quantum_type=CMYKQuantum;
1238 if (image->matte != MagickFalse)
1239 quantum_type=CMYKAQuantum;
1241 if (image->storage_class == PseudoClass)
1243 quantum_type=IndexQuantum;
1244 if (image->matte != MagickFalse)
1245 quantum_type=IndexAlphaQuantum;
1247 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1248 (image->type == GrayscaleMatteType))
1250 quantum_type=GrayQuantum;
1251 if (image->matte != MagickFalse)
1252 quantum_type=GrayAlphaQuantum;
1255 switch (image->compression)
1257 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1258 case BZipCompression:
1263 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1264 bzip_info.bzalloc=AcquireBZIPMemory;
1265 bzip_info.bzfree=RelinquishBZIPMemory;
1266 bzip_info.opaque=(void *) NULL;
1267 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1274 #if defined(MAGICKCORE_LZMA_DELEGATE)
1275 case LZMACompression:
1280 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1281 allocator.alloc=AcquireLZMAMemory;
1282 allocator.free=RelinquishLZMAMemory;
1283 lzma_info=initialize_lzma;
1284 lzma_info.allocator=(&allocator);
1285 code=lzma_auto_decoder(&lzma_info,-1,0);
1286 if (code != LZMA_OK)
1291 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1292 case LZWCompression:
1293 case ZipCompression:
1298 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1299 zip_info.zalloc=AcquireZIPMemory;
1300 zip_info.zfree=RelinquishZIPMemory;
1301 zip_info.opaque=(voidpf) NULL;
1302 code=inflateInit(&zip_info);
1308 case RLECompression:
1310 GetPixelInfo(image,&pixel);
1316 pixels=GetQuantumPixels(quantum_info);
1318 for (y=0; y < (ssize_t) image->rows; y++)
1326 if (status == MagickFalse)
1328 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1329 if (q == (Quantum *) NULL)
1331 switch (image->compression)
1333 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1334 case BZipCompression:
1336 bzip_info.next_out=(char *) pixels;
1337 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1340 if (bzip_info.avail_in == 0)
1342 bzip_info.next_in=(char *) compress_pixels;
1343 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1345 length=(size_t) ReadBlobMSBLong(image);
1346 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1347 (unsigned char *) bzip_info.next_in);
1349 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1351 } while (bzip_info.avail_out != 0);
1352 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1353 quantum_type,pixels,exception);
1357 #if defined(MAGICKCORE_LZMA_DELEGATE)
1358 case LZMACompression:
1360 lzma_info.next_out=pixels;
1361 lzma_info.avail_out=packet_size*image->columns;
1367 if (lzma_info.avail_in == 0)
1369 lzma_info.next_in=compress_pixels;
1370 length=(size_t) ReadBlobMSBLong(image);
1371 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1372 (unsigned char *) lzma_info.next_in);
1374 code=lzma_code(&lzma_info,LZMA_RUN);
1380 if (code == LZMA_STREAM_END)
1382 } while (lzma_info.avail_out != 0);
1383 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1384 quantum_type,pixels,exception);
1388 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1389 case LZWCompression:
1390 case ZipCompression:
1392 zip_info.next_out=pixels;
1393 zip_info.avail_out=(uInt) (packet_size*image->columns);
1396 if (zip_info.avail_in == 0)
1398 zip_info.next_in=compress_pixels;
1399 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1401 length=(size_t) ReadBlobMSBLong(image);
1402 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1405 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1407 } while (zip_info.avail_out != 0);
1408 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1409 quantum_type,pixels,exception);
1413 case RLECompression:
1415 for (x=0; x < (ssize_t) image->columns; x++)
1419 count=ReadBlob(image,packet_size,pixels);
1420 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1423 if (image->storage_class == PseudoClass)
1424 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1427 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1428 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1429 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1430 if (image->colorspace == CMYKColorspace)
1431 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1433 if (image->matte != MagickFalse)
1434 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1435 q+=GetPixelChannels(image);
1441 count=ReadBlob(image,packet_size*image->columns,pixels);
1442 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1443 quantum_type,pixels,exception);
1447 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1450 SetQuantumImageType(image,quantum_type);
1451 switch (image->compression)
1453 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1454 case BZipCompression:
1464 offset=SeekBlob(image,-((MagickOffsetType)
1465 bzip_info.avail_in),SEEK_CUR);
1467 ThrowReaderException(CorruptImageError,
1468 "ImproperImageHeader");
1470 code=BZ2_bzDecompressEnd(&bzip_info);
1476 #if defined(MAGICKCORE_LZMA_DELEGATE)
1477 case LZMACompression:
1482 code=lzma_code(&lzma_info,LZMA_FINISH);
1483 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1485 lzma_end(&lzma_info);
1489 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1490 case LZWCompression:
1491 case ZipCompression:
1501 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1504 ThrowReaderException(CorruptImageError,
1505 "ImproperImageHeader");
1507 code=inflateEnd(&zip_info);
1508 if (code != LZMA_OK)
1516 quantum_info=DestroyQuantumInfo(quantum_info);
1517 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1518 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1520 image=DestroyImageList(image);
1521 return((Image *) NULL);
1523 if (EOFBlob(image) != MagickFalse)
1525 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1530 Proceed to next image.
1532 if (image_info->number_scenes != 0)
1533 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1537 c=ReadBlobByte(image);
1538 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1542 Allocate next image structure.
1544 AcquireNextImage(image_info,image,exception);
1545 if (GetNextImageInList(image) == (Image *) NULL)
1547 image=DestroyImageList(image);
1548 return((Image *) NULL);
1550 image=SyncNextImageInList(image);
1551 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1552 GetBlobSize(image));
1553 if (status == MagickFalse)
1557 (void) CloseBlob(image);
1558 return(GetFirstImageInList(image));
1562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1566 % R e g i s t e r M I F F I m a g e %
1570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1572 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1573 % supported formats. The properties include the image format tag, a method to
1574 % read and/or write the format, whether the format supports the saving of more
1575 % than one frame to the same file or blob, whether the format supports native
1576 % in-memory I/O, and a brief description of the format.
1578 % The format of the RegisterMIFFImage method is:
1580 % size_t RegisterMIFFImage(void)
1583 ModuleExport size_t RegisterMIFFImage(void)
1586 version[MaxTextExtent];
1592 #if defined(MagickImageCoderSignatureText)
1593 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1594 #if defined(ZLIB_VERSION)
1595 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1596 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1598 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1599 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1602 entry=SetMagickInfo("MIFF");
1603 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1604 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1605 entry->magick=(IsImageFormatHandler *) IsMIFF;
1606 entry->seekable_stream=MagickTrue;
1607 entry->description=ConstantString("Magick Image File Format");
1608 if (*version != '\0')
1609 entry->version=ConstantString(version);
1610 entry->module=ConstantString("MIFF");
1611 (void) RegisterMagickInfo(entry);
1612 return(MagickImageCoderSignature);
1616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1620 % U n r e g i s t e r M I F F I m a g e %
1624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1626 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1627 % from the list of supported formats.
1629 % The format of the UnregisterMIFFImage method is:
1631 % UnregisterMIFFImage(void)
1634 ModuleExport void UnregisterMIFFImage(void)
1636 (void) UnregisterMagickInfo("MIFF");
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % W r i t e M I F F I m a g e %
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650 % WriteMIFFImage() writes a MIFF image to a file.
1652 % The format of the WriteMIFFImage method is:
1654 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1655 % Image *image,ExceptionInfo *exception)
1657 % Compression code contributed by Kyle Shorter.
1659 % A description of each parameter follows:
1661 % o image_info: the image info.
1663 % o image: the image.
1665 % o exception: return any errors or warnings in this structure.
1669 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1670 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1672 if (image->storage_class != DirectClass)
1674 switch (image->depth)
1678 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1679 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1682 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1685 *pixels++=(unsigned char) pixel->index;
1689 (void) ThrowMagickException(exception,GetMagickModule(),
1690 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1692 switch (image->depth)
1699 if (image->matte != MagickFalse)
1701 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1702 pixels=PopLongPixel(MSBEndian,value,pixels);
1711 if (image->matte != MagickFalse)
1713 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1714 pixels=PopShortPixel(MSBEndian,value,pixels);
1723 if (image->matte != MagickFalse)
1725 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1727 pixels=PopCharPixel(value,pixels);
1732 (void) ThrowMagickException(exception,GetMagickModule(),
1733 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1735 *pixels++=(unsigned char) length;
1738 switch (image->depth)
1745 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1746 pixels=PopLongPixel(MSBEndian,value,pixels);
1747 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1748 pixels=PopLongPixel(MSBEndian,value,pixels);
1749 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1750 pixels=PopLongPixel(MSBEndian,value,pixels);
1751 if (image->colorspace == CMYKColorspace)
1753 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1754 pixels=PopLongPixel(MSBEndian,value,pixels);
1756 if (image->matte != MagickFalse)
1758 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1759 pixels=PopLongPixel(MSBEndian,value,pixels);
1768 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1769 pixels=PopShortPixel(MSBEndian,value,pixels);
1770 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1771 pixels=PopShortPixel(MSBEndian,value,pixels);
1772 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1773 pixels=PopShortPixel(MSBEndian,value,pixels);
1774 if (image->colorspace == CMYKColorspace)
1776 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1777 pixels=PopShortPixel(MSBEndian,value,pixels);
1779 if (image->matte != MagickFalse)
1781 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1782 pixels=PopShortPixel(MSBEndian,value,pixels);
1791 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1792 pixels=PopCharPixel(value,pixels);
1793 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->green));
1794 pixels=PopCharPixel(value,pixels);
1795 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1796 pixels=PopCharPixel(value,pixels);
1797 if (image->colorspace == CMYKColorspace)
1799 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1801 pixels=PopCharPixel(value,pixels);
1803 if (image->matte != MagickFalse)
1805 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1807 pixels=PopCharPixel(value,pixels);
1812 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1813 "ImageDepthNotSupported","`%s'",image->filename);
1815 *pixels++=(unsigned char) length;
1819 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1820 Image *image,ExceptionInfo *exception)
1822 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1828 buffer[MaxTextExtent];
1837 #if defined(MAGICKCORE_LZMA_DELEGATE)
1842 initialize_lzma = LZMA_STREAM_INIT,
1877 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1883 Open output image file.
1885 assert(image_info != (const ImageInfo *) NULL);
1886 assert(image_info->signature == MagickSignature);
1887 assert(image != (Image *) NULL);
1888 assert(image->signature == MagickSignature);
1889 if (image->debug != MagickFalse)
1890 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1891 assert(exception != (ExceptionInfo *) NULL);
1892 assert(exception->signature == MagickSignature);
1893 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1894 if (status == MagickFalse)
1900 Allocate image pixels.
1902 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1903 image->depth <= 32 ? 32UL : 64UL;
1904 quantum_info=AcquireQuantumInfo(image_info,image);
1905 if (quantum_info == (QuantumInfo *) NULL)
1906 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1907 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1908 (quantum_info->format == UndefinedQuantumFormat) &&
1909 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1911 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1912 if (status == MagickFalse)
1913 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1915 if ((image->storage_class == PseudoClass) &&
1916 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1917 (void) SetImageStorageClass(image,DirectClass,exception);
1918 if (IsImageGray(image,exception) != MagickFalse)
1920 (void) SetImageStorageClass(image,DirectClass,exception);
1921 (void) SetImageColorspace(image,GRAYColorspace,exception);
1923 compression=image->compression;
1924 if (image_info->compression != UndefinedCompression)
1925 compression=image_info->compression;
1926 switch (compression)
1928 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1929 case LZMACompression: compression=NoCompression; break;
1931 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1932 case LZWCompression:
1933 case ZipCompression: compression=NoCompression; break;
1935 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1936 case BZipCompression: compression=NoCompression; break;
1938 case RLECompression:
1940 if (quantum_info->format == FloatingPointQuantumFormat)
1941 compression=NoCompression;
1942 GetPixelInfo(image,&target);
1948 packet_size=(size_t) (quantum_info->depth/8);
1949 if (image->storage_class == DirectClass)
1950 packet_size=(size_t) (3*quantum_info->depth/8);
1951 if (IsImageGray(image,exception) != MagickFalse)
1952 packet_size=(size_t) (quantum_info->depth/8);
1953 if (image->matte != MagickFalse)
1954 packet_size+=quantum_info->depth/8;
1955 if (image->colorspace == CMYKColorspace)
1956 packet_size+=quantum_info->depth/8;
1957 if (compression == RLECompression)
1959 length=image->columns;
1960 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1961 packet_size*image->columns));
1962 if ((compression == BZipCompression) || (compression == ZipCompression))
1963 if (length != (size_t) ((unsigned int) length))
1964 compression=NoCompression;
1965 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1966 sizeof(*compress_pixels));
1967 if (compress_pixels == (unsigned char *) NULL)
1968 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1972 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1973 (void) FormatLocaleString(buffer,MaxTextExtent,
1974 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1975 MagickClassOptions,image->storage_class),(double) image->colors,
1976 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1977 (void) WriteBlobString(image,buffer);
1978 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1979 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1981 (void) WriteBlobString(image,buffer);
1982 if (image->type != UndefinedType)
1984 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1985 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1986 (void) WriteBlobString(image,buffer);
1988 if (image->colorspace != UndefinedColorspace)
1990 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1991 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1992 (void) WriteBlobString(image,buffer);
1994 if (compression != UndefinedCompression)
1996 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
1997 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
1998 compression),(double) image->quality);
1999 (void) WriteBlobString(image,buffer);
2001 if (image->units != UndefinedResolution)
2003 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2004 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2005 (void) WriteBlobString(image,buffer);
2007 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2009 (void) FormatLocaleString(buffer,MaxTextExtent,
2010 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2011 (void) WriteBlobString(image,buffer);
2013 if ((image->page.width != 0) || (image->page.height != 0))
2015 (void) FormatLocaleString(buffer,MaxTextExtent,
2016 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2017 image->page.height,(double) image->page.x,(double) image->page.y);
2018 (void) WriteBlobString(image,buffer);
2021 if ((image->page.x != 0) || (image->page.y != 0))
2023 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2024 (long) image->page.x,(long) image->page.y);
2025 (void) WriteBlobString(image,buffer);
2027 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2029 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2030 (long) image->tile_offset.x,(long) image->tile_offset.y);
2031 (void) WriteBlobString(image,buffer);
2033 if ((GetNextImageInList(image) != (Image *) NULL) ||
2034 (GetPreviousImageInList(image) != (Image *) NULL))
2036 if (image->scene == 0)
2037 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2038 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2039 (double) image->delay,(double) image->ticks_per_second);
2041 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2042 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2043 image->scene,(double) image->iterations,(double) image->delay,
2044 (double) image->ticks_per_second);
2045 (void) WriteBlobString(image,buffer);
2049 if (image->scene != 0)
2051 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2052 (double) image->scene);
2053 (void) WriteBlobString(image,buffer);
2055 if (image->iterations != 0)
2057 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2058 (double) image->iterations);
2059 (void) WriteBlobString(image,buffer);
2061 if (image->delay != 0)
2063 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2064 (double) image->delay);
2065 (void) WriteBlobString(image,buffer);
2067 if (image->ticks_per_second != UndefinedTicksPerSecond)
2069 (void) FormatLocaleString(buffer,MaxTextExtent,
2070 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2071 (void) WriteBlobString(image,buffer);
2074 if (image->gravity != UndefinedGravity)
2076 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2077 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2078 (void) WriteBlobString(image,buffer);
2080 if (image->dispose != UndefinedDispose)
2082 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2083 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2084 (void) WriteBlobString(image,buffer);
2086 if (image->rendering_intent != UndefinedIntent)
2088 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2089 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2090 (void) WriteBlobString(image,buffer);
2092 if (image->gamma != 0.0)
2094 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2096 (void) WriteBlobString(image,buffer);
2098 if (image->chromaticity.white_point.x != 0.0)
2101 Note chomaticity points.
2103 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2104 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2105 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2106 image->chromaticity.green_primary.x,
2107 image->chromaticity.green_primary.y,
2108 image->chromaticity.blue_primary.x,
2109 image->chromaticity.blue_primary.y);
2110 (void) WriteBlobString(image,buffer);
2111 (void) FormatLocaleString(buffer,MaxTextExtent,
2112 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2113 image->chromaticity.white_point.y);
2114 (void) WriteBlobString(image,buffer);
2116 if (image->orientation != UndefinedOrientation)
2118 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2119 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2120 (void) WriteBlobString(image,buffer);
2122 if (image->profiles != (void *) NULL)
2131 Write image profiles.
2133 ResetImageProfileIterator(image);
2134 name=GetNextImageProfile(image);
2135 while (name != (const char *) NULL)
2137 profile=GetImageProfile(image,name);
2138 if (profile != (StringInfo *) NULL)
2140 (void) FormatLocaleString(buffer,MaxTextExtent,
2141 "profile:%s=%.20g\n",name,(double)
2142 GetStringInfoLength(profile));
2143 (void) WriteBlobString(image,buffer);
2145 name=GetNextImageProfile(image);
2148 if (image->montage != (char *) NULL)
2150 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2152 (void) WriteBlobString(image,buffer);
2154 if (quantum_info->format == FloatingPointQuantumFormat)
2155 (void) SetImageProperty(image,"quantum:format","floating-point",
2157 ResetImagePropertyIterator(image);
2158 property=GetNextImageProperty(image);
2159 while (property != (const char *) NULL)
2161 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2162 (void) WriteBlobString(image,buffer);
2163 value=GetImageProperty(image,property,exception);
2164 if (value != (const char *) NULL)
2166 for (i=0; i < (ssize_t) strlen(value); i++)
2167 if (isspace((int) ((unsigned char) value[i])) != 0)
2169 if (i <= (ssize_t) strlen(value))
2170 (void) WriteBlobByte(image,'{');
2171 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2172 if (i <= (ssize_t) strlen(value))
2173 (void) WriteBlobByte(image,'}');
2175 (void) WriteBlobByte(image,'\n');
2176 property=GetNextImageProperty(image);
2178 (void) WriteBlobString(image,"\f\n:\032");
2179 if (image->montage != (char *) NULL)
2182 Write montage tile directory.
2184 if (image->directory != (char *) NULL)
2185 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2187 (void) WriteBlobByte(image,'\0');
2189 if (image->profiles != (void *) NULL)
2200 ResetImageProfileIterator(image);
2201 name=GetNextImageProfile(image);
2202 while (name != (const char *) NULL)
2204 profile=GetImageProfile(image,name);
2205 (void) WriteBlob(image,GetStringInfoLength(profile),
2206 GetStringInfoDatum(profile));
2207 name=GetNextImageProfile(image);
2210 if (image->storage_class == PseudoClass)
2222 packet_size=(size_t) (3*quantum_info->depth/8);
2223 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2224 packet_size*sizeof(*colormap));
2225 if (colormap == (unsigned char *) NULL)
2226 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2228 Write colormap to file.
2231 for (i=0; i < (ssize_t) image->colors; i++)
2233 switch (quantum_info->depth)
2236 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2239 register unsigned int
2242 pixel=ScaleQuantumToLong(image->colormap[i].red);
2243 q=PopLongPixel(MSBEndian,pixel,q);
2244 pixel=ScaleQuantumToLong(image->colormap[i].green);
2245 q=PopLongPixel(MSBEndian,pixel,q);
2246 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2247 q=PopLongPixel(MSBEndian,pixel,q);
2252 register unsigned short
2255 pixel=ScaleQuantumToShort(image->colormap[i].red);
2256 q=PopShortPixel(MSBEndian,pixel,q);
2257 pixel=ScaleQuantumToShort(image->colormap[i].green);
2258 q=PopShortPixel(MSBEndian,pixel,q);
2259 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2260 q=PopShortPixel(MSBEndian,pixel,q);
2265 register unsigned char
2268 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2269 q=PopCharPixel(pixel,q);
2270 pixel=(unsigned char) ScaleQuantumToChar(
2271 image->colormap[i].green);
2272 q=PopCharPixel(pixel,q);
2273 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2274 q=PopCharPixel(pixel,q);
2279 (void) WriteBlob(image,packet_size*image->colors,colormap);
2280 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2283 Write image pixels to file.
2286 switch (compression)
2288 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2289 case BZipCompression:
2294 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2295 bzip_info.bzalloc=AcquireBZIPMemory;
2296 bzip_info.bzfree=RelinquishBZIPMemory;
2297 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2298 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2299 9)),(int) image_info->verbose,0);
2305 #if defined(MAGICKCORE_LZMA_DELEGATE)
2306 case LZMACompression:
2311 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2312 allocator.alloc=AcquireLZMAMemory;
2313 allocator.free=RelinquishLZMAMemory;
2314 lzma_info=initialize_lzma;
2315 lzma_info.allocator=&allocator;
2316 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2317 if (code != LZMA_OK)
2322 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2323 case LZWCompression:
2324 case ZipCompression:
2329 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2330 zip_info.zalloc=AcquireZIPMemory;
2331 zip_info.zfree=RelinquishZIPMemory;
2332 code=deflateInit(&zip_info,(int) (image->quality ==
2333 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2342 quantum_type=GetQuantumType(image,exception);
2343 pixels=GetQuantumPixels(quantum_info);
2344 for (y=0; y < (ssize_t) image->rows; y++)
2346 register const Quantum
2352 if (status == MagickFalse)
2354 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2355 if (p == (const Quantum *) NULL)
2358 switch (compression)
2360 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2361 case BZipCompression:
2363 bzip_info.next_in=(char *) pixels;
2364 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2365 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2366 quantum_type,pixels,exception);
2372 bzip_info.next_out=(char *) compress_pixels;
2373 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2375 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2378 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2381 (void) WriteBlobMSBLong(image,(unsigned int) length);
2382 (void) WriteBlob(image,length,compress_pixels);
2384 } while (bzip_info.avail_in != 0);
2388 #if defined(MAGICKCORE_LZMA_DELEGATE)
2389 case LZMACompression:
2391 lzma_info.next_in=pixels;
2392 lzma_info.avail_in=packet_size*image->columns;
2393 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2394 quantum_type,pixels,exception);
2400 lzma_info.next_out=compress_pixels;
2401 lzma_info.avail_out=packet_size*image->columns;
2402 code=lzma_code(&lzma_info,LZMA_RUN);
2403 if (code != LZMA_OK)
2405 length=(size_t) (lzma_info.next_out-compress_pixels);
2408 (void) WriteBlobMSBLong(image,(unsigned int) length);
2409 (void) WriteBlob(image,length,compress_pixels);
2411 } while (lzma_info.avail_in != 0);
2415 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2416 case LZWCompression:
2417 case ZipCompression:
2419 zip_info.next_in=pixels;
2420 zip_info.avail_in=(uInt) (packet_size*image->columns);
2421 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2422 quantum_type,pixels,exception);
2428 zip_info.next_out=compress_pixels;
2429 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2430 code=deflate(&zip_info,Z_SYNC_FLUSH);
2433 length=(size_t) (zip_info.next_out-compress_pixels);
2436 (void) WriteBlobMSBLong(image,(unsigned int) length);
2437 (void) WriteBlob(image,length,compress_pixels);
2439 } while (zip_info.avail_in != 0);
2443 case RLECompression:
2446 GetPixelInfoPixel(image,p,&pixel);
2447 p+=GetPixelChannels(image);
2448 for (x=1; x < (ssize_t) image->columns; x++)
2450 GetPixelInfoPixel(image,p,&target);
2451 if ((length < 255) &&
2452 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2456 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2459 GetPixelInfoPixel(image,p,&pixel);
2460 p+=GetPixelChannels(image);
2462 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2463 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2468 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2469 quantum_type,pixels,exception);
2470 (void) WriteBlob(image,packet_size*image->columns,pixels);
2474 if (image->previous == (Image *) NULL)
2476 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2478 if (status == MagickFalse)
2482 switch (compression)
2484 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2485 case BZipCompression:
2492 if (status == MagickFalse)
2494 bzip_info.next_out=(char *) compress_pixels;
2495 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2497 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2498 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2501 (void) WriteBlobMSBLong(image,(unsigned int) length);
2502 (void) WriteBlob(image,length,compress_pixels);
2504 if (code == BZ_STREAM_END)
2507 code=BZ2_bzCompressEnd(&bzip_info);
2513 #if defined(MAGICKCORE_LZMA_DELEGATE)
2514 case LZMACompression:
2521 if (status == MagickFalse)
2523 lzma_info.next_out=compress_pixels;
2524 lzma_info.avail_out=packet_size*image->columns;
2525 code=lzma_code(&lzma_info,LZMA_FINISH);
2526 length=(size_t) (lzma_info.next_out-compress_pixels);
2529 (void) WriteBlobMSBLong(image,(unsigned int) length);
2530 (void) WriteBlob(image,length,compress_pixels);
2532 if (code == LZMA_STREAM_END)
2535 lzma_end(&lzma_info);
2539 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2540 case LZWCompression:
2541 case ZipCompression:
2548 if (status == MagickFalse)
2550 zip_info.next_out=compress_pixels;
2551 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2552 code=deflate(&zip_info,Z_FINISH);
2553 length=(size_t) (zip_info.next_out-compress_pixels);
2556 (void) WriteBlobMSBLong(image,(unsigned int) length);
2557 (void) WriteBlob(image,length,compress_pixels);
2559 if (code == Z_STREAM_END)
2562 code=deflateEnd(&zip_info);
2571 quantum_info=DestroyQuantumInfo(quantum_info);
2572 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2573 if (GetNextImageInList(image) == (Image *) NULL)
2575 image=SyncNextImageInList(image);
2576 status=SetImageProgress(image,SaveImagesTag,scene++,
2577 GetImageListLength(image));
2578 if (status == MagickFalse)
2580 } while (image_info->adjoin != MagickFalse);
2581 (void) CloseBlob(image);