2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2015 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/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/hashmap.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/profile.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/static.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #if defined(MAGICKCORE_BZLIB_DELEGATE)
79 #if defined(MAGICKCORE_LZMA_DELEGATE)
82 #if defined(MAGICKCORE_ZLIB_DELEGATE)
96 static MagickBooleanType
97 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % IsMIFF() returns MagickTrue if the image format type, identified by the
111 % magick string, is MIFF.
113 % The format of the IsMIFF method is:
115 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
117 % A description of each parameter follows:
119 % o magick: compare image format pattern against these bytes.
121 % o length: Specifies the length of the magick string.
124 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
128 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % R e a d M I F F I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
145 % memory necessary for the new Image structure and returns a pointer to the
148 % The format of the ReadMIFFImage method is:
150 % Image *ReadMIFFImage(const ImageInfo *image_info,
151 % ExceptionInfo *exception)
153 % Decompression code contributed by Kyle Shorter.
155 % A description of each parameter follows:
157 % o image_info: the image info.
159 % o exception: return any errors or warnings in this structure.
163 #if defined(MAGICKCORE_BZLIB_DELEGATE)
164 static void *AcquireBZIPMemory(void *context,int items,int size)
167 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
171 #if defined(MAGICKCORE_LZMA_DELEGATE)
172 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
175 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
179 #if defined(MAGICKCORE_ZLIB_DELEGATE)
180 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
184 return((voidpf) AcquireQuantumMemory(items,size));
188 static inline size_t MagickMax(const size_t x,const size_t y)
195 static inline size_t MagickMin(const size_t x,const size_t y)
202 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
203 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
209 if (image->storage_class == PseudoClass)
212 switch (image->depth)
216 pixel->index=ConstrainColormapIndex(image,((size_t) *p << 24) |
217 ((size_t) *(p+1) << 16) | ((size_t) *(p+2) << 8) | (size_t) *(p+3),
224 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
231 pixel->index=ConstrainColormapIndex(image,*p,exception);
236 (void) ThrowMagickException(exception,GetMagickModule(),
237 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
239 switch (image->depth)
246 if (image->alpha_trait != UndefinedPixelTrait)
248 p=PushCharPixel(p,&quantum);
249 pixel->alpha=ScaleCharToQuantum(quantum);
258 if (image->alpha_trait != UndefinedPixelTrait)
260 p=PushShortPixel(MSBEndian,p,&quantum);
261 pixel->alpha=(Quantum) (quantum >> (image->depth-
262 MAGICKCORE_QUANTUM_DEPTH));
271 if (image->alpha_trait != UndefinedPixelTrait)
273 p=PushLongPixel(MSBEndian,p,&quantum);
274 pixel->alpha=(Quantum) (quantum >> (image->depth-
275 MAGICKCORE_QUANTUM_DEPTH));
280 (void) ThrowMagickException(exception,GetMagickModule(),
281 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
283 *length=(size_t) (*p++)+1;
286 switch (image->depth)
293 p=PushCharPixel(p,&quantum);
294 pixel->red=ScaleCharToQuantum(quantum);
295 pixel->green=pixel->red;
296 pixel->blue=pixel->red;
297 if (IsGrayColorspace(image->colorspace) == MagickFalse)
299 p=PushCharPixel(p,&quantum);
300 pixel->green=ScaleCharToQuantum(quantum);
301 p=PushCharPixel(p,&quantum);
302 pixel->blue=ScaleCharToQuantum(quantum);
304 if (image->colorspace == CMYKColorspace)
306 p=PushCharPixel(p,&quantum);
307 pixel->black=ScaleCharToQuantum(quantum);
309 if (image->alpha_trait != UndefinedPixelTrait)
311 p=PushCharPixel(p,&quantum);
312 pixel->alpha=ScaleCharToQuantum(quantum);
321 p=PushShortPixel(MSBEndian,p,&quantum);
322 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
323 pixel->green=pixel->red;
324 pixel->blue=pixel->red;
325 if (IsGrayColorspace(image->colorspace) == MagickFalse)
327 p=PushShortPixel(MSBEndian,p,&quantum);
328 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
329 p=PushShortPixel(MSBEndian,p,&quantum);
330 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
332 if (image->colorspace == CMYKColorspace)
334 p=PushShortPixel(MSBEndian,p,&quantum);
335 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
337 if (image->alpha_trait != UndefinedPixelTrait)
339 p=PushShortPixel(MSBEndian,p,&quantum);
340 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
349 p=PushLongPixel(MSBEndian,p,&quantum);
350 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
351 pixel->green=pixel->red;
352 pixel->blue=pixel->red;
353 if (IsGrayColorspace(image->colorspace) == MagickFalse)
355 p=PushLongPixel(MSBEndian,p,&quantum);
356 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
357 p=PushLongPixel(MSBEndian,p,&quantum);
358 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
360 if (image->colorspace == CMYKColorspace)
362 p=PushLongPixel(MSBEndian,p,&quantum);
363 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
365 if (image->alpha_trait != UndefinedPixelTrait)
367 p=PushLongPixel(MSBEndian,p,&quantum);
368 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
373 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
374 "ImageDepthNotSupported","`%s'",image->filename);
376 *length=(size_t) (*p++)+1;
379 #if defined(MAGICKCORE_BZLIB_DELEGATE)
380 static void RelinquishBZIPMemory(void *context,void *memory)
383 memory=RelinquishMagickMemory(memory);
387 #if defined(MAGICKCORE_LZMA_DELEGATE)
388 static void RelinquishLZMAMemory(void *context,void *memory)
391 memory=RelinquishMagickMemory(memory);
395 #if defined(MAGICKCORE_ZLIB_DELEGATE)
396 static void RelinquishZIPMemory(voidpf context,voidpf memory)
399 memory=RelinquishMagickMemory(memory);
403 static Image *ReadMIFFImage(const ImageInfo *image_info,
404 ExceptionInfo *exception)
406 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
407 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
408 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
410 #if defined(MAGICKCORE_BZLIB_DELEGATE)
417 keyword[MaxTextExtent],
438 #if defined(MAGICKCORE_LZMA_DELEGATE)
440 initialize_lzma = LZMA_STREAM_INIT,
485 #if defined(MAGICKCORE_ZLIB_DELEGATE)
493 assert(image_info != (const ImageInfo *) NULL);
494 assert(image_info->signature == MagickSignature);
495 if (image_info->debug != MagickFalse)
496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
497 image_info->filename);
498 assert(exception != (ExceptionInfo *) NULL);
499 assert(exception->signature == MagickSignature);
500 image=AcquireImage(image_info,exception);
501 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
502 if (status == MagickFalse)
504 image=DestroyImageList(image);
505 return((Image *) NULL);
508 Decode image header; header terminates one character beyond a ':'.
510 c=ReadBlobByte(image);
512 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
514 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
520 Decode image header; header terminates one character beyond a ':'.
522 length=MaxTextExtent;
523 options=AcquireString((char *) NULL);
524 quantum_format=UndefinedQuantumFormat;
525 profiles=(LinkedListInfo *) NULL;
528 image->compression=NoCompression;
529 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
540 Read comment-- any text between { }.
542 length=MaxTextExtent;
543 comment=AcquireString((char *) NULL);
544 for (p=comment; comment != (char *) NULL; p++)
546 c=ReadBlobByte(image);
548 c=ReadBlobByte(image);
550 if ((c == EOF) || (c == (int) '}'))
552 if ((size_t) (p-comment+1) >= length)
556 comment=(char *) ResizeQuantumMemory(comment,length+
557 MaxTextExtent,sizeof(*comment));
558 if (comment == (char *) NULL)
560 p=comment+strlen(comment);
564 if (comment == (char *) NULL)
565 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
567 (void) SetImageProperty(image,"comment",comment,exception);
568 comment=DestroyString(comment);
569 c=ReadBlobByte(image);
572 if (isalnum(c) != MagickFalse)
582 if ((size_t) (p-keyword) < (MaxTextExtent-1))
584 c=ReadBlobByte(image);
588 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
589 c=ReadBlobByte(image);
593 Get the keyword value.
595 c=ReadBlobByte(image);
596 while ((c != (int) '}') && (c != EOF))
598 if ((size_t) (p-options+1) >= length)
602 options=(char *) ResizeQuantumMemory(options,length+
603 MaxTextExtent,sizeof(*options));
604 if (options == (char *) NULL)
606 p=options+strlen(options);
608 if (options == (char *) NULL)
609 ThrowReaderException(ResourceLimitError,
610 "MemoryAllocationFailed");
612 c=ReadBlobByte(image);
615 c=ReadBlobByte(image);
619 c=ReadBlobByte(image);
623 if (isspace((int) ((unsigned char) c)) != 0)
628 (void) CopyMagickString(options,options+1,strlen(options));
630 Assign a value to the specified keyword.
637 if (LocaleCompare(keyword,"alpha-trait") == 0)
642 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
643 MagickFalse,options);
646 image->alpha_trait=(PixelTrait) alpha_trait;
649 (void) SetImageProperty(image,keyword,options,exception);
655 if (LocaleCompare(keyword,"background-color") == 0)
657 (void) QueryColorCompliance(options,AllCompliance,
658 &image->background_color,exception);
661 if (LocaleCompare(keyword,"blue-primary") == 0)
663 flags=ParseGeometry(options,&geometry_info);
664 image->chromaticity.blue_primary.x=geometry_info.rho;
665 image->chromaticity.blue_primary.y=geometry_info.sigma;
666 if ((flags & SigmaValue) == 0)
667 image->chromaticity.blue_primary.y=
668 image->chromaticity.blue_primary.x;
671 if (LocaleCompare(keyword,"border-color") == 0)
673 (void) QueryColorCompliance(options,AllCompliance,
674 &image->border_color,exception);
677 (void) SetImageProperty(image,keyword,options,exception);
683 if (LocaleCompare(keyword,"class") == 0)
688 storage_class=ParseCommandOption(MagickClassOptions,
689 MagickFalse,options);
690 if (storage_class < 0)
692 image->storage_class=(ClassType) storage_class;
695 if (LocaleCompare(keyword,"colors") == 0)
697 colors=StringToUnsignedLong(options);
700 if (LocaleCompare(keyword,"colorspace") == 0)
705 colorspace=ParseCommandOption(MagickColorspaceOptions,
706 MagickFalse,options);
709 image->colorspace=(ColorspaceType) colorspace;
712 if (LocaleCompare(keyword,"compression") == 0)
717 compression=ParseCommandOption(MagickCompressOptions,
718 MagickFalse,options);
721 image->compression=(CompressionType) compression;
724 if (LocaleCompare(keyword,"columns") == 0)
726 image->columns=StringToUnsignedLong(options);
729 (void) SetImageProperty(image,keyword,options,exception);
735 if (LocaleCompare(keyword,"delay") == 0)
737 image->delay=StringToUnsignedLong(options);
740 if (LocaleCompare(keyword,"depth") == 0)
742 image->depth=StringToUnsignedLong(options);
745 if (LocaleCompare(keyword,"dispose") == 0)
750 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
754 image->dispose=(DisposeType) dispose;
757 (void) SetImageProperty(image,keyword,options,exception);
763 if (LocaleCompare(keyword,"endian") == 0)
768 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
772 image->endian=(EndianType) endian;
775 (void) SetImageProperty(image,keyword,options,exception);
781 if (LocaleCompare(keyword,"gamma") == 0)
783 image->gamma=StringToDouble(options,(char **) NULL);
786 if (LocaleCompare(keyword,"gravity") == 0)
791 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
795 image->gravity=(GravityType) gravity;
798 if (LocaleCompare(keyword,"green-primary") == 0)
800 flags=ParseGeometry(options,&geometry_info);
801 image->chromaticity.green_primary.x=geometry_info.rho;
802 image->chromaticity.green_primary.y=geometry_info.sigma;
803 if ((flags & SigmaValue) == 0)
804 image->chromaticity.green_primary.y=
805 image->chromaticity.green_primary.x;
808 (void) SetImageProperty(image,keyword,options,exception);
814 if (LocaleCompare(keyword,"id") == 0)
816 (void) CopyMagickString(id,options,MaxTextExtent);
819 if (LocaleCompare(keyword,"iterations") == 0)
821 image->iterations=StringToUnsignedLong(options);
824 (void) SetImageProperty(image,keyword,options,exception);
830 if (LocaleCompare(keyword,"matte") == 0)
835 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
839 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
843 if (LocaleCompare(keyword,"matte-color") == 0)
845 (void) QueryColorCompliance(options,AllCompliance,
846 &image->matte_color,exception);
849 if (LocaleCompare(keyword,"montage") == 0)
851 (void) CloneString(&image->montage,options);
854 (void) SetImageProperty(image,keyword,options,exception);
860 if (LocaleCompare(keyword,"orientation") == 0)
865 orientation=ParseCommandOption(MagickOrientationOptions,
866 MagickFalse,options);
869 image->orientation=(OrientationType) orientation;
872 (void) SetImageProperty(image,keyword,options,exception);
878 if (LocaleCompare(keyword,"page") == 0)
883 geometry=GetPageGeometry(options);
884 (void) ParseAbsoluteGeometry(geometry,&image->page);
885 geometry=DestroyString(geometry);
888 if (LocaleCompare(keyword,"pixel-intensity") == 0)
893 intensity=ParseCommandOption(MagickPixelIntensityOptions,
894 MagickFalse,options);
897 image->intensity=(PixelIntensityMethod) intensity;
900 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
901 (LocaleNCompare(keyword,"profile-",8) == 0))
906 if (profiles == (LinkedListInfo *) NULL)
907 profiles=NewLinkedList(0);
908 (void) AppendValueToLinkedList(profiles,
909 AcquireString(keyword+8));
910 profile=BlobToStringInfo((const void *) NULL,(size_t)
911 StringToLong(options));
912 if (profile == (StringInfo *) NULL)
913 ThrowReaderException(ResourceLimitError,
914 "MemoryAllocationFailed");
915 (void) SetImageProfile(image,keyword+8,profile,exception);
916 profile=DestroyStringInfo(profile);
919 (void) SetImageProperty(image,keyword,options,exception);
925 if (LocaleCompare(keyword,"quality") == 0)
927 image->quality=StringToUnsignedLong(options);
930 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
931 (LocaleCompare(keyword,"quantum:format") == 0))
936 format=ParseCommandOption(MagickQuantumFormatOptions,
937 MagickFalse,options);
940 quantum_format=(QuantumFormatType) format;
943 (void) SetImageProperty(image,keyword,options,exception);
949 if (LocaleCompare(keyword,"red-primary") == 0)
951 flags=ParseGeometry(options,&geometry_info);
952 image->chromaticity.red_primary.x=geometry_info.rho;
953 image->chromaticity.red_primary.y=geometry_info.sigma;
954 if ((flags & SigmaValue) == 0)
955 image->chromaticity.red_primary.y=
956 image->chromaticity.red_primary.x;
959 if (LocaleCompare(keyword,"rendering-intent") == 0)
964 rendering_intent=ParseCommandOption(MagickIntentOptions,
965 MagickFalse,options);
966 if (rendering_intent < 0)
968 image->rendering_intent=(RenderingIntent) rendering_intent;
971 if (LocaleCompare(keyword,"resolution") == 0)
973 flags=ParseGeometry(options,&geometry_info);
974 image->resolution.x=geometry_info.rho;
975 image->resolution.y=geometry_info.sigma;
976 if ((flags & SigmaValue) == 0)
977 image->resolution.y=image->resolution.x;
980 if (LocaleCompare(keyword,"rows") == 0)
982 image->rows=StringToUnsignedLong(options);
985 (void) SetImageProperty(image,keyword,options,exception);
991 if (LocaleCompare(keyword,"scene") == 0)
993 image->scene=StringToUnsignedLong(options);
996 (void) SetImageProperty(image,keyword,options,exception);
1002 if (LocaleCompare(keyword,"ticks-per-second") == 0)
1004 image->ticks_per_second=(ssize_t) StringToLong(options);
1007 if (LocaleCompare(keyword,"tile-offset") == 0)
1012 geometry=GetPageGeometry(options);
1013 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1014 geometry=DestroyString(geometry);
1017 if (LocaleCompare(keyword,"type") == 0)
1022 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1026 image->type=(ImageType) type;
1029 (void) SetImageProperty(image,keyword,options,exception);
1035 if (LocaleCompare(keyword,"units") == 0)
1040 units=ParseCommandOption(MagickResolutionOptions,
1041 MagickFalse,options);
1044 image->units=(ResolutionType) units;
1047 (void) SetImageProperty(image,keyword,options,exception);
1053 if (LocaleCompare(keyword,"version") == 0)
1055 version=StringToDouble(options,(char **) NULL);
1058 (void) SetImageProperty(image,keyword,options,exception);
1064 if (LocaleCompare(keyword,"white-point") == 0)
1066 flags=ParseGeometry(options,&geometry_info);
1067 image->chromaticity.white_point.x=geometry_info.rho;
1068 image->chromaticity.white_point.y=geometry_info.sigma;
1069 if ((flags & SigmaValue) == 0)
1070 image->chromaticity.white_point.y=
1071 image->chromaticity.white_point.x;
1074 (void) SetImageProperty(image,keyword,options,exception);
1079 (void) SetImageProperty(image,keyword,options,exception);
1085 c=ReadBlobByte(image);
1086 while (isspace((int) ((unsigned char) c)) != 0)
1087 c=ReadBlobByte(image);
1089 options=DestroyString(options);
1090 (void) ReadBlobByte(image);
1092 Verify that required image information is defined.
1094 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1095 (image->storage_class == UndefinedClass) ||
1096 (image->columns == 0) || (image->rows == 0))
1097 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1098 if (image->montage != (char *) NULL)
1106 length=MaxTextExtent;
1107 image->directory=AcquireString((char *) NULL);
1112 if ((strlen(image->directory)+MaxTextExtent) >= length)
1115 Allocate more memory for the image directory.
1118 image->directory=(char *) ResizeQuantumMemory(image->directory,
1119 length+MaxTextExtent,sizeof(*image->directory));
1120 if (image->directory == (char *) NULL)
1121 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1122 p=image->directory+strlen(image->directory);
1124 c=ReadBlobByte(image);
1126 } while (c != (int) '\0');
1128 if (profiles != (LinkedListInfo *) NULL)
1137 Read image profiles.
1139 ResetLinkedListIterator(profiles);
1140 name=(const char *) GetNextValueInLinkedList(profiles);
1141 while (name != (const char *) NULL)
1143 profile=GetImageProfile(image,name);
1144 if (profile != (StringInfo *) NULL)
1146 register unsigned char
1149 p=GetStringInfoDatum(profile);
1150 count=ReadBlob(image,GetStringInfoLength(profile),p);
1153 name=(const char *) GetNextValueInLinkedList(profiles);
1155 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1157 image->depth=GetImageQuantumDepth(image,MagickFalse);
1158 if (image->storage_class == PseudoClass)
1161 Create image colormap.
1163 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1164 if (status == MagickFalse)
1165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1175 Read image colormap from file.
1177 packet_size=(size_t) (3UL*image->depth/8UL);
1178 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1179 packet_size*sizeof(*colormap));
1180 if (colormap == (unsigned char *) NULL)
1181 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1182 count=ReadBlob(image,packet_size*image->colors,colormap);
1184 switch (image->depth)
1187 ThrowReaderException(CorruptImageError,
1188 "ImageDepthNotSupported");
1194 for (i=0; i < (ssize_t) image->colors; i++)
1196 p=PushCharPixel(p,&pixel);
1197 image->colormap[i].red=ScaleCharToQuantum(pixel);
1198 p=PushCharPixel(p,&pixel);
1199 image->colormap[i].green=ScaleCharToQuantum(pixel);
1200 p=PushCharPixel(p,&pixel);
1201 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1210 for (i=0; i < (ssize_t) image->colors; i++)
1212 p=PushShortPixel(MSBEndian,p,&pixel);
1213 image->colormap[i].red=ScaleShortToQuantum(pixel);
1214 p=PushShortPixel(MSBEndian,p,&pixel);
1215 image->colormap[i].green=ScaleShortToQuantum(pixel);
1216 p=PushShortPixel(MSBEndian,p,&pixel);
1217 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1226 for (i=0; i < (ssize_t) image->colors; i++)
1228 p=PushLongPixel(MSBEndian,p,&pixel);
1229 image->colormap[i].red=ScaleLongToQuantum(pixel);
1230 p=PushLongPixel(MSBEndian,p,&pixel);
1231 image->colormap[i].green=ScaleLongToQuantum(pixel);
1232 p=PushLongPixel(MSBEndian,p,&pixel);
1233 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1238 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1241 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1242 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1244 status=SetImageExtent(image,image->columns,image->rows,exception);
1245 if (status == MagickFalse)
1246 return(DestroyImageList(image));
1248 Allocate image pixels.
1250 quantum_info=AcquireQuantumInfo(image_info,image);
1251 if (quantum_info == (QuantumInfo *) NULL)
1252 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1253 if (quantum_format != UndefinedQuantumFormat)
1255 status=SetQuantumFormat(image,quantum_info,quantum_format);
1256 if (status == MagickFalse)
1257 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1259 packet_size=(size_t) (quantum_info->depth/8);
1260 if (image->storage_class == DirectClass)
1261 packet_size=(size_t) (3*quantum_info->depth/8);
1262 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1263 packet_size=quantum_info->depth/8;
1264 if (image->alpha_trait != UndefinedPixelTrait)
1265 packet_size+=quantum_info->depth/8;
1266 if (image->colorspace == CMYKColorspace)
1267 packet_size+=quantum_info->depth/8;
1268 if (image->compression == RLECompression)
1270 length=image->columns;
1271 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1272 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1274 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1275 sizeof(*compress_pixels));
1276 if (compress_pixels == (unsigned char *) NULL)
1277 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1281 quantum_type=RGBQuantum;
1282 if (image->alpha_trait != UndefinedPixelTrait)
1283 quantum_type=RGBAQuantum;
1284 if (image->colorspace == CMYKColorspace)
1286 quantum_type=CMYKQuantum;
1287 if (image->alpha_trait != UndefinedPixelTrait)
1288 quantum_type=CMYKAQuantum;
1290 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1292 quantum_type=GrayQuantum;
1293 if (image->alpha_trait != UndefinedPixelTrait)
1294 quantum_type=GrayAlphaQuantum;
1296 if (image->storage_class == PseudoClass)
1298 quantum_type=IndexQuantum;
1299 if (image->alpha_trait != UndefinedPixelTrait)
1300 quantum_type=IndexAlphaQuantum;
1303 GetPixelInfo(image,&pixel);
1304 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1305 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1307 #if defined(MAGICKCORE_LZMA_DELEGATE)
1308 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1310 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1311 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1313 switch (image->compression)
1315 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1316 case BZipCompression:
1321 bzip_info.bzalloc=AcquireBZIPMemory;
1322 bzip_info.bzfree=RelinquishBZIPMemory;
1323 bzip_info.opaque=(void *) NULL;
1324 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1331 #if defined(MAGICKCORE_LZMA_DELEGATE)
1332 case LZMACompression:
1337 allocator.alloc=AcquireLZMAMemory;
1338 allocator.free=RelinquishLZMAMemory;
1339 lzma_info=initialize_lzma;
1340 lzma_info.allocator=(&allocator);
1341 code=lzma_auto_decoder(&lzma_info,-1,0);
1342 if (code != LZMA_OK)
1347 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1348 case LZWCompression:
1349 case ZipCompression:
1354 zip_info.zalloc=AcquireZIPMemory;
1355 zip_info.zfree=RelinquishZIPMemory;
1356 zip_info.opaque=(voidpf) NULL;
1357 code=inflateInit(&zip_info);
1363 case RLECompression:
1368 pixels=GetQuantumPixels(quantum_info);
1370 for (y=0; y < (ssize_t) image->rows; y++)
1378 if (status == MagickFalse)
1380 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1381 if (q == (Quantum *) NULL)
1383 switch (image->compression)
1385 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1386 case BZipCompression:
1388 bzip_info.next_out=(char *) pixels;
1389 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1392 if (bzip_info.avail_in == 0)
1394 bzip_info.next_in=(char *) compress_pixels;
1395 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1397 length=(size_t) ReadBlobMSBLong(image);
1398 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1399 (unsigned char *) bzip_info.next_in);
1401 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1403 } while (bzip_info.avail_out != 0);
1404 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1405 quantum_type,pixels,exception);
1409 #if defined(MAGICKCORE_LZMA_DELEGATE)
1410 case LZMACompression:
1412 lzma_info.next_out=pixels;
1413 lzma_info.avail_out=packet_size*image->columns;
1419 if (lzma_info.avail_in == 0)
1421 lzma_info.next_in=compress_pixels;
1422 length=(size_t) ReadBlobMSBLong(image);
1423 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1424 (unsigned char *) lzma_info.next_in);
1426 code=lzma_code(&lzma_info,LZMA_RUN);
1432 if (code == LZMA_STREAM_END)
1434 } while (lzma_info.avail_out != 0);
1435 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1436 quantum_type,pixels,exception);
1440 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1441 case LZWCompression:
1442 case ZipCompression:
1444 zip_info.next_out=pixels;
1445 zip_info.avail_out=(uInt) (packet_size*image->columns);
1448 if (zip_info.avail_in == 0)
1450 zip_info.next_in=compress_pixels;
1451 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1453 length=(size_t) ReadBlobMSBLong(image);
1454 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1457 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1459 } while (zip_info.avail_out != 0);
1460 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1461 quantum_type,pixels,exception);
1465 case RLECompression:
1467 for (x=0; x < (ssize_t) image->columns; x++)
1471 count=ReadBlob(image,packet_size,pixels);
1472 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1475 if (image->storage_class == PseudoClass)
1476 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1479 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1480 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1481 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1482 if (image->colorspace == CMYKColorspace)
1483 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1485 if (image->alpha_trait != UndefinedPixelTrait)
1486 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1487 q+=GetPixelChannels(image);
1493 count=ReadBlob(image,packet_size*image->columns,pixels);
1494 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1495 quantum_type,pixels,exception);
1499 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1502 SetQuantumImageType(image,quantum_type);
1503 switch (image->compression)
1505 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1506 case BZipCompression:
1516 offset=SeekBlob(image,-((MagickOffsetType)
1517 bzip_info.avail_in),SEEK_CUR);
1519 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1521 code=BZ2_bzDecompressEnd(&bzip_info);
1527 #if defined(MAGICKCORE_LZMA_DELEGATE)
1528 case LZMACompression:
1533 code=lzma_code(&lzma_info,LZMA_FINISH);
1534 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1536 lzma_end(&lzma_info);
1540 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1541 case LZWCompression:
1542 case ZipCompression:
1552 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1555 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1557 code=inflateEnd(&zip_info);
1558 if (code != LZMA_OK)
1566 quantum_info=DestroyQuantumInfo(quantum_info);
1567 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1568 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1570 image=DestroyImageList(image);
1571 return((Image *) NULL);
1573 if (EOFBlob(image) != MagickFalse)
1575 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1580 Proceed to next image.
1582 if (image_info->number_scenes != 0)
1583 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1587 c=ReadBlobByte(image);
1588 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1592 Allocate next image structure.
1594 AcquireNextImage(image_info,image,exception);
1595 if (GetNextImageInList(image) == (Image *) NULL)
1597 image=DestroyImageList(image);
1598 return((Image *) NULL);
1600 image=SyncNextImageInList(image);
1601 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1602 GetBlobSize(image));
1603 if (status == MagickFalse)
1607 (void) CloseBlob(image);
1608 return(GetFirstImageInList(image));
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1616 % R e g i s t e r M I F F I m a g e %
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1622 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1623 % supported formats. The properties include the image format tag, a method to
1624 % read and/or write the format, whether the format supports the saving of more
1625 % than one frame to the same file or blob, whether the format supports native
1626 % in-memory I/O, and a brief description of the format.
1628 % The format of the RegisterMIFFImage method is:
1630 % size_t RegisterMIFFImage(void)
1633 ModuleExport size_t RegisterMIFFImage(void)
1636 version[MaxTextExtent];
1642 #if defined(MagickImageCoderSignatureText)
1643 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1644 #if defined(ZLIB_VERSION)
1645 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1646 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1648 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1649 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1652 entry=SetMagickInfo("MIFF");
1653 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1654 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1655 entry->magick=(IsImageFormatHandler *) IsMIFF;
1656 entry->seekable_stream=MagickTrue;
1657 entry->description=ConstantString("Magick Image File Format");
1658 if (*version != '\0')
1659 entry->version=ConstantString(version);
1660 entry->module=ConstantString("MIFF");
1661 (void) RegisterMagickInfo(entry);
1662 return(MagickImageCoderSignature);
1666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1670 % U n r e g i s t e r M I F F I m a g e %
1674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1677 % from the list of supported formats.
1679 % The format of the UnregisterMIFFImage method is:
1681 % UnregisterMIFFImage(void)
1684 ModuleExport void UnregisterMIFFImage(void)
1686 (void) UnregisterMagickInfo("MIFF");
1690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1694 % W r i t e M I F F I m a g e %
1698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1700 % WriteMIFFImage() writes a MIFF image to a file.
1702 % The format of the WriteMIFFImage method is:
1704 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1705 % Image *image,ExceptionInfo *exception)
1707 % Compression code contributed by Kyle Shorter.
1709 % A description of each parameter follows:
1711 % o image_info: the image info.
1713 % o image: the image.
1715 % o exception: return any errors or warnings in this structure.
1719 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1720 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1722 if (image->storage_class != DirectClass)
1727 value=(unsigned int) ClampToQuantum(pixel->index);
1728 switch (image->depth)
1732 *pixels++=(unsigned char) (value >> 24);
1733 *pixels++=(unsigned char) (value >> 16);
1736 *pixels++=(unsigned char) (value >> 8);
1739 *pixels++=(unsigned char) value;
1743 (void) ThrowMagickException(exception,GetMagickModule(),
1744 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1746 switch (image->depth)
1753 if (image->alpha_trait != UndefinedPixelTrait)
1755 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1756 pixels=PopLongPixel(MSBEndian,value,pixels);
1765 if (image->alpha_trait != UndefinedPixelTrait)
1767 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1768 pixels=PopShortPixel(MSBEndian,value,pixels);
1777 if (image->alpha_trait != UndefinedPixelTrait)
1779 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1781 pixels=PopCharPixel(value,pixels);
1786 (void) ThrowMagickException(exception,GetMagickModule(),
1787 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1789 *pixels++=(unsigned char) length;
1792 switch (image->depth)
1799 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1800 pixels=PopLongPixel(MSBEndian,value,pixels);
1801 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1803 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1804 pixels=PopLongPixel(MSBEndian,value,pixels);
1805 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1806 pixels=PopLongPixel(MSBEndian,value,pixels);
1808 if (image->colorspace == CMYKColorspace)
1810 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1811 pixels=PopLongPixel(MSBEndian,value,pixels);
1813 if (image->alpha_trait != UndefinedPixelTrait)
1815 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1816 pixels=PopLongPixel(MSBEndian,value,pixels);
1825 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1826 pixels=PopShortPixel(MSBEndian,value,pixels);
1827 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1829 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1830 pixels=PopShortPixel(MSBEndian,value,pixels);
1831 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1832 pixels=PopShortPixel(MSBEndian,value,pixels);
1834 if (image->colorspace == CMYKColorspace)
1836 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1837 pixels=PopShortPixel(MSBEndian,value,pixels);
1839 if (image->alpha_trait != UndefinedPixelTrait)
1841 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1842 pixels=PopShortPixel(MSBEndian,value,pixels);
1851 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1852 pixels=PopCharPixel(value,pixels);
1853 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1855 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1857 pixels=PopCharPixel(value,pixels);
1858 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1859 pixels=PopCharPixel(value,pixels);
1861 if (image->colorspace == CMYKColorspace)
1863 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1865 pixels=PopCharPixel(value,pixels);
1867 if (image->alpha_trait != UndefinedPixelTrait)
1869 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1871 pixels=PopCharPixel(value,pixels);
1876 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1877 "ImageDepthNotSupported","`%s'",image->filename);
1879 *pixels++=(unsigned char) length;
1883 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1884 Image *image,ExceptionInfo *exception)
1886 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1892 buffer[MaxTextExtent];
1901 #if defined(MAGICKCORE_LZMA_DELEGATE)
1906 initialize_lzma = LZMA_STREAM_INIT,
1941 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1947 Open output image file.
1949 assert(image_info != (const ImageInfo *) NULL);
1950 assert(image_info->signature == MagickSignature);
1951 assert(image != (Image *) NULL);
1952 assert(image->signature == MagickSignature);
1953 if (image->debug != MagickFalse)
1954 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1955 assert(exception != (ExceptionInfo *) NULL);
1956 assert(exception->signature == MagickSignature);
1957 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1958 if (status == MagickFalse)
1964 Allocate image pixels.
1966 if ((image->storage_class == PseudoClass) &&
1967 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1968 (void) SetImageStorageClass(image,DirectClass,exception);
1969 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1970 image->depth <= 32 ? 32UL : 64UL;
1971 if (IsImageGray(image,exception) == MagickFalse)
1974 sRGB masquerading as a grayscale image?
1976 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1977 (void) SetImageColorspace(image,sRGBColorspace,exception);
1980 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1981 (void) SetImageColorspace(image,GRAYColorspace,exception);
1982 quantum_info=AcquireQuantumInfo(image_info,image);
1983 if (quantum_info == (QuantumInfo *) NULL)
1984 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1985 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1986 (quantum_info->format == UndefinedQuantumFormat) &&
1987 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1989 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1990 if (status == MagickFalse)
1991 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1993 compression=UndefinedCompression;
1994 if (image_info->compression != UndefinedCompression)
1995 compression=image_info->compression;
1996 switch (compression)
1998 #if !defined(MAGICKCORE_LZMA_DELEGATE)
1999 case LZMACompression: compression=NoCompression; break;
2001 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
2002 case LZWCompression:
2003 case ZipCompression: compression=NoCompression; break;
2005 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
2006 case BZipCompression: compression=NoCompression; break;
2008 case RLECompression:
2010 if (quantum_info->format == FloatingPointQuantumFormat)
2011 compression=NoCompression;
2012 GetPixelInfo(image,&target);
2018 packet_size=(size_t) (quantum_info->depth/8);
2019 if (image->storage_class == DirectClass)
2020 packet_size=(size_t) (3*quantum_info->depth/8);
2021 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2022 packet_size=(size_t) (quantum_info->depth/8);
2023 if (image->alpha_trait != UndefinedPixelTrait)
2024 packet_size+=quantum_info->depth/8;
2025 if (image->colorspace == CMYKColorspace)
2026 packet_size+=quantum_info->depth/8;
2027 if (compression == RLECompression)
2029 length=image->columns;
2030 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2031 packet_size*image->columns));
2032 if ((compression == BZipCompression) || (compression == ZipCompression))
2033 if (length != (size_t) ((unsigned int) length))
2034 compression=NoCompression;
2035 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2036 sizeof(*compress_pixels));
2037 if (compress_pixels == (unsigned char *) NULL)
2038 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2042 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2043 (void) FormatLocaleString(buffer,MaxTextExtent,
2044 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2045 MagickClassOptions,image->storage_class),(double) image->colors,
2046 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2047 image->alpha_trait));
2048 (void) WriteBlobString(image,buffer);
2049 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2050 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2052 (void) WriteBlobString(image,buffer);
2053 if (image->type != UndefinedType)
2055 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2056 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2057 (void) WriteBlobString(image,buffer);
2059 if (image->colorspace != UndefinedColorspace)
2061 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2062 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2063 (void) WriteBlobString(image,buffer);
2065 if (image->intensity != UndefinedPixelIntensityMethod)
2067 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
2068 CommandOptionToMnemonic(MagickPixelIntensityOptions,
2070 (void) WriteBlobString(image,buffer);
2072 if (image->endian != UndefinedEndian)
2074 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
2075 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2076 (void) WriteBlobString(image,buffer);
2078 if (compression != UndefinedCompression)
2080 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2081 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2082 compression),(double) image->quality);
2083 (void) WriteBlobString(image,buffer);
2085 if (image->units != UndefinedResolution)
2087 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2088 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2089 (void) WriteBlobString(image,buffer);
2091 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2093 (void) FormatLocaleString(buffer,MaxTextExtent,
2094 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2095 (void) WriteBlobString(image,buffer);
2097 if ((image->page.width != 0) || (image->page.height != 0))
2099 (void) FormatLocaleString(buffer,MaxTextExtent,
2100 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2101 image->page.height,(double) image->page.x,(double) image->page.y);
2102 (void) WriteBlobString(image,buffer);
2105 if ((image->page.x != 0) || (image->page.y != 0))
2107 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2108 (long) image->page.x,(long) image->page.y);
2109 (void) WriteBlobString(image,buffer);
2111 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2113 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2114 (long) image->tile_offset.x,(long) image->tile_offset.y);
2115 (void) WriteBlobString(image,buffer);
2117 if ((GetNextImageInList(image) != (Image *) NULL) ||
2118 (GetPreviousImageInList(image) != (Image *) NULL))
2120 if (image->scene == 0)
2121 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2122 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2123 (double) image->delay,(double) image->ticks_per_second);
2125 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2126 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2127 image->scene,(double) image->iterations,(double) image->delay,
2128 (double) image->ticks_per_second);
2129 (void) WriteBlobString(image,buffer);
2133 if (image->scene != 0)
2135 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2136 (double) image->scene);
2137 (void) WriteBlobString(image,buffer);
2139 if (image->iterations != 0)
2141 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2142 (double) image->iterations);
2143 (void) WriteBlobString(image,buffer);
2145 if (image->delay != 0)
2147 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2148 (double) image->delay);
2149 (void) WriteBlobString(image,buffer);
2151 if (image->ticks_per_second != UndefinedTicksPerSecond)
2153 (void) FormatLocaleString(buffer,MaxTextExtent,
2154 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2155 (void) WriteBlobString(image,buffer);
2158 if (image->gravity != UndefinedGravity)
2160 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2161 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2162 (void) WriteBlobString(image,buffer);
2164 if (image->dispose != UndefinedDispose)
2166 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2167 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2168 (void) WriteBlobString(image,buffer);
2170 if (image->rendering_intent != UndefinedIntent)
2172 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2173 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2174 (void) WriteBlobString(image,buffer);
2176 if (image->gamma != 0.0)
2178 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2180 (void) WriteBlobString(image,buffer);
2182 if (image->chromaticity.white_point.x != 0.0)
2185 Note chomaticity points.
2187 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2188 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2189 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2190 image->chromaticity.green_primary.x,
2191 image->chromaticity.green_primary.y,
2192 image->chromaticity.blue_primary.x,
2193 image->chromaticity.blue_primary.y);
2194 (void) WriteBlobString(image,buffer);
2195 (void) FormatLocaleString(buffer,MaxTextExtent,
2196 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2197 image->chromaticity.white_point.y);
2198 (void) WriteBlobString(image,buffer);
2200 if (image->orientation != UndefinedOrientation)
2202 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2203 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2204 (void) WriteBlobString(image,buffer);
2206 if (image->profiles != (void *) NULL)
2215 Write image profiles.
2217 ResetImageProfileIterator(image);
2218 name=GetNextImageProfile(image);
2219 while (name != (const char *) NULL)
2221 profile=GetImageProfile(image,name);
2222 if (profile != (StringInfo *) NULL)
2224 (void) FormatLocaleString(buffer,MaxTextExtent,
2225 "profile:%s=%.20g\n",name,(double)
2226 GetStringInfoLength(profile));
2227 (void) WriteBlobString(image,buffer);
2229 name=GetNextImageProfile(image);
2232 if (image->montage != (char *) NULL)
2234 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2236 (void) WriteBlobString(image,buffer);
2238 if (quantum_info->format == FloatingPointQuantumFormat)
2239 (void) SetImageProperty(image,"quantum:format","floating-point",
2241 ResetImagePropertyIterator(image);
2242 property=GetNextImageProperty(image);
2243 while (property != (const char *) NULL)
2245 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2246 (void) WriteBlobString(image,buffer);
2247 value=GetImageProperty(image,property,exception);
2248 if (value != (const char *) NULL)
2253 length=strlen(value);
2254 for (i=0; i < (ssize_t) length; i++)
2255 if (isspace((int) ((unsigned char) value[i])) != 0)
2257 if ((i == (ssize_t) length) && (i != 0))
2258 (void) WriteBlob(image,length,(const unsigned char *) value);
2261 (void) WriteBlobByte(image,'{');
2262 if (strchr(value,'}') == (char *) NULL)
2263 (void) WriteBlob(image,length,(const unsigned char *) value);
2265 for (i=0; i < (ssize_t) length; i++)
2267 if (value[i] == (int) '}')
2268 (void) WriteBlobByte(image,'\\');
2269 (void) WriteBlobByte(image,value[i]);
2271 (void) WriteBlobByte(image,'}');
2274 (void) WriteBlobByte(image,'\n');
2275 property=GetNextImageProperty(image);
2277 (void) WriteBlobString(image,"\f\n:\032");
2278 if (image->montage != (char *) NULL)
2281 Write montage tile directory.
2283 if (image->directory != (char *) NULL)
2284 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2286 (void) WriteBlobByte(image,'\0');
2288 if (image->profiles != (void *) NULL)
2299 ResetImageProfileIterator(image);
2300 name=GetNextImageProfile(image);
2301 while (name != (const char *) NULL)
2303 profile=GetImageProfile(image,name);
2304 (void) WriteBlob(image,GetStringInfoLength(profile),
2305 GetStringInfoDatum(profile));
2306 name=GetNextImageProfile(image);
2309 if (image->storage_class == PseudoClass)
2321 packet_size=(size_t) (3*quantum_info->depth/8);
2322 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2323 packet_size*sizeof(*colormap));
2324 if (colormap == (unsigned char *) NULL)
2325 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2327 Write colormap to file.
2330 for (i=0; i < (ssize_t) image->colors; i++)
2332 switch (quantum_info->depth)
2335 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2338 register unsigned int
2341 pixel=ScaleQuantumToLong(image->colormap[i].red);
2342 q=PopLongPixel(MSBEndian,pixel,q);
2343 pixel=ScaleQuantumToLong(image->colormap[i].green);
2344 q=PopLongPixel(MSBEndian,pixel,q);
2345 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2346 q=PopLongPixel(MSBEndian,pixel,q);
2351 register unsigned short
2354 pixel=ScaleQuantumToShort(image->colormap[i].red);
2355 q=PopShortPixel(MSBEndian,pixel,q);
2356 pixel=ScaleQuantumToShort(image->colormap[i].green);
2357 q=PopShortPixel(MSBEndian,pixel,q);
2358 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2359 q=PopShortPixel(MSBEndian,pixel,q);
2364 register unsigned char
2367 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2368 q=PopCharPixel(pixel,q);
2369 pixel=(unsigned char) ScaleQuantumToChar(
2370 image->colormap[i].green);
2371 q=PopCharPixel(pixel,q);
2372 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2373 q=PopCharPixel(pixel,q);
2378 (void) WriteBlob(image,packet_size*image->colors,colormap);
2379 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2382 Write image pixels to file.
2385 switch (compression)
2387 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2388 case BZipCompression:
2393 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2394 bzip_info.bzalloc=AcquireBZIPMemory;
2395 bzip_info.bzfree=RelinquishBZIPMemory;
2396 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2397 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2398 9)),(int) image_info->verbose,0);
2404 #if defined(MAGICKCORE_LZMA_DELEGATE)
2405 case LZMACompression:
2410 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2411 allocator.alloc=AcquireLZMAMemory;
2412 allocator.free=RelinquishLZMAMemory;
2413 lzma_info=initialize_lzma;
2414 lzma_info.allocator=&allocator;
2415 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2416 if (code != LZMA_OK)
2421 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2422 case LZWCompression:
2423 case ZipCompression:
2428 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2429 zip_info.zalloc=AcquireZIPMemory;
2430 zip_info.zfree=RelinquishZIPMemory;
2431 code=deflateInit(&zip_info,(int) (image->quality ==
2432 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2441 quantum_type=GetQuantumType(image,exception);
2442 pixels=GetQuantumPixels(quantum_info);
2443 for (y=0; y < (ssize_t) image->rows; y++)
2445 register const Quantum
2451 if (status == MagickFalse)
2453 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2454 if (p == (const Quantum *) NULL)
2457 switch (compression)
2459 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2460 case BZipCompression:
2462 bzip_info.next_in=(char *) pixels;
2463 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2464 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2465 quantum_type,pixels,exception);
2471 bzip_info.next_out=(char *) compress_pixels;
2472 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2474 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2477 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2480 (void) WriteBlobMSBLong(image,(unsigned int) length);
2481 (void) WriteBlob(image,length,compress_pixels);
2483 } while (bzip_info.avail_in != 0);
2487 #if defined(MAGICKCORE_LZMA_DELEGATE)
2488 case LZMACompression:
2490 lzma_info.next_in=pixels;
2491 lzma_info.avail_in=packet_size*image->columns;
2492 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2493 quantum_type,pixels,exception);
2499 lzma_info.next_out=compress_pixels;
2500 lzma_info.avail_out=packet_size*image->columns;
2501 code=lzma_code(&lzma_info,LZMA_RUN);
2502 if (code != LZMA_OK)
2504 length=(size_t) (lzma_info.next_out-compress_pixels);
2507 (void) WriteBlobMSBLong(image,(unsigned int) length);
2508 (void) WriteBlob(image,length,compress_pixels);
2510 } while (lzma_info.avail_in != 0);
2514 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2515 case LZWCompression:
2516 case ZipCompression:
2518 zip_info.next_in=pixels;
2519 zip_info.avail_in=(uInt) (packet_size*image->columns);
2520 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2521 quantum_type,pixels,exception);
2527 zip_info.next_out=compress_pixels;
2528 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2529 code=deflate(&zip_info,Z_SYNC_FLUSH);
2532 length=(size_t) (zip_info.next_out-compress_pixels);
2535 (void) WriteBlobMSBLong(image,(unsigned int) length);
2536 (void) WriteBlob(image,length,compress_pixels);
2538 } while (zip_info.avail_in != 0);
2542 case RLECompression:
2545 GetPixelInfoPixel(image,p,&pixel);
2546 p+=GetPixelChannels(image);
2547 for (x=1; x < (ssize_t) image->columns; x++)
2549 GetPixelInfoPixel(image,p,&target);
2550 if ((length < 255) &&
2551 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2555 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2558 GetPixelInfoPixel(image,p,&pixel);
2559 p+=GetPixelChannels(image);
2561 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2562 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2567 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2568 quantum_type,pixels,exception);
2569 (void) WriteBlob(image,packet_size*image->columns,pixels);
2573 if (image->previous == (Image *) NULL)
2575 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2577 if (status == MagickFalse)
2581 switch (compression)
2583 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2584 case BZipCompression:
2591 if (status == MagickFalse)
2593 bzip_info.next_out=(char *) compress_pixels;
2594 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2596 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2597 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2600 (void) WriteBlobMSBLong(image,(unsigned int) length);
2601 (void) WriteBlob(image,length,compress_pixels);
2603 if (code == BZ_STREAM_END)
2606 code=BZ2_bzCompressEnd(&bzip_info);
2612 #if defined(MAGICKCORE_LZMA_DELEGATE)
2613 case LZMACompression:
2620 if (status == MagickFalse)
2622 lzma_info.next_out=compress_pixels;
2623 lzma_info.avail_out=packet_size*image->columns;
2624 code=lzma_code(&lzma_info,LZMA_FINISH);
2625 length=(size_t) (lzma_info.next_out-compress_pixels);
2628 (void) WriteBlobMSBLong(image,(unsigned int) length);
2629 (void) WriteBlob(image,length,compress_pixels);
2631 if (code == LZMA_STREAM_END)
2634 lzma_end(&lzma_info);
2638 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2639 case LZWCompression:
2640 case ZipCompression:
2647 if (status == MagickFalse)
2649 zip_info.next_out=compress_pixels;
2650 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2651 code=deflate(&zip_info,Z_FINISH);
2652 length=(size_t) (zip_info.next_out-compress_pixels);
2655 (void) WriteBlobMSBLong(image,(unsigned int) length);
2656 (void) WriteBlob(image,length,compress_pixels);
2658 if (code == Z_STREAM_END)
2661 code=deflateEnd(&zip_info);
2670 quantum_info=DestroyQuantumInfo(quantum_info);
2671 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2672 if (GetNextImageInList(image) == (Image *) NULL)
2674 image=SyncNextImageInList(image);
2675 status=SetImageProgress(image,SaveImagesTag,scene++,
2676 GetImageListLength(image));
2677 if (status == MagickFalse)
2679 } while (image_info->adjoin != MagickFalse);
2680 (void) CloseBlob(image);