2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colormap.h"
50 #include "magick/colormap-private.h"
51 #include "magick/colorspace.h"
52 #include "magick/constitute.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/hashmap.h"
56 #include "magick/geometry.h"
57 #include "magick/image.h"
58 #include "magick/image-private.h"
59 #include "magick/list.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/module.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/option.h"
66 #include "magick/pixel.h"
67 #include "magick/profile.h"
68 #include "magick/property.h"
69 #include "magick/quantum-private.h"
70 #include "magick/static.h"
71 #include "magick/statistic.h"
72 #include "magick/string_.h"
73 #include "magick/string-private.h"
74 #if defined(MAGICKCORE_BZLIB_DELEGATE)
77 #if defined(MAGICKCORE_LZMA_DELEGATE)
80 #if defined(MAGICKCORE_ZLIB_DELEGATE)
94 static MagickBooleanType
95 WriteMIFFImage(const ImageInfo *,Image *);
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % IsMIFF() returns MagickTrue if the image format type, identified by the
109 % magick string, is MIFF.
111 % The format of the IsMIFF method is:
113 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
115 % A description of each parameter follows:
117 % o magick: compare image format pattern against these bytes.
119 % o length: Specifies the length of the magick string.
122 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
126 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 % R e a d M I F F I m a g e %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
143 % memory necessary for the new Image structure and returns a pointer to the
146 % The format of the ReadMIFFImage method is:
148 % Image *ReadMIFFImage(const ImageInfo *image_info,
149 % ExceptionInfo *exception)
151 % Decompression code contributed by Kyle Shorter.
153 % A description of each parameter follows:
155 % o image_info: the image info.
157 % o exception: return any errors or warnings in this structure.
161 #if defined(MAGICKCORE_BZLIB_DELEGATE)
162 static void *AcquireBZIPMemory(void *context,int items,int size)
165 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
169 #if defined(MAGICKCORE_LZMA_DELEGATE)
170 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
173 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
177 #if defined(MAGICKCORE_ZLIB_DELEGATE)
178 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
182 return((voidpf) AcquireQuantumMemory(items,size));
186 static inline size_t MagickMax(const size_t x,const size_t y)
193 static inline size_t MagickMin(const size_t x,const size_t y)
200 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
201 size_t *length,PixelPacket *pixel,IndexPacket *index)
207 if (image->storage_class == PseudoClass)
209 *index=(IndexPacket) 0;
210 switch (image->depth)
214 *index=ConstrainColormapIndex(image,
215 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3));
221 *index=ConstrainColormapIndex(image,(*p << 8) | *(p+1));
227 *index=ConstrainColormapIndex(image,*p);
232 (void) ThrowMagickException(&image->exception,GetMagickModule(),
233 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
235 *pixel=image->colormap[(ssize_t) *index];
236 switch (image->depth)
243 if (image->matte != MagickFalse)
245 p=PushCharPixel(p,&quantum);
246 pixel->opacity=ScaleCharToQuantum(quantum);
255 if (image->matte != MagickFalse)
257 p=PushShortPixel(MSBEndian,p,&quantum);
258 pixel->opacity=(Quantum) (quantum >> (image->depth-
259 MAGICKCORE_QUANTUM_DEPTH));
268 if (image->matte != MagickFalse)
270 p=PushLongPixel(MSBEndian,p,&quantum);
271 pixel->opacity=(Quantum) (quantum >> (image->depth-
272 MAGICKCORE_QUANTUM_DEPTH));
277 (void) ThrowMagickException(&image->exception,GetMagickModule(),
278 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
280 *length=(size_t) (*p++)+1;
283 switch (image->depth)
290 p=PushCharPixel(p,&quantum);
291 SetRedPixelComponent(pixel,ScaleCharToQuantum(quantum));
292 p=PushCharPixel(p,&quantum);
293 SetGreenPixelComponent(pixel,ScaleCharToQuantum(quantum));
294 p=PushCharPixel(p,&quantum);
295 SetBluePixelComponent(pixel,ScaleCharToQuantum(quantum));
296 if (image->matte != MagickFalse)
298 p=PushCharPixel(p,&quantum);
299 SetOpacityPixelComponent(pixel,ScaleCharToQuantum(quantum));
301 if (image->colorspace == CMYKColorspace)
303 p=PushCharPixel(p,&quantum);
304 SetBlackPixelComponent(index,ScaleCharToQuantum(quantum));
313 p=PushShortPixel(MSBEndian,p,&quantum);
314 SetRedPixelComponent(pixel,quantum >> (image->depth-
315 MAGICKCORE_QUANTUM_DEPTH));
316 p=PushShortPixel(MSBEndian,p,&quantum);
317 SetGreenPixelComponent(pixel,quantum >> (image->depth-
318 MAGICKCORE_QUANTUM_DEPTH));
319 p=PushShortPixel(MSBEndian,p,&quantum);
320 SetBluePixelComponent(pixel,quantum >> (image->depth-
321 MAGICKCORE_QUANTUM_DEPTH));
322 if (image->matte != MagickFalse)
324 p=PushShortPixel(MSBEndian,p,&quantum);
325 SetOpacityPixelComponent(pixel,quantum >> (image->depth-
326 MAGICKCORE_QUANTUM_DEPTH));
328 if (image->colorspace == CMYKColorspace)
330 p=PushShortPixel(MSBEndian,p,&quantum);
331 SetBlackPixelComponent(index,quantum >> (image->depth-
332 MAGICKCORE_QUANTUM_DEPTH));
341 p=PushLongPixel(MSBEndian,p,&quantum);
342 SetRedPixelComponent(pixel,quantum >> (image->depth-
343 MAGICKCORE_QUANTUM_DEPTH));
344 p=PushLongPixel(MSBEndian,p,&quantum);
345 SetGreenPixelComponent(pixel,quantum >> (image->depth-
346 MAGICKCORE_QUANTUM_DEPTH));
347 p=PushLongPixel(MSBEndian,p,&quantum);
348 SetBluePixelComponent(pixel,quantum >> (image->depth-
349 MAGICKCORE_QUANTUM_DEPTH));
350 if (image->matte != MagickFalse)
352 p=PushLongPixel(MSBEndian,p,&quantum);
353 SetOpacityPixelComponent(pixel,quantum >> (image->depth-
354 MAGICKCORE_QUANTUM_DEPTH));
356 if (image->colorspace == CMYKColorspace)
358 p=PushLongPixel(MSBEndian,p,&quantum);
359 SetIndexPixelComponent(index,quantum >> (image->depth-
360 MAGICKCORE_QUANTUM_DEPTH));
365 (void) ThrowMagickException(&image->exception,GetMagickModule(),
366 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
368 *length=(size_t) (*p++)+1;
371 #if defined(MAGICKCORE_BZLIB_DELEGATE)
372 static void RelinquishBZIPMemory(void *context,void *memory)
375 memory=RelinquishMagickMemory(memory);
379 #if defined(MAGICKCORE_LZMA_DELEGATE)
380 static void RelinquishLZMAMemory(void *context,void *memory)
383 memory=RelinquishMagickMemory(memory);
387 #if defined(MAGICKCORE_ZLIB_DELEGATE)
388 static void RelinquishZIPMemory(voidpf context,voidpf memory)
391 memory=RelinquishMagickMemory(memory);
395 static Image *ReadMIFFImage(const ImageInfo *image_info,
396 ExceptionInfo *exception)
398 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
399 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
400 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
402 #if defined(MAGICKCORE_BZLIB_DELEGATE)
409 keyword[MaxTextExtent],
431 #if defined(MAGICKCORE_LZMA_DELEGATE)
433 initialize_lzma = LZMA_STREAM_INIT,
481 #if defined(MAGICKCORE_ZLIB_DELEGATE)
489 assert(image_info != (const ImageInfo *) NULL);
490 assert(image_info->signature == MagickSignature);
491 if (image_info->debug != MagickFalse)
492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
493 image_info->filename);
494 assert(exception != (ExceptionInfo *) NULL);
495 assert(exception->signature == MagickSignature);
496 image=AcquireImage(image_info);
497 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
498 if (status == MagickFalse)
500 image=DestroyImageList(image);
501 return((Image *) NULL);
504 Decode image header; header terminates one character beyond a ':'.
506 c=ReadBlobByte(image);
508 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
511 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
516 Decode image header; header terminates one character beyond a ':'.
518 length=MaxTextExtent;
519 options=AcquireString((char *) NULL);
520 quantum_format=UndefinedQuantumFormat;
521 profiles=(LinkedListInfo *) NULL;
524 image->compression=NoCompression;
525 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
536 Read comment-- any text between { }.
538 length=MaxTextExtent;
539 comment=AcquireString((char *) NULL);
540 for (p=comment; comment != (char *) NULL; p++)
542 c=ReadBlobByte(image);
543 if ((c == EOF) || (c == (int) '}'))
545 if ((size_t) (p-comment+1) >= length)
549 comment=(char *) ResizeQuantumMemory(comment,length+
550 MaxTextExtent,sizeof(*comment));
551 if (comment == (char *) NULL)
553 p=comment+strlen(comment);
557 if (comment == (char *) NULL)
558 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
560 (void) SetImageProperty(image,"comment",comment);
561 comment=DestroyString(comment);
562 c=ReadBlobByte(image);
565 if (isalnum(c) != MagickFalse)
575 if ((size_t) (p-keyword) < (MaxTextExtent-1))
577 c=ReadBlobByte(image);
581 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
582 c=ReadBlobByte(image);
586 Get the keyword value.
588 c=ReadBlobByte(image);
589 while ((c != (int) '}') && (c != EOF))
591 if ((size_t) (p-options+1) >= length)
595 options=(char *) ResizeQuantumMemory(options,length+
596 MaxTextExtent,sizeof(*options));
597 if (options == (char *) NULL)
599 p=options+strlen(options);
601 if (options == (char *) NULL)
602 ThrowReaderException(ResourceLimitError,
603 "MemoryAllocationFailed");
605 c=ReadBlobByte(image);
607 if (isspace((int) ((unsigned char) c)) != 0)
613 (void) CopyMagickString(options,options+1,MaxTextExtent);
615 Assign a value to the specified keyword.
622 if (LocaleCompare(keyword,"background-color") == 0)
624 (void) QueryColorDatabase(options,&image->background_color,
628 if (LocaleCompare(keyword,"blue-primary") == 0)
630 flags=ParseGeometry(options,&geometry_info);
631 image->chromaticity.blue_primary.x=geometry_info.rho;
632 image->chromaticity.blue_primary.y=geometry_info.sigma;
633 if ((flags & SigmaValue) == 0)
634 image->chromaticity.blue_primary.y=
635 image->chromaticity.blue_primary.x;
638 if (LocaleCompare(keyword,"border-color") == 0)
640 (void) QueryColorDatabase(options,&image->border_color,
644 (void) SetImageProperty(image,keyword,options);
650 if (LocaleCompare(keyword,"class") == 0)
655 storage_class=ParseCommandOption(MagickClassOptions,
656 MagickFalse,options);
657 if (storage_class < 0)
659 image->storage_class=(ClassType) storage_class;
662 if (LocaleCompare(keyword,"colors") == 0)
664 colors=StringToUnsignedLong(options);
667 if (LocaleCompare(keyword,"colorspace") == 0)
672 colorspace=ParseCommandOption(MagickColorspaceOptions,
673 MagickFalse,options);
676 image->colorspace=(ColorspaceType) colorspace;
679 if (LocaleCompare(keyword,"compression") == 0)
684 compression=ParseCommandOption(MagickCompressOptions,
685 MagickFalse,options);
688 image->compression=(CompressionType) compression;
691 if (LocaleCompare(keyword,"columns") == 0)
693 image->columns=StringToUnsignedLong(options);
696 (void) SetImageProperty(image,keyword,options);
702 if (LocaleCompare(keyword,"delay") == 0)
704 image->delay=StringToUnsignedLong(options);
707 if (LocaleCompare(keyword,"depth") == 0)
709 image->depth=StringToUnsignedLong(options);
712 if (LocaleCompare(keyword,"dispose") == 0)
717 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
721 image->dispose=(DisposeType) dispose;
724 (void) SetImageProperty(image,keyword,options);
730 if (LocaleCompare(keyword,"endian") == 0)
735 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
739 image->endian=(EndianType) endian;
742 (void) SetImageProperty(image,keyword,options);
748 if (LocaleCompare(keyword,"gamma") == 0)
750 image->gamma=StringToDouble(options,(char **) NULL);
753 if (LocaleCompare(keyword,"gravity") == 0)
758 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
762 image->gravity=(GravityType) gravity;
765 if (LocaleCompare(keyword,"green-primary") == 0)
767 flags=ParseGeometry(options,&geometry_info);
768 image->chromaticity.green_primary.x=geometry_info.rho;
769 image->chromaticity.green_primary.y=geometry_info.sigma;
770 if ((flags & SigmaValue) == 0)
771 image->chromaticity.green_primary.y=
772 image->chromaticity.green_primary.x;
775 (void) SetImageProperty(image,keyword,options);
781 if (LocaleCompare(keyword,"id") == 0)
783 (void) CopyMagickString(id,options,MaxTextExtent);
786 if (LocaleCompare(keyword,"iterations") == 0)
788 image->iterations=StringToUnsignedLong(options);
791 (void) SetImageProperty(image,keyword,options);
797 if (LocaleCompare(keyword,"matte") == 0)
802 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
806 image->matte=(MagickBooleanType) matte;
809 if (LocaleCompare(keyword,"matte-color") == 0)
811 (void) QueryColorDatabase(options,&image->matte_color,
815 if (LocaleCompare(keyword,"montage") == 0)
817 (void) CloneString(&image->montage,options);
820 (void) SetImageProperty(image,keyword,options);
826 if (LocaleCompare(keyword,"opaque") == 0)
831 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
835 image->matte=(MagickBooleanType) matte;
838 if (LocaleCompare(keyword,"orientation") == 0)
843 orientation=ParseCommandOption(MagickOrientationOptions,
844 MagickFalse,options);
847 image->orientation=(OrientationType) orientation;
850 (void) SetImageProperty(image,keyword,options);
856 if (LocaleCompare(keyword,"page") == 0)
861 geometry=GetPageGeometry(options);
862 (void) ParseAbsoluteGeometry(geometry,&image->page);
863 geometry=DestroyString(geometry);
866 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
867 (LocaleNCompare(keyword,"profile-",8) == 0))
872 if (profiles == (LinkedListInfo *) NULL)
873 profiles=NewLinkedList(0);
874 (void) AppendValueToLinkedList(profiles,
875 AcquireString(keyword+8));
876 profile=AcquireStringInfo((size_t) StringToLong(options));
877 (void) SetImageProfile(image,keyword+8,profile);
878 profile=DestroyStringInfo(profile);
881 (void) SetImageProperty(image,keyword,options);
887 if (LocaleCompare(keyword,"quality") == 0)
889 image->quality=StringToUnsignedLong(options);
892 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
893 (LocaleCompare(keyword,"quantum:format") == 0))
898 format=ParseCommandOption(MagickQuantumFormatOptions,
899 MagickFalse,options);
902 quantum_format=(QuantumFormatType) format;
905 (void) SetImageProperty(image,keyword,options);
911 if (LocaleCompare(keyword,"red-primary") == 0)
913 flags=ParseGeometry(options,&geometry_info);
914 image->chromaticity.red_primary.x=geometry_info.rho;
915 image->chromaticity.red_primary.y=geometry_info.sigma;
916 if ((flags & SigmaValue) == 0)
917 image->chromaticity.red_primary.y=
918 image->chromaticity.red_primary.x;
921 if (LocaleCompare(keyword,"rendering-intent") == 0)
926 rendering_intent=ParseCommandOption(MagickIntentOptions,
927 MagickFalse,options);
928 if (rendering_intent < 0)
930 image->rendering_intent=(RenderingIntent) rendering_intent;
933 if (LocaleCompare(keyword,"resolution") == 0)
935 flags=ParseGeometry(options,&geometry_info);
936 image->x_resolution=geometry_info.rho;
937 image->y_resolution=geometry_info.sigma;
938 if ((flags & SigmaValue) == 0)
939 image->y_resolution=image->x_resolution;
942 if (LocaleCompare(keyword,"rows") == 0)
944 image->rows=StringToUnsignedLong(options);
947 (void) SetImageProperty(image,keyword,options);
953 if (LocaleCompare(keyword,"scene") == 0)
955 image->scene=StringToUnsignedLong(options);
958 (void) SetImageProperty(image,keyword,options);
964 if (LocaleCompare(keyword,"ticks-per-second") == 0)
966 image->ticks_per_second=(ssize_t) StringToLong(options);
969 if (LocaleCompare(keyword,"tile-offset") == 0)
974 geometry=GetPageGeometry(options);
975 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
976 geometry=DestroyString(geometry);
979 if (LocaleCompare(keyword,"type") == 0)
984 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
988 image->type=(ImageType) type;
991 (void) SetImageProperty(image,keyword,options);
997 if (LocaleCompare(keyword,"units") == 0)
1002 units=ParseCommandOption(MagickResolutionOptions,
1003 MagickFalse,options);
1006 image->units=(ResolutionType) units;
1009 (void) SetImageProperty(image,keyword,options);
1015 if (LocaleCompare(keyword,"version") == 0)
1017 version=StringToDouble(options,(char **) NULL);
1020 (void) SetImageProperty(image,keyword,options);
1026 if (LocaleCompare(keyword,"white-point") == 0)
1028 flags=ParseGeometry(options,&geometry_info);
1029 image->chromaticity.white_point.x=geometry_info.rho;
1030 image->chromaticity.white_point.y=geometry_info.rho;
1031 if ((flags & SigmaValue) != 0)
1032 image->chromaticity.white_point.y=
1033 image->chromaticity.white_point.x;
1036 (void) SetImageProperty(image,keyword,options);
1041 (void) SetImageProperty(image,keyword,options);
1047 c=ReadBlobByte(image);
1048 while (isspace((int) ((unsigned char) c)) != 0)
1049 c=ReadBlobByte(image);
1051 options=DestroyString(options);
1052 (void) ReadBlobByte(image);
1054 Verify that required image information is defined.
1056 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1057 (image->storage_class == UndefinedClass) ||
1058 (image->columns == 0) || (image->rows == 0))
1059 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1060 if (image->montage != (char *) NULL)
1068 length=MaxTextExtent;
1069 image->directory=AcquireString((char *) NULL);
1074 if ((strlen(image->directory)+MaxTextExtent) >= length)
1077 Allocate more memory for the image directory.
1080 image->directory=(char *) ResizeQuantumMemory(image->directory,
1081 length+MaxTextExtent,sizeof(*image->directory));
1082 if (image->directory == (char *) NULL)
1083 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1084 p=image->directory+strlen(image->directory);
1086 c=ReadBlobByte(image);
1088 } while (c != (int) '\0');
1090 if (profiles != (LinkedListInfo *) NULL)
1099 Read image profiles.
1101 ResetLinkedListIterator(profiles);
1102 name=(const char *) GetNextValueInLinkedList(profiles);
1103 while (name != (const char *) NULL)
1105 profile=GetImageProfile(image,name);
1106 if (profile != (StringInfo *) NULL)
1108 register unsigned char
1111 p=GetStringInfoDatum(profile);
1112 count=ReadBlob(image,GetStringInfoLength(profile),p);
1115 name=(const char *) GetNextValueInLinkedList(profiles);
1117 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1119 image->depth=GetImageQuantumDepth(image,MagickFalse);
1120 if (image->storage_class == PseudoClass)
1123 Create image colormap.
1125 status=AcquireImageColormap(image,colors != 0 ? colors : 256);
1126 if (status == MagickFalse)
1127 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1137 Read image colormap from file.
1139 packet_size=(size_t) (3UL*image->depth/8UL);
1140 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1141 packet_size*sizeof(*colormap));
1142 if (colormap == (unsigned char *) NULL)
1143 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1144 count=ReadBlob(image,packet_size*image->colors,colormap);
1146 switch (image->depth)
1149 ThrowReaderException(CorruptImageError,
1150 "ImageDepthNotSupported");
1156 for (i=0; i < (ssize_t) image->colors; i++)
1158 p=PushCharPixel(p,&pixel);
1159 image->colormap[i].red=ScaleCharToQuantum(pixel);
1160 p=PushCharPixel(p,&pixel);
1161 image->colormap[i].green=ScaleCharToQuantum(pixel);
1162 p=PushCharPixel(p,&pixel);
1163 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1172 for (i=0; i < (ssize_t) image->colors; i++)
1174 p=PushShortPixel(MSBEndian,p,&pixel);
1175 image->colormap[i].red=ScaleShortToQuantum(pixel);
1176 p=PushShortPixel(MSBEndian,p,&pixel);
1177 image->colormap[i].green=ScaleShortToQuantum(pixel);
1178 p=PushShortPixel(MSBEndian,p,&pixel);
1179 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1188 for (i=0; i < (ssize_t) image->colors; i++)
1190 p=PushLongPixel(MSBEndian,p,&pixel);
1191 image->colormap[i].red=ScaleLongToQuantum(pixel);
1192 p=PushLongPixel(MSBEndian,p,&pixel);
1193 image->colormap[i].green=ScaleLongToQuantum(pixel);
1194 p=PushLongPixel(MSBEndian,p,&pixel);
1195 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1200 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1203 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1204 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1207 Allocate image pixels.
1209 quantum_info=AcquireQuantumInfo(image_info,image);
1210 if (quantum_info == (QuantumInfo *) NULL)
1211 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1212 if (quantum_format != UndefinedQuantumFormat)
1214 status=SetQuantumFormat(image,quantum_info,quantum_format);
1215 if (status == MagickFalse)
1216 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1218 packet_size=(size_t) (quantum_info->depth/8);
1219 if (image->storage_class == DirectClass)
1220 packet_size=(size_t) (3*quantum_info->depth/8);
1221 if (image->matte != MagickFalse)
1222 packet_size+=quantum_info->depth/8;
1223 if (image->colorspace == CMYKColorspace)
1224 packet_size+=quantum_info->depth/8;
1225 if (image->compression == RLECompression)
1227 length=image->columns;
1228 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1229 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1231 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1232 sizeof(*compress_pixels));
1233 if (compress_pixels == (unsigned char *) NULL)
1234 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1238 quantum_type=RGBQuantum;
1239 if (image->matte != MagickFalse)
1240 quantum_type=RGBAQuantum;
1241 if (image->colorspace == CMYKColorspace)
1243 quantum_type=CMYKQuantum;
1244 if (image->matte != MagickFalse)
1245 quantum_type=CMYKAQuantum;
1247 if (image->storage_class == PseudoClass)
1249 quantum_type=IndexQuantum;
1250 if (image->matte != MagickFalse)
1251 quantum_type=IndexAlphaQuantum;
1253 if (image->colorspace == GRAYColorspace)
1255 quantum_type=GrayQuantum;
1256 if (image->matte != MagickFalse)
1257 quantum_type=GrayAlphaQuantum;
1260 switch (image->compression)
1262 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1263 case BZipCompression:
1265 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1266 bzip_info.bzalloc=AcquireBZIPMemory;
1267 bzip_info.bzfree=RelinquishBZIPMemory;
1268 bzip_info.opaque=(void *) NULL;
1269 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1276 #if defined(MAGICKCORE_LZMA_DELEGATE)
1277 case LZMACompression:
1279 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1280 allocator.alloc=AcquireLZMAMemory;
1281 allocator.free=RelinquishLZMAMemory;
1282 lzma_info=initialize_lzma;
1283 lzma_info.allocator=(&allocator);
1284 code=lzma_auto_decoder(&lzma_info,-1,0);
1285 if (code != LZMA_OK)
1290 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1291 case LZWCompression:
1292 case ZipCompression:
1294 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1295 zip_info.zalloc=AcquireZIPMemory;
1296 zip_info.zfree=RelinquishZIPMemory;
1297 zip_info.opaque=(voidpf) NULL;
1298 code=inflateInit(&zip_info);
1304 case RLECompression:
1306 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1307 pixel.opacity=(Quantum) TransparentOpacity;
1308 index=(IndexPacket) 0;
1314 pixels=GetQuantumPixels(quantum_info);
1315 index=(IndexPacket) 0;
1317 for (y=0; y < (ssize_t) image->rows; y++)
1319 register IndexPacket
1325 register PixelPacket
1328 if (status == MagickFalse)
1330 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1331 if (q == (PixelPacket *) NULL)
1333 indexes=GetAuthenticIndexQueue(image);
1334 switch (image->compression)
1336 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1337 case BZipCompression:
1339 bzip_info.next_out=(char *) pixels;
1340 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1343 if (bzip_info.avail_in == 0)
1345 bzip_info.next_in=(char *) compress_pixels;
1346 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1348 length=(size_t) ReadBlobMSBLong(image);
1349 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1350 (unsigned char *) bzip_info.next_in);
1352 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1354 } while (bzip_info.avail_out != 0);
1355 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1356 quantum_type,pixels,exception);
1360 #if defined(MAGICKCORE_LZMA_DELEGATE)
1361 case LZMACompression:
1363 lzma_info.next_out=pixels;
1364 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,&index);
1423 if ((image->storage_class == PseudoClass) ||
1424 (image->colorspace == CMYKColorspace))
1425 SetIndexPixelComponent(indexes+x,index);
1426 SetRedPixelComponent(q,pixel.red);
1427 SetGreenPixelComponent(q,pixel.green);
1428 SetBluePixelComponent(q,pixel.blue);
1429 SetOpacityPixelComponent(q,pixel.opacity);
1436 count=ReadBlob(image,packet_size*image->columns,pixels);
1437 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1438 quantum_type,pixels,exception);
1442 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1445 SetQuantumImageType(image,quantum_type);
1446 switch (image->compression)
1448 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1449 case BZipCompression:
1456 offset=SeekBlob(image,-((MagickOffsetType)
1457 bzip_info.avail_in),SEEK_CUR);
1459 ThrowReaderException(CorruptImageError,
1460 "ImproperImageHeader");
1462 code=BZ2_bzDecompressEnd(&bzip_info);
1468 #if defined(MAGICKCORE_LZMA_DELEGATE)
1469 case LZMACompression:
1471 code=lzma_code(&lzma_info,LZMA_FINISH);
1472 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1474 lzma_end(&lzma_info);
1478 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1479 case LZWCompression:
1480 case ZipCompression:
1487 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1490 ThrowReaderException(CorruptImageError,
1491 "ImproperImageHeader");
1493 code=inflateEnd(&zip_info);
1494 if (code != LZMA_OK)
1502 quantum_info=DestroyQuantumInfo(quantum_info);
1503 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1504 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1506 image=DestroyImageList(image);
1507 return((Image *) NULL);
1509 if (EOFBlob(image) != MagickFalse)
1511 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1516 Proceed to next image.
1518 if (image_info->number_scenes != 0)
1519 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1523 c=ReadBlobByte(image);
1524 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1528 Allocate next image structure.
1530 AcquireNextImage(image_info,image);
1531 if (GetNextImageInList(image) == (Image *) NULL)
1533 image=DestroyImageList(image);
1534 return((Image *) NULL);
1536 image=SyncNextImageInList(image);
1537 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1538 GetBlobSize(image));
1539 if (status == MagickFalse)
1543 (void) CloseBlob(image);
1544 return(GetFirstImageInList(image));
1548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1552 % R e g i s t e r M I F F I m a g e %
1556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1558 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1559 % supported formats. The properties include the image format tag, a method to
1560 % read and/or write the format, whether the format supports the saving of more
1561 % than one frame to the same file or blob, whether the format supports native
1562 % in-memory I/O, and a brief description of the format.
1564 % The format of the RegisterMIFFImage method is:
1566 % size_t RegisterMIFFImage(void)
1569 ModuleExport size_t RegisterMIFFImage(void)
1572 version[MaxTextExtent];
1578 #if defined(MagickImageCoderSignatureText)
1579 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1580 #if defined(ZLIB_VERSION)
1581 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1582 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1584 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1585 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1588 entry=SetMagickInfo("MIFF");
1589 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1590 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1591 entry->magick=(IsImageFormatHandler *) IsMIFF;
1592 entry->seekable_stream=MagickTrue;
1593 entry->description=ConstantString("Magick Image File Format");
1594 if (*version != '\0')
1595 entry->version=ConstantString(version);
1596 entry->module=ConstantString("MIFF");
1597 (void) RegisterMagickInfo(entry);
1598 return(MagickImageCoderSignature);
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606 % U n r e g i s t e r M I F F I m a g e %
1610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1612 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1613 % from the list of supported formats.
1615 % The format of the UnregisterMIFFImage method is:
1617 % UnregisterMIFFImage(void)
1620 ModuleExport void UnregisterMIFFImage(void)
1622 (void) UnregisterMagickInfo("MIFF");
1626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % W r i t e M I F F I m a g e %
1634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636 % WriteMIFFImage() writes a MIFF image to a file.
1638 % The format of the WriteMIFFImage method is:
1640 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1643 % Compression code contributed by Kyle Shorter.
1645 % A description of each parameter follows:
1647 % o image_info: the image info.
1649 % o image: the image.
1653 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1654 size_t length,PixelPacket pixel,IndexPacket index)
1656 if (image->storage_class != DirectClass)
1658 switch (image->depth)
1662 *pixels++=(unsigned char) ((size_t) index >> 24);
1663 *pixels++=(unsigned char) ((size_t) index >> 16);
1666 *pixels++=(unsigned char) ((size_t) index >> 8);
1669 *pixels++=(unsigned char) index;
1673 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1674 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1676 switch (image->depth)
1683 if (image->matte != MagickFalse)
1685 value=ScaleQuantumToLong(pixel.opacity);
1686 pixels=PopLongPixel(MSBEndian,value,pixels);
1695 if (image->matte != MagickFalse)
1697 value=ScaleQuantumToShort(pixel.opacity);
1698 pixels=PopShortPixel(MSBEndian,value,pixels);
1707 if (image->matte != MagickFalse)
1709 value=(unsigned char) ScaleQuantumToChar(pixel.opacity);
1710 pixels=PopCharPixel(value,pixels);
1715 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1716 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1718 *pixels++=(unsigned char) length;
1721 switch (image->depth)
1728 value=ScaleQuantumToLong(pixel.red);
1729 pixels=PopLongPixel(MSBEndian,value,pixels);
1730 value=ScaleQuantumToLong(pixel.green);
1731 pixels=PopLongPixel(MSBEndian,value,pixels);
1732 value=ScaleQuantumToLong(pixel.blue);
1733 pixels=PopLongPixel(MSBEndian,value,pixels);
1734 if (image->matte != MagickFalse)
1736 value=ScaleQuantumToLong(pixel.opacity);
1737 pixels=PopLongPixel(MSBEndian,value,pixels);
1739 if (image->colorspace == CMYKColorspace)
1741 value=ScaleQuantumToLong(index);
1742 pixels=PopLongPixel(MSBEndian,value,pixels);
1751 value=ScaleQuantumToShort(pixel.red);
1752 pixels=PopShortPixel(MSBEndian,value,pixels);
1753 value=ScaleQuantumToShort(pixel.green);
1754 pixels=PopShortPixel(MSBEndian,value,pixels);
1755 value=ScaleQuantumToShort(pixel.blue);
1756 pixels=PopShortPixel(MSBEndian,value,pixels);
1757 if (image->matte != MagickFalse)
1759 value=ScaleQuantumToShort(pixel.opacity);
1760 pixels=PopShortPixel(MSBEndian,value,pixels);
1762 if (image->colorspace == CMYKColorspace)
1764 value=ScaleQuantumToShort(index);
1765 pixels=PopShortPixel(MSBEndian,value,pixels);
1774 value=(unsigned char) ScaleQuantumToChar(pixel.red);
1775 pixels=PopCharPixel(value,pixels);
1776 value=(unsigned char) ScaleQuantumToChar(pixel.green);
1777 pixels=PopCharPixel(value,pixels);
1778 value=(unsigned char) ScaleQuantumToChar(pixel.blue);
1779 pixels=PopCharPixel(value,pixels);
1780 if (image->matte != MagickFalse)
1782 value=(unsigned char) ScaleQuantumToChar(pixel.opacity);
1783 pixels=PopCharPixel(value,pixels);
1785 if (image->colorspace == CMYKColorspace)
1787 value=(unsigned char) ScaleQuantumToChar(index);
1788 pixels=PopCharPixel(value,pixels);
1793 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1794 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1796 *pixels++=(unsigned char) length;
1800 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1803 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1809 buffer[MaxTextExtent];
1824 #if defined(MAGICKCORE_LZMA_DELEGATE)
1829 initialize_lzma = LZMA_STREAM_INIT,
1863 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1869 Open output image file.
1871 assert(image_info != (const ImageInfo *) NULL);
1872 assert(image_info->signature == MagickSignature);
1873 assert(image != (Image *) NULL);
1874 assert(image->signature == MagickSignature);
1875 if (image->debug != MagickFalse)
1876 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1877 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1878 if (status == MagickFalse)
1885 Allocate image pixels.
1887 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1888 image->depth <= 32 ? 32UL : 64UL;
1889 quantum_info=AcquireQuantumInfo(image_info,image);
1890 if (quantum_info == (QuantumInfo *) NULL)
1891 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1892 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1893 (quantum_info->format == UndefinedQuantumFormat) &&
1894 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
1896 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1897 if (status == MagickFalse)
1898 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1900 if ((image->storage_class == PseudoClass) &&
1901 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1902 (void) SetImageStorageClass(image,DirectClass);
1903 if (IsGrayImage(image,&image->exception) != MagickFalse)
1905 image->storage_class=DirectClass;
1906 (void) SetImageColorspace(image,GRAYColorspace);
1908 compression=image->compression;
1909 if (image_info->compression != UndefinedCompression)
1910 compression=image_info->compression;
1911 switch (compression)
1913 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1914 case LZMACompression: compression=NoCompression; break;
1916 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1917 case LZWCompression:
1918 case ZipCompression: compression=NoCompression; break;
1920 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1921 case BZipCompression: compression=NoCompression; break;
1923 case RLECompression:
1925 if (quantum_info->format == FloatingPointQuantumFormat)
1926 compression=NoCompression;
1932 packet_size=(size_t) (quantum_info->depth/8);
1933 if (image->storage_class == DirectClass)
1934 packet_size=(size_t) (3*quantum_info->depth/8);
1935 if (image->matte != MagickFalse)
1936 packet_size+=quantum_info->depth/8;
1937 if (image->colorspace == CMYKColorspace)
1938 packet_size+=quantum_info->depth/8;
1939 if (compression == RLECompression)
1941 length=image->columns;
1942 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1943 packet_size*image->columns));
1944 if ((compression == BZipCompression) || (compression == ZipCompression))
1945 if (length != (size_t) ((unsigned int) length))
1946 compression=NoCompression;
1947 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1948 sizeof(*compress_pixels));
1949 if (compress_pixels == (unsigned char *) NULL)
1950 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1954 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1955 (void) FormatMagickString(buffer,MaxTextExtent,
1956 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1957 MagickClassOptions,image->storage_class),(double) image->colors,
1958 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1959 (void) WriteBlobString(image,buffer);
1960 (void) FormatMagickString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1961 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1963 (void) WriteBlobString(image,buffer);
1964 if (image->type != UndefinedType)
1966 (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
1967 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1968 (void) WriteBlobString(image,buffer);
1970 if (image->colorspace != UndefinedColorspace)
1972 (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
1973 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1974 (void) WriteBlobString(image,buffer);
1976 if (compression != UndefinedCompression)
1978 (void) FormatMagickString(buffer,MaxTextExtent,"compression=%s "
1979 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
1980 compression),(double) image->quality);
1981 (void) WriteBlobString(image,buffer);
1983 if (image->units != UndefinedResolution)
1985 (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n",
1986 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1987 (void) WriteBlobString(image,buffer);
1989 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1991 (void) FormatMagickString(buffer,MaxTextExtent,
1992 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
1993 (void) WriteBlobString(image,buffer);
1995 if ((image->page.width != 0) || (image->page.height != 0))
1997 (void) FormatMagickString(buffer,MaxTextExtent,
1998 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1999 image->page.height,(double) image->page.x,(double) image->page.y);
2000 (void) WriteBlobString(image,buffer);
2003 if ((image->page.x != 0) || (image->page.y != 0))
2005 (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2006 (long) image->page.x,(long) image->page.y);
2007 (void) WriteBlobString(image,buffer);
2009 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2011 (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2012 (long) image->tile_offset.x,(long) image->tile_offset.y);
2013 (void) WriteBlobString(image,buffer);
2015 if ((GetNextImageInList(image) != (Image *) NULL) ||
2016 (GetPreviousImageInList(image) != (Image *) NULL))
2018 if (image->scene == 0)
2019 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g "
2020 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2021 (double) image->delay,(double) image->ticks_per_second);
2023 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g "
2024 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2025 image->scene,(double) image->iterations,(double) image->delay,
2026 (double) image->ticks_per_second);
2027 (void) WriteBlobString(image,buffer);
2031 if (image->scene != 0)
2033 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g\n",
2034 (double) image->scene);
2035 (void) WriteBlobString(image,buffer);
2037 if (image->iterations != 0)
2039 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g\n",
2040 (double) image->iterations);
2041 (void) WriteBlobString(image,buffer);
2043 if (image->delay != 0)
2045 (void) FormatMagickString(buffer,MaxTextExtent,"delay=%.20g\n",
2046 (double) image->delay);
2047 (void) WriteBlobString(image,buffer);
2049 if (image->ticks_per_second != UndefinedTicksPerSecond)
2051 (void) FormatMagickString(buffer,MaxTextExtent,
2052 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2053 (void) WriteBlobString(image,buffer);
2056 if (image->gravity != UndefinedGravity)
2058 (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
2059 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2060 (void) WriteBlobString(image,buffer);
2062 if (image->dispose != UndefinedDispose)
2064 (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
2065 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2066 (void) WriteBlobString(image,buffer);
2068 if (image->rendering_intent != UndefinedIntent)
2070 (void) FormatMagickString(buffer,MaxTextExtent,
2071 "rendering-intent=%s\n",
2072 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2073 (void) WriteBlobString(image,buffer);
2075 if (image->gamma != 0.0)
2077 (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n",
2079 (void) WriteBlobString(image,buffer);
2081 if (image->chromaticity.white_point.x != 0.0)
2084 Note chomaticity points.
2086 (void) FormatMagickString(buffer,MaxTextExtent,"red-primary=%g,"
2087 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2088 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2089 image->chromaticity.green_primary.x,
2090 image->chromaticity.green_primary.y,
2091 image->chromaticity.blue_primary.x,
2092 image->chromaticity.blue_primary.y);
2093 (void) WriteBlobString(image,buffer);
2094 (void) FormatMagickString(buffer,MaxTextExtent,
2095 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2096 image->chromaticity.white_point.y);
2097 (void) WriteBlobString(image,buffer);
2099 if (image->orientation != UndefinedOrientation)
2101 (void) FormatMagickString(buffer,MaxTextExtent,"orientation=%s\n",
2102 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2103 (void) WriteBlobString(image,buffer);
2105 if (image->profiles != (void *) NULL)
2114 Write image profiles.
2116 ResetImageProfileIterator(image);
2117 name=GetNextImageProfile(image);
2118 while (name != (const char *) NULL)
2120 profile=GetImageProfile(image,name);
2121 if (profile != (StringInfo *) NULL)
2123 (void) FormatMagickString(buffer,MaxTextExtent,
2124 "profile:%s=%.20g\n",name,(double)
2125 GetStringInfoLength(profile));
2126 (void) WriteBlobString(image,buffer);
2128 name=GetNextImageProfile(image);
2131 if (image->montage != (char *) NULL)
2133 (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n",
2135 (void) WriteBlobString(image,buffer);
2137 if (quantum_info->format == FloatingPointQuantumFormat)
2138 (void) SetImageProperty(image,"quantum:format","floating-point");
2139 ResetImagePropertyIterator(image);
2140 property=GetNextImageProperty(image);
2141 while (property != (const char *) NULL)
2143 (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property);
2144 (void) WriteBlobString(image,buffer);
2145 value=GetImageProperty(image,property);
2146 if (value != (const char *) NULL)
2148 for (i=0; i < (ssize_t) strlen(value); i++)
2149 if (isspace((int) ((unsigned char) value[i])) != 0)
2151 if (i <= (ssize_t) strlen(value))
2152 (void) WriteBlobByte(image,'{');
2153 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2154 if (i <= (ssize_t) strlen(value))
2155 (void) WriteBlobByte(image,'}');
2157 (void) WriteBlobByte(image,'\n');
2158 property=GetNextImageProperty(image);
2160 (void) WriteBlobString(image,"\f\n:\032");
2161 if (image->montage != (char *) NULL)
2164 Write montage tile directory.
2166 if (image->directory != (char *) NULL)
2167 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2169 (void) WriteBlobByte(image,'\0');
2171 if (image->profiles != (void *) NULL)
2182 ResetImageProfileIterator(image);
2183 name=GetNextImageProfile(image);
2184 while (name != (const char *) NULL)
2186 profile=GetImageProfile(image,name);
2187 (void) WriteBlob(image,GetStringInfoLength(profile),
2188 GetStringInfoDatum(profile));
2189 name=GetNextImageProfile(image);
2192 if (image->storage_class == PseudoClass)
2204 packet_size=(size_t) (3*quantum_info->depth/8);
2205 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2206 packet_size*sizeof(*colormap));
2207 if (colormap == (unsigned char *) NULL)
2208 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2210 Write colormap to file.
2213 for (i=0; i < (ssize_t) image->colors; i++)
2215 switch (quantum_info->depth)
2218 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2221 register unsigned int
2224 pixel=ScaleQuantumToLong(image->colormap[i].red);
2225 q=PopLongPixel(MSBEndian,pixel,q);
2226 pixel=ScaleQuantumToLong(image->colormap[i].green);
2227 q=PopLongPixel(MSBEndian,pixel,q);
2228 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2229 q=PopLongPixel(MSBEndian,pixel,q);
2234 register unsigned short
2237 pixel=ScaleQuantumToShort(image->colormap[i].red);
2238 q=PopShortPixel(MSBEndian,pixel,q);
2239 pixel=ScaleQuantumToShort(image->colormap[i].green);
2240 q=PopShortPixel(MSBEndian,pixel,q);
2241 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2242 q=PopShortPixel(MSBEndian,pixel,q);
2247 register unsigned char
2250 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2251 q=PopCharPixel(pixel,q);
2252 pixel=(unsigned char) ScaleQuantumToChar(
2253 image->colormap[i].green);
2254 q=PopCharPixel(pixel,q);
2255 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2256 q=PopCharPixel(pixel,q);
2261 (void) WriteBlob(image,packet_size*image->colors,colormap);
2262 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2265 Write image pixels to file.
2268 switch (compression)
2270 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2271 case BZipCompression:
2273 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2274 bzip_info.bzalloc=AcquireBZIPMemory;
2275 bzip_info.bzfree=RelinquishBZIPMemory;
2276 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2277 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2278 9)),(int) image_info->verbose,0);
2284 #if defined(MAGICKCORE_LZMA_DELEGATE)
2285 case LZMACompression:
2287 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2288 allocator.alloc=AcquireLZMAMemory;
2289 allocator.free=RelinquishLZMAMemory;
2290 lzma_info=initialize_lzma;
2291 lzma_info.allocator=&allocator;
2292 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2293 if (code != LZMA_OK)
2298 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2299 case LZWCompression:
2300 case ZipCompression:
2302 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2303 zip_info.zalloc=AcquireZIPMemory;
2304 zip_info.zfree=RelinquishZIPMemory;
2305 code=deflateInit(&zip_info,(int) (image->quality ==
2306 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2315 quantum_type=GetQuantumType(image,&image->exception);
2316 pixels=GetQuantumPixels(quantum_info);
2317 for (y=0; y < (ssize_t) image->rows; y++)
2319 register const IndexPacket
2322 register const PixelPacket
2328 if (status == MagickFalse)
2330 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2331 if (p == (const PixelPacket *) NULL)
2333 indexes=GetVirtualIndexQueue(image);
2335 switch (compression)
2337 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2338 case BZipCompression:
2340 bzip_info.next_in=(char *) pixels;
2341 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2342 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2343 quantum_info,quantum_type,pixels,&image->exception);
2346 bzip_info.next_out=(char *) compress_pixels;
2347 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2349 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2352 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2355 (void) WriteBlobMSBLong(image,(unsigned int) length);
2356 (void) WriteBlob(image,length,compress_pixels);
2358 } while (bzip_info.avail_in != 0);
2362 #if defined(MAGICKCORE_LZMA_DELEGATE)
2363 case LZMACompression:
2365 lzma_info.next_in=pixels;
2366 lzma_info.avail_in=packet_size*image->columns;
2367 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2368 quantum_info,quantum_type,pixels,&image->exception);
2371 lzma_info.next_out=compress_pixels;
2372 lzma_info.avail_out=packet_size*image->columns;
2373 code=lzma_code(&lzma_info,LZMA_RUN);
2374 if (code != LZMA_OK)
2376 length=(size_t) (lzma_info.next_out-compress_pixels);
2379 (void) WriteBlobMSBLong(image,(unsigned int) length);
2380 (void) WriteBlob(image,length,compress_pixels);
2382 } while (lzma_info.avail_in != 0);
2386 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2387 case LZWCompression:
2388 case ZipCompression:
2390 zip_info.next_in=pixels;
2391 zip_info.avail_in=(uInt) (packet_size*image->columns);
2392 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2393 quantum_info,quantum_type,pixels,&image->exception);
2396 zip_info.next_out=compress_pixels;
2397 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2398 code=deflate(&zip_info,Z_SYNC_FLUSH);
2401 length=(size_t) (zip_info.next_out-compress_pixels);
2404 (void) WriteBlobMSBLong(image,(unsigned int) length);
2405 (void) WriteBlob(image,length,compress_pixels);
2407 } while (zip_info.avail_in != 0);
2411 case RLECompression:
2414 index=(IndexPacket) 0;
2415 if (indexes != (IndexPacket *) NULL)
2418 for (x=0; x < (ssize_t) image->columns; x++)
2420 if ((length < 255) && (x < (ssize_t) (image->columns-1)) &&
2421 (IsColorEqual(p,&pixel) != MagickFalse) &&
2422 ((image->matte == MagickFalse) ||
2423 (GetOpacityPixelComponent(p) == pixel.opacity)) &&
2424 ((indexes == (IndexPacket *) NULL) ||
2425 (index == GetIndexPixelComponent(indexes+x))))
2430 q=PopRunlengthPacket(image,q,length,pixel,index);
2434 if (indexes != (IndexPacket *) NULL)
2435 index=GetIndexPixelComponent(indexes+x);
2438 q=PopRunlengthPacket(image,q,length,pixel,index);
2439 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2444 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2445 quantum_info,quantum_type,pixels,&image->exception);
2446 (void) WriteBlob(image,packet_size*image->columns,pixels);
2450 if (image->previous == (Image *) NULL)
2452 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2454 if (status == MagickFalse)
2458 switch (compression)
2460 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2461 case BZipCompression:
2465 if (status == MagickFalse)
2467 bzip_info.next_out=(char *) compress_pixels;
2468 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2470 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2473 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2476 (void) WriteBlobMSBLong(image,(unsigned int) length);
2477 (void) WriteBlob(image,length,compress_pixels);
2479 if (code == BZ_STREAM_END)
2482 code=BZ2_bzCompressEnd(&bzip_info);
2488 #if defined(MAGICKCORE_LZMA_DELEGATE)
2489 case LZMACompression:
2493 if (status == MagickFalse)
2495 lzma_info.next_out=compress_pixels;
2496 lzma_info.avail_out=packet_size*image->columns;
2497 code=lzma_code(&lzma_info,LZMA_FINISH);
2498 if (code != LZMA_OK)
2500 length=(size_t) (lzma_info.next_out-compress_pixels);
2503 (void) WriteBlobMSBLong(image,(unsigned int) length);
2504 (void) WriteBlob(image,length,compress_pixels);
2506 if (code == LZMA_STREAM_END)
2509 lzma_end(&lzma_info);
2513 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2514 case LZWCompression:
2515 case ZipCompression:
2519 if (status == MagickFalse)
2521 zip_info.next_out=compress_pixels;
2522 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
2524 code=deflate(&zip_info,Z_FINISH);
2527 length=(size_t) (zip_info.next_out-compress_pixels);
2530 (void) WriteBlobMSBLong(image,(unsigned int) length);
2531 (void) WriteBlob(image,length,compress_pixels);
2533 if (code == Z_STREAM_END)
2536 code=deflateEnd(&zip_info);
2545 quantum_info=DestroyQuantumInfo(quantum_info);
2546 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2547 if (GetNextImageInList(image) == (Image *) NULL)
2549 image=SyncNextImageInList(image);
2550 status=SetImageProgress(image,SaveImagesTag,scene++,
2551 GetImageListLength(image));
2552 if (status == MagickFalse)
2554 } while (image_info->adjoin != MagickFalse);
2555 (void) CloseBlob(image);