2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/hashmap.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/profile.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/static.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #if defined(MAGICKCORE_BZLIB_DELEGATE)
79 #if defined(MAGICKCORE_LZMA_DELEGATE)
82 #if defined(MAGICKCORE_ZLIB_DELEGATE)
96 static MagickBooleanType
97 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % IsMIFF() returns MagickTrue if the image format type, identified by the
111 % magick string, is MIFF.
113 % The format of the IsMIFF method is:
115 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
117 % A description of each parameter follows:
119 % o magick: compare image format pattern against these bytes.
121 % o length: Specifies the length of the magick string.
124 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
128 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % R e a d M I F F I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
145 % memory necessary for the new Image structure and returns a pointer to the
148 % The format of the ReadMIFFImage method is:
150 % Image *ReadMIFFImage(const ImageInfo *image_info,
151 % ExceptionInfo *exception)
153 % Decompression code contributed by Kyle Shorter.
155 % A description of each parameter follows:
157 % o image_info: the image info.
159 % o exception: return any errors or warnings in this structure.
163 #if defined(MAGICKCORE_BZLIB_DELEGATE)
164 static void *AcquireBZIPMemory(void *context,int items,int size)
167 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
171 #if defined(MAGICKCORE_LZMA_DELEGATE)
172 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
175 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
179 #if defined(MAGICKCORE_ZLIB_DELEGATE)
180 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
184 return((voidpf) AcquireQuantumMemory(items,size));
188 static inline size_t MagickMax(const size_t x,const size_t y)
195 static inline size_t MagickMin(const size_t x,const size_t y)
202 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
203 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
209 if (image->storage_class == PseudoClass)
212 switch (image->depth)
216 pixel->index=ConstrainColormapIndex(image,
217 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3),exception);
223 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
230 pixel->index=ConstrainColormapIndex(image,*p,exception);
235 (void) ThrowMagickException(exception,GetMagickModule(),
236 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
238 switch (image->depth)
245 if (image->alpha_trait == BlendPixelTrait)
247 p=PushCharPixel(p,&quantum);
248 pixel->alpha=ScaleCharToQuantum(quantum);
257 if (image->alpha_trait == BlendPixelTrait)
259 p=PushShortPixel(MSBEndian,p,&quantum);
260 pixel->alpha=(Quantum) (quantum >> (image->depth-
261 MAGICKCORE_QUANTUM_DEPTH));
270 if (image->alpha_trait == BlendPixelTrait)
272 p=PushLongPixel(MSBEndian,p,&quantum);
273 pixel->alpha=(Quantum) (quantum >> (image->depth-
274 MAGICKCORE_QUANTUM_DEPTH));
279 (void) ThrowMagickException(exception,GetMagickModule(),
280 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
282 *length=(size_t) (*p++)+1;
285 switch (image->depth)
292 p=PushCharPixel(p,&quantum);
293 pixel->red=ScaleCharToQuantum(quantum);
294 pixel->green=pixel->red;
295 pixel->blue=pixel->red;
296 if (IsGrayColorspace(image->colorspace) == MagickFalse)
298 p=PushCharPixel(p,&quantum);
299 pixel->green=ScaleCharToQuantum(quantum);
300 p=PushCharPixel(p,&quantum);
301 pixel->blue=ScaleCharToQuantum(quantum);
303 if (image->colorspace == CMYKColorspace)
305 p=PushCharPixel(p,&quantum);
306 pixel->black=ScaleCharToQuantum(quantum);
308 if (image->alpha_trait == BlendPixelTrait)
310 p=PushCharPixel(p,&quantum);
311 pixel->alpha=ScaleCharToQuantum(quantum);
320 p=PushShortPixel(MSBEndian,p,&quantum);
321 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
322 pixel->green=pixel->red;
323 pixel->blue=pixel->red;
324 if (IsGrayColorspace(image->colorspace) == MagickFalse)
326 p=PushShortPixel(MSBEndian,p,&quantum);
327 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
328 p=PushShortPixel(MSBEndian,p,&quantum);
329 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
331 if (image->colorspace == CMYKColorspace)
333 p=PushShortPixel(MSBEndian,p,&quantum);
334 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
336 if (image->alpha_trait == BlendPixelTrait)
338 p=PushShortPixel(MSBEndian,p,&quantum);
339 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
348 p=PushLongPixel(MSBEndian,p,&quantum);
349 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
350 pixel->green=pixel->red;
351 pixel->blue=pixel->red;
352 if (IsGrayColorspace(image->colorspace) == MagickFalse)
354 p=PushLongPixel(MSBEndian,p,&quantum);
355 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
356 p=PushLongPixel(MSBEndian,p,&quantum);
357 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
359 if (image->colorspace == CMYKColorspace)
361 p=PushLongPixel(MSBEndian,p,&quantum);
362 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
364 if (image->alpha_trait == BlendPixelTrait)
366 p=PushLongPixel(MSBEndian,p,&quantum);
367 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
372 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
373 "ImageDepthNotSupported","`%s'",image->filename);
375 *length=(size_t) (*p++)+1;
378 #if defined(MAGICKCORE_BZLIB_DELEGATE)
379 static void RelinquishBZIPMemory(void *context,void *memory)
382 memory=RelinquishMagickMemory(memory);
386 #if defined(MAGICKCORE_LZMA_DELEGATE)
387 static void RelinquishLZMAMemory(void *context,void *memory)
390 memory=RelinquishMagickMemory(memory);
394 #if defined(MAGICKCORE_ZLIB_DELEGATE)
395 static void RelinquishZIPMemory(voidpf context,voidpf memory)
398 memory=RelinquishMagickMemory(memory);
402 static Image *ReadMIFFImage(const ImageInfo *image_info,
403 ExceptionInfo *exception)
405 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
406 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
407 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
409 #if defined(MAGICKCORE_BZLIB_DELEGATE)
416 keyword[MaxTextExtent],
437 #if defined(MAGICKCORE_LZMA_DELEGATE)
439 initialize_lzma = LZMA_STREAM_INIT,
484 #if defined(MAGICKCORE_ZLIB_DELEGATE)
492 assert(image_info != (const ImageInfo *) NULL);
493 assert(image_info->signature == MagickSignature);
494 if (image_info->debug != MagickFalse)
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
496 image_info->filename);
497 assert(exception != (ExceptionInfo *) NULL);
498 assert(exception->signature == MagickSignature);
499 image=AcquireImage(image_info,exception);
500 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
501 if (status == MagickFalse)
503 image=DestroyImageList(image);
504 return((Image *) NULL);
507 Decode image header; header terminates one character beyond a ':'.
509 c=ReadBlobByte(image);
511 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
513 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
519 Decode image header; header terminates one character beyond a ':'.
521 length=MaxTextExtent;
522 options=AcquireString((char *) NULL);
523 quantum_format=UndefinedQuantumFormat;
524 profiles=(LinkedListInfo *) NULL;
527 image->compression=NoCompression;
528 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
539 Read comment-- any text between { }.
541 length=MaxTextExtent;
542 comment=AcquireString((char *) NULL);
543 for (p=comment; comment != (char *) NULL; p++)
545 c=ReadBlobByte(image);
547 c=ReadBlobByte(image);
549 if ((c == EOF) || (c == (int) '}'))
551 if ((size_t) (p-comment+1) >= length)
555 comment=(char *) ResizeQuantumMemory(comment,length+
556 MaxTextExtent,sizeof(*comment));
557 if (comment == (char *) NULL)
559 p=comment+strlen(comment);
563 if (comment == (char *) NULL)
564 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
566 (void) SetImageProperty(image,"comment",comment,exception);
567 comment=DestroyString(comment);
568 c=ReadBlobByte(image);
571 if (isalnum(c) != MagickFalse)
581 if ((size_t) (p-keyword) < (MaxTextExtent-1))
583 c=ReadBlobByte(image);
587 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
588 c=ReadBlobByte(image);
592 Get the keyword value.
594 c=ReadBlobByte(image);
595 while ((c != (int) '}') && (c != EOF))
597 if ((size_t) (p-options+1) >= length)
601 options=(char *) ResizeQuantumMemory(options,length+
602 MaxTextExtent,sizeof(*options));
603 if (options == (char *) NULL)
605 p=options+strlen(options);
607 if (options == (char *) NULL)
608 ThrowReaderException(ResourceLimitError,
609 "MemoryAllocationFailed");
611 c=ReadBlobByte(image);
614 c=ReadBlobByte(image);
618 c=ReadBlobByte(image);
622 if (isspace((int) ((unsigned char) c)) != 0)
627 (void) CopyMagickString(options,options+1,strlen(options));
629 Assign a value to the specified keyword.
636 if (LocaleCompare(keyword,"alpha-trait") == 0)
641 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
642 MagickFalse,options);
645 image->alpha_trait=(PixelTrait) alpha_trait;
648 (void) SetImageProperty(image,keyword,options,exception);
654 if (LocaleCompare(keyword,"background-color") == 0)
656 (void) QueryColorCompliance(options,AllCompliance,
657 &image->background_color,exception);
660 if (LocaleCompare(keyword,"blue-primary") == 0)
662 flags=ParseGeometry(options,&geometry_info);
663 image->chromaticity.blue_primary.x=geometry_info.rho;
664 image->chromaticity.blue_primary.y=geometry_info.sigma;
665 if ((flags & SigmaValue) == 0)
666 image->chromaticity.blue_primary.y=
667 image->chromaticity.blue_primary.x;
670 if (LocaleCompare(keyword,"border-color") == 0)
672 (void) QueryColorCompliance(options,AllCompliance,
673 &image->border_color,exception);
676 (void) SetImageProperty(image,keyword,options,exception);
682 if (LocaleCompare(keyword,"class") == 0)
687 storage_class=ParseCommandOption(MagickClassOptions,
688 MagickFalse,options);
689 if (storage_class < 0)
691 image->storage_class=(ClassType) storage_class;
694 if (LocaleCompare(keyword,"colors") == 0)
696 colors=StringToUnsignedLong(options);
699 if (LocaleCompare(keyword,"colorspace") == 0)
704 colorspace=ParseCommandOption(MagickColorspaceOptions,
705 MagickFalse,options);
708 (void) SetImageColorspace(image,(ColorspaceType) colorspace, exception);
711 if (LocaleCompare(keyword,"compression") == 0)
716 compression=ParseCommandOption(MagickCompressOptions,
717 MagickFalse,options);
720 image->compression=(CompressionType) compression;
723 if (LocaleCompare(keyword,"columns") == 0)
725 image->columns=StringToUnsignedLong(options);
728 (void) SetImageProperty(image,keyword,options,exception);
734 if (LocaleCompare(keyword,"delay") == 0)
736 image->delay=StringToUnsignedLong(options);
739 if (LocaleCompare(keyword,"depth") == 0)
741 image->depth=StringToUnsignedLong(options);
744 if (LocaleCompare(keyword,"dispose") == 0)
749 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
753 image->dispose=(DisposeType) dispose;
756 (void) SetImageProperty(image,keyword,options,exception);
762 if (LocaleCompare(keyword,"endian") == 0)
767 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
771 image->endian=(EndianType) endian;
774 (void) SetImageProperty(image,keyword,options,exception);
780 if (LocaleCompare(keyword,"gamma") == 0)
782 image->gamma=StringToDouble(options,(char **) NULL);
785 if (LocaleCompare(keyword,"gravity") == 0)
790 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
794 image->gravity=(GravityType) gravity;
797 if (LocaleCompare(keyword,"green-primary") == 0)
799 flags=ParseGeometry(options,&geometry_info);
800 image->chromaticity.green_primary.x=geometry_info.rho;
801 image->chromaticity.green_primary.y=geometry_info.sigma;
802 if ((flags & SigmaValue) == 0)
803 image->chromaticity.green_primary.y=
804 image->chromaticity.green_primary.x;
807 (void) SetImageProperty(image,keyword,options,exception);
813 if (LocaleCompare(keyword,"id") == 0)
815 (void) CopyMagickString(id,options,MaxTextExtent);
818 if (LocaleCompare(keyword,"iterations") == 0)
820 image->iterations=StringToUnsignedLong(options);
823 (void) SetImageProperty(image,keyword,options,exception);
829 if (LocaleCompare(keyword,"matte") == 0)
834 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
838 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
842 if (LocaleCompare(keyword,"matte-color") == 0)
844 (void) QueryColorCompliance(options,AllCompliance,
845 &image->matte_color,exception);
848 if (LocaleCompare(keyword,"montage") == 0)
850 (void) CloneString(&image->montage,options);
853 (void) SetImageProperty(image,keyword,options,exception);
859 if (LocaleCompare(keyword,"orientation") == 0)
864 orientation=ParseCommandOption(MagickOrientationOptions,
865 MagickFalse,options);
868 image->orientation=(OrientationType) orientation;
871 (void) SetImageProperty(image,keyword,options,exception);
877 if (LocaleCompare(keyword,"page") == 0)
882 geometry=GetPageGeometry(options);
883 (void) ParseAbsoluteGeometry(geometry,&image->page);
884 geometry=DestroyString(geometry);
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))
1232 Allocate image pixels.
1234 quantum_info=AcquireQuantumInfo(image_info,image);
1235 if (quantum_info == (QuantumInfo *) NULL)
1236 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1237 if (quantum_format != UndefinedQuantumFormat)
1239 status=SetQuantumFormat(image,quantum_info,quantum_format);
1240 if (status == MagickFalse)
1241 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1243 packet_size=(size_t) (quantum_info->depth/8);
1244 if (image->storage_class == DirectClass)
1245 packet_size=(size_t) (3*quantum_info->depth/8);
1246 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1247 packet_size=quantum_info->depth/8;
1248 if (image->alpha_trait == BlendPixelTrait)
1249 packet_size+=quantum_info->depth/8;
1250 if (image->colorspace == CMYKColorspace)
1251 packet_size+=quantum_info->depth/8;
1252 if (image->compression == RLECompression)
1254 length=image->columns;
1255 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1256 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1258 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1259 sizeof(*compress_pixels));
1260 if (compress_pixels == (unsigned char *) NULL)
1261 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1265 quantum_type=RGBQuantum;
1266 if (image->alpha_trait == BlendPixelTrait)
1267 quantum_type=RGBAQuantum;
1268 if (image->colorspace == CMYKColorspace)
1270 quantum_type=CMYKQuantum;
1271 if (image->alpha_trait == BlendPixelTrait)
1272 quantum_type=CMYKAQuantum;
1274 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1276 quantum_type=GrayQuantum;
1277 if (image->alpha_trait == BlendPixelTrait)
1278 quantum_type=GrayAlphaQuantum;
1280 if (image->storage_class == PseudoClass)
1282 quantum_type=IndexQuantum;
1283 if (image->alpha_trait == BlendPixelTrait)
1284 quantum_type=IndexAlphaQuantum;
1287 switch (image->compression)
1289 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1290 case BZipCompression:
1295 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1296 bzip_info.bzalloc=AcquireBZIPMemory;
1297 bzip_info.bzfree=RelinquishBZIPMemory;
1298 bzip_info.opaque=(void *) NULL;
1299 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1306 #if defined(MAGICKCORE_LZMA_DELEGATE)
1307 case LZMACompression:
1312 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1313 allocator.alloc=AcquireLZMAMemory;
1314 allocator.free=RelinquishLZMAMemory;
1315 lzma_info=initialize_lzma;
1316 lzma_info.allocator=(&allocator);
1317 code=lzma_auto_decoder(&lzma_info,-1,0);
1318 if (code != LZMA_OK)
1323 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1324 case LZWCompression:
1325 case ZipCompression:
1330 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1331 zip_info.zalloc=AcquireZIPMemory;
1332 zip_info.zfree=RelinquishZIPMemory;
1333 zip_info.opaque=(voidpf) NULL;
1334 code=inflateInit(&zip_info);
1340 case RLECompression:
1342 GetPixelInfo(image,&pixel);
1348 pixels=GetQuantumPixels(quantum_info);
1350 for (y=0; y < (ssize_t) image->rows; y++)
1358 if (status == MagickFalse)
1360 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1361 if (q == (Quantum *) NULL)
1363 switch (image->compression)
1365 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1366 case BZipCompression:
1368 bzip_info.next_out=(char *) pixels;
1369 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1372 if (bzip_info.avail_in == 0)
1374 bzip_info.next_in=(char *) compress_pixels;
1375 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1377 length=(size_t) ReadBlobMSBLong(image);
1378 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1379 (unsigned char *) bzip_info.next_in);
1381 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1383 } while (bzip_info.avail_out != 0);
1384 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1385 quantum_type,pixels,exception);
1389 #if defined(MAGICKCORE_LZMA_DELEGATE)
1390 case LZMACompression:
1392 lzma_info.next_out=pixels;
1393 lzma_info.avail_out=packet_size*image->columns;
1399 if (lzma_info.avail_in == 0)
1401 lzma_info.next_in=compress_pixels;
1402 length=(size_t) ReadBlobMSBLong(image);
1403 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1404 (unsigned char *) lzma_info.next_in);
1406 code=lzma_code(&lzma_info,LZMA_RUN);
1412 if (code == LZMA_STREAM_END)
1414 } while (lzma_info.avail_out != 0);
1415 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1416 quantum_type,pixels,exception);
1420 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1421 case LZWCompression:
1422 case ZipCompression:
1424 zip_info.next_out=pixels;
1425 zip_info.avail_out=(uInt) (packet_size*image->columns);
1428 if (zip_info.avail_in == 0)
1430 zip_info.next_in=compress_pixels;
1431 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1433 length=(size_t) ReadBlobMSBLong(image);
1434 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1437 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1439 } while (zip_info.avail_out != 0);
1440 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1441 quantum_type,pixels,exception);
1445 case RLECompression:
1447 for (x=0; x < (ssize_t) image->columns; x++)
1451 count=ReadBlob(image,packet_size,pixels);
1452 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1455 if (image->storage_class == PseudoClass)
1456 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1459 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1460 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1461 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1462 if (image->colorspace == CMYKColorspace)
1463 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1465 if (image->alpha_trait == BlendPixelTrait)
1466 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1467 q+=GetPixelChannels(image);
1473 count=ReadBlob(image,packet_size*image->columns,pixels);
1474 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1475 quantum_type,pixels,exception);
1479 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1482 SetQuantumImageType(image,quantum_type);
1483 switch (image->compression)
1485 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1486 case BZipCompression:
1496 offset=SeekBlob(image,-((MagickOffsetType)
1497 bzip_info.avail_in),SEEK_CUR);
1499 ThrowReaderException(CorruptImageError,
1500 "ImproperImageHeader");
1502 code=BZ2_bzDecompressEnd(&bzip_info);
1508 #if defined(MAGICKCORE_LZMA_DELEGATE)
1509 case LZMACompression:
1514 code=lzma_code(&lzma_info,LZMA_FINISH);
1515 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1517 lzma_end(&lzma_info);
1521 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1522 case LZWCompression:
1523 case ZipCompression:
1533 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1536 ThrowReaderException(CorruptImageError,
1537 "ImproperImageHeader");
1539 code=inflateEnd(&zip_info);
1540 if (code != LZMA_OK)
1548 quantum_info=DestroyQuantumInfo(quantum_info);
1549 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1550 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1552 image=DestroyImageList(image);
1553 return((Image *) NULL);
1555 if (EOFBlob(image) != MagickFalse)
1557 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1562 Proceed to next image.
1564 if (image_info->number_scenes != 0)
1565 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1569 c=ReadBlobByte(image);
1570 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1574 Allocate next image structure.
1576 AcquireNextImage(image_info,image,exception);
1577 if (GetNextImageInList(image) == (Image *) NULL)
1579 image=DestroyImageList(image);
1580 return((Image *) NULL);
1582 image=SyncNextImageInList(image);
1583 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1584 GetBlobSize(image));
1585 if (status == MagickFalse)
1589 (void) CloseBlob(image);
1590 return(GetFirstImageInList(image));
1594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598 % R e g i s t e r M I F F I m a g e %
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1604 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1605 % supported formats. The properties include the image format tag, a method to
1606 % read and/or write the format, whether the format supports the saving of more
1607 % than one frame to the same file or blob, whether the format supports native
1608 % in-memory I/O, and a brief description of the format.
1610 % The format of the RegisterMIFFImage method is:
1612 % size_t RegisterMIFFImage(void)
1615 ModuleExport size_t RegisterMIFFImage(void)
1618 version[MaxTextExtent];
1624 #if defined(MagickImageCoderSignatureText)
1625 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1626 #if defined(ZLIB_VERSION)
1627 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1628 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1630 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1631 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1634 entry=SetMagickInfo("MIFF");
1635 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1636 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1637 entry->magick=(IsImageFormatHandler *) IsMIFF;
1638 entry->seekable_stream=MagickTrue;
1639 entry->description=ConstantString("Magick Image File Format");
1640 if (*version != '\0')
1641 entry->version=ConstantString(version);
1642 entry->module=ConstantString("MIFF");
1643 (void) RegisterMagickInfo(entry);
1644 return(MagickImageCoderSignature);
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1652 % U n r e g i s t e r M I F F I m a g e %
1656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1658 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1659 % from the list of supported formats.
1661 % The format of the UnregisterMIFFImage method is:
1663 % UnregisterMIFFImage(void)
1666 ModuleExport void UnregisterMIFFImage(void)
1668 (void) UnregisterMagickInfo("MIFF");
1672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 % W r i t e M I F F I m a g e %
1680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % WriteMIFFImage() writes a MIFF image to a file.
1684 % The format of the WriteMIFFImage method is:
1686 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1687 % Image *image,ExceptionInfo *exception)
1689 % Compression code contributed by Kyle Shorter.
1691 % A description of each parameter follows:
1693 % o image_info: the image info.
1695 % o image: the image.
1697 % o exception: return any errors or warnings in this structure.
1701 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1702 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1704 if (image->storage_class != DirectClass)
1706 switch (image->depth)
1710 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1711 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1714 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1717 *pixels++=(unsigned char) pixel->index;
1721 (void) ThrowMagickException(exception,GetMagickModule(),
1722 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1724 switch (image->depth)
1731 if (image->alpha_trait == BlendPixelTrait)
1733 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1734 pixels=PopLongPixel(MSBEndian,value,pixels);
1743 if (image->alpha_trait == BlendPixelTrait)
1745 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1746 pixels=PopShortPixel(MSBEndian,value,pixels);
1755 if (image->alpha_trait == BlendPixelTrait)
1757 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1759 pixels=PopCharPixel(value,pixels);
1764 (void) ThrowMagickException(exception,GetMagickModule(),
1765 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1767 *pixels++=(unsigned char) length;
1770 switch (image->depth)
1777 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1778 pixels=PopLongPixel(MSBEndian,value,pixels);
1779 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1781 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1782 pixels=PopLongPixel(MSBEndian,value,pixels);
1783 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1784 pixels=PopLongPixel(MSBEndian,value,pixels);
1786 if (image->colorspace == CMYKColorspace)
1788 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1789 pixels=PopLongPixel(MSBEndian,value,pixels);
1791 if (image->alpha_trait == BlendPixelTrait)
1793 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1794 pixels=PopLongPixel(MSBEndian,value,pixels);
1803 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1804 pixels=PopShortPixel(MSBEndian,value,pixels);
1805 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1807 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1808 pixels=PopShortPixel(MSBEndian,value,pixels);
1809 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1810 pixels=PopShortPixel(MSBEndian,value,pixels);
1812 if (image->colorspace == CMYKColorspace)
1814 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1815 pixels=PopShortPixel(MSBEndian,value,pixels);
1817 if (image->alpha_trait == BlendPixelTrait)
1819 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1820 pixels=PopShortPixel(MSBEndian,value,pixels);
1829 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1830 pixels=PopCharPixel(value,pixels);
1831 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1833 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1835 pixels=PopCharPixel(value,pixels);
1836 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1837 pixels=PopCharPixel(value,pixels);
1839 if (image->colorspace == CMYKColorspace)
1841 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1843 pixels=PopCharPixel(value,pixels);
1845 if (image->alpha_trait == BlendPixelTrait)
1847 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1849 pixels=PopCharPixel(value,pixels);
1854 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1855 "ImageDepthNotSupported","`%s'",image->filename);
1857 *pixels++=(unsigned char) length;
1861 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1862 Image *image,ExceptionInfo *exception)
1864 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1870 buffer[MaxTextExtent];
1879 #if defined(MAGICKCORE_LZMA_DELEGATE)
1884 initialize_lzma = LZMA_STREAM_INIT,
1919 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1925 Open output image file.
1927 assert(image_info != (const ImageInfo *) NULL);
1928 assert(image_info->signature == MagickSignature);
1929 assert(image != (Image *) NULL);
1930 assert(image->signature == MagickSignature);
1931 if (image->debug != MagickFalse)
1932 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1933 assert(exception != (ExceptionInfo *) NULL);
1934 assert(exception->signature == MagickSignature);
1935 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1936 if (status == MagickFalse)
1942 Allocate image pixels.
1944 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1945 image->depth <= 32 ? 32UL : 64UL;
1946 quantum_info=AcquireQuantumInfo(image_info,image);
1947 if (quantum_info == (QuantumInfo *) NULL)
1948 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1949 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1950 (quantum_info->format == UndefinedQuantumFormat) &&
1951 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1953 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1954 if (status == MagickFalse)
1955 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1957 if ((image->storage_class == PseudoClass) &&
1958 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1959 (void) SetImageStorageClass(image,DirectClass,exception);
1960 if (IsImageGray(image,exception) != MagickFalse)
1961 (void) SetImageColorspace(image,GRAYColorspace,exception);
1962 compression=image->compression;
1963 if (image_info->compression != UndefinedCompression)
1964 compression=image_info->compression;
1965 switch (compression)
1967 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1968 case LZMACompression: compression=NoCompression; break;
1970 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1971 case LZWCompression:
1972 case ZipCompression: compression=NoCompression; break;
1974 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1975 case BZipCompression: compression=NoCompression; break;
1977 case RLECompression:
1979 if (quantum_info->format == FloatingPointQuantumFormat)
1980 compression=NoCompression;
1981 GetPixelInfo(image,&target);
1987 packet_size=(size_t) (quantum_info->depth/8);
1988 if (image->storage_class == DirectClass)
1989 packet_size=(size_t) (3*quantum_info->depth/8);
1990 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1991 packet_size=(size_t) (quantum_info->depth/8);
1992 if (image->alpha_trait == BlendPixelTrait)
1993 packet_size+=quantum_info->depth/8;
1994 if (image->colorspace == CMYKColorspace)
1995 packet_size+=quantum_info->depth/8;
1996 if (compression == RLECompression)
1998 length=image->columns;
1999 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2000 packet_size*image->columns));
2001 if ((compression == BZipCompression) || (compression == ZipCompression))
2002 if (length != (size_t) ((unsigned int) length))
2003 compression=NoCompression;
2004 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2005 sizeof(*compress_pixels));
2006 if (compress_pixels == (unsigned char *) NULL)
2007 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2011 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2012 (void) FormatLocaleString(buffer,MaxTextExtent,
2013 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2014 MagickClassOptions,image->storage_class),(double) image->colors,
2015 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2016 image->alpha_trait));
2017 (void) WriteBlobString(image,buffer);
2018 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2019 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2021 (void) WriteBlobString(image,buffer);
2022 if (image->type != UndefinedType)
2024 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2025 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2026 (void) WriteBlobString(image,buffer);
2028 if (image->colorspace != UndefinedColorspace)
2030 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2031 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2032 (void) WriteBlobString(image,buffer);
2034 if (compression != UndefinedCompression)
2036 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2037 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2038 compression),(double) image->quality);
2039 (void) WriteBlobString(image,buffer);
2041 if (image->units != UndefinedResolution)
2043 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2044 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2045 (void) WriteBlobString(image,buffer);
2047 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2049 (void) FormatLocaleString(buffer,MaxTextExtent,
2050 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2051 (void) WriteBlobString(image,buffer);
2053 if ((image->page.width != 0) || (image->page.height != 0))
2055 (void) FormatLocaleString(buffer,MaxTextExtent,
2056 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2057 image->page.height,(double) image->page.x,(double) image->page.y);
2058 (void) WriteBlobString(image,buffer);
2061 if ((image->page.x != 0) || (image->page.y != 0))
2063 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2064 (long) image->page.x,(long) image->page.y);
2065 (void) WriteBlobString(image,buffer);
2067 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2069 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2070 (long) image->tile_offset.x,(long) image->tile_offset.y);
2071 (void) WriteBlobString(image,buffer);
2073 if ((GetNextImageInList(image) != (Image *) NULL) ||
2074 (GetPreviousImageInList(image) != (Image *) NULL))
2076 if (image->scene == 0)
2077 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2078 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2079 (double) image->delay,(double) image->ticks_per_second);
2081 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2082 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2083 image->scene,(double) image->iterations,(double) image->delay,
2084 (double) image->ticks_per_second);
2085 (void) WriteBlobString(image,buffer);
2089 if (image->scene != 0)
2091 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2092 (double) image->scene);
2093 (void) WriteBlobString(image,buffer);
2095 if (image->iterations != 0)
2097 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2098 (double) image->iterations);
2099 (void) WriteBlobString(image,buffer);
2101 if (image->delay != 0)
2103 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2104 (double) image->delay);
2105 (void) WriteBlobString(image,buffer);
2107 if (image->ticks_per_second != UndefinedTicksPerSecond)
2109 (void) FormatLocaleString(buffer,MaxTextExtent,
2110 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2111 (void) WriteBlobString(image,buffer);
2114 if (image->gravity != UndefinedGravity)
2116 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2117 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2118 (void) WriteBlobString(image,buffer);
2120 if (image->dispose != UndefinedDispose)
2122 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2123 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2124 (void) WriteBlobString(image,buffer);
2126 if (image->rendering_intent != UndefinedIntent)
2128 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2129 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2130 (void) WriteBlobString(image,buffer);
2132 if (image->gamma != 0.0)
2134 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2136 (void) WriteBlobString(image,buffer);
2138 if (image->chromaticity.white_point.x != 0.0)
2141 Note chomaticity points.
2143 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2144 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2145 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2146 image->chromaticity.green_primary.x,
2147 image->chromaticity.green_primary.y,
2148 image->chromaticity.blue_primary.x,
2149 image->chromaticity.blue_primary.y);
2150 (void) WriteBlobString(image,buffer);
2151 (void) FormatLocaleString(buffer,MaxTextExtent,
2152 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2153 image->chromaticity.white_point.y);
2154 (void) WriteBlobString(image,buffer);
2156 if (image->orientation != UndefinedOrientation)
2158 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2159 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2160 (void) WriteBlobString(image,buffer);
2162 if (image->profiles != (void *) NULL)
2171 Write image profiles.
2173 ResetImageProfileIterator(image);
2174 name=GetNextImageProfile(image);
2175 while (name != (const char *) NULL)
2177 profile=GetImageProfile(image,name);
2178 if (profile != (StringInfo *) NULL)
2180 (void) FormatLocaleString(buffer,MaxTextExtent,
2181 "profile:%s=%.20g\n",name,(double)
2182 GetStringInfoLength(profile));
2183 (void) WriteBlobString(image,buffer);
2185 name=GetNextImageProfile(image);
2188 if (image->montage != (char *) NULL)
2190 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2192 (void) WriteBlobString(image,buffer);
2194 if (quantum_info->format == FloatingPointQuantumFormat)
2195 (void) SetImageProperty(image,"quantum:format","floating-point",
2197 ResetImagePropertyIterator(image);
2198 property=GetNextImageProperty(image);
2199 while (property != (const char *) NULL)
2201 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2202 (void) WriteBlobString(image,buffer);
2203 value=GetImageProperty(image,property,exception);
2204 if (value != (const char *) NULL)
2209 length=strlen(value);
2210 for (i=0; i < (ssize_t) length; i++)
2211 if (isspace((int) ((unsigned char) value[i])) != 0)
2213 if (i == (ssize_t) length)
2214 (void) WriteBlob(image,length,(const unsigned char *) value);
2217 (void) WriteBlobByte(image,'{');
2218 if (strchr(value,'}') == (char *) NULL)
2219 (void) WriteBlob(image,length,(const unsigned char *) value);
2221 for (i=0; i < (ssize_t) length; i++)
2223 if (value[i] == (int) '}')
2224 (void) WriteBlobByte(image,'\\');
2225 (void) WriteBlobByte(image,value[i]);
2227 (void) WriteBlobByte(image,'}');
2230 (void) WriteBlobByte(image,'\n');
2231 property=GetNextImageProperty(image);
2233 (void) WriteBlobString(image,"\f\n:\032");
2234 if (image->montage != (char *) NULL)
2237 Write montage tile directory.
2239 if (image->directory != (char *) NULL)
2240 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2242 (void) WriteBlobByte(image,'\0');
2244 if (image->profiles != (void *) NULL)
2255 ResetImageProfileIterator(image);
2256 name=GetNextImageProfile(image);
2257 while (name != (const char *) NULL)
2259 profile=GetImageProfile(image,name);
2260 (void) WriteBlob(image,GetStringInfoLength(profile),
2261 GetStringInfoDatum(profile));
2262 name=GetNextImageProfile(image);
2265 if (image->storage_class == PseudoClass)
2277 packet_size=(size_t) (3*quantum_info->depth/8);
2278 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2279 packet_size*sizeof(*colormap));
2280 if (colormap == (unsigned char *) NULL)
2281 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2283 Write colormap to file.
2286 for (i=0; i < (ssize_t) image->colors; i++)
2288 switch (quantum_info->depth)
2291 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2294 register unsigned int
2297 pixel=ScaleQuantumToLong(image->colormap[i].red);
2298 q=PopLongPixel(MSBEndian,pixel,q);
2299 pixel=ScaleQuantumToLong(image->colormap[i].green);
2300 q=PopLongPixel(MSBEndian,pixel,q);
2301 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2302 q=PopLongPixel(MSBEndian,pixel,q);
2307 register unsigned short
2310 pixel=ScaleQuantumToShort(image->colormap[i].red);
2311 q=PopShortPixel(MSBEndian,pixel,q);
2312 pixel=ScaleQuantumToShort(image->colormap[i].green);
2313 q=PopShortPixel(MSBEndian,pixel,q);
2314 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2315 q=PopShortPixel(MSBEndian,pixel,q);
2320 register unsigned char
2323 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2324 q=PopCharPixel(pixel,q);
2325 pixel=(unsigned char) ScaleQuantumToChar(
2326 image->colormap[i].green);
2327 q=PopCharPixel(pixel,q);
2328 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2329 q=PopCharPixel(pixel,q);
2334 (void) WriteBlob(image,packet_size*image->colors,colormap);
2335 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2338 Write image pixels to file.
2341 switch (compression)
2343 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2344 case BZipCompression:
2349 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2350 bzip_info.bzalloc=AcquireBZIPMemory;
2351 bzip_info.bzfree=RelinquishBZIPMemory;
2352 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2353 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2354 9)),(int) image_info->verbose,0);
2360 #if defined(MAGICKCORE_LZMA_DELEGATE)
2361 case LZMACompression:
2366 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2367 allocator.alloc=AcquireLZMAMemory;
2368 allocator.free=RelinquishLZMAMemory;
2369 lzma_info=initialize_lzma;
2370 lzma_info.allocator=&allocator;
2371 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2372 if (code != LZMA_OK)
2377 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2378 case LZWCompression:
2379 case ZipCompression:
2384 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2385 zip_info.zalloc=AcquireZIPMemory;
2386 zip_info.zfree=RelinquishZIPMemory;
2387 code=deflateInit(&zip_info,(int) (image->quality ==
2388 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2397 quantum_type=GetQuantumType(image,exception);
2398 pixels=GetQuantumPixels(quantum_info);
2399 for (y=0; y < (ssize_t) image->rows; y++)
2401 register const Quantum
2407 if (status == MagickFalse)
2409 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2410 if (p == (const Quantum *) NULL)
2413 switch (compression)
2415 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2416 case BZipCompression:
2418 bzip_info.next_in=(char *) pixels;
2419 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2420 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2421 quantum_type,pixels,exception);
2427 bzip_info.next_out=(char *) compress_pixels;
2428 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2430 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2433 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2436 (void) WriteBlobMSBLong(image,(unsigned int) length);
2437 (void) WriteBlob(image,length,compress_pixels);
2439 } while (bzip_info.avail_in != 0);
2443 #if defined(MAGICKCORE_LZMA_DELEGATE)
2444 case LZMACompression:
2446 lzma_info.next_in=pixels;
2447 lzma_info.avail_in=packet_size*image->columns;
2448 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2449 quantum_type,pixels,exception);
2455 lzma_info.next_out=compress_pixels;
2456 lzma_info.avail_out=packet_size*image->columns;
2457 code=lzma_code(&lzma_info,LZMA_RUN);
2458 if (code != LZMA_OK)
2460 length=(size_t) (lzma_info.next_out-compress_pixels);
2463 (void) WriteBlobMSBLong(image,(unsigned int) length);
2464 (void) WriteBlob(image,length,compress_pixels);
2466 } while (lzma_info.avail_in != 0);
2470 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2471 case LZWCompression:
2472 case ZipCompression:
2474 zip_info.next_in=pixels;
2475 zip_info.avail_in=(uInt) (packet_size*image->columns);
2476 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2477 quantum_type,pixels,exception);
2483 zip_info.next_out=compress_pixels;
2484 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2485 code=deflate(&zip_info,Z_SYNC_FLUSH);
2488 length=(size_t) (zip_info.next_out-compress_pixels);
2491 (void) WriteBlobMSBLong(image,(unsigned int) length);
2492 (void) WriteBlob(image,length,compress_pixels);
2494 } while (zip_info.avail_in != 0);
2498 case RLECompression:
2501 GetPixelInfoPixel(image,p,&pixel);
2502 p+=GetPixelChannels(image);
2503 for (x=1; x < (ssize_t) image->columns; x++)
2505 GetPixelInfoPixel(image,p,&target);
2506 if ((length < 255) &&
2507 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2511 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2514 GetPixelInfoPixel(image,p,&pixel);
2515 p+=GetPixelChannels(image);
2517 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2518 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2523 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2524 quantum_type,pixels,exception);
2525 (void) WriteBlob(image,packet_size*image->columns,pixels);
2529 if (image->previous == (Image *) NULL)
2531 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2533 if (status == MagickFalse)
2537 switch (compression)
2539 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2540 case BZipCompression:
2547 if (status == MagickFalse)
2549 bzip_info.next_out=(char *) compress_pixels;
2550 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2552 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2553 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2556 (void) WriteBlobMSBLong(image,(unsigned int) length);
2557 (void) WriteBlob(image,length,compress_pixels);
2559 if (code == BZ_STREAM_END)
2562 code=BZ2_bzCompressEnd(&bzip_info);
2568 #if defined(MAGICKCORE_LZMA_DELEGATE)
2569 case LZMACompression:
2576 if (status == MagickFalse)
2578 lzma_info.next_out=compress_pixels;
2579 lzma_info.avail_out=packet_size*image->columns;
2580 code=lzma_code(&lzma_info,LZMA_FINISH);
2581 length=(size_t) (lzma_info.next_out-compress_pixels);
2584 (void) WriteBlobMSBLong(image,(unsigned int) length);
2585 (void) WriteBlob(image,length,compress_pixels);
2587 if (code == LZMA_STREAM_END)
2590 lzma_end(&lzma_info);
2594 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2595 case LZWCompression:
2596 case ZipCompression:
2603 if (status == MagickFalse)
2605 zip_info.next_out=compress_pixels;
2606 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2607 code=deflate(&zip_info,Z_FINISH);
2608 length=(size_t) (zip_info.next_out-compress_pixels);
2611 (void) WriteBlobMSBLong(image,(unsigned int) length);
2612 (void) WriteBlob(image,length,compress_pixels);
2614 if (code == Z_STREAM_END)
2617 code=deflateEnd(&zip_info);
2626 quantum_info=DestroyQuantumInfo(quantum_info);
2627 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2628 if (GetNextImageInList(image) == (Image *) NULL)
2630 image=SyncNextImageInList(image);
2631 status=SetImageProgress(image,SaveImagesTag,scene++,
2632 GetImageListLength(image));
2633 if (status == MagickFalse)
2635 } while (image_info->adjoin != MagickFalse);
2636 (void) CloseBlob(image);