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 void PushRunlengthPacket(Image *image,const unsigned char *pixels,
189 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
195 if (image->storage_class == PseudoClass)
198 switch (image->depth)
202 pixel->index=ConstrainColormapIndex(image,((size_t) *p << 24) |
203 ((size_t) *(p+1) << 16) | ((size_t) *(p+2) << 8) | (size_t) *(p+3),
210 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
217 pixel->index=ConstrainColormapIndex(image,*p,exception);
222 (void) ThrowMagickException(exception,GetMagickModule(),
223 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
225 switch (image->depth)
232 if (image->alpha_trait != UndefinedPixelTrait)
234 p=PushCharPixel(p,&quantum);
235 pixel->alpha=ScaleCharToQuantum(quantum);
244 if (image->alpha_trait != UndefinedPixelTrait)
246 p=PushShortPixel(MSBEndian,p,&quantum);
247 pixel->alpha=(Quantum) (quantum >> (image->depth-
248 MAGICKCORE_QUANTUM_DEPTH));
257 if (image->alpha_trait != UndefinedPixelTrait)
259 p=PushLongPixel(MSBEndian,p,&quantum);
260 pixel->alpha=(Quantum) (quantum >> (image->depth-
261 MAGICKCORE_QUANTUM_DEPTH));
266 (void) ThrowMagickException(exception,GetMagickModule(),
267 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
269 *length=(size_t) (*p++)+1;
272 switch (image->depth)
279 p=PushCharPixel(p,&quantum);
280 pixel->red=ScaleCharToQuantum(quantum);
281 pixel->green=pixel->red;
282 pixel->blue=pixel->red;
283 if (IsGrayColorspace(image->colorspace) == MagickFalse)
285 p=PushCharPixel(p,&quantum);
286 pixel->green=ScaleCharToQuantum(quantum);
287 p=PushCharPixel(p,&quantum);
288 pixel->blue=ScaleCharToQuantum(quantum);
290 if (image->colorspace == CMYKColorspace)
292 p=PushCharPixel(p,&quantum);
293 pixel->black=ScaleCharToQuantum(quantum);
295 if (image->alpha_trait != UndefinedPixelTrait)
297 p=PushCharPixel(p,&quantum);
298 pixel->alpha=ScaleCharToQuantum(quantum);
307 p=PushShortPixel(MSBEndian,p,&quantum);
308 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
309 pixel->green=pixel->red;
310 pixel->blue=pixel->red;
311 if (IsGrayColorspace(image->colorspace) == MagickFalse)
313 p=PushShortPixel(MSBEndian,p,&quantum);
314 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
315 p=PushShortPixel(MSBEndian,p,&quantum);
316 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
318 if (image->colorspace == CMYKColorspace)
320 p=PushShortPixel(MSBEndian,p,&quantum);
321 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
323 if (image->alpha_trait != UndefinedPixelTrait)
325 p=PushShortPixel(MSBEndian,p,&quantum);
326 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
335 p=PushLongPixel(MSBEndian,p,&quantum);
336 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
337 pixel->green=pixel->red;
338 pixel->blue=pixel->red;
339 if (IsGrayColorspace(image->colorspace) == MagickFalse)
341 p=PushLongPixel(MSBEndian,p,&quantum);
342 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
343 p=PushLongPixel(MSBEndian,p,&quantum);
344 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
346 if (image->colorspace == CMYKColorspace)
348 p=PushLongPixel(MSBEndian,p,&quantum);
349 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
351 if (image->alpha_trait != UndefinedPixelTrait)
353 p=PushLongPixel(MSBEndian,p,&quantum);
354 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
359 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
360 "ImageDepthNotSupported","`%s'",image->filename);
362 *length=(size_t) (*p++)+1;
365 #if defined(MAGICKCORE_BZLIB_DELEGATE)
366 static void RelinquishBZIPMemory(void *context,void *memory)
369 memory=RelinquishMagickMemory(memory);
373 #if defined(MAGICKCORE_LZMA_DELEGATE)
374 static void RelinquishLZMAMemory(void *context,void *memory)
377 memory=RelinquishMagickMemory(memory);
381 #if defined(MAGICKCORE_ZLIB_DELEGATE)
382 static void RelinquishZIPMemory(voidpf context,voidpf memory)
385 memory=RelinquishMagickMemory(memory);
389 static Image *ReadMIFFImage(const ImageInfo *image_info,
390 ExceptionInfo *exception)
392 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
393 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
394 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
396 #if defined(MAGICKCORE_BZLIB_DELEGATE)
403 keyword[MaxTextExtent],
424 #if defined(MAGICKCORE_LZMA_DELEGATE)
426 initialize_lzma = LZMA_STREAM_INIT,
472 #if defined(MAGICKCORE_ZLIB_DELEGATE)
480 assert(image_info != (const ImageInfo *) NULL);
481 assert(image_info->signature == MagickSignature);
482 if (image_info->debug != MagickFalse)
483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
484 image_info->filename);
485 assert(exception != (ExceptionInfo *) NULL);
486 assert(exception->signature == MagickSignature);
487 image=AcquireImage(image_info,exception);
488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
491 image=DestroyImageList(image);
492 return((Image *) NULL);
495 Decode image header; header terminates one character beyond a ':'.
497 c=ReadBlobByte(image);
499 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
501 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
507 Decode image header; header terminates one character beyond a ':'.
509 length=MaxTextExtent;
510 options=AcquireString((char *) NULL);
511 quantum_format=UndefinedQuantumFormat;
512 profiles=(LinkedListInfo *) NULL;
515 image->compression=NoCompression;
516 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
527 Read comment-- any text between { }.
529 length=MaxTextExtent;
530 comment=AcquireString((char *) NULL);
531 for (p=comment; comment != (char *) NULL; p++)
533 c=ReadBlobByte(image);
535 c=ReadBlobByte(image);
537 if ((c == EOF) || (c == (int) '}'))
539 if ((size_t) (p-comment+1) >= length)
543 comment=(char *) ResizeQuantumMemory(comment,length+
544 MaxTextExtent,sizeof(*comment));
545 if (comment == (char *) NULL)
547 p=comment+strlen(comment);
551 if (comment == (char *) NULL)
552 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
554 (void) SetImageProperty(image,"comment",comment,exception);
555 comment=DestroyString(comment);
556 c=ReadBlobByte(image);
559 if (isalnum(c) != MagickFalse)
564 length=MaxTextExtent;
570 if ((size_t) (p-keyword) < (MaxTextExtent-1))
572 c=ReadBlobByte(image);
576 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
577 c=ReadBlobByte(image);
581 Get the keyword value.
583 c=ReadBlobByte(image);
584 while ((c != (int) '}') && (c != EOF))
586 if ((size_t) (p-options+1) >= length)
590 options=(char *) ResizeQuantumMemory(options,length+
591 MaxTextExtent,sizeof(*options));
592 if (options == (char *) NULL)
594 p=options+strlen(options);
597 c=ReadBlobByte(image);
600 c=ReadBlobByte(image);
604 c=ReadBlobByte(image);
608 if (isspace((int) ((unsigned char) c)) != 0)
611 if (options == (char *) NULL)
612 ThrowReaderException(ResourceLimitError,
613 "MemoryAllocationFailed");
617 (void) CopyMagickString(options,options+1,strlen(options));
619 Assign a value to the specified keyword.
626 if (LocaleCompare(keyword,"alpha-trait") == 0)
631 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
632 MagickFalse,options);
635 image->alpha_trait=(PixelTrait) alpha_trait;
638 (void) SetImageProperty(image,keyword,options,exception);
644 if (LocaleCompare(keyword,"background-color") == 0)
646 (void) QueryColorCompliance(options,AllCompliance,
647 &image->background_color,exception);
650 if (LocaleCompare(keyword,"blue-primary") == 0)
652 flags=ParseGeometry(options,&geometry_info);
653 image->chromaticity.blue_primary.x=geometry_info.rho;
654 image->chromaticity.blue_primary.y=geometry_info.sigma;
655 if ((flags & SigmaValue) == 0)
656 image->chromaticity.blue_primary.y=
657 image->chromaticity.blue_primary.x;
660 if (LocaleCompare(keyword,"border-color") == 0)
662 (void) QueryColorCompliance(options,AllCompliance,
663 &image->border_color,exception);
666 (void) SetImageProperty(image,keyword,options,exception);
672 if (LocaleCompare(keyword,"class") == 0)
677 storage_class=ParseCommandOption(MagickClassOptions,
678 MagickFalse,options);
679 if (storage_class < 0)
681 image->storage_class=(ClassType) storage_class;
684 if (LocaleCompare(keyword,"colors") == 0)
686 colors=StringToUnsignedLong(options);
689 if (LocaleCompare(keyword,"colorspace") == 0)
694 colorspace=ParseCommandOption(MagickColorspaceOptions,
695 MagickFalse,options);
698 image->colorspace=(ColorspaceType) colorspace;
701 if (LocaleCompare(keyword,"compression") == 0)
706 compression=ParseCommandOption(MagickCompressOptions,
707 MagickFalse,options);
710 image->compression=(CompressionType) compression;
713 if (LocaleCompare(keyword,"columns") == 0)
715 image->columns=StringToUnsignedLong(options);
718 (void) SetImageProperty(image,keyword,options,exception);
724 if (LocaleCompare(keyword,"delay") == 0)
726 image->delay=StringToUnsignedLong(options);
729 if (LocaleCompare(keyword,"depth") == 0)
731 image->depth=StringToUnsignedLong(options);
734 if (LocaleCompare(keyword,"dispose") == 0)
739 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
743 image->dispose=(DisposeType) dispose;
746 (void) SetImageProperty(image,keyword,options,exception);
752 if (LocaleCompare(keyword,"endian") == 0)
757 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
761 image->endian=(EndianType) endian;
764 (void) SetImageProperty(image,keyword,options,exception);
770 if (LocaleCompare(keyword,"gamma") == 0)
772 image->gamma=StringToDouble(options,(char **) NULL);
775 if (LocaleCompare(keyword,"gravity") == 0)
780 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
784 image->gravity=(GravityType) gravity;
787 if (LocaleCompare(keyword,"green-primary") == 0)
789 flags=ParseGeometry(options,&geometry_info);
790 image->chromaticity.green_primary.x=geometry_info.rho;
791 image->chromaticity.green_primary.y=geometry_info.sigma;
792 if ((flags & SigmaValue) == 0)
793 image->chromaticity.green_primary.y=
794 image->chromaticity.green_primary.x;
797 (void) SetImageProperty(image,keyword,options,exception);
803 if (LocaleCompare(keyword,"id") == 0)
805 (void) CopyMagickString(id,options,MaxTextExtent);
808 if (LocaleCompare(keyword,"iterations") == 0)
810 image->iterations=StringToUnsignedLong(options);
813 (void) SetImageProperty(image,keyword,options,exception);
819 if (LocaleCompare(keyword,"matte") == 0)
824 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
828 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
832 if (LocaleCompare(keyword,"matte-color") == 0)
834 (void) QueryColorCompliance(options,AllCompliance,
835 &image->matte_color,exception);
838 if (LocaleCompare(keyword,"montage") == 0)
840 (void) CloneString(&image->montage,options);
843 (void) SetImageProperty(image,keyword,options,exception);
849 if (LocaleCompare(keyword,"orientation") == 0)
854 orientation=ParseCommandOption(MagickOrientationOptions,
855 MagickFalse,options);
858 image->orientation=(OrientationType) orientation;
861 (void) SetImageProperty(image,keyword,options,exception);
867 if (LocaleCompare(keyword,"page") == 0)
872 geometry=GetPageGeometry(options);
873 (void) ParseAbsoluteGeometry(geometry,&image->page);
874 geometry=DestroyString(geometry);
877 if (LocaleCompare(keyword,"pixel-intensity") == 0)
882 intensity=ParseCommandOption(MagickPixelIntensityOptions,
883 MagickFalse,options);
886 image->intensity=(PixelIntensityMethod) intensity;
889 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
890 (LocaleNCompare(keyword,"profile-",8) == 0))
895 if (profiles == (LinkedListInfo *) NULL)
896 profiles=NewLinkedList(0);
897 (void) AppendValueToLinkedList(profiles,
898 AcquireString(keyword+8));
899 profile=BlobToStringInfo((const void *) NULL,(size_t)
900 StringToLong(options));
901 if (profile == (StringInfo *) NULL)
902 ThrowReaderException(ResourceLimitError,
903 "MemoryAllocationFailed");
904 (void) SetImageProfile(image,keyword+8,profile,exception);
905 profile=DestroyStringInfo(profile);
908 (void) SetImageProperty(image,keyword,options,exception);
914 if (LocaleCompare(keyword,"quality") == 0)
916 image->quality=StringToUnsignedLong(options);
919 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
920 (LocaleCompare(keyword,"quantum:format") == 0))
925 format=ParseCommandOption(MagickQuantumFormatOptions,
926 MagickFalse,options);
929 quantum_format=(QuantumFormatType) format;
932 (void) SetImageProperty(image,keyword,options,exception);
938 if (LocaleCompare(keyword,"red-primary") == 0)
940 flags=ParseGeometry(options,&geometry_info);
941 image->chromaticity.red_primary.x=geometry_info.rho;
942 image->chromaticity.red_primary.y=geometry_info.sigma;
943 if ((flags & SigmaValue) == 0)
944 image->chromaticity.red_primary.y=
945 image->chromaticity.red_primary.x;
948 if (LocaleCompare(keyword,"rendering-intent") == 0)
953 rendering_intent=ParseCommandOption(MagickIntentOptions,
954 MagickFalse,options);
955 if (rendering_intent < 0)
957 image->rendering_intent=(RenderingIntent) rendering_intent;
960 if (LocaleCompare(keyword,"resolution") == 0)
962 flags=ParseGeometry(options,&geometry_info);
963 image->resolution.x=geometry_info.rho;
964 image->resolution.y=geometry_info.sigma;
965 if ((flags & SigmaValue) == 0)
966 image->resolution.y=image->resolution.x;
969 if (LocaleCompare(keyword,"rows") == 0)
971 image->rows=StringToUnsignedLong(options);
974 (void) SetImageProperty(image,keyword,options,exception);
980 if (LocaleCompare(keyword,"scene") == 0)
982 image->scene=StringToUnsignedLong(options);
985 (void) SetImageProperty(image,keyword,options,exception);
991 if (LocaleCompare(keyword,"ticks-per-second") == 0)
993 image->ticks_per_second=(ssize_t) StringToLong(options);
996 if (LocaleCompare(keyword,"tile-offset") == 0)
1001 geometry=GetPageGeometry(options);
1002 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1003 geometry=DestroyString(geometry);
1006 if (LocaleCompare(keyword,"type") == 0)
1011 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1015 image->type=(ImageType) type;
1018 (void) SetImageProperty(image,keyword,options,exception);
1024 if (LocaleCompare(keyword,"units") == 0)
1029 units=ParseCommandOption(MagickResolutionOptions,
1030 MagickFalse,options);
1033 image->units=(ResolutionType) units;
1036 (void) SetImageProperty(image,keyword,options,exception);
1042 if (LocaleCompare(keyword,"version") == 0)
1044 version=StringToDouble(options,(char **) NULL);
1047 (void) SetImageProperty(image,keyword,options,exception);
1053 if (LocaleCompare(keyword,"white-point") == 0)
1055 flags=ParseGeometry(options,&geometry_info);
1056 image->chromaticity.white_point.x=geometry_info.rho;
1057 image->chromaticity.white_point.y=geometry_info.sigma;
1058 if ((flags & SigmaValue) == 0)
1059 image->chromaticity.white_point.y=
1060 image->chromaticity.white_point.x;
1063 (void) SetImageProperty(image,keyword,options,exception);
1068 (void) SetImageProperty(image,keyword,options,exception);
1074 c=ReadBlobByte(image);
1075 while (isspace((int) ((unsigned char) c)) != 0)
1076 c=ReadBlobByte(image);
1078 options=DestroyString(options);
1079 (void) ReadBlobByte(image);
1081 Verify that required image information is defined.
1083 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1084 (image->storage_class == UndefinedClass) ||
1085 (image->columns == 0) || (image->rows == 0))
1086 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1087 if (image->montage != (char *) NULL)
1095 length=MaxTextExtent;
1096 image->directory=AcquireString((char *) NULL);
1101 if ((strlen(image->directory)+MaxTextExtent) >= length)
1104 Allocate more memory for the image directory.
1107 image->directory=(char *) ResizeQuantumMemory(image->directory,
1108 length+MaxTextExtent,sizeof(*image->directory));
1109 if (image->directory == (char *) NULL)
1110 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1111 p=image->directory+strlen(image->directory);
1113 c=ReadBlobByte(image);
1115 } while (c != (int) '\0');
1117 if (profiles != (LinkedListInfo *) NULL)
1126 Read image profiles.
1128 ResetLinkedListIterator(profiles);
1129 name=(const char *) GetNextValueInLinkedList(profiles);
1130 while (name != (const char *) NULL)
1132 profile=GetImageProfile(image,name);
1133 if (profile != (StringInfo *) NULL)
1135 register unsigned char
1138 p=GetStringInfoDatum(profile);
1139 count=ReadBlob(image,GetStringInfoLength(profile),p);
1142 name=(const char *) GetNextValueInLinkedList(profiles);
1144 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1146 image->depth=GetImageQuantumDepth(image,MagickFalse);
1147 if (image->storage_class == PseudoClass)
1150 Create image colormap.
1152 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1153 if (status == MagickFalse)
1154 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1164 Read image colormap from file.
1166 packet_size=(size_t) (3UL*image->depth/8UL);
1167 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1168 packet_size*sizeof(*colormap));
1169 if (colormap == (unsigned char *) NULL)
1170 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1171 count=ReadBlob(image,packet_size*image->colors,colormap);
1173 switch (image->depth)
1176 ThrowReaderException(CorruptImageError,
1177 "ImageDepthNotSupported");
1183 for (i=0; i < (ssize_t) image->colors; i++)
1185 p=PushCharPixel(p,&pixel);
1186 image->colormap[i].red=ScaleCharToQuantum(pixel);
1187 p=PushCharPixel(p,&pixel);
1188 image->colormap[i].green=ScaleCharToQuantum(pixel);
1189 p=PushCharPixel(p,&pixel);
1190 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1199 for (i=0; i < (ssize_t) image->colors; i++)
1201 p=PushShortPixel(MSBEndian,p,&pixel);
1202 image->colormap[i].red=ScaleShortToQuantum(pixel);
1203 p=PushShortPixel(MSBEndian,p,&pixel);
1204 image->colormap[i].green=ScaleShortToQuantum(pixel);
1205 p=PushShortPixel(MSBEndian,p,&pixel);
1206 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1215 for (i=0; i < (ssize_t) image->colors; i++)
1217 p=PushLongPixel(MSBEndian,p,&pixel);
1218 image->colormap[i].red=ScaleLongToQuantum(pixel);
1219 p=PushLongPixel(MSBEndian,p,&pixel);
1220 image->colormap[i].green=ScaleLongToQuantum(pixel);
1221 p=PushLongPixel(MSBEndian,p,&pixel);
1222 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1227 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1230 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1231 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1233 status=SetImageExtent(image,image->columns,image->rows,exception);
1234 if (status == MagickFalse)
1235 return(DestroyImageList(image));
1237 Allocate image pixels.
1239 quantum_info=AcquireQuantumInfo(image_info,image);
1240 if (quantum_info == (QuantumInfo *) NULL)
1241 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1242 if (quantum_format != UndefinedQuantumFormat)
1244 status=SetQuantumFormat(image,quantum_info,quantum_format);
1245 if (status == MagickFalse)
1246 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1248 packet_size=(size_t) (quantum_info->depth/8);
1249 if (image->storage_class == DirectClass)
1250 packet_size=(size_t) (3*quantum_info->depth/8);
1251 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1252 packet_size=quantum_info->depth/8;
1253 if (image->alpha_trait != UndefinedPixelTrait)
1254 packet_size+=quantum_info->depth/8;
1255 if (image->colorspace == CMYKColorspace)
1256 packet_size+=quantum_info->depth/8;
1257 if (image->compression == RLECompression)
1259 compress_extent=MagickMax(MagickMax(BZipMaxExtent(packet_size*
1260 image->columns),LZMAMaxExtent(packet_size*image->columns)),
1261 ZipMaxExtent(packet_size*image->columns));
1262 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_extent,
1263 sizeof(*compress_pixels));
1264 if (compress_pixels == (unsigned char *) NULL)
1265 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1269 quantum_type=RGBQuantum;
1270 if (image->alpha_trait != UndefinedPixelTrait)
1271 quantum_type=RGBAQuantum;
1272 if (image->colorspace == CMYKColorspace)
1274 quantum_type=CMYKQuantum;
1275 if (image->alpha_trait != UndefinedPixelTrait)
1276 quantum_type=CMYKAQuantum;
1278 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1280 quantum_type=GrayQuantum;
1281 if (image->alpha_trait != UndefinedPixelTrait)
1282 quantum_type=GrayAlphaQuantum;
1284 if (image->storage_class == PseudoClass)
1286 quantum_type=IndexQuantum;
1287 if (image->alpha_trait != UndefinedPixelTrait)
1288 quantum_type=IndexAlphaQuantum;
1291 GetPixelInfo(image,&pixel);
1292 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1293 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1295 #if defined(MAGICKCORE_LZMA_DELEGATE)
1296 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1298 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1299 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1301 switch (image->compression)
1303 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1304 case BZipCompression:
1309 bzip_info.bzalloc=AcquireBZIPMemory;
1310 bzip_info.bzfree=RelinquishBZIPMemory;
1311 bzip_info.opaque=(void *) NULL;
1312 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1319 #if defined(MAGICKCORE_LZMA_DELEGATE)
1320 case LZMACompression:
1325 allocator.alloc=AcquireLZMAMemory;
1326 allocator.free=RelinquishLZMAMemory;
1327 lzma_info=initialize_lzma;
1328 lzma_info.allocator=(&allocator);
1329 code=lzma_auto_decoder(&lzma_info,-1,0);
1330 if (code != LZMA_OK)
1335 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1336 case LZWCompression:
1337 case ZipCompression:
1342 zip_info.zalloc=AcquireZIPMemory;
1343 zip_info.zfree=RelinquishZIPMemory;
1344 zip_info.opaque=(voidpf) NULL;
1345 code=inflateInit(&zip_info);
1351 case RLECompression:
1356 pixels=GetQuantumPixels(quantum_info);
1358 for (y=0; y < (ssize_t) image->rows; y++)
1366 if (status == MagickFalse)
1368 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1369 if (q == (Quantum *) NULL)
1371 switch (image->compression)
1373 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1374 case BZipCompression:
1376 bzip_info.next_out=(char *) pixels;
1377 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1383 if (bzip_info.avail_in == 0)
1385 bzip_info.next_in=(char *) compress_pixels;
1386 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1388 length=(size_t) ReadBlobMSBLong(image);
1389 if (length > compress_extent)
1390 ThrowReaderException(CorruptImageError,
1391 "UnableToReadImageData");
1392 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1393 (unsigned char *) bzip_info.next_in);
1395 code=BZ2_bzDecompress(&bzip_info);
1401 if (code == 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 if (length > compress_extent)
1424 ThrowReaderException(CorruptImageError,
1425 "UnableToReadImageData");
1426 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1427 (unsigned char *) lzma_info.next_in);
1429 code=lzma_code(&lzma_info,LZMA_RUN);
1435 if (code == LZMA_STREAM_END)
1437 } while (lzma_info.avail_out != 0);
1438 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1439 quantum_type,pixels,exception);
1443 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1444 case LZWCompression:
1445 case ZipCompression:
1447 zip_info.next_out=pixels;
1448 zip_info.avail_out=(uInt) (packet_size*image->columns);
1454 if (zip_info.avail_in == 0)
1456 zip_info.next_in=compress_pixels;
1457 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1459 length=(size_t) ReadBlobMSBLong(image);
1460 if (length > compress_extent)
1461 ThrowReaderException(CorruptImageError,
1462 "UnableToReadImageData");
1463 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1466 code=inflate(&zip_info,Z_SYNC_FLUSH);
1472 if (code == Z_STREAM_END)
1474 } while (zip_info.avail_out != 0);
1475 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1476 quantum_type,pixels,exception);
1480 case RLECompression:
1482 for (x=0; x < (ssize_t) image->columns; x++)
1486 count=ReadBlob(image,packet_size,pixels);
1487 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1490 if (image->storage_class == PseudoClass)
1491 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1494 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1495 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1496 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1497 if (image->colorspace == CMYKColorspace)
1498 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1500 if (image->alpha_trait != UndefinedPixelTrait)
1501 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1502 q+=GetPixelChannels(image);
1508 count=ReadBlob(image,packet_size*image->columns,pixels);
1509 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1510 quantum_type,pixels,exception);
1514 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1517 SetQuantumImageType(image,quantum_type);
1518 switch (image->compression)
1520 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1521 case BZipCompression:
1531 offset=SeekBlob(image,-((MagickOffsetType)
1532 bzip_info.avail_in),SEEK_CUR);
1534 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1536 code=BZ2_bzDecompressEnd(&bzip_info);
1542 #if defined(MAGICKCORE_LZMA_DELEGATE)
1543 case LZMACompression:
1548 code=lzma_code(&lzma_info,LZMA_FINISH);
1549 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1551 lzma_end(&lzma_info);
1555 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1556 case LZWCompression:
1557 case ZipCompression:
1567 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1570 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1572 code=inflateEnd(&zip_info);
1573 if (code != LZMA_OK)
1581 quantum_info=DestroyQuantumInfo(quantum_info);
1582 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1583 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1585 image=DestroyImageList(image);
1586 return((Image *) NULL);
1588 if (EOFBlob(image) != MagickFalse)
1590 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1595 Proceed to next image.
1597 if (image_info->number_scenes != 0)
1598 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1602 c=ReadBlobByte(image);
1603 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1607 Allocate next image structure.
1609 AcquireNextImage(image_info,image,exception);
1610 if (GetNextImageInList(image) == (Image *) NULL)
1612 image=DestroyImageList(image);
1613 return((Image *) NULL);
1615 image=SyncNextImageInList(image);
1616 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1617 GetBlobSize(image));
1618 if (status == MagickFalse)
1622 (void) CloseBlob(image);
1623 return(GetFirstImageInList(image));
1627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1631 % R e g i s t e r M I F F I m a g e %
1635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1638 % supported formats. The properties include the image format tag, a method to
1639 % read and/or write the format, whether the format supports the saving of more
1640 % than one frame to the same file or blob, whether the format supports native
1641 % in-memory I/O, and a brief description of the format.
1643 % The format of the RegisterMIFFImage method is:
1645 % size_t RegisterMIFFImage(void)
1648 ModuleExport size_t RegisterMIFFImage(void)
1651 version[MaxTextExtent];
1657 #if defined(MagickImageCoderSignatureText)
1658 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1659 #if defined(ZLIB_VERSION)
1660 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1661 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1663 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1664 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1667 entry=SetMagickInfo("MIFF");
1668 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1669 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1670 entry->magick=(IsImageFormatHandler *) IsMIFF;
1671 entry->flags|=CoderSeekableStreamFlag;
1672 entry->description=ConstantString("Magick Image File Format");
1673 if (*version != '\0')
1674 entry->version=ConstantString(version);
1675 entry->module=ConstantString("MIFF");
1676 (void) RegisterMagickInfo(entry);
1677 return(MagickImageCoderSignature);
1681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1685 % U n r e g i s t e r M I F F I m a g e %
1689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1691 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1692 % from the list of supported formats.
1694 % The format of the UnregisterMIFFImage method is:
1696 % UnregisterMIFFImage(void)
1699 ModuleExport void UnregisterMIFFImage(void)
1701 (void) UnregisterMagickInfo("MIFF");
1705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 % W r i t e M I F F I m a g e %
1713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715 % WriteMIFFImage() writes a MIFF image to a file.
1717 % The format of the WriteMIFFImage method is:
1719 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1720 % Image *image,ExceptionInfo *exception)
1722 % Compression code contributed by Kyle Shorter.
1724 % A description of each parameter follows:
1726 % o image_info: the image info.
1728 % o image: the image.
1730 % o exception: return any errors or warnings in this structure.
1734 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1735 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1737 if (image->storage_class != DirectClass)
1742 value=(unsigned int) ClampToQuantum(pixel->index);
1743 switch (image->depth)
1747 *pixels++=(unsigned char) (value >> 24);
1748 *pixels++=(unsigned char) (value >> 16);
1751 *pixels++=(unsigned char) (value >> 8);
1754 *pixels++=(unsigned char) value;
1758 (void) ThrowMagickException(exception,GetMagickModule(),
1759 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1761 switch (image->depth)
1768 if (image->alpha_trait != UndefinedPixelTrait)
1770 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1771 pixels=PopLongPixel(MSBEndian,value,pixels);
1780 if (image->alpha_trait != UndefinedPixelTrait)
1782 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1783 pixels=PopShortPixel(MSBEndian,value,pixels);
1792 if (image->alpha_trait != UndefinedPixelTrait)
1794 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1796 pixels=PopCharPixel(value,pixels);
1801 (void) ThrowMagickException(exception,GetMagickModule(),
1802 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1804 *pixels++=(unsigned char) length;
1807 switch (image->depth)
1814 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1815 pixels=PopLongPixel(MSBEndian,value,pixels);
1816 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1818 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1819 pixels=PopLongPixel(MSBEndian,value,pixels);
1820 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1821 pixels=PopLongPixel(MSBEndian,value,pixels);
1823 if (image->colorspace == CMYKColorspace)
1825 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1826 pixels=PopLongPixel(MSBEndian,value,pixels);
1828 if (image->alpha_trait != UndefinedPixelTrait)
1830 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1831 pixels=PopLongPixel(MSBEndian,value,pixels);
1840 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1841 pixels=PopShortPixel(MSBEndian,value,pixels);
1842 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1844 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1845 pixels=PopShortPixel(MSBEndian,value,pixels);
1846 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1847 pixels=PopShortPixel(MSBEndian,value,pixels);
1849 if (image->colorspace == CMYKColorspace)
1851 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1852 pixels=PopShortPixel(MSBEndian,value,pixels);
1854 if (image->alpha_trait != UndefinedPixelTrait)
1856 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1857 pixels=PopShortPixel(MSBEndian,value,pixels);
1866 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1867 pixels=PopCharPixel(value,pixels);
1868 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1870 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1872 pixels=PopCharPixel(value,pixels);
1873 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1874 pixels=PopCharPixel(value,pixels);
1876 if (image->colorspace == CMYKColorspace)
1878 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1880 pixels=PopCharPixel(value,pixels);
1882 if (image->alpha_trait != UndefinedPixelTrait)
1884 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1886 pixels=PopCharPixel(value,pixels);
1891 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1892 "ImageDepthNotSupported","`%s'",image->filename);
1894 *pixels++=(unsigned char) length;
1898 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1899 Image *image,ExceptionInfo *exception)
1901 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1907 buffer[MaxTextExtent];
1916 #if defined(MAGICKCORE_LZMA_DELEGATE)
1921 initialize_lzma = LZMA_STREAM_INIT,
1956 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1962 Open output image file.
1964 assert(image_info != (const ImageInfo *) NULL);
1965 assert(image_info->signature == MagickSignature);
1966 assert(image != (Image *) NULL);
1967 assert(image->signature == MagickSignature);
1968 if (image->debug != MagickFalse)
1969 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1970 assert(exception != (ExceptionInfo *) NULL);
1971 assert(exception->signature == MagickSignature);
1972 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1973 if (status == MagickFalse)
1979 Allocate image pixels.
1981 if ((image->storage_class == PseudoClass) &&
1982 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1983 (void) SetImageStorageClass(image,DirectClass,exception);
1984 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1985 image->depth <= 32 ? 32UL : 64UL;
1986 if (IsImageGray(image,exception) == MagickFalse)
1989 sRGB masquerading as a grayscale image?
1991 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1992 (void) SetImageColorspace(image,sRGBColorspace,exception);
1995 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1996 (void) SetImageColorspace(image,GRAYColorspace,exception);
1997 quantum_info=AcquireQuantumInfo(image_info,image);
1998 if (quantum_info == (QuantumInfo *) NULL)
1999 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2000 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2001 (quantum_info->format == UndefinedQuantumFormat) &&
2002 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
2004 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2005 if (status == MagickFalse)
2006 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2008 compression=UndefinedCompression;
2009 if (image_info->compression != UndefinedCompression)
2010 compression=image_info->compression;
2011 switch (compression)
2013 #if !defined(MAGICKCORE_LZMA_DELEGATE)
2014 case LZMACompression: compression=NoCompression; break;
2016 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
2017 case LZWCompression:
2018 case ZipCompression: compression=NoCompression; break;
2020 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
2021 case BZipCompression: compression=NoCompression; break;
2023 case RLECompression:
2025 if (quantum_info->format == FloatingPointQuantumFormat)
2026 compression=NoCompression;
2027 GetPixelInfo(image,&target);
2033 packet_size=(size_t) (quantum_info->depth/8);
2034 if (image->storage_class == DirectClass)
2035 packet_size=(size_t) (3*quantum_info->depth/8);
2036 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2037 packet_size=(size_t) (quantum_info->depth/8);
2038 if (image->alpha_trait != UndefinedPixelTrait)
2039 packet_size+=quantum_info->depth/8;
2040 if (image->colorspace == CMYKColorspace)
2041 packet_size+=quantum_info->depth/8;
2042 if (compression == RLECompression)
2044 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2045 packet_size*image->columns));
2046 if ((compression == BZipCompression) || (compression == ZipCompression))
2047 if (length != (size_t) ((unsigned int) length))
2048 compression=NoCompression;
2049 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2050 sizeof(*compress_pixels));
2051 if (compress_pixels == (unsigned char *) NULL)
2052 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2056 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2057 (void) FormatLocaleString(buffer,MaxTextExtent,
2058 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2059 MagickClassOptions,image->storage_class),(double) image->colors,
2060 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2061 image->alpha_trait));
2062 (void) WriteBlobString(image,buffer);
2063 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2064 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2066 (void) WriteBlobString(image,buffer);
2067 if (image->type != UndefinedType)
2069 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2070 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2071 (void) WriteBlobString(image,buffer);
2073 if (image->colorspace != UndefinedColorspace)
2075 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2076 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2077 (void) WriteBlobString(image,buffer);
2079 if (image->intensity != UndefinedPixelIntensityMethod)
2081 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
2082 CommandOptionToMnemonic(MagickPixelIntensityOptions,
2084 (void) WriteBlobString(image,buffer);
2086 if (image->endian != UndefinedEndian)
2088 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
2089 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2090 (void) WriteBlobString(image,buffer);
2092 if (compression != UndefinedCompression)
2094 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2095 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2096 compression),(double) image->quality);
2097 (void) WriteBlobString(image,buffer);
2099 if (image->units != UndefinedResolution)
2101 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2102 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2103 (void) WriteBlobString(image,buffer);
2105 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2107 (void) FormatLocaleString(buffer,MaxTextExtent,
2108 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2109 (void) WriteBlobString(image,buffer);
2111 if ((image->page.width != 0) || (image->page.height != 0))
2113 (void) FormatLocaleString(buffer,MaxTextExtent,
2114 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2115 image->page.height,(double) image->page.x,(double) image->page.y);
2116 (void) WriteBlobString(image,buffer);
2119 if ((image->page.x != 0) || (image->page.y != 0))
2121 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2122 (long) image->page.x,(long) image->page.y);
2123 (void) WriteBlobString(image,buffer);
2125 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2127 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2128 (long) image->tile_offset.x,(long) image->tile_offset.y);
2129 (void) WriteBlobString(image,buffer);
2131 if ((GetNextImageInList(image) != (Image *) NULL) ||
2132 (GetPreviousImageInList(image) != (Image *) NULL))
2134 if (image->scene == 0)
2135 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2136 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2137 (double) image->delay,(double) image->ticks_per_second);
2139 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2140 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2141 image->scene,(double) image->iterations,(double) image->delay,
2142 (double) image->ticks_per_second);
2143 (void) WriteBlobString(image,buffer);
2147 if (image->scene != 0)
2149 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2150 (double) image->scene);
2151 (void) WriteBlobString(image,buffer);
2153 if (image->iterations != 0)
2155 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2156 (double) image->iterations);
2157 (void) WriteBlobString(image,buffer);
2159 if (image->delay != 0)
2161 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2162 (double) image->delay);
2163 (void) WriteBlobString(image,buffer);
2165 if (image->ticks_per_second != UndefinedTicksPerSecond)
2167 (void) FormatLocaleString(buffer,MaxTextExtent,
2168 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2169 (void) WriteBlobString(image,buffer);
2172 if (image->gravity != UndefinedGravity)
2174 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2175 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2176 (void) WriteBlobString(image,buffer);
2178 if (image->dispose != UndefinedDispose)
2180 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2181 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2182 (void) WriteBlobString(image,buffer);
2184 if (image->rendering_intent != UndefinedIntent)
2186 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2187 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2188 (void) WriteBlobString(image,buffer);
2190 if (image->gamma != 0.0)
2192 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2194 (void) WriteBlobString(image,buffer);
2196 if (image->chromaticity.white_point.x != 0.0)
2199 Note chomaticity points.
2201 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2202 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2203 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2204 image->chromaticity.green_primary.x,
2205 image->chromaticity.green_primary.y,
2206 image->chromaticity.blue_primary.x,
2207 image->chromaticity.blue_primary.y);
2208 (void) WriteBlobString(image,buffer);
2209 (void) FormatLocaleString(buffer,MaxTextExtent,
2210 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2211 image->chromaticity.white_point.y);
2212 (void) WriteBlobString(image,buffer);
2214 if (image->orientation != UndefinedOrientation)
2216 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2217 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2218 (void) WriteBlobString(image,buffer);
2220 if (image->profiles != (void *) NULL)
2229 Write image profiles.
2231 ResetImageProfileIterator(image);
2232 name=GetNextImageProfile(image);
2233 while (name != (const char *) NULL)
2235 profile=GetImageProfile(image,name);
2236 if (profile != (StringInfo *) NULL)
2238 (void) FormatLocaleString(buffer,MaxTextExtent,
2239 "profile:%s=%.20g\n",name,(double)
2240 GetStringInfoLength(profile));
2241 (void) WriteBlobString(image,buffer);
2243 name=GetNextImageProfile(image);
2246 if (image->montage != (char *) NULL)
2248 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2250 (void) WriteBlobString(image,buffer);
2252 if (quantum_info->format == FloatingPointQuantumFormat)
2253 (void) SetImageProperty(image,"quantum:format","floating-point",
2255 ResetImagePropertyIterator(image);
2256 property=GetNextImageProperty(image);
2257 while (property != (const char *) NULL)
2259 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2260 (void) WriteBlobString(image,buffer);
2261 value=GetImageProperty(image,property,exception);
2262 if (value != (const char *) NULL)
2267 length=strlen(value);
2268 for (i=0; i < (ssize_t) length; i++)
2269 if (isspace((int) ((unsigned char) value[i])) != 0)
2271 if ((i == (ssize_t) length) && (i != 0))
2272 (void) WriteBlob(image,length,(const unsigned char *) value);
2275 (void) WriteBlobByte(image,'{');
2276 if (strchr(value,'}') == (char *) NULL)
2277 (void) WriteBlob(image,length,(const unsigned char *) value);
2279 for (i=0; i < (ssize_t) length; i++)
2281 if (value[i] == (int) '}')
2282 (void) WriteBlobByte(image,'\\');
2283 (void) WriteBlobByte(image,value[i]);
2285 (void) WriteBlobByte(image,'}');
2288 (void) WriteBlobByte(image,'\n');
2289 property=GetNextImageProperty(image);
2291 (void) WriteBlobString(image,"\f\n:\032");
2292 if (image->montage != (char *) NULL)
2295 Write montage tile directory.
2297 if (image->directory != (char *) NULL)
2298 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2300 (void) WriteBlobByte(image,'\0');
2302 if (image->profiles != (void *) NULL)
2313 ResetImageProfileIterator(image);
2314 name=GetNextImageProfile(image);
2315 while (name != (const char *) NULL)
2317 profile=GetImageProfile(image,name);
2318 (void) WriteBlob(image,GetStringInfoLength(profile),
2319 GetStringInfoDatum(profile));
2320 name=GetNextImageProfile(image);
2323 if (image->storage_class == PseudoClass)
2335 packet_size=(size_t) (3*quantum_info->depth/8);
2336 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2337 packet_size*sizeof(*colormap));
2338 if (colormap == (unsigned char *) NULL)
2339 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2341 Write colormap to file.
2344 for (i=0; i < (ssize_t) image->colors; i++)
2346 switch (quantum_info->depth)
2349 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2352 register unsigned int
2355 pixel=ScaleQuantumToLong(image->colormap[i].red);
2356 q=PopLongPixel(MSBEndian,pixel,q);
2357 pixel=ScaleQuantumToLong(image->colormap[i].green);
2358 q=PopLongPixel(MSBEndian,pixel,q);
2359 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2360 q=PopLongPixel(MSBEndian,pixel,q);
2365 register unsigned short
2368 pixel=ScaleQuantumToShort(image->colormap[i].red);
2369 q=PopShortPixel(MSBEndian,pixel,q);
2370 pixel=ScaleQuantumToShort(image->colormap[i].green);
2371 q=PopShortPixel(MSBEndian,pixel,q);
2372 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2373 q=PopShortPixel(MSBEndian,pixel,q);
2378 register unsigned char
2381 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2382 q=PopCharPixel(pixel,q);
2383 pixel=(unsigned char) ScaleQuantumToChar(
2384 image->colormap[i].green);
2385 q=PopCharPixel(pixel,q);
2386 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2387 q=PopCharPixel(pixel,q);
2392 (void) WriteBlob(image,packet_size*image->colors,colormap);
2393 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2396 Write image pixels to file.
2399 switch (compression)
2401 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2402 case BZipCompression:
2407 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2408 bzip_info.bzalloc=AcquireBZIPMemory;
2409 bzip_info.bzfree=RelinquishBZIPMemory;
2410 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2411 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2412 9)),(int) image_info->verbose,0);
2418 #if defined(MAGICKCORE_LZMA_DELEGATE)
2419 case LZMACompression:
2424 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2425 allocator.alloc=AcquireLZMAMemory;
2426 allocator.free=RelinquishLZMAMemory;
2427 lzma_info=initialize_lzma;
2428 lzma_info.allocator=&allocator;
2429 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2430 if (code != LZMA_OK)
2435 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2436 case LZWCompression:
2437 case ZipCompression:
2442 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2443 zip_info.zalloc=AcquireZIPMemory;
2444 zip_info.zfree=RelinquishZIPMemory;
2445 code=deflateInit(&zip_info,(int) (image->quality ==
2446 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2455 quantum_type=GetQuantumType(image,exception);
2456 pixels=GetQuantumPixels(quantum_info);
2457 for (y=0; y < (ssize_t) image->rows; y++)
2459 register const Quantum
2465 if (status == MagickFalse)
2467 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2468 if (p == (const Quantum *) NULL)
2471 switch (compression)
2473 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2474 case BZipCompression:
2476 bzip_info.next_in=(char *) pixels;
2477 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2478 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2479 quantum_type,pixels,exception);
2485 bzip_info.next_out=(char *) compress_pixels;
2486 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2488 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2491 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2494 (void) WriteBlobMSBLong(image,(unsigned int) length);
2495 (void) WriteBlob(image,length,compress_pixels);
2497 } while (bzip_info.avail_in != 0);
2501 #if defined(MAGICKCORE_LZMA_DELEGATE)
2502 case LZMACompression:
2504 lzma_info.next_in=pixels;
2505 lzma_info.avail_in=packet_size*image->columns;
2506 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2507 quantum_type,pixels,exception);
2513 lzma_info.next_out=compress_pixels;
2514 lzma_info.avail_out=packet_size*image->columns;
2515 code=lzma_code(&lzma_info,LZMA_RUN);
2516 if (code != LZMA_OK)
2518 length=(size_t) (lzma_info.next_out-compress_pixels);
2521 (void) WriteBlobMSBLong(image,(unsigned int) length);
2522 (void) WriteBlob(image,length,compress_pixels);
2524 } while (lzma_info.avail_in != 0);
2528 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2529 case LZWCompression:
2530 case ZipCompression:
2532 zip_info.next_in=pixels;
2533 zip_info.avail_in=(uInt) (packet_size*image->columns);
2534 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2535 quantum_type,pixels,exception);
2541 zip_info.next_out=compress_pixels;
2542 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2543 code=deflate(&zip_info,Z_SYNC_FLUSH);
2546 length=(size_t) (zip_info.next_out-compress_pixels);
2549 (void) WriteBlobMSBLong(image,(unsigned int) length);
2550 (void) WriteBlob(image,length,compress_pixels);
2552 } while (zip_info.avail_in != 0);
2556 case RLECompression:
2559 GetPixelInfoPixel(image,p,&pixel);
2560 p+=GetPixelChannels(image);
2561 for (x=1; x < (ssize_t) image->columns; x++)
2563 GetPixelInfoPixel(image,p,&target);
2564 if ((length < 255) &&
2565 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2569 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2572 GetPixelInfoPixel(image,p,&pixel);
2573 p+=GetPixelChannels(image);
2575 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2576 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2581 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2582 quantum_type,pixels,exception);
2583 (void) WriteBlob(image,packet_size*image->columns,pixels);
2587 if (image->previous == (Image *) NULL)
2589 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2591 if (status == MagickFalse)
2595 switch (compression)
2597 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2598 case BZipCompression:
2605 if (status == MagickFalse)
2607 bzip_info.next_out=(char *) compress_pixels;
2608 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2610 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2611 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2614 (void) WriteBlobMSBLong(image,(unsigned int) length);
2615 (void) WriteBlob(image,length,compress_pixels);
2617 if (code == BZ_STREAM_END)
2620 code=BZ2_bzCompressEnd(&bzip_info);
2626 #if defined(MAGICKCORE_LZMA_DELEGATE)
2627 case LZMACompression:
2634 if (status == MagickFalse)
2636 lzma_info.next_out=compress_pixels;
2637 lzma_info.avail_out=packet_size*image->columns;
2638 code=lzma_code(&lzma_info,LZMA_FINISH);
2639 length=(size_t) (lzma_info.next_out-compress_pixels);
2642 (void) WriteBlobMSBLong(image,(unsigned int) length);
2643 (void) WriteBlob(image,length,compress_pixels);
2645 if (code == LZMA_STREAM_END)
2648 lzma_end(&lzma_info);
2652 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2653 case LZWCompression:
2654 case ZipCompression:
2661 if (status == MagickFalse)
2663 zip_info.next_out=compress_pixels;
2664 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2665 code=deflate(&zip_info,Z_FINISH);
2666 length=(size_t) (zip_info.next_out-compress_pixels);
2669 (void) WriteBlobMSBLong(image,(unsigned int) length);
2670 (void) WriteBlob(image,length,compress_pixels);
2672 if (code == Z_STREAM_END)
2675 code=deflateEnd(&zip_info);
2684 quantum_info=DestroyQuantumInfo(quantum_info);
2685 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2686 if (GetNextImageInList(image) == (Image *) NULL)
2688 image=SyncNextImageInList(image);
2689 status=SetImageProgress(image,SaveImagesTag,scene++,
2690 GetImageListLength(image));
2691 if (status == MagickFalse)
2693 } while (image_info->adjoin != MagickFalse);
2694 (void) CloseBlob(image);