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)
569 if ((size_t) (p-keyword) < (MaxTextExtent-1))
571 c=ReadBlobByte(image);
575 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
576 c=ReadBlobByte(image);
580 Get the keyword value.
582 c=ReadBlobByte(image);
583 while ((c != (int) '}') && (c != EOF))
585 if ((size_t) (p-options+1) >= length)
589 options=(char *) ResizeQuantumMemory(options,length+
590 MaxTextExtent,sizeof(*options));
591 if (options == (char *) NULL)
593 p=options+strlen(options);
595 if (options == (char *) NULL)
596 ThrowReaderException(ResourceLimitError,
597 "MemoryAllocationFailed");
599 c=ReadBlobByte(image);
602 c=ReadBlobByte(image);
606 c=ReadBlobByte(image);
610 if (isspace((int) ((unsigned char) c)) != 0)
615 (void) CopyMagickString(options,options+1,strlen(options));
617 Assign a value to the specified keyword.
624 if (LocaleCompare(keyword,"alpha-trait") == 0)
629 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
630 MagickFalse,options);
633 image->alpha_trait=(PixelTrait) alpha_trait;
636 (void) SetImageProperty(image,keyword,options,exception);
642 if (LocaleCompare(keyword,"background-color") == 0)
644 (void) QueryColorCompliance(options,AllCompliance,
645 &image->background_color,exception);
648 if (LocaleCompare(keyword,"blue-primary") == 0)
650 flags=ParseGeometry(options,&geometry_info);
651 image->chromaticity.blue_primary.x=geometry_info.rho;
652 image->chromaticity.blue_primary.y=geometry_info.sigma;
653 if ((flags & SigmaValue) == 0)
654 image->chromaticity.blue_primary.y=
655 image->chromaticity.blue_primary.x;
658 if (LocaleCompare(keyword,"border-color") == 0)
660 (void) QueryColorCompliance(options,AllCompliance,
661 &image->border_color,exception);
664 (void) SetImageProperty(image,keyword,options,exception);
670 if (LocaleCompare(keyword,"class") == 0)
675 storage_class=ParseCommandOption(MagickClassOptions,
676 MagickFalse,options);
677 if (storage_class < 0)
679 image->storage_class=(ClassType) storage_class;
682 if (LocaleCompare(keyword,"colors") == 0)
684 colors=StringToUnsignedLong(options);
687 if (LocaleCompare(keyword,"colorspace") == 0)
692 colorspace=ParseCommandOption(MagickColorspaceOptions,
693 MagickFalse,options);
696 image->colorspace=(ColorspaceType) colorspace;
699 if (LocaleCompare(keyword,"compression") == 0)
704 compression=ParseCommandOption(MagickCompressOptions,
705 MagickFalse,options);
708 image->compression=(CompressionType) compression;
711 if (LocaleCompare(keyword,"columns") == 0)
713 image->columns=StringToUnsignedLong(options);
716 (void) SetImageProperty(image,keyword,options,exception);
722 if (LocaleCompare(keyword,"delay") == 0)
724 image->delay=StringToUnsignedLong(options);
727 if (LocaleCompare(keyword,"depth") == 0)
729 image->depth=StringToUnsignedLong(options);
732 if (LocaleCompare(keyword,"dispose") == 0)
737 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
741 image->dispose=(DisposeType) dispose;
744 (void) SetImageProperty(image,keyword,options,exception);
750 if (LocaleCompare(keyword,"endian") == 0)
755 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
759 image->endian=(EndianType) endian;
762 (void) SetImageProperty(image,keyword,options,exception);
768 if (LocaleCompare(keyword,"gamma") == 0)
770 image->gamma=StringToDouble(options,(char **) NULL);
773 if (LocaleCompare(keyword,"gravity") == 0)
778 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
782 image->gravity=(GravityType) gravity;
785 if (LocaleCompare(keyword,"green-primary") == 0)
787 flags=ParseGeometry(options,&geometry_info);
788 image->chromaticity.green_primary.x=geometry_info.rho;
789 image->chromaticity.green_primary.y=geometry_info.sigma;
790 if ((flags & SigmaValue) == 0)
791 image->chromaticity.green_primary.y=
792 image->chromaticity.green_primary.x;
795 (void) SetImageProperty(image,keyword,options,exception);
801 if (LocaleCompare(keyword,"id") == 0)
803 (void) CopyMagickString(id,options,MaxTextExtent);
806 if (LocaleCompare(keyword,"iterations") == 0)
808 image->iterations=StringToUnsignedLong(options);
811 (void) SetImageProperty(image,keyword,options,exception);
817 if (LocaleCompare(keyword,"matte") == 0)
822 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
826 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
830 if (LocaleCompare(keyword,"matte-color") == 0)
832 (void) QueryColorCompliance(options,AllCompliance,
833 &image->matte_color,exception);
836 if (LocaleCompare(keyword,"montage") == 0)
838 (void) CloneString(&image->montage,options);
841 (void) SetImageProperty(image,keyword,options,exception);
847 if (LocaleCompare(keyword,"orientation") == 0)
852 orientation=ParseCommandOption(MagickOrientationOptions,
853 MagickFalse,options);
856 image->orientation=(OrientationType) orientation;
859 (void) SetImageProperty(image,keyword,options,exception);
865 if (LocaleCompare(keyword,"page") == 0)
870 geometry=GetPageGeometry(options);
871 (void) ParseAbsoluteGeometry(geometry,&image->page);
872 geometry=DestroyString(geometry);
875 if (LocaleCompare(keyword,"pixel-intensity") == 0)
880 intensity=ParseCommandOption(MagickPixelIntensityOptions,
881 MagickFalse,options);
884 image->intensity=(PixelIntensityMethod) intensity;
887 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
888 (LocaleNCompare(keyword,"profile-",8) == 0))
893 if (profiles == (LinkedListInfo *) NULL)
894 profiles=NewLinkedList(0);
895 (void) AppendValueToLinkedList(profiles,
896 AcquireString(keyword+8));
897 profile=BlobToStringInfo((const void *) NULL,(size_t)
898 StringToLong(options));
899 if (profile == (StringInfo *) NULL)
900 ThrowReaderException(ResourceLimitError,
901 "MemoryAllocationFailed");
902 (void) SetImageProfile(image,keyword+8,profile,exception);
903 profile=DestroyStringInfo(profile);
906 (void) SetImageProperty(image,keyword,options,exception);
912 if (LocaleCompare(keyword,"quality") == 0)
914 image->quality=StringToUnsignedLong(options);
917 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
918 (LocaleCompare(keyword,"quantum:format") == 0))
923 format=ParseCommandOption(MagickQuantumFormatOptions,
924 MagickFalse,options);
927 quantum_format=(QuantumFormatType) format;
930 (void) SetImageProperty(image,keyword,options,exception);
936 if (LocaleCompare(keyword,"red-primary") == 0)
938 flags=ParseGeometry(options,&geometry_info);
939 image->chromaticity.red_primary.x=geometry_info.rho;
940 image->chromaticity.red_primary.y=geometry_info.sigma;
941 if ((flags & SigmaValue) == 0)
942 image->chromaticity.red_primary.y=
943 image->chromaticity.red_primary.x;
946 if (LocaleCompare(keyword,"rendering-intent") == 0)
951 rendering_intent=ParseCommandOption(MagickIntentOptions,
952 MagickFalse,options);
953 if (rendering_intent < 0)
955 image->rendering_intent=(RenderingIntent) rendering_intent;
958 if (LocaleCompare(keyword,"resolution") == 0)
960 flags=ParseGeometry(options,&geometry_info);
961 image->resolution.x=geometry_info.rho;
962 image->resolution.y=geometry_info.sigma;
963 if ((flags & SigmaValue) == 0)
964 image->resolution.y=image->resolution.x;
967 if (LocaleCompare(keyword,"rows") == 0)
969 image->rows=StringToUnsignedLong(options);
972 (void) SetImageProperty(image,keyword,options,exception);
978 if (LocaleCompare(keyword,"scene") == 0)
980 image->scene=StringToUnsignedLong(options);
983 (void) SetImageProperty(image,keyword,options,exception);
989 if (LocaleCompare(keyword,"ticks-per-second") == 0)
991 image->ticks_per_second=(ssize_t) StringToLong(options);
994 if (LocaleCompare(keyword,"tile-offset") == 0)
999 geometry=GetPageGeometry(options);
1000 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1001 geometry=DestroyString(geometry);
1004 if (LocaleCompare(keyword,"type") == 0)
1009 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1013 image->type=(ImageType) type;
1016 (void) SetImageProperty(image,keyword,options,exception);
1022 if (LocaleCompare(keyword,"units") == 0)
1027 units=ParseCommandOption(MagickResolutionOptions,
1028 MagickFalse,options);
1031 image->units=(ResolutionType) units;
1034 (void) SetImageProperty(image,keyword,options,exception);
1040 if (LocaleCompare(keyword,"version") == 0)
1042 version=StringToDouble(options,(char **) NULL);
1045 (void) SetImageProperty(image,keyword,options,exception);
1051 if (LocaleCompare(keyword,"white-point") == 0)
1053 flags=ParseGeometry(options,&geometry_info);
1054 image->chromaticity.white_point.x=geometry_info.rho;
1055 image->chromaticity.white_point.y=geometry_info.sigma;
1056 if ((flags & SigmaValue) == 0)
1057 image->chromaticity.white_point.y=
1058 image->chromaticity.white_point.x;
1061 (void) SetImageProperty(image,keyword,options,exception);
1066 (void) SetImageProperty(image,keyword,options,exception);
1072 c=ReadBlobByte(image);
1073 while (isspace((int) ((unsigned char) c)) != 0)
1074 c=ReadBlobByte(image);
1076 options=DestroyString(options);
1077 (void) ReadBlobByte(image);
1079 Verify that required image information is defined.
1081 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1082 (image->storage_class == UndefinedClass) ||
1083 (image->columns == 0) || (image->rows == 0))
1084 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1085 if (image->montage != (char *) NULL)
1093 length=MaxTextExtent;
1094 image->directory=AcquireString((char *) NULL);
1099 if ((strlen(image->directory)+MaxTextExtent) >= length)
1102 Allocate more memory for the image directory.
1105 image->directory=(char *) ResizeQuantumMemory(image->directory,
1106 length+MaxTextExtent,sizeof(*image->directory));
1107 if (image->directory == (char *) NULL)
1108 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1109 p=image->directory+strlen(image->directory);
1111 c=ReadBlobByte(image);
1113 } while (c != (int) '\0');
1115 if (profiles != (LinkedListInfo *) NULL)
1124 Read image profiles.
1126 ResetLinkedListIterator(profiles);
1127 name=(const char *) GetNextValueInLinkedList(profiles);
1128 while (name != (const char *) NULL)
1130 profile=GetImageProfile(image,name);
1131 if (profile != (StringInfo *) NULL)
1133 register unsigned char
1136 p=GetStringInfoDatum(profile);
1137 count=ReadBlob(image,GetStringInfoLength(profile),p);
1140 name=(const char *) GetNextValueInLinkedList(profiles);
1142 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1144 image->depth=GetImageQuantumDepth(image,MagickFalse);
1145 if (image->storage_class == PseudoClass)
1148 Create image colormap.
1150 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1151 if (status == MagickFalse)
1152 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1162 Read image colormap from file.
1164 packet_size=(size_t) (3UL*image->depth/8UL);
1165 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1166 packet_size*sizeof(*colormap));
1167 if (colormap == (unsigned char *) NULL)
1168 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1169 count=ReadBlob(image,packet_size*image->colors,colormap);
1171 switch (image->depth)
1174 ThrowReaderException(CorruptImageError,
1175 "ImageDepthNotSupported");
1181 for (i=0; i < (ssize_t) image->colors; i++)
1183 p=PushCharPixel(p,&pixel);
1184 image->colormap[i].red=ScaleCharToQuantum(pixel);
1185 p=PushCharPixel(p,&pixel);
1186 image->colormap[i].green=ScaleCharToQuantum(pixel);
1187 p=PushCharPixel(p,&pixel);
1188 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1197 for (i=0; i < (ssize_t) image->colors; i++)
1199 p=PushShortPixel(MSBEndian,p,&pixel);
1200 image->colormap[i].red=ScaleShortToQuantum(pixel);
1201 p=PushShortPixel(MSBEndian,p,&pixel);
1202 image->colormap[i].green=ScaleShortToQuantum(pixel);
1203 p=PushShortPixel(MSBEndian,p,&pixel);
1204 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1213 for (i=0; i < (ssize_t) image->colors; i++)
1215 p=PushLongPixel(MSBEndian,p,&pixel);
1216 image->colormap[i].red=ScaleLongToQuantum(pixel);
1217 p=PushLongPixel(MSBEndian,p,&pixel);
1218 image->colormap[i].green=ScaleLongToQuantum(pixel);
1219 p=PushLongPixel(MSBEndian,p,&pixel);
1220 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1225 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1228 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1229 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1231 status=SetImageExtent(image,image->columns,image->rows,exception);
1232 if (status == MagickFalse)
1233 return(DestroyImageList(image));
1235 Allocate image pixels.
1237 quantum_info=AcquireQuantumInfo(image_info,image);
1238 if (quantum_info == (QuantumInfo *) NULL)
1239 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1240 if (quantum_format != UndefinedQuantumFormat)
1242 status=SetQuantumFormat(image,quantum_info,quantum_format);
1243 if (status == MagickFalse)
1244 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1246 packet_size=(size_t) (quantum_info->depth/8);
1247 if (image->storage_class == DirectClass)
1248 packet_size=(size_t) (3*quantum_info->depth/8);
1249 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1250 packet_size=quantum_info->depth/8;
1251 if (image->alpha_trait != UndefinedPixelTrait)
1252 packet_size+=quantum_info->depth/8;
1253 if (image->colorspace == CMYKColorspace)
1254 packet_size+=quantum_info->depth/8;
1255 if (image->compression == RLECompression)
1257 compress_extent=MagickMax(MagickMax(BZipMaxExtent(packet_size*
1258 image->columns),LZMAMaxExtent(packet_size*image->columns)),
1259 ZipMaxExtent(packet_size*image->columns));
1260 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_extent,
1261 sizeof(*compress_pixels));
1262 if (compress_pixels == (unsigned char *) NULL)
1263 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1267 quantum_type=RGBQuantum;
1268 if (image->alpha_trait != UndefinedPixelTrait)
1269 quantum_type=RGBAQuantum;
1270 if (image->colorspace == CMYKColorspace)
1272 quantum_type=CMYKQuantum;
1273 if (image->alpha_trait != UndefinedPixelTrait)
1274 quantum_type=CMYKAQuantum;
1276 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1278 quantum_type=GrayQuantum;
1279 if (image->alpha_trait != UndefinedPixelTrait)
1280 quantum_type=GrayAlphaQuantum;
1282 if (image->storage_class == PseudoClass)
1284 quantum_type=IndexQuantum;
1285 if (image->alpha_trait != UndefinedPixelTrait)
1286 quantum_type=IndexAlphaQuantum;
1289 GetPixelInfo(image,&pixel);
1290 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1291 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1293 #if defined(MAGICKCORE_LZMA_DELEGATE)
1294 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1296 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1297 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1299 switch (image->compression)
1301 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1302 case BZipCompression:
1307 bzip_info.bzalloc=AcquireBZIPMemory;
1308 bzip_info.bzfree=RelinquishBZIPMemory;
1309 bzip_info.opaque=(void *) NULL;
1310 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1317 #if defined(MAGICKCORE_LZMA_DELEGATE)
1318 case LZMACompression:
1323 allocator.alloc=AcquireLZMAMemory;
1324 allocator.free=RelinquishLZMAMemory;
1325 lzma_info=initialize_lzma;
1326 lzma_info.allocator=(&allocator);
1327 code=lzma_auto_decoder(&lzma_info,-1,0);
1328 if (code != LZMA_OK)
1333 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1334 case LZWCompression:
1335 case ZipCompression:
1340 zip_info.zalloc=AcquireZIPMemory;
1341 zip_info.zfree=RelinquishZIPMemory;
1342 zip_info.opaque=(voidpf) NULL;
1343 code=inflateInit(&zip_info);
1349 case RLECompression:
1354 pixels=GetQuantumPixels(quantum_info);
1356 for (y=0; y < (ssize_t) image->rows; y++)
1364 if (status == MagickFalse)
1366 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1367 if (q == (Quantum *) NULL)
1369 switch (image->compression)
1371 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1372 case BZipCompression:
1374 bzip_info.next_out=(char *) pixels;
1375 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1378 if (bzip_info.avail_in == 0)
1380 bzip_info.next_in=(char *) compress_pixels;
1381 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1383 length=(size_t) ReadBlobMSBLong(image);
1384 if (length > compress_extent)
1385 ThrowReaderException(CorruptImageError,
1386 "UnableToReadImageData");
1387 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1388 (unsigned char *) bzip_info.next_in);
1390 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1392 } while (bzip_info.avail_out != 0);
1393 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1394 quantum_type,pixels,exception);
1398 #if defined(MAGICKCORE_LZMA_DELEGATE)
1399 case LZMACompression:
1401 lzma_info.next_out=pixels;
1402 lzma_info.avail_out=packet_size*image->columns;
1408 if (lzma_info.avail_in == 0)
1410 lzma_info.next_in=compress_pixels;
1411 length=(size_t) ReadBlobMSBLong(image);
1412 if (length > compress_extent)
1413 ThrowReaderException(CorruptImageError,
1414 "UnableToReadImageData");
1415 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1416 (unsigned char *) lzma_info.next_in);
1418 code=lzma_code(&lzma_info,LZMA_RUN);
1424 if (code == LZMA_STREAM_END)
1426 } while (lzma_info.avail_out != 0);
1427 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1428 quantum_type,pixels,exception);
1432 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1433 case LZWCompression:
1434 case ZipCompression:
1436 zip_info.next_out=pixels;
1437 zip_info.avail_out=(uInt) (packet_size*image->columns);
1440 if (zip_info.avail_in == 0)
1442 zip_info.next_in=compress_pixels;
1443 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1445 length=(size_t) ReadBlobMSBLong(image);
1446 if (length > compress_extent)
1447 ThrowReaderException(CorruptImageError,
1448 "UnableToReadImageData");
1449 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1452 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1454 } while (zip_info.avail_out != 0);
1455 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1456 quantum_type,pixels,exception);
1460 case RLECompression:
1462 for (x=0; x < (ssize_t) image->columns; x++)
1466 count=ReadBlob(image,packet_size,pixels);
1467 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1470 if (image->storage_class == PseudoClass)
1471 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1474 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1475 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1476 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1477 if (image->colorspace == CMYKColorspace)
1478 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1480 if (image->alpha_trait != UndefinedPixelTrait)
1481 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1482 q+=GetPixelChannels(image);
1488 count=ReadBlob(image,packet_size*image->columns,pixels);
1489 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1490 quantum_type,pixels,exception);
1494 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1497 SetQuantumImageType(image,quantum_type);
1498 switch (image->compression)
1500 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1501 case BZipCompression:
1511 offset=SeekBlob(image,-((MagickOffsetType)
1512 bzip_info.avail_in),SEEK_CUR);
1514 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1516 code=BZ2_bzDecompressEnd(&bzip_info);
1522 #if defined(MAGICKCORE_LZMA_DELEGATE)
1523 case LZMACompression:
1528 code=lzma_code(&lzma_info,LZMA_FINISH);
1529 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1531 lzma_end(&lzma_info);
1535 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1536 case LZWCompression:
1537 case ZipCompression:
1547 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1550 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1552 code=inflateEnd(&zip_info);
1553 if (code != LZMA_OK)
1561 quantum_info=DestroyQuantumInfo(quantum_info);
1562 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1563 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1565 image=DestroyImageList(image);
1566 return((Image *) NULL);
1568 if (EOFBlob(image) != MagickFalse)
1570 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1575 Proceed to next image.
1577 if (image_info->number_scenes != 0)
1578 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1582 c=ReadBlobByte(image);
1583 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1587 Allocate next image structure.
1589 AcquireNextImage(image_info,image,exception);
1590 if (GetNextImageInList(image) == (Image *) NULL)
1592 image=DestroyImageList(image);
1593 return((Image *) NULL);
1595 image=SyncNextImageInList(image);
1596 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1597 GetBlobSize(image));
1598 if (status == MagickFalse)
1602 (void) CloseBlob(image);
1603 return(GetFirstImageInList(image));
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 % R e g i s t e r M I F F I m a g e %
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1618 % supported formats. The properties include the image format tag, a method to
1619 % read and/or write the format, whether the format supports the saving of more
1620 % than one frame to the same file or blob, whether the format supports native
1621 % in-memory I/O, and a brief description of the format.
1623 % The format of the RegisterMIFFImage method is:
1625 % size_t RegisterMIFFImage(void)
1628 ModuleExport size_t RegisterMIFFImage(void)
1631 version[MaxTextExtent];
1637 #if defined(MagickImageCoderSignatureText)
1638 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1639 #if defined(ZLIB_VERSION)
1640 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1641 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1643 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1644 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1647 entry=SetMagickInfo("MIFF");
1648 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1649 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1650 entry->magick=(IsImageFormatHandler *) IsMIFF;
1651 entry->seekable_stream=MagickTrue;
1652 entry->description=ConstantString("Magick Image File Format");
1653 if (*version != '\0')
1654 entry->version=ConstantString(version);
1655 entry->module=ConstantString("MIFF");
1656 (void) RegisterMagickInfo(entry);
1657 return(MagickImageCoderSignature);
1661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1665 % U n r e g i s t e r M I F F I m a g e %
1669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1671 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1672 % from the list of supported formats.
1674 % The format of the UnregisterMIFFImage method is:
1676 % UnregisterMIFFImage(void)
1679 ModuleExport void UnregisterMIFFImage(void)
1681 (void) UnregisterMagickInfo("MIFF");
1685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1689 % W r i t e M I F F I m a g e %
1693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1695 % WriteMIFFImage() writes a MIFF image to a file.
1697 % The format of the WriteMIFFImage method is:
1699 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1700 % Image *image,ExceptionInfo *exception)
1702 % Compression code contributed by Kyle Shorter.
1704 % A description of each parameter follows:
1706 % o image_info: the image info.
1708 % o image: the image.
1710 % o exception: return any errors or warnings in this structure.
1714 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1715 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1717 if (image->storage_class != DirectClass)
1722 value=(unsigned int) ClampToQuantum(pixel->index);
1723 switch (image->depth)
1727 *pixels++=(unsigned char) (value >> 24);
1728 *pixels++=(unsigned char) (value >> 16);
1731 *pixels++=(unsigned char) (value >> 8);
1734 *pixels++=(unsigned char) value;
1738 (void) ThrowMagickException(exception,GetMagickModule(),
1739 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1741 switch (image->depth)
1748 if (image->alpha_trait != UndefinedPixelTrait)
1750 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1751 pixels=PopLongPixel(MSBEndian,value,pixels);
1760 if (image->alpha_trait != UndefinedPixelTrait)
1762 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1763 pixels=PopShortPixel(MSBEndian,value,pixels);
1772 if (image->alpha_trait != UndefinedPixelTrait)
1774 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1776 pixels=PopCharPixel(value,pixels);
1781 (void) ThrowMagickException(exception,GetMagickModule(),
1782 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1784 *pixels++=(unsigned char) length;
1787 switch (image->depth)
1794 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1795 pixels=PopLongPixel(MSBEndian,value,pixels);
1796 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1798 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1799 pixels=PopLongPixel(MSBEndian,value,pixels);
1800 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1801 pixels=PopLongPixel(MSBEndian,value,pixels);
1803 if (image->colorspace == CMYKColorspace)
1805 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1806 pixels=PopLongPixel(MSBEndian,value,pixels);
1808 if (image->alpha_trait != UndefinedPixelTrait)
1810 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1811 pixels=PopLongPixel(MSBEndian,value,pixels);
1820 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1821 pixels=PopShortPixel(MSBEndian,value,pixels);
1822 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1824 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1825 pixels=PopShortPixel(MSBEndian,value,pixels);
1826 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1827 pixels=PopShortPixel(MSBEndian,value,pixels);
1829 if (image->colorspace == CMYKColorspace)
1831 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1832 pixels=PopShortPixel(MSBEndian,value,pixels);
1834 if (image->alpha_trait != UndefinedPixelTrait)
1836 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1837 pixels=PopShortPixel(MSBEndian,value,pixels);
1846 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1847 pixels=PopCharPixel(value,pixels);
1848 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1850 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1852 pixels=PopCharPixel(value,pixels);
1853 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1854 pixels=PopCharPixel(value,pixels);
1856 if (image->colorspace == CMYKColorspace)
1858 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1860 pixels=PopCharPixel(value,pixels);
1862 if (image->alpha_trait != UndefinedPixelTrait)
1864 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1866 pixels=PopCharPixel(value,pixels);
1871 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1872 "ImageDepthNotSupported","`%s'",image->filename);
1874 *pixels++=(unsigned char) length;
1878 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1879 Image *image,ExceptionInfo *exception)
1881 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1887 buffer[MaxTextExtent];
1896 #if defined(MAGICKCORE_LZMA_DELEGATE)
1901 initialize_lzma = LZMA_STREAM_INIT,
1936 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1942 Open output image file.
1944 assert(image_info != (const ImageInfo *) NULL);
1945 assert(image_info->signature == MagickSignature);
1946 assert(image != (Image *) NULL);
1947 assert(image->signature == MagickSignature);
1948 if (image->debug != MagickFalse)
1949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1950 assert(exception != (ExceptionInfo *) NULL);
1951 assert(exception->signature == MagickSignature);
1952 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1953 if (status == MagickFalse)
1959 Allocate image pixels.
1961 if ((image->storage_class == PseudoClass) &&
1962 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1963 (void) SetImageStorageClass(image,DirectClass,exception);
1964 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1965 image->depth <= 32 ? 32UL : 64UL;
1966 if (IsImageGray(image,exception) == MagickFalse)
1969 sRGB masquerading as a grayscale image?
1971 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1972 (void) SetImageColorspace(image,sRGBColorspace,exception);
1975 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1976 (void) SetImageColorspace(image,GRAYColorspace,exception);
1977 quantum_info=AcquireQuantumInfo(image_info,image);
1978 if (quantum_info == (QuantumInfo *) NULL)
1979 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1980 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1981 (quantum_info->format == UndefinedQuantumFormat) &&
1982 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1984 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1985 if (status == MagickFalse)
1986 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1988 compression=UndefinedCompression;
1989 if (image_info->compression != UndefinedCompression)
1990 compression=image_info->compression;
1991 switch (compression)
1993 #if !defined(MAGICKCORE_LZMA_DELEGATE)
1994 case LZMACompression: compression=NoCompression; break;
1996 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1997 case LZWCompression:
1998 case ZipCompression: compression=NoCompression; break;
2000 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
2001 case BZipCompression: compression=NoCompression; break;
2003 case RLECompression:
2005 if (quantum_info->format == FloatingPointQuantumFormat)
2006 compression=NoCompression;
2007 GetPixelInfo(image,&target);
2013 packet_size=(size_t) (quantum_info->depth/8);
2014 if (image->storage_class == DirectClass)
2015 packet_size=(size_t) (3*quantum_info->depth/8);
2016 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2017 packet_size=(size_t) (quantum_info->depth/8);
2018 if (image->alpha_trait != UndefinedPixelTrait)
2019 packet_size+=quantum_info->depth/8;
2020 if (image->colorspace == CMYKColorspace)
2021 packet_size+=quantum_info->depth/8;
2022 if (compression == RLECompression)
2024 length=image->columns;
2025 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2026 packet_size*image->columns));
2027 if ((compression == BZipCompression) || (compression == ZipCompression))
2028 if (length != (size_t) ((unsigned int) length))
2029 compression=NoCompression;
2030 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2031 sizeof(*compress_pixels));
2032 if (compress_pixels == (unsigned char *) NULL)
2033 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2037 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2038 (void) FormatLocaleString(buffer,MaxTextExtent,
2039 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2040 MagickClassOptions,image->storage_class),(double) image->colors,
2041 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2042 image->alpha_trait));
2043 (void) WriteBlobString(image,buffer);
2044 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2045 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2047 (void) WriteBlobString(image,buffer);
2048 if (image->type != UndefinedType)
2050 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2051 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2052 (void) WriteBlobString(image,buffer);
2054 if (image->colorspace != UndefinedColorspace)
2056 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2057 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2058 (void) WriteBlobString(image,buffer);
2060 if (image->intensity != UndefinedPixelIntensityMethod)
2062 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
2063 CommandOptionToMnemonic(MagickPixelIntensityOptions,
2065 (void) WriteBlobString(image,buffer);
2067 if (image->endian != UndefinedEndian)
2069 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
2070 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2071 (void) WriteBlobString(image,buffer);
2073 if (compression != UndefinedCompression)
2075 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2076 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2077 compression),(double) image->quality);
2078 (void) WriteBlobString(image,buffer);
2080 if (image->units != UndefinedResolution)
2082 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2083 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2084 (void) WriteBlobString(image,buffer);
2086 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2088 (void) FormatLocaleString(buffer,MaxTextExtent,
2089 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2090 (void) WriteBlobString(image,buffer);
2092 if ((image->page.width != 0) || (image->page.height != 0))
2094 (void) FormatLocaleString(buffer,MaxTextExtent,
2095 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2096 image->page.height,(double) image->page.x,(double) image->page.y);
2097 (void) WriteBlobString(image,buffer);
2100 if ((image->page.x != 0) || (image->page.y != 0))
2102 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2103 (long) image->page.x,(long) image->page.y);
2104 (void) WriteBlobString(image,buffer);
2106 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2108 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2109 (long) image->tile_offset.x,(long) image->tile_offset.y);
2110 (void) WriteBlobString(image,buffer);
2112 if ((GetNextImageInList(image) != (Image *) NULL) ||
2113 (GetPreviousImageInList(image) != (Image *) NULL))
2115 if (image->scene == 0)
2116 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2117 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2118 (double) image->delay,(double) image->ticks_per_second);
2120 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2121 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2122 image->scene,(double) image->iterations,(double) image->delay,
2123 (double) image->ticks_per_second);
2124 (void) WriteBlobString(image,buffer);
2128 if (image->scene != 0)
2130 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2131 (double) image->scene);
2132 (void) WriteBlobString(image,buffer);
2134 if (image->iterations != 0)
2136 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2137 (double) image->iterations);
2138 (void) WriteBlobString(image,buffer);
2140 if (image->delay != 0)
2142 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2143 (double) image->delay);
2144 (void) WriteBlobString(image,buffer);
2146 if (image->ticks_per_second != UndefinedTicksPerSecond)
2148 (void) FormatLocaleString(buffer,MaxTextExtent,
2149 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2150 (void) WriteBlobString(image,buffer);
2153 if (image->gravity != UndefinedGravity)
2155 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2156 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2157 (void) WriteBlobString(image,buffer);
2159 if (image->dispose != UndefinedDispose)
2161 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2162 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2163 (void) WriteBlobString(image,buffer);
2165 if (image->rendering_intent != UndefinedIntent)
2167 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2168 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2169 (void) WriteBlobString(image,buffer);
2171 if (image->gamma != 0.0)
2173 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2175 (void) WriteBlobString(image,buffer);
2177 if (image->chromaticity.white_point.x != 0.0)
2180 Note chomaticity points.
2182 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2183 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2184 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2185 image->chromaticity.green_primary.x,
2186 image->chromaticity.green_primary.y,
2187 image->chromaticity.blue_primary.x,
2188 image->chromaticity.blue_primary.y);
2189 (void) WriteBlobString(image,buffer);
2190 (void) FormatLocaleString(buffer,MaxTextExtent,
2191 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2192 image->chromaticity.white_point.y);
2193 (void) WriteBlobString(image,buffer);
2195 if (image->orientation != UndefinedOrientation)
2197 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2198 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2199 (void) WriteBlobString(image,buffer);
2201 if (image->profiles != (void *) NULL)
2210 Write image profiles.
2212 ResetImageProfileIterator(image);
2213 name=GetNextImageProfile(image);
2214 while (name != (const char *) NULL)
2216 profile=GetImageProfile(image,name);
2217 if (profile != (StringInfo *) NULL)
2219 (void) FormatLocaleString(buffer,MaxTextExtent,
2220 "profile:%s=%.20g\n",name,(double)
2221 GetStringInfoLength(profile));
2222 (void) WriteBlobString(image,buffer);
2224 name=GetNextImageProfile(image);
2227 if (image->montage != (char *) NULL)
2229 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2231 (void) WriteBlobString(image,buffer);
2233 if (quantum_info->format == FloatingPointQuantumFormat)
2234 (void) SetImageProperty(image,"quantum:format","floating-point",
2236 ResetImagePropertyIterator(image);
2237 property=GetNextImageProperty(image);
2238 while (property != (const char *) NULL)
2240 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2241 (void) WriteBlobString(image,buffer);
2242 value=GetImageProperty(image,property,exception);
2243 if (value != (const char *) NULL)
2248 length=strlen(value);
2249 for (i=0; i < (ssize_t) length; i++)
2250 if (isspace((int) ((unsigned char) value[i])) != 0)
2252 if ((i == (ssize_t) length) && (i != 0))
2253 (void) WriteBlob(image,length,(const unsigned char *) value);
2256 (void) WriteBlobByte(image,'{');
2257 if (strchr(value,'}') == (char *) NULL)
2258 (void) WriteBlob(image,length,(const unsigned char *) value);
2260 for (i=0; i < (ssize_t) length; i++)
2262 if (value[i] == (int) '}')
2263 (void) WriteBlobByte(image,'\\');
2264 (void) WriteBlobByte(image,value[i]);
2266 (void) WriteBlobByte(image,'}');
2269 (void) WriteBlobByte(image,'\n');
2270 property=GetNextImageProperty(image);
2272 (void) WriteBlobString(image,"\f\n:\032");
2273 if (image->montage != (char *) NULL)
2276 Write montage tile directory.
2278 if (image->directory != (char *) NULL)
2279 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2281 (void) WriteBlobByte(image,'\0');
2283 if (image->profiles != (void *) NULL)
2294 ResetImageProfileIterator(image);
2295 name=GetNextImageProfile(image);
2296 while (name != (const char *) NULL)
2298 profile=GetImageProfile(image,name);
2299 (void) WriteBlob(image,GetStringInfoLength(profile),
2300 GetStringInfoDatum(profile));
2301 name=GetNextImageProfile(image);
2304 if (image->storage_class == PseudoClass)
2316 packet_size=(size_t) (3*quantum_info->depth/8);
2317 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2318 packet_size*sizeof(*colormap));
2319 if (colormap == (unsigned char *) NULL)
2320 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2322 Write colormap to file.
2325 for (i=0; i < (ssize_t) image->colors; i++)
2327 switch (quantum_info->depth)
2330 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2333 register unsigned int
2336 pixel=ScaleQuantumToLong(image->colormap[i].red);
2337 q=PopLongPixel(MSBEndian,pixel,q);
2338 pixel=ScaleQuantumToLong(image->colormap[i].green);
2339 q=PopLongPixel(MSBEndian,pixel,q);
2340 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2341 q=PopLongPixel(MSBEndian,pixel,q);
2346 register unsigned short
2349 pixel=ScaleQuantumToShort(image->colormap[i].red);
2350 q=PopShortPixel(MSBEndian,pixel,q);
2351 pixel=ScaleQuantumToShort(image->colormap[i].green);
2352 q=PopShortPixel(MSBEndian,pixel,q);
2353 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2354 q=PopShortPixel(MSBEndian,pixel,q);
2359 register unsigned char
2362 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2363 q=PopCharPixel(pixel,q);
2364 pixel=(unsigned char) ScaleQuantumToChar(
2365 image->colormap[i].green);
2366 q=PopCharPixel(pixel,q);
2367 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2368 q=PopCharPixel(pixel,q);
2373 (void) WriteBlob(image,packet_size*image->colors,colormap);
2374 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2377 Write image pixels to file.
2380 switch (compression)
2382 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2383 case BZipCompression:
2388 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2389 bzip_info.bzalloc=AcquireBZIPMemory;
2390 bzip_info.bzfree=RelinquishBZIPMemory;
2391 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2392 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2393 9)),(int) image_info->verbose,0);
2399 #if defined(MAGICKCORE_LZMA_DELEGATE)
2400 case LZMACompression:
2405 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2406 allocator.alloc=AcquireLZMAMemory;
2407 allocator.free=RelinquishLZMAMemory;
2408 lzma_info=initialize_lzma;
2409 lzma_info.allocator=&allocator;
2410 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2411 if (code != LZMA_OK)
2416 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2417 case LZWCompression:
2418 case ZipCompression:
2423 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2424 zip_info.zalloc=AcquireZIPMemory;
2425 zip_info.zfree=RelinquishZIPMemory;
2426 code=deflateInit(&zip_info,(int) (image->quality ==
2427 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2436 quantum_type=GetQuantumType(image,exception);
2437 pixels=GetQuantumPixels(quantum_info);
2438 for (y=0; y < (ssize_t) image->rows; y++)
2440 register const Quantum
2446 if (status == MagickFalse)
2448 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2449 if (p == (const Quantum *) NULL)
2452 switch (compression)
2454 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2455 case BZipCompression:
2457 bzip_info.next_in=(char *) pixels;
2458 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2459 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2460 quantum_type,pixels,exception);
2466 bzip_info.next_out=(char *) compress_pixels;
2467 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2469 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2472 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2475 (void) WriteBlobMSBLong(image,(unsigned int) length);
2476 (void) WriteBlob(image,length,compress_pixels);
2478 } while (bzip_info.avail_in != 0);
2482 #if defined(MAGICKCORE_LZMA_DELEGATE)
2483 case LZMACompression:
2485 lzma_info.next_in=pixels;
2486 lzma_info.avail_in=packet_size*image->columns;
2487 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2488 quantum_type,pixels,exception);
2494 lzma_info.next_out=compress_pixels;
2495 lzma_info.avail_out=packet_size*image->columns;
2496 code=lzma_code(&lzma_info,LZMA_RUN);
2497 if (code != LZMA_OK)
2499 length=(size_t) (lzma_info.next_out-compress_pixels);
2502 (void) WriteBlobMSBLong(image,(unsigned int) length);
2503 (void) WriteBlob(image,length,compress_pixels);
2505 } while (lzma_info.avail_in != 0);
2509 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2510 case LZWCompression:
2511 case ZipCompression:
2513 zip_info.next_in=pixels;
2514 zip_info.avail_in=(uInt) (packet_size*image->columns);
2515 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2516 quantum_type,pixels,exception);
2522 zip_info.next_out=compress_pixels;
2523 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2524 code=deflate(&zip_info,Z_SYNC_FLUSH);
2527 length=(size_t) (zip_info.next_out-compress_pixels);
2530 (void) WriteBlobMSBLong(image,(unsigned int) length);
2531 (void) WriteBlob(image,length,compress_pixels);
2533 } while (zip_info.avail_in != 0);
2537 case RLECompression:
2540 GetPixelInfoPixel(image,p,&pixel);
2541 p+=GetPixelChannels(image);
2542 for (x=1; x < (ssize_t) image->columns; x++)
2544 GetPixelInfoPixel(image,p,&target);
2545 if ((length < 255) &&
2546 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2550 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2553 GetPixelInfoPixel(image,p,&pixel);
2554 p+=GetPixelChannels(image);
2556 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2557 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2562 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2563 quantum_type,pixels,exception);
2564 (void) WriteBlob(image,packet_size*image->columns,pixels);
2568 if (image->previous == (Image *) NULL)
2570 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2572 if (status == MagickFalse)
2576 switch (compression)
2578 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2579 case BZipCompression:
2586 if (status == MagickFalse)
2588 bzip_info.next_out=(char *) compress_pixels;
2589 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2591 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2592 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2595 (void) WriteBlobMSBLong(image,(unsigned int) length);
2596 (void) WriteBlob(image,length,compress_pixels);
2598 if (code == BZ_STREAM_END)
2601 code=BZ2_bzCompressEnd(&bzip_info);
2607 #if defined(MAGICKCORE_LZMA_DELEGATE)
2608 case LZMACompression:
2615 if (status == MagickFalse)
2617 lzma_info.next_out=compress_pixels;
2618 lzma_info.avail_out=packet_size*image->columns;
2619 code=lzma_code(&lzma_info,LZMA_FINISH);
2620 length=(size_t) (lzma_info.next_out-compress_pixels);
2623 (void) WriteBlobMSBLong(image,(unsigned int) length);
2624 (void) WriteBlob(image,length,compress_pixels);
2626 if (code == LZMA_STREAM_END)
2629 lzma_end(&lzma_info);
2633 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2634 case LZWCompression:
2635 case ZipCompression:
2642 if (status == MagickFalse)
2644 zip_info.next_out=compress_pixels;
2645 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2646 code=deflate(&zip_info,Z_FINISH);
2647 length=(size_t) (zip_info.next_out-compress_pixels);
2650 (void) WriteBlobMSBLong(image,(unsigned int) length);
2651 (void) WriteBlob(image,length,compress_pixels);
2653 if (code == Z_STREAM_END)
2656 code=deflateEnd(&zip_info);
2665 quantum_info=DestroyQuantumInfo(quantum_info);
2666 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2667 if (GetNextImageInList(image) == (Image *) NULL)
2669 image=SyncNextImageInList(image);
2670 status=SetImageProgress(image,SaveImagesTag,scene++,
2671 GetImageListLength(image));
2672 if (status == MagickFalse)
2674 } while (image_info->adjoin != MagickFalse);
2675 (void) CloseBlob(image);