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);
531 c=ReadBlobByte(image);
533 if ((c == EOF) || (c == (int) '}'))
535 if ((size_t) (p-comment+1) >= length)
539 comment=(char *) ResizeQuantumMemory(comment,length+
540 MaxTextExtent,sizeof(*comment));
541 if (comment == (char *) NULL)
543 p=comment+strlen(comment);
547 if (comment == (char *) NULL)
548 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
550 (void) SetImageProperty(image,"comment",comment,exception);
551 comment=DestroyString(comment);
552 c=ReadBlobByte(image);
555 if (isalnum(c) != MagickFalse)
565 if ((size_t) (p-keyword) < (MaxTextExtent-1))
567 c=ReadBlobByte(image);
571 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
572 c=ReadBlobByte(image);
576 Get the keyword value.
578 c=ReadBlobByte(image);
579 while ((c != (int) '}') && (c != EOF))
581 if ((size_t) (p-options+1) >= length)
585 options=(char *) ResizeQuantumMemory(options,length+
586 MaxTextExtent,sizeof(*options));
587 if (options == (char *) NULL)
589 p=options+strlen(options);
591 if (options == (char *) NULL)
592 ThrowReaderException(ResourceLimitError,
593 "MemoryAllocationFailed");
595 c=ReadBlobByte(image);
598 c=ReadBlobByte(image);
602 c=ReadBlobByte(image);
606 if (isspace((int) ((unsigned char) c)) != 0)
612 (void) CopyMagickString(options,options+1,MaxTextExtent);
614 Assign a value to the specified keyword.
621 if (LocaleCompare(keyword,"background-color") == 0)
623 (void) QueryColorCompliance(options,AllCompliance,
624 &image->background_color,exception);
627 if (LocaleCompare(keyword,"blue-primary") == 0)
629 flags=ParseGeometry(options,&geometry_info);
630 image->chromaticity.blue_primary.x=geometry_info.rho;
631 image->chromaticity.blue_primary.y=geometry_info.sigma;
632 if ((flags & SigmaValue) == 0)
633 image->chromaticity.blue_primary.y=
634 image->chromaticity.blue_primary.x;
637 if (LocaleCompare(keyword,"border-color") == 0)
639 (void) QueryColorCompliance(options,AllCompliance,
640 &image->border_color,exception);
643 (void) SetImageProperty(image,keyword,options,exception);
649 if (LocaleCompare(keyword,"class") == 0)
654 storage_class=ParseCommandOption(MagickClassOptions,
655 MagickFalse,options);
656 if (storage_class < 0)
658 image->storage_class=(ClassType) storage_class;
661 if (LocaleCompare(keyword,"colors") == 0)
663 colors=StringToUnsignedLong(options);
666 if (LocaleCompare(keyword,"colorspace") == 0)
671 colorspace=ParseCommandOption(MagickColorspaceOptions,
672 MagickFalse,options);
675 image->colorspace=(ColorspaceType) colorspace;
678 if (LocaleCompare(keyword,"compression") == 0)
683 compression=ParseCommandOption(MagickCompressOptions,
684 MagickFalse,options);
687 image->compression=(CompressionType) compression;
690 if (LocaleCompare(keyword,"columns") == 0)
692 image->columns=StringToUnsignedLong(options);
695 (void) SetImageProperty(image,keyword,options,exception);
701 if (LocaleCompare(keyword,"delay") == 0)
703 image->delay=StringToUnsignedLong(options);
706 if (LocaleCompare(keyword,"depth") == 0)
708 image->depth=StringToUnsignedLong(options);
711 if (LocaleCompare(keyword,"dispose") == 0)
716 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
720 image->dispose=(DisposeType) dispose;
723 (void) SetImageProperty(image,keyword,options,exception);
729 if (LocaleCompare(keyword,"endian") == 0)
734 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
738 image->endian=(EndianType) endian;
741 (void) SetImageProperty(image,keyword,options,exception);
747 if (LocaleCompare(keyword,"gamma") == 0)
749 image->gamma=StringToDouble(options,(char **) NULL);
752 if (LocaleCompare(keyword,"gravity") == 0)
757 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
761 image->gravity=(GravityType) gravity;
764 if (LocaleCompare(keyword,"green-primary") == 0)
766 flags=ParseGeometry(options,&geometry_info);
767 image->chromaticity.green_primary.x=geometry_info.rho;
768 image->chromaticity.green_primary.y=geometry_info.sigma;
769 if ((flags & SigmaValue) == 0)
770 image->chromaticity.green_primary.y=
771 image->chromaticity.green_primary.x;
774 (void) SetImageProperty(image,keyword,options,exception);
780 if (LocaleCompare(keyword,"id") == 0)
782 (void) CopyMagickString(id,options,MaxTextExtent);
785 if (LocaleCompare(keyword,"iterations") == 0)
787 image->iterations=StringToUnsignedLong(options);
790 (void) SetImageProperty(image,keyword,options,exception);
796 if (LocaleCompare(keyword,"matte") == 0)
801 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
805 image->matte=(MagickBooleanType) matte;
808 if (LocaleCompare(keyword,"matte-color") == 0)
810 (void) QueryColorCompliance(options,AllCompliance,
811 &image->matte_color,exception);
814 if (LocaleCompare(keyword,"montage") == 0)
816 (void) CloneString(&image->montage,options);
819 (void) SetImageProperty(image,keyword,options,exception);
825 if (LocaleCompare(keyword,"opaque") == 0)
830 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
834 image->matte=(MagickBooleanType) matte;
837 if (LocaleCompare(keyword,"orientation") == 0)
842 orientation=ParseCommandOption(MagickOrientationOptions,
843 MagickFalse,options);
846 image->orientation=(OrientationType) orientation;
849 (void) SetImageProperty(image,keyword,options,exception);
855 if (LocaleCompare(keyword,"page") == 0)
860 geometry=GetPageGeometry(options);
861 (void) ParseAbsoluteGeometry(geometry,&image->page);
862 geometry=DestroyString(geometry);
865 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
866 (LocaleNCompare(keyword,"profile-",8) == 0))
871 if (profiles == (LinkedListInfo *) NULL)
872 profiles=NewLinkedList(0);
873 (void) AppendValueToLinkedList(profiles,
874 AcquireString(keyword+8));
875 profile=BlobToStringInfo((const void *) NULL,(size_t)
876 StringToLong(options));
877 if (profile == (StringInfo *) NULL)
878 ThrowReaderException(ResourceLimitError,
879 "MemoryAllocationFailed");
880 (void) SetImageProfile(image,keyword+8,profile,exception);
881 profile=DestroyStringInfo(profile);
884 (void) SetImageProperty(image,keyword,options,exception);
890 if (LocaleCompare(keyword,"quality") == 0)
892 image->quality=StringToUnsignedLong(options);
895 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
896 (LocaleCompare(keyword,"quantum:format") == 0))
901 format=ParseCommandOption(MagickQuantumFormatOptions,
902 MagickFalse,options);
905 quantum_format=(QuantumFormatType) format;
908 (void) SetImageProperty(image,keyword,options,exception);
914 if (LocaleCompare(keyword,"red-primary") == 0)
916 flags=ParseGeometry(options,&geometry_info);
917 image->chromaticity.red_primary.x=geometry_info.rho;
918 image->chromaticity.red_primary.y=geometry_info.sigma;
919 if ((flags & SigmaValue) == 0)
920 image->chromaticity.red_primary.y=
921 image->chromaticity.red_primary.x;
924 if (LocaleCompare(keyword,"rendering-intent") == 0)
929 rendering_intent=ParseCommandOption(MagickIntentOptions,
930 MagickFalse,options);
931 if (rendering_intent < 0)
933 image->rendering_intent=(RenderingIntent) rendering_intent;
936 if (LocaleCompare(keyword,"resolution") == 0)
938 flags=ParseGeometry(options,&geometry_info);
939 image->resolution.x=geometry_info.rho;
940 image->resolution.y=geometry_info.sigma;
941 if ((flags & SigmaValue) == 0)
942 image->resolution.y=image->resolution.x;
945 if (LocaleCompare(keyword,"rows") == 0)
947 image->rows=StringToUnsignedLong(options);
950 (void) SetImageProperty(image,keyword,options,exception);
956 if (LocaleCompare(keyword,"scene") == 0)
958 image->scene=StringToUnsignedLong(options);
961 (void) SetImageProperty(image,keyword,options,exception);
967 if (LocaleCompare(keyword,"ticks-per-second") == 0)
969 image->ticks_per_second=(ssize_t) StringToLong(options);
972 if (LocaleCompare(keyword,"tile-offset") == 0)
977 geometry=GetPageGeometry(options);
978 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
979 geometry=DestroyString(geometry);
982 if (LocaleCompare(keyword,"type") == 0)
987 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
991 image->type=(ImageType) type;
994 (void) SetImageProperty(image,keyword,options,exception);
1000 if (LocaleCompare(keyword,"units") == 0)
1005 units=ParseCommandOption(MagickResolutionOptions,
1006 MagickFalse,options);
1009 image->units=(ResolutionType) units;
1012 (void) SetImageProperty(image,keyword,options,exception);
1018 if (LocaleCompare(keyword,"version") == 0)
1020 version=StringToDouble(options,(char **) NULL);
1023 (void) SetImageProperty(image,keyword,options,exception);
1029 if (LocaleCompare(keyword,"white-point") == 0)
1031 flags=ParseGeometry(options,&geometry_info);
1032 image->chromaticity.white_point.x=geometry_info.rho;
1033 image->chromaticity.white_point.y=geometry_info.rho;
1034 if ((flags & SigmaValue) != 0)
1035 image->chromaticity.white_point.y=
1036 image->chromaticity.white_point.x;
1039 (void) SetImageProperty(image,keyword,options,exception);
1044 (void) SetImageProperty(image,keyword,options,exception);
1050 c=ReadBlobByte(image);
1051 while (isspace((int) ((unsigned char) c)) != 0)
1052 c=ReadBlobByte(image);
1054 options=DestroyString(options);
1055 (void) ReadBlobByte(image);
1057 Verify that required image information is defined.
1059 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1060 (image->storage_class == UndefinedClass) ||
1061 (image->columns == 0) || (image->rows == 0))
1062 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1063 if (image->montage != (char *) NULL)
1071 length=MaxTextExtent;
1072 image->directory=AcquireString((char *) NULL);
1077 if ((strlen(image->directory)+MaxTextExtent) >= length)
1080 Allocate more memory for the image directory.
1083 image->directory=(char *) ResizeQuantumMemory(image->directory,
1084 length+MaxTextExtent,sizeof(*image->directory));
1085 if (image->directory == (char *) NULL)
1086 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1087 p=image->directory+strlen(image->directory);
1089 c=ReadBlobByte(image);
1091 } while (c != (int) '\0');
1093 if (profiles != (LinkedListInfo *) NULL)
1102 Read image profiles.
1104 ResetLinkedListIterator(profiles);
1105 name=(const char *) GetNextValueInLinkedList(profiles);
1106 while (name != (const char *) NULL)
1108 profile=GetImageProfile(image,name);
1109 if (profile != (StringInfo *) NULL)
1111 register unsigned char
1114 p=GetStringInfoDatum(profile);
1115 count=ReadBlob(image,GetStringInfoLength(profile),p);
1118 name=(const char *) GetNextValueInLinkedList(profiles);
1120 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1122 image->depth=GetImageQuantumDepth(image,MagickFalse);
1123 if (image->storage_class == PseudoClass)
1126 Create image colormap.
1128 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1129 if (status == MagickFalse)
1130 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1140 Read image colormap from file.
1142 packet_size=(size_t) (3UL*image->depth/8UL);
1143 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1144 packet_size*sizeof(*colormap));
1145 if (colormap == (unsigned char *) NULL)
1146 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1147 count=ReadBlob(image,packet_size*image->colors,colormap);
1149 switch (image->depth)
1152 ThrowReaderException(CorruptImageError,
1153 "ImageDepthNotSupported");
1159 for (i=0; i < (ssize_t) image->colors; i++)
1161 p=PushCharPixel(p,&pixel);
1162 image->colormap[i].red=ScaleCharToQuantum(pixel);
1163 p=PushCharPixel(p,&pixel);
1164 image->colormap[i].green=ScaleCharToQuantum(pixel);
1165 p=PushCharPixel(p,&pixel);
1166 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1175 for (i=0; i < (ssize_t) image->colors; i++)
1177 p=PushShortPixel(MSBEndian,p,&pixel);
1178 image->colormap[i].red=ScaleShortToQuantum(pixel);
1179 p=PushShortPixel(MSBEndian,p,&pixel);
1180 image->colormap[i].green=ScaleShortToQuantum(pixel);
1181 p=PushShortPixel(MSBEndian,p,&pixel);
1182 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1191 for (i=0; i < (ssize_t) image->colors; i++)
1193 p=PushLongPixel(MSBEndian,p,&pixel);
1194 image->colormap[i].red=ScaleLongToQuantum(pixel);
1195 p=PushLongPixel(MSBEndian,p,&pixel);
1196 image->colormap[i].green=ScaleLongToQuantum(pixel);
1197 p=PushLongPixel(MSBEndian,p,&pixel);
1198 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1203 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1206 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1207 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1210 Allocate image pixels.
1212 quantum_info=AcquireQuantumInfo(image_info,image);
1213 if (quantum_info == (QuantumInfo *) NULL)
1214 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1215 if (quantum_format != UndefinedQuantumFormat)
1217 status=SetQuantumFormat(image,quantum_info,quantum_format);
1218 if (status == MagickFalse)
1219 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1221 packet_size=(size_t) (quantum_info->depth/8);
1222 if (image->storage_class == DirectClass)
1223 packet_size=(size_t) (3*quantum_info->depth/8);
1224 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1225 (image->type == GrayscaleMatteType))
1226 packet_size=quantum_info->depth/8;
1227 if (image->matte != MagickFalse)
1228 packet_size+=quantum_info->depth/8;
1229 if (image->colorspace == CMYKColorspace)
1230 packet_size+=quantum_info->depth/8;
1231 if (image->compression == RLECompression)
1233 length=image->columns;
1234 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1235 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1237 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1238 sizeof(*compress_pixels));
1239 if (compress_pixels == (unsigned char *) NULL)
1240 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1244 quantum_type=RGBQuantum;
1245 if (image->matte != MagickFalse)
1246 quantum_type=RGBAQuantum;
1247 if (image->colorspace == CMYKColorspace)
1249 quantum_type=CMYKQuantum;
1250 if (image->matte != MagickFalse)
1251 quantum_type=CMYKAQuantum;
1253 if (image->storage_class == PseudoClass)
1255 quantum_type=IndexQuantum;
1256 if (image->matte != MagickFalse)
1257 quantum_type=IndexAlphaQuantum;
1259 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1260 (image->type == GrayscaleMatteType))
1262 quantum_type=GrayQuantum;
1263 if (image->matte != MagickFalse)
1264 quantum_type=GrayAlphaQuantum;
1267 switch (image->compression)
1269 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1270 case BZipCompression:
1275 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1276 bzip_info.bzalloc=AcquireBZIPMemory;
1277 bzip_info.bzfree=RelinquishBZIPMemory;
1278 bzip_info.opaque=(void *) NULL;
1279 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1286 #if defined(MAGICKCORE_LZMA_DELEGATE)
1287 case LZMACompression:
1292 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1293 allocator.alloc=AcquireLZMAMemory;
1294 allocator.free=RelinquishLZMAMemory;
1295 lzma_info=initialize_lzma;
1296 lzma_info.allocator=(&allocator);
1297 code=lzma_auto_decoder(&lzma_info,-1,0);
1298 if (code != LZMA_OK)
1303 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1304 case LZWCompression:
1305 case ZipCompression:
1310 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1311 zip_info.zalloc=AcquireZIPMemory;
1312 zip_info.zfree=RelinquishZIPMemory;
1313 zip_info.opaque=(voidpf) NULL;
1314 code=inflateInit(&zip_info);
1320 case RLECompression:
1322 GetPixelInfo(image,&pixel);
1328 pixels=GetQuantumPixels(quantum_info);
1330 for (y=0; y < (ssize_t) image->rows; y++)
1338 if (status == MagickFalse)
1340 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1341 if (q == (Quantum *) NULL)
1343 switch (image->compression)
1345 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1346 case BZipCompression:
1348 bzip_info.next_out=(char *) pixels;
1349 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1352 if (bzip_info.avail_in == 0)
1354 bzip_info.next_in=(char *) compress_pixels;
1355 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1357 length=(size_t) ReadBlobMSBLong(image);
1358 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1359 (unsigned char *) bzip_info.next_in);
1361 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1363 } while (bzip_info.avail_out != 0);
1364 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1365 quantum_type,pixels,exception);
1369 #if defined(MAGICKCORE_LZMA_DELEGATE)
1370 case LZMACompression:
1372 lzma_info.next_out=pixels;
1373 lzma_info.avail_out=packet_size*image->columns;
1379 if (lzma_info.avail_in == 0)
1381 lzma_info.next_in=compress_pixels;
1382 length=(size_t) ReadBlobMSBLong(image);
1383 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1384 (unsigned char *) lzma_info.next_in);
1386 code=lzma_code(&lzma_info,LZMA_RUN);
1392 if (code == LZMA_STREAM_END)
1394 } while (lzma_info.avail_out != 0);
1395 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1396 quantum_type,pixels,exception);
1400 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1401 case LZWCompression:
1402 case ZipCompression:
1404 zip_info.next_out=pixels;
1405 zip_info.avail_out=(uInt) (packet_size*image->columns);
1408 if (zip_info.avail_in == 0)
1410 zip_info.next_in=compress_pixels;
1411 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1413 length=(size_t) ReadBlobMSBLong(image);
1414 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1417 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1419 } while (zip_info.avail_out != 0);
1420 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1421 quantum_type,pixels,exception);
1425 case RLECompression:
1427 for (x=0; x < (ssize_t) image->columns; x++)
1431 count=ReadBlob(image,packet_size,pixels);
1432 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1435 if (image->storage_class == PseudoClass)
1436 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1439 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1440 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1441 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1442 if (image->colorspace == CMYKColorspace)
1443 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1445 if (image->matte != MagickFalse)
1446 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1447 q+=GetPixelChannels(image);
1453 count=ReadBlob(image,packet_size*image->columns,pixels);
1454 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1455 quantum_type,pixels,exception);
1459 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1462 SetQuantumImageType(image,quantum_type);
1463 switch (image->compression)
1465 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1466 case BZipCompression:
1476 offset=SeekBlob(image,-((MagickOffsetType)
1477 bzip_info.avail_in),SEEK_CUR);
1479 ThrowReaderException(CorruptImageError,
1480 "ImproperImageHeader");
1482 code=BZ2_bzDecompressEnd(&bzip_info);
1488 #if defined(MAGICKCORE_LZMA_DELEGATE)
1489 case LZMACompression:
1494 code=lzma_code(&lzma_info,LZMA_FINISH);
1495 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1497 lzma_end(&lzma_info);
1501 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1502 case LZWCompression:
1503 case ZipCompression:
1513 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1516 ThrowReaderException(CorruptImageError,
1517 "ImproperImageHeader");
1519 code=inflateEnd(&zip_info);
1520 if (code != LZMA_OK)
1528 quantum_info=DestroyQuantumInfo(quantum_info);
1529 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1530 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1532 image=DestroyImageList(image);
1533 return((Image *) NULL);
1535 if (EOFBlob(image) != MagickFalse)
1537 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1542 Proceed to next image.
1544 if (image_info->number_scenes != 0)
1545 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1549 c=ReadBlobByte(image);
1550 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1554 Allocate next image structure.
1556 AcquireNextImage(image_info,image,exception);
1557 if (GetNextImageInList(image) == (Image *) NULL)
1559 image=DestroyImageList(image);
1560 return((Image *) NULL);
1562 image=SyncNextImageInList(image);
1563 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1564 GetBlobSize(image));
1565 if (status == MagickFalse)
1569 (void) CloseBlob(image);
1570 return(GetFirstImageInList(image));
1574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1578 % R e g i s t e r M I F F I m a g e %
1582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1584 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1585 % supported formats. The properties include the image format tag, a method to
1586 % read and/or write the format, whether the format supports the saving of more
1587 % than one frame to the same file or blob, whether the format supports native
1588 % in-memory I/O, and a brief description of the format.
1590 % The format of the RegisterMIFFImage method is:
1592 % size_t RegisterMIFFImage(void)
1595 ModuleExport size_t RegisterMIFFImage(void)
1598 version[MaxTextExtent];
1604 #if defined(MagickImageCoderSignatureText)
1605 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1606 #if defined(ZLIB_VERSION)
1607 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1608 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1610 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1611 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1614 entry=SetMagickInfo("MIFF");
1615 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1616 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1617 entry->magick=(IsImageFormatHandler *) IsMIFF;
1618 entry->seekable_stream=MagickTrue;
1619 entry->description=ConstantString("Magick Image File Format");
1620 if (*version != '\0')
1621 entry->version=ConstantString(version);
1622 entry->module=ConstantString("MIFF");
1623 (void) RegisterMagickInfo(entry);
1624 return(MagickImageCoderSignature);
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632 % U n r e g i s t e r M I F F I m a g e %
1636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1639 % from the list of supported formats.
1641 % The format of the UnregisterMIFFImage method is:
1643 % UnregisterMIFFImage(void)
1646 ModuleExport void UnregisterMIFFImage(void)
1648 (void) UnregisterMagickInfo("MIFF");
1652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1656 % W r i t e M I F F I m a g e %
1660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1662 % WriteMIFFImage() writes a MIFF image to a file.
1664 % The format of the WriteMIFFImage method is:
1666 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1667 % Image *image,ExceptionInfo *exception)
1669 % Compression code contributed by Kyle Shorter.
1671 % A description of each parameter follows:
1673 % o image_info: the image info.
1675 % o image: the image.
1677 % o exception: return any errors or warnings in this structure.
1681 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1682 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1684 if (image->storage_class != DirectClass)
1686 switch (image->depth)
1690 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1691 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1694 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1697 *pixels++=(unsigned char) pixel->index;
1701 (void) ThrowMagickException(exception,GetMagickModule(),
1702 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1704 switch (image->depth)
1711 if (image->matte != MagickFalse)
1713 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1714 pixels=PopLongPixel(MSBEndian,value,pixels);
1723 if (image->matte != MagickFalse)
1725 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1726 pixels=PopShortPixel(MSBEndian,value,pixels);
1735 if (image->matte != MagickFalse)
1737 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1739 pixels=PopCharPixel(value,pixels);
1744 (void) ThrowMagickException(exception,GetMagickModule(),
1745 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1747 *pixels++=(unsigned char) length;
1750 switch (image->depth)
1757 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1758 pixels=PopLongPixel(MSBEndian,value,pixels);
1759 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1760 pixels=PopLongPixel(MSBEndian,value,pixels);
1761 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1762 pixels=PopLongPixel(MSBEndian,value,pixels);
1763 if (image->colorspace == CMYKColorspace)
1765 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1766 pixels=PopLongPixel(MSBEndian,value,pixels);
1768 if (image->matte != MagickFalse)
1770 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1771 pixels=PopLongPixel(MSBEndian,value,pixels);
1780 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1781 pixels=PopShortPixel(MSBEndian,value,pixels);
1782 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1783 pixels=PopShortPixel(MSBEndian,value,pixels);
1784 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1785 pixels=PopShortPixel(MSBEndian,value,pixels);
1786 if (image->colorspace == CMYKColorspace)
1788 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1789 pixels=PopShortPixel(MSBEndian,value,pixels);
1791 if (image->matte != MagickFalse)
1793 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1794 pixels=PopShortPixel(MSBEndian,value,pixels);
1803 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1804 pixels=PopCharPixel(value,pixels);
1805 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->green));
1806 pixels=PopCharPixel(value,pixels);
1807 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1808 pixels=PopCharPixel(value,pixels);
1809 if (image->colorspace == CMYKColorspace)
1811 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1813 pixels=PopCharPixel(value,pixels);
1815 if (image->matte != MagickFalse)
1817 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1819 pixels=PopCharPixel(value,pixels);
1824 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1825 "ImageDepthNotSupported","`%s'",image->filename);
1827 *pixels++=(unsigned char) length;
1831 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1832 Image *image,ExceptionInfo *exception)
1834 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1840 buffer[MaxTextExtent];
1849 #if defined(MAGICKCORE_LZMA_DELEGATE)
1854 initialize_lzma = LZMA_STREAM_INIT,
1889 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1895 Open output image file.
1897 assert(image_info != (const ImageInfo *) NULL);
1898 assert(image_info->signature == MagickSignature);
1899 assert(image != (Image *) NULL);
1900 assert(image->signature == MagickSignature);
1901 if (image->debug != MagickFalse)
1902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1903 assert(exception != (ExceptionInfo *) NULL);
1904 assert(exception->signature == MagickSignature);
1905 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1906 if (status == MagickFalse)
1912 Allocate image pixels.
1914 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1915 image->depth <= 32 ? 32UL : 64UL;
1916 quantum_info=AcquireQuantumInfo(image_info,image);
1917 if (quantum_info == (QuantumInfo *) NULL)
1918 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1919 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1920 (quantum_info->format == UndefinedQuantumFormat) &&
1921 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1923 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1924 if (status == MagickFalse)
1925 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1927 if ((image->storage_class == PseudoClass) &&
1928 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1929 (void) SetImageStorageClass(image,DirectClass,exception);
1930 if (IsImageGray(image,exception) != MagickFalse)
1932 (void) SetImageStorageClass(image,DirectClass,exception);
1933 (void) SetImageColorspace(image,GRAYColorspace,exception);
1935 compression=image->compression;
1936 if (image_info->compression != UndefinedCompression)
1937 compression=image_info->compression;
1938 switch (compression)
1940 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1941 case LZMACompression: compression=NoCompression; break;
1943 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1944 case LZWCompression:
1945 case ZipCompression: compression=NoCompression; break;
1947 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1948 case BZipCompression: compression=NoCompression; break;
1950 case RLECompression:
1952 if (quantum_info->format == FloatingPointQuantumFormat)
1953 compression=NoCompression;
1954 GetPixelInfo(image,&target);
1960 packet_size=(size_t) (quantum_info->depth/8);
1961 if (image->storage_class == DirectClass)
1962 packet_size=(size_t) (3*quantum_info->depth/8);
1963 if (IsImageGray(image,exception) != MagickFalse)
1964 packet_size=(size_t) (quantum_info->depth/8);
1965 if (image->matte != MagickFalse)
1966 packet_size+=quantum_info->depth/8;
1967 if (image->colorspace == CMYKColorspace)
1968 packet_size+=quantum_info->depth/8;
1969 if (compression == RLECompression)
1971 length=image->columns;
1972 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1973 packet_size*image->columns));
1974 if ((compression == BZipCompression) || (compression == ZipCompression))
1975 if (length != (size_t) ((unsigned int) length))
1976 compression=NoCompression;
1977 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1978 sizeof(*compress_pixels));
1979 if (compress_pixels == (unsigned char *) NULL)
1980 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1984 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1985 (void) FormatLocaleString(buffer,MaxTextExtent,
1986 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1987 MagickClassOptions,image->storage_class),(double) image->colors,
1988 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1989 (void) WriteBlobString(image,buffer);
1990 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1991 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1993 (void) WriteBlobString(image,buffer);
1994 if (image->type != UndefinedType)
1996 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1997 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1998 (void) WriteBlobString(image,buffer);
2000 if (image->colorspace != UndefinedColorspace)
2002 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2003 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2004 (void) WriteBlobString(image,buffer);
2006 if (compression != UndefinedCompression)
2008 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2009 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2010 compression),(double) image->quality);
2011 (void) WriteBlobString(image,buffer);
2013 if (image->units != UndefinedResolution)
2015 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2016 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2017 (void) WriteBlobString(image,buffer);
2019 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2021 (void) FormatLocaleString(buffer,MaxTextExtent,
2022 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2023 (void) WriteBlobString(image,buffer);
2025 if ((image->page.width != 0) || (image->page.height != 0))
2027 (void) FormatLocaleString(buffer,MaxTextExtent,
2028 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2029 image->page.height,(double) image->page.x,(double) image->page.y);
2030 (void) WriteBlobString(image,buffer);
2033 if ((image->page.x != 0) || (image->page.y != 0))
2035 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2036 (long) image->page.x,(long) image->page.y);
2037 (void) WriteBlobString(image,buffer);
2039 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2041 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2042 (long) image->tile_offset.x,(long) image->tile_offset.y);
2043 (void) WriteBlobString(image,buffer);
2045 if ((GetNextImageInList(image) != (Image *) NULL) ||
2046 (GetPreviousImageInList(image) != (Image *) NULL))
2048 if (image->scene == 0)
2049 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2050 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2051 (double) image->delay,(double) image->ticks_per_second);
2053 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2054 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2055 image->scene,(double) image->iterations,(double) image->delay,
2056 (double) image->ticks_per_second);
2057 (void) WriteBlobString(image,buffer);
2061 if (image->scene != 0)
2063 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2064 (double) image->scene);
2065 (void) WriteBlobString(image,buffer);
2067 if (image->iterations != 0)
2069 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2070 (double) image->iterations);
2071 (void) WriteBlobString(image,buffer);
2073 if (image->delay != 0)
2075 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2076 (double) image->delay);
2077 (void) WriteBlobString(image,buffer);
2079 if (image->ticks_per_second != UndefinedTicksPerSecond)
2081 (void) FormatLocaleString(buffer,MaxTextExtent,
2082 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2083 (void) WriteBlobString(image,buffer);
2086 if (image->gravity != UndefinedGravity)
2088 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2089 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2090 (void) WriteBlobString(image,buffer);
2092 if (image->dispose != UndefinedDispose)
2094 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2095 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2096 (void) WriteBlobString(image,buffer);
2098 if (image->rendering_intent != UndefinedIntent)
2100 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2101 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2102 (void) WriteBlobString(image,buffer);
2104 if (image->gamma != 0.0)
2106 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2108 (void) WriteBlobString(image,buffer);
2110 if (image->chromaticity.white_point.x != 0.0)
2113 Note chomaticity points.
2115 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2116 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2117 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2118 image->chromaticity.green_primary.x,
2119 image->chromaticity.green_primary.y,
2120 image->chromaticity.blue_primary.x,
2121 image->chromaticity.blue_primary.y);
2122 (void) WriteBlobString(image,buffer);
2123 (void) FormatLocaleString(buffer,MaxTextExtent,
2124 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2125 image->chromaticity.white_point.y);
2126 (void) WriteBlobString(image,buffer);
2128 if (image->orientation != UndefinedOrientation)
2130 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2131 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2132 (void) WriteBlobString(image,buffer);
2134 if (image->profiles != (void *) NULL)
2143 Write image profiles.
2145 ResetImageProfileIterator(image);
2146 name=GetNextImageProfile(image);
2147 while (name != (const char *) NULL)
2149 profile=GetImageProfile(image,name);
2150 if (profile != (StringInfo *) NULL)
2152 (void) FormatLocaleString(buffer,MaxTextExtent,
2153 "profile:%s=%.20g\n",name,(double)
2154 GetStringInfoLength(profile));
2155 (void) WriteBlobString(image,buffer);
2157 name=GetNextImageProfile(image);
2160 if (image->montage != (char *) NULL)
2162 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2164 (void) WriteBlobString(image,buffer);
2166 if (quantum_info->format == FloatingPointQuantumFormat)
2167 (void) SetImageProperty(image,"quantum:format","floating-point",
2169 ResetImagePropertyIterator(image);
2170 property=GetNextImageProperty(image);
2171 while (property != (const char *) NULL)
2173 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2174 (void) WriteBlobString(image,buffer);
2175 value=GetImageProperty(image,property,exception);
2176 if (value != (const char *) NULL)
2178 for (i=0; i < (ssize_t) strlen(value); i++)
2179 if (isspace((int) ((unsigned char) value[i])) != 0)
2181 if (i == (ssize_t) strlen(value))
2182 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2185 (void) WriteBlobByte(image,'{');
2186 for (i=0; i < (ssize_t) strlen(value); i++)
2188 if (value[i] == (int) '}')
2189 (void) WriteBlobByte(image,'\\');
2190 (void) WriteBlobByte(image,value[i]);
2192 (void) WriteBlobByte(image,'}');
2195 (void) WriteBlobByte(image,'\n');
2196 property=GetNextImageProperty(image);
2198 (void) WriteBlobString(image,"\f\n:\032");
2199 if (image->montage != (char *) NULL)
2202 Write montage tile directory.
2204 if (image->directory != (char *) NULL)
2205 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2207 (void) WriteBlobByte(image,'\0');
2209 if (image->profiles != (void *) NULL)
2220 ResetImageProfileIterator(image);
2221 name=GetNextImageProfile(image);
2222 while (name != (const char *) NULL)
2224 profile=GetImageProfile(image,name);
2225 (void) WriteBlob(image,GetStringInfoLength(profile),
2226 GetStringInfoDatum(profile));
2227 name=GetNextImageProfile(image);
2230 if (image->storage_class == PseudoClass)
2242 packet_size=(size_t) (3*quantum_info->depth/8);
2243 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2244 packet_size*sizeof(*colormap));
2245 if (colormap == (unsigned char *) NULL)
2246 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2248 Write colormap to file.
2251 for (i=0; i < (ssize_t) image->colors; i++)
2253 switch (quantum_info->depth)
2256 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2259 register unsigned int
2262 pixel=ScaleQuantumToLong(image->colormap[i].red);
2263 q=PopLongPixel(MSBEndian,pixel,q);
2264 pixel=ScaleQuantumToLong(image->colormap[i].green);
2265 q=PopLongPixel(MSBEndian,pixel,q);
2266 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2267 q=PopLongPixel(MSBEndian,pixel,q);
2272 register unsigned short
2275 pixel=ScaleQuantumToShort(image->colormap[i].red);
2276 q=PopShortPixel(MSBEndian,pixel,q);
2277 pixel=ScaleQuantumToShort(image->colormap[i].green);
2278 q=PopShortPixel(MSBEndian,pixel,q);
2279 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2280 q=PopShortPixel(MSBEndian,pixel,q);
2285 register unsigned char
2288 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2289 q=PopCharPixel(pixel,q);
2290 pixel=(unsigned char) ScaleQuantumToChar(
2291 image->colormap[i].green);
2292 q=PopCharPixel(pixel,q);
2293 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2294 q=PopCharPixel(pixel,q);
2299 (void) WriteBlob(image,packet_size*image->colors,colormap);
2300 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2303 Write image pixels to file.
2306 switch (compression)
2308 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2309 case BZipCompression:
2314 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2315 bzip_info.bzalloc=AcquireBZIPMemory;
2316 bzip_info.bzfree=RelinquishBZIPMemory;
2317 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2318 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2319 9)),(int) image_info->verbose,0);
2325 #if defined(MAGICKCORE_LZMA_DELEGATE)
2326 case LZMACompression:
2331 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2332 allocator.alloc=AcquireLZMAMemory;
2333 allocator.free=RelinquishLZMAMemory;
2334 lzma_info=initialize_lzma;
2335 lzma_info.allocator=&allocator;
2336 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2337 if (code != LZMA_OK)
2342 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2343 case LZWCompression:
2344 case ZipCompression:
2349 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2350 zip_info.zalloc=AcquireZIPMemory;
2351 zip_info.zfree=RelinquishZIPMemory;
2352 code=deflateInit(&zip_info,(int) (image->quality ==
2353 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2362 quantum_type=GetQuantumType(image,exception);
2363 pixels=GetQuantumPixels(quantum_info);
2364 for (y=0; y < (ssize_t) image->rows; y++)
2366 register const Quantum
2372 if (status == MagickFalse)
2374 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2375 if (p == (const Quantum *) NULL)
2378 switch (compression)
2380 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2381 case BZipCompression:
2383 bzip_info.next_in=(char *) pixels;
2384 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2385 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2386 quantum_type,pixels,exception);
2392 bzip_info.next_out=(char *) compress_pixels;
2393 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2395 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2398 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2401 (void) WriteBlobMSBLong(image,(unsigned int) length);
2402 (void) WriteBlob(image,length,compress_pixels);
2404 } while (bzip_info.avail_in != 0);
2408 #if defined(MAGICKCORE_LZMA_DELEGATE)
2409 case LZMACompression:
2411 lzma_info.next_in=pixels;
2412 lzma_info.avail_in=packet_size*image->columns;
2413 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2414 quantum_type,pixels,exception);
2420 lzma_info.next_out=compress_pixels;
2421 lzma_info.avail_out=packet_size*image->columns;
2422 code=lzma_code(&lzma_info,LZMA_RUN);
2423 if (code != LZMA_OK)
2425 length=(size_t) (lzma_info.next_out-compress_pixels);
2428 (void) WriteBlobMSBLong(image,(unsigned int) length);
2429 (void) WriteBlob(image,length,compress_pixels);
2431 } while (lzma_info.avail_in != 0);
2435 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2436 case LZWCompression:
2437 case ZipCompression:
2439 zip_info.next_in=pixels;
2440 zip_info.avail_in=(uInt) (packet_size*image->columns);
2441 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2442 quantum_type,pixels,exception);
2448 zip_info.next_out=compress_pixels;
2449 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2450 code=deflate(&zip_info,Z_SYNC_FLUSH);
2453 length=(size_t) (zip_info.next_out-compress_pixels);
2456 (void) WriteBlobMSBLong(image,(unsigned int) length);
2457 (void) WriteBlob(image,length,compress_pixels);
2459 } while (zip_info.avail_in != 0);
2463 case RLECompression:
2466 GetPixelInfoPixel(image,p,&pixel);
2467 p+=GetPixelChannels(image);
2468 for (x=1; x < (ssize_t) image->columns; x++)
2470 GetPixelInfoPixel(image,p,&target);
2471 if ((length < 255) &&
2472 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2476 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2479 GetPixelInfoPixel(image,p,&pixel);
2480 p+=GetPixelChannels(image);
2482 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2483 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2488 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2489 quantum_type,pixels,exception);
2490 (void) WriteBlob(image,packet_size*image->columns,pixels);
2494 if (image->previous == (Image *) NULL)
2496 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2498 if (status == MagickFalse)
2502 switch (compression)
2504 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2505 case BZipCompression:
2512 if (status == MagickFalse)
2514 bzip_info.next_out=(char *) compress_pixels;
2515 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2517 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2518 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2521 (void) WriteBlobMSBLong(image,(unsigned int) length);
2522 (void) WriteBlob(image,length,compress_pixels);
2524 if (code == BZ_STREAM_END)
2527 code=BZ2_bzCompressEnd(&bzip_info);
2533 #if defined(MAGICKCORE_LZMA_DELEGATE)
2534 case LZMACompression:
2541 if (status == MagickFalse)
2543 lzma_info.next_out=compress_pixels;
2544 lzma_info.avail_out=packet_size*image->columns;
2545 code=lzma_code(&lzma_info,LZMA_FINISH);
2546 length=(size_t) (lzma_info.next_out-compress_pixels);
2549 (void) WriteBlobMSBLong(image,(unsigned int) length);
2550 (void) WriteBlob(image,length,compress_pixels);
2552 if (code == LZMA_STREAM_END)
2555 lzma_end(&lzma_info);
2559 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2560 case LZWCompression:
2561 case ZipCompression:
2568 if (status == MagickFalse)
2570 zip_info.next_out=compress_pixels;
2571 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2572 code=deflate(&zip_info,Z_FINISH);
2573 length=(size_t) (zip_info.next_out-compress_pixels);
2576 (void) WriteBlobMSBLong(image,(unsigned int) length);
2577 (void) WriteBlob(image,length,compress_pixels);
2579 if (code == Z_STREAM_END)
2582 code=deflateEnd(&zip_info);
2591 quantum_info=DestroyQuantumInfo(quantum_info);
2592 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2593 if (GetNextImageInList(image) == (Image *) NULL)
2595 image=SyncNextImageInList(image);
2596 status=SetImageProgress(image,SaveImagesTag,scene++,
2597 GetImageListLength(image));
2598 if (status == MagickFalse)
2600 } while (image_info->adjoin != MagickFalse);
2601 (void) CloseBlob(image);