2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/hashmap.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/profile.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/static.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/string-private.h"
76 #if defined(MAGICKCORE_BZLIB_DELEGATE)
79 #if defined(MAGICKCORE_LZMA_DELEGATE)
82 #if defined(MAGICKCORE_ZLIB_DELEGATE)
96 static MagickBooleanType
97 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % IsMIFF() returns MagickTrue if the image format type, identified by the
111 % magick string, is MIFF.
113 % The format of the IsMIFF method is:
115 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
117 % A description of each parameter follows:
119 % o magick: compare image format pattern against these bytes.
121 % o length: Specifies the length of the magick string.
124 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
128 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % R e a d M I F F I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
145 % memory necessary for the new Image structure and returns a pointer to the
148 % The format of the ReadMIFFImage method is:
150 % Image *ReadMIFFImage(const ImageInfo *image_info,
151 % ExceptionInfo *exception)
153 % Decompression code contributed by Kyle Shorter.
155 % A description of each parameter follows:
157 % o image_info: the image info.
159 % o exception: return any errors or warnings in this structure.
163 #if defined(MAGICKCORE_BZLIB_DELEGATE)
164 static void *AcquireBZIPMemory(void *context,int items,int size)
167 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
171 #if defined(MAGICKCORE_LZMA_DELEGATE)
172 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
175 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
179 #if defined(MAGICKCORE_ZLIB_DELEGATE)
180 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
184 return((voidpf) AcquireQuantumMemory(items,size));
188 static inline size_t MagickMax(const size_t x,const size_t y)
195 static inline size_t MagickMin(const size_t x,const size_t y)
202 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
203 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
209 if (image->storage_class == PseudoClass)
212 switch (image->depth)
216 pixel->index=ConstrainColormapIndex(image,((size_t) *p << 24) |
217 ((size_t) *(p+1) << 16) | ((size_t) *(p+2) << 8) | (size_t) *(p+3),
224 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
231 pixel->index=ConstrainColormapIndex(image,*p,exception);
236 (void) ThrowMagickException(exception,GetMagickModule(),
237 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
239 switch (image->depth)
246 if (image->alpha_trait == BlendPixelTrait)
248 p=PushCharPixel(p,&quantum);
249 pixel->alpha=ScaleCharToQuantum(quantum);
258 if (image->alpha_trait == BlendPixelTrait)
260 p=PushShortPixel(MSBEndian,p,&quantum);
261 pixel->alpha=(Quantum) (quantum >> (image->depth-
262 MAGICKCORE_QUANTUM_DEPTH));
271 if (image->alpha_trait == BlendPixelTrait)
273 p=PushLongPixel(MSBEndian,p,&quantum);
274 pixel->alpha=(Quantum) (quantum >> (image->depth-
275 MAGICKCORE_QUANTUM_DEPTH));
280 (void) ThrowMagickException(exception,GetMagickModule(),
281 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
283 *length=(size_t) (*p++)+1;
286 switch (image->depth)
293 p=PushCharPixel(p,&quantum);
294 pixel->red=ScaleCharToQuantum(quantum);
295 pixel->green=pixel->red;
296 pixel->blue=pixel->red;
297 if (IsGrayColorspace(image->colorspace) == MagickFalse)
299 p=PushCharPixel(p,&quantum);
300 pixel->green=ScaleCharToQuantum(quantum);
301 p=PushCharPixel(p,&quantum);
302 pixel->blue=ScaleCharToQuantum(quantum);
304 if (image->colorspace == CMYKColorspace)
306 p=PushCharPixel(p,&quantum);
307 pixel->black=ScaleCharToQuantum(quantum);
309 if (image->alpha_trait == BlendPixelTrait)
311 p=PushCharPixel(p,&quantum);
312 pixel->alpha=ScaleCharToQuantum(quantum);
321 p=PushShortPixel(MSBEndian,p,&quantum);
322 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
323 pixel->green=pixel->red;
324 pixel->blue=pixel->red;
325 if (IsGrayColorspace(image->colorspace) == MagickFalse)
327 p=PushShortPixel(MSBEndian,p,&quantum);
328 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
329 p=PushShortPixel(MSBEndian,p,&quantum);
330 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
332 if (image->colorspace == CMYKColorspace)
334 p=PushShortPixel(MSBEndian,p,&quantum);
335 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
337 if (image->alpha_trait == BlendPixelTrait)
339 p=PushShortPixel(MSBEndian,p,&quantum);
340 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
349 p=PushLongPixel(MSBEndian,p,&quantum);
350 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
351 pixel->green=pixel->red;
352 pixel->blue=pixel->red;
353 if (IsGrayColorspace(image->colorspace) == MagickFalse)
355 p=PushLongPixel(MSBEndian,p,&quantum);
356 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
357 p=PushLongPixel(MSBEndian,p,&quantum);
358 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
360 if (image->colorspace == CMYKColorspace)
362 p=PushLongPixel(MSBEndian,p,&quantum);
363 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
365 if (image->alpha_trait == BlendPixelTrait)
367 p=PushLongPixel(MSBEndian,p,&quantum);
368 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
373 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
374 "ImageDepthNotSupported","`%s'",image->filename);
376 *length=(size_t) (*p++)+1;
379 #if defined(MAGICKCORE_BZLIB_DELEGATE)
380 static void RelinquishBZIPMemory(void *context,void *memory)
383 memory=RelinquishMagickMemory(memory);
387 #if defined(MAGICKCORE_LZMA_DELEGATE)
388 static void RelinquishLZMAMemory(void *context,void *memory)
391 memory=RelinquishMagickMemory(memory);
395 #if defined(MAGICKCORE_ZLIB_DELEGATE)
396 static void RelinquishZIPMemory(voidpf context,voidpf memory)
399 memory=RelinquishMagickMemory(memory);
403 static Image *ReadMIFFImage(const ImageInfo *image_info,
404 ExceptionInfo *exception)
406 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
407 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
408 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
410 #if defined(MAGICKCORE_BZLIB_DELEGATE)
417 keyword[MaxTextExtent],
438 #if defined(MAGICKCORE_LZMA_DELEGATE)
440 initialize_lzma = LZMA_STREAM_INIT,
485 #if defined(MAGICKCORE_ZLIB_DELEGATE)
493 assert(image_info != (const ImageInfo *) NULL);
494 assert(image_info->signature == MagickSignature);
495 if (image_info->debug != MagickFalse)
496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
497 image_info->filename);
498 assert(exception != (ExceptionInfo *) NULL);
499 assert(exception->signature == MagickSignature);
500 image=AcquireImage(image_info,exception);
501 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
502 if (status == MagickFalse)
504 image=DestroyImageList(image);
505 return((Image *) NULL);
508 Decode image header; header terminates one character beyond a ':'.
510 c=ReadBlobByte(image);
512 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
514 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
520 Decode image header; header terminates one character beyond a ':'.
522 length=MaxTextExtent;
523 options=AcquireString((char *) NULL);
524 quantum_format=UndefinedQuantumFormat;
525 profiles=(LinkedListInfo *) NULL;
528 image->compression=NoCompression;
529 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
540 Read comment-- any text between { }.
542 length=MaxTextExtent;
543 comment=AcquireString((char *) NULL);
544 for (p=comment; comment != (char *) NULL; p++)
546 c=ReadBlobByte(image);
548 c=ReadBlobByte(image);
550 if ((c == EOF) || (c == (int) '}'))
552 if ((size_t) (p-comment+1) >= length)
556 comment=(char *) ResizeQuantumMemory(comment,length+
557 MaxTextExtent,sizeof(*comment));
558 if (comment == (char *) NULL)
560 p=comment+strlen(comment);
564 if (comment == (char *) NULL)
565 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
567 (void) SetImageProperty(image,"comment",comment,exception);
568 comment=DestroyString(comment);
569 c=ReadBlobByte(image);
572 if (isalnum(c) != MagickFalse)
582 if ((size_t) (p-keyword) < (MaxTextExtent-1))
584 c=ReadBlobByte(image);
588 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
589 c=ReadBlobByte(image);
593 Get the keyword value.
595 c=ReadBlobByte(image);
596 while ((c != (int) '}') && (c != EOF))
598 if ((size_t) (p-options+1) >= length)
602 options=(char *) ResizeQuantumMemory(options,length+
603 MaxTextExtent,sizeof(*options));
604 if (options == (char *) NULL)
606 p=options+strlen(options);
608 if (options == (char *) NULL)
609 ThrowReaderException(ResourceLimitError,
610 "MemoryAllocationFailed");
612 c=ReadBlobByte(image);
615 c=ReadBlobByte(image);
619 c=ReadBlobByte(image);
623 if (isspace((int) ((unsigned char) c)) != 0)
628 (void) CopyMagickString(options,options+1,strlen(options));
630 Assign a value to the specified keyword.
637 if (LocaleCompare(keyword,"alpha-trait") == 0)
642 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
643 MagickFalse,options);
646 image->alpha_trait=(PixelTrait) alpha_trait;
649 (void) SetImageProperty(image,keyword,options,exception);
655 if (LocaleCompare(keyword,"background-color") == 0)
657 (void) QueryColorCompliance(options,AllCompliance,
658 &image->background_color,exception);
661 if (LocaleCompare(keyword,"blue-primary") == 0)
663 flags=ParseGeometry(options,&geometry_info);
664 image->chromaticity.blue_primary.x=geometry_info.rho;
665 image->chromaticity.blue_primary.y=geometry_info.sigma;
666 if ((flags & SigmaValue) == 0)
667 image->chromaticity.blue_primary.y=
668 image->chromaticity.blue_primary.x;
671 if (LocaleCompare(keyword,"border-color") == 0)
673 (void) QueryColorCompliance(options,AllCompliance,
674 &image->border_color,exception);
677 (void) SetImageProperty(image,keyword,options,exception);
683 if (LocaleCompare(keyword,"class") == 0)
688 storage_class=ParseCommandOption(MagickClassOptions,
689 MagickFalse,options);
690 if (storage_class < 0)
692 image->storage_class=(ClassType) storage_class;
695 if (LocaleCompare(keyword,"colors") == 0)
697 colors=StringToUnsignedLong(options);
700 if (LocaleCompare(keyword,"colorspace") == 0)
705 colorspace=ParseCommandOption(MagickColorspaceOptions,
706 MagickFalse,options);
709 image->colorspace=(ColorspaceType) colorspace;
712 if (LocaleCompare(keyword,"compression") == 0)
717 compression=ParseCommandOption(MagickCompressOptions,
718 MagickFalse,options);
721 image->compression=(CompressionType) compression;
724 if (LocaleCompare(keyword,"columns") == 0)
726 image->columns=StringToUnsignedLong(options);
729 (void) SetImageProperty(image,keyword,options,exception);
735 if (LocaleCompare(keyword,"delay") == 0)
737 image->delay=StringToUnsignedLong(options);
740 if (LocaleCompare(keyword,"depth") == 0)
742 image->depth=StringToUnsignedLong(options);
745 if (LocaleCompare(keyword,"dispose") == 0)
750 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
754 image->dispose=(DisposeType) dispose;
757 (void) SetImageProperty(image,keyword,options,exception);
763 if (LocaleCompare(keyword,"endian") == 0)
768 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
772 image->endian=(EndianType) endian;
775 (void) SetImageProperty(image,keyword,options,exception);
781 if (LocaleCompare(keyword,"gamma") == 0)
783 image->gamma=StringToDouble(options,(char **) NULL);
786 if (LocaleCompare(keyword,"gravity") == 0)
791 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
795 image->gravity=(GravityType) gravity;
798 if (LocaleCompare(keyword,"green-primary") == 0)
800 flags=ParseGeometry(options,&geometry_info);
801 image->chromaticity.green_primary.x=geometry_info.rho;
802 image->chromaticity.green_primary.y=geometry_info.sigma;
803 if ((flags & SigmaValue) == 0)
804 image->chromaticity.green_primary.y=
805 image->chromaticity.green_primary.x;
808 (void) SetImageProperty(image,keyword,options,exception);
814 if (LocaleCompare(keyword,"id") == 0)
816 (void) CopyMagickString(id,options,MaxTextExtent);
819 if (LocaleCompare(keyword,"iterations") == 0)
821 image->iterations=StringToUnsignedLong(options);
824 (void) SetImageProperty(image,keyword,options,exception);
830 if (LocaleCompare(keyword,"matte") == 0)
835 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
839 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
843 if (LocaleCompare(keyword,"matte-color") == 0)
845 (void) QueryColorCompliance(options,AllCompliance,
846 &image->matte_color,exception);
849 if (LocaleCompare(keyword,"montage") == 0)
851 (void) CloneString(&image->montage,options);
854 (void) SetImageProperty(image,keyword,options,exception);
860 if (LocaleCompare(keyword,"orientation") == 0)
865 orientation=ParseCommandOption(MagickOrientationOptions,
866 MagickFalse,options);
869 image->orientation=(OrientationType) orientation;
872 (void) SetImageProperty(image,keyword,options,exception);
878 if (LocaleCompare(keyword,"page") == 0)
883 geometry=GetPageGeometry(options);
884 (void) ParseAbsoluteGeometry(geometry,&image->page);
885 geometry=DestroyString(geometry);
888 if (LocaleCompare(keyword,"pixel-intensity") == 0)
893 intensity=ParseCommandOption(MagickPixelIntensityOptions,
894 MagickFalse,options);
897 image->intensity=(PixelIntensityMethod) intensity;
900 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
901 (LocaleNCompare(keyword,"profile-",8) == 0))
906 if (profiles == (LinkedListInfo *) NULL)
907 profiles=NewLinkedList(0);
908 (void) AppendValueToLinkedList(profiles,
909 AcquireString(keyword+8));
910 profile=BlobToStringInfo((const void *) NULL,(size_t)
911 StringToLong(options));
912 if (profile == (StringInfo *) NULL)
913 ThrowReaderException(ResourceLimitError,
914 "MemoryAllocationFailed");
915 (void) SetImageProfile(image,keyword+8,profile,exception);
916 profile=DestroyStringInfo(profile);
919 (void) SetImageProperty(image,keyword,options,exception);
925 if (LocaleCompare(keyword,"quality") == 0)
927 image->quality=StringToUnsignedLong(options);
930 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
931 (LocaleCompare(keyword,"quantum:format") == 0))
936 format=ParseCommandOption(MagickQuantumFormatOptions,
937 MagickFalse,options);
940 quantum_format=(QuantumFormatType) format;
943 (void) SetImageProperty(image,keyword,options,exception);
949 if (LocaleCompare(keyword,"red-primary") == 0)
951 flags=ParseGeometry(options,&geometry_info);
952 image->chromaticity.red_primary.x=geometry_info.rho;
953 image->chromaticity.red_primary.y=geometry_info.sigma;
954 if ((flags & SigmaValue) == 0)
955 image->chromaticity.red_primary.y=
956 image->chromaticity.red_primary.x;
959 if (LocaleCompare(keyword,"rendering-intent") == 0)
964 rendering_intent=ParseCommandOption(MagickIntentOptions,
965 MagickFalse,options);
966 if (rendering_intent < 0)
968 image->rendering_intent=(RenderingIntent) rendering_intent;
971 if (LocaleCompare(keyword,"resolution") == 0)
973 flags=ParseGeometry(options,&geometry_info);
974 image->resolution.x=geometry_info.rho;
975 image->resolution.y=geometry_info.sigma;
976 if ((flags & SigmaValue) == 0)
977 image->resolution.y=image->resolution.x;
980 if (LocaleCompare(keyword,"rows") == 0)
982 image->rows=StringToUnsignedLong(options);
985 (void) SetImageProperty(image,keyword,options,exception);
991 if (LocaleCompare(keyword,"scene") == 0)
993 image->scene=StringToUnsignedLong(options);
996 (void) SetImageProperty(image,keyword,options,exception);
1002 if (LocaleCompare(keyword,"ticks-per-second") == 0)
1004 image->ticks_per_second=(ssize_t) StringToLong(options);
1007 if (LocaleCompare(keyword,"tile-offset") == 0)
1012 geometry=GetPageGeometry(options);
1013 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1014 geometry=DestroyString(geometry);
1017 if (LocaleCompare(keyword,"type") == 0)
1022 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
1026 image->type=(ImageType) type;
1029 (void) SetImageProperty(image,keyword,options,exception);
1035 if (LocaleCompare(keyword,"units") == 0)
1040 units=ParseCommandOption(MagickResolutionOptions,
1041 MagickFalse,options);
1044 image->units=(ResolutionType) units;
1047 (void) SetImageProperty(image,keyword,options,exception);
1053 if (LocaleCompare(keyword,"version") == 0)
1055 version=StringToDouble(options,(char **) NULL);
1058 (void) SetImageProperty(image,keyword,options,exception);
1064 if (LocaleCompare(keyword,"white-point") == 0)
1066 flags=ParseGeometry(options,&geometry_info);
1067 image->chromaticity.white_point.x=geometry_info.rho;
1068 image->chromaticity.white_point.y=geometry_info.sigma;
1069 if ((flags & SigmaValue) == 0)
1070 image->chromaticity.white_point.y=
1071 image->chromaticity.white_point.x;
1074 (void) SetImageProperty(image,keyword,options,exception);
1079 (void) SetImageProperty(image,keyword,options,exception);
1085 c=ReadBlobByte(image);
1086 while (isspace((int) ((unsigned char) c)) != 0)
1087 c=ReadBlobByte(image);
1089 options=DestroyString(options);
1090 (void) ReadBlobByte(image);
1092 Verify that required image information is defined.
1094 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1095 (image->storage_class == UndefinedClass) ||
1096 (image->columns == 0) || (image->rows == 0))
1097 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1098 if (image->montage != (char *) NULL)
1106 length=MaxTextExtent;
1107 image->directory=AcquireString((char *) NULL);
1112 if ((strlen(image->directory)+MaxTextExtent) >= length)
1115 Allocate more memory for the image directory.
1118 image->directory=(char *) ResizeQuantumMemory(image->directory,
1119 length+MaxTextExtent,sizeof(*image->directory));
1120 if (image->directory == (char *) NULL)
1121 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1122 p=image->directory+strlen(image->directory);
1124 c=ReadBlobByte(image);
1126 } while (c != (int) '\0');
1128 if (profiles != (LinkedListInfo *) NULL)
1137 Read image profiles.
1139 ResetLinkedListIterator(profiles);
1140 name=(const char *) GetNextValueInLinkedList(profiles);
1141 while (name != (const char *) NULL)
1143 profile=GetImageProfile(image,name);
1144 if (profile != (StringInfo *) NULL)
1146 register unsigned char
1149 p=GetStringInfoDatum(profile);
1150 count=ReadBlob(image,GetStringInfoLength(profile),p);
1153 name=(const char *) GetNextValueInLinkedList(profiles);
1155 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1157 image->depth=GetImageQuantumDepth(image,MagickFalse);
1158 if (image->storage_class == PseudoClass)
1161 Create image colormap.
1163 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1164 if (status == MagickFalse)
1165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1175 Read image colormap from file.
1177 packet_size=(size_t) (3UL*image->depth/8UL);
1178 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1179 packet_size*sizeof(*colormap));
1180 if (colormap == (unsigned char *) NULL)
1181 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1182 count=ReadBlob(image,packet_size*image->colors,colormap);
1184 switch (image->depth)
1187 ThrowReaderException(CorruptImageError,
1188 "ImageDepthNotSupported");
1194 for (i=0; i < (ssize_t) image->colors; i++)
1196 p=PushCharPixel(p,&pixel);
1197 image->colormap[i].red=ScaleCharToQuantum(pixel);
1198 p=PushCharPixel(p,&pixel);
1199 image->colormap[i].green=ScaleCharToQuantum(pixel);
1200 p=PushCharPixel(p,&pixel);
1201 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1210 for (i=0; i < (ssize_t) image->colors; i++)
1212 p=PushShortPixel(MSBEndian,p,&pixel);
1213 image->colormap[i].red=ScaleShortToQuantum(pixel);
1214 p=PushShortPixel(MSBEndian,p,&pixel);
1215 image->colormap[i].green=ScaleShortToQuantum(pixel);
1216 p=PushShortPixel(MSBEndian,p,&pixel);
1217 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1226 for (i=0; i < (ssize_t) image->colors; i++)
1228 p=PushLongPixel(MSBEndian,p,&pixel);
1229 image->colormap[i].red=ScaleLongToQuantum(pixel);
1230 p=PushLongPixel(MSBEndian,p,&pixel);
1231 image->colormap[i].green=ScaleLongToQuantum(pixel);
1232 p=PushLongPixel(MSBEndian,p,&pixel);
1233 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1238 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1241 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1242 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1245 Allocate image pixels.
1247 quantum_info=AcquireQuantumInfo(image_info,image);
1248 if (quantum_info == (QuantumInfo *) NULL)
1249 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1250 if (quantum_format != UndefinedQuantumFormat)
1252 status=SetQuantumFormat(image,quantum_info,quantum_format);
1253 if (status == MagickFalse)
1254 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1256 packet_size=(size_t) (quantum_info->depth/8);
1257 if (image->storage_class == DirectClass)
1258 packet_size=(size_t) (3*quantum_info->depth/8);
1259 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1260 packet_size=quantum_info->depth/8;
1261 if (image->alpha_trait == BlendPixelTrait)
1262 packet_size+=quantum_info->depth/8;
1263 if (image->colorspace == CMYKColorspace)
1264 packet_size+=quantum_info->depth/8;
1265 if (image->compression == RLECompression)
1267 length=image->columns;
1268 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1269 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1271 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1272 sizeof(*compress_pixels));
1273 if (compress_pixels == (unsigned char *) NULL)
1274 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1278 quantum_type=RGBQuantum;
1279 if (image->alpha_trait == BlendPixelTrait)
1280 quantum_type=RGBAQuantum;
1281 if (image->colorspace == CMYKColorspace)
1283 quantum_type=CMYKQuantum;
1284 if (image->alpha_trait == BlendPixelTrait)
1285 quantum_type=CMYKAQuantum;
1287 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1289 quantum_type=GrayQuantum;
1290 if (image->alpha_trait == BlendPixelTrait)
1291 quantum_type=GrayAlphaQuantum;
1293 if (image->storage_class == PseudoClass)
1295 quantum_type=IndexQuantum;
1296 if (image->alpha_trait == BlendPixelTrait)
1297 quantum_type=IndexAlphaQuantum;
1300 GetPixelInfo(image,&pixel);
1301 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1302 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1304 #if defined(MAGICKCORE_LZMA_DELEGATE)
1305 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1307 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1308 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1310 switch (image->compression)
1312 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1313 case BZipCompression:
1318 bzip_info.bzalloc=AcquireBZIPMemory;
1319 bzip_info.bzfree=RelinquishBZIPMemory;
1320 bzip_info.opaque=(void *) NULL;
1321 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1328 #if defined(MAGICKCORE_LZMA_DELEGATE)
1329 case LZMACompression:
1334 allocator.alloc=AcquireLZMAMemory;
1335 allocator.free=RelinquishLZMAMemory;
1336 lzma_info=initialize_lzma;
1337 lzma_info.allocator=(&allocator);
1338 code=lzma_auto_decoder(&lzma_info,-1,0);
1339 if (code != LZMA_OK)
1344 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1345 case LZWCompression:
1346 case ZipCompression:
1351 zip_info.zalloc=AcquireZIPMemory;
1352 zip_info.zfree=RelinquishZIPMemory;
1353 zip_info.opaque=(voidpf) NULL;
1354 code=inflateInit(&zip_info);
1360 case RLECompression:
1365 pixels=GetQuantumPixels(quantum_info);
1367 for (y=0; y < (ssize_t) image->rows; y++)
1375 if (status == MagickFalse)
1377 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1378 if (q == (Quantum *) NULL)
1380 switch (image->compression)
1382 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1383 case BZipCompression:
1385 bzip_info.next_out=(char *) pixels;
1386 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1389 if (bzip_info.avail_in == 0)
1391 bzip_info.next_in=(char *) compress_pixels;
1392 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1394 length=(size_t) ReadBlobMSBLong(image);
1395 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1396 (unsigned char *) bzip_info.next_in);
1398 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1400 } while (bzip_info.avail_out != 0);
1401 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1402 quantum_type,pixels,exception);
1406 #if defined(MAGICKCORE_LZMA_DELEGATE)
1407 case LZMACompression:
1409 lzma_info.next_out=pixels;
1410 lzma_info.avail_out=packet_size*image->columns;
1416 if (lzma_info.avail_in == 0)
1418 lzma_info.next_in=compress_pixels;
1419 length=(size_t) ReadBlobMSBLong(image);
1420 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1421 (unsigned char *) lzma_info.next_in);
1423 code=lzma_code(&lzma_info,LZMA_RUN);
1429 if (code == LZMA_STREAM_END)
1431 } while (lzma_info.avail_out != 0);
1432 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1433 quantum_type,pixels,exception);
1437 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1438 case LZWCompression:
1439 case ZipCompression:
1441 zip_info.next_out=pixels;
1442 zip_info.avail_out=(uInt) (packet_size*image->columns);
1445 if (zip_info.avail_in == 0)
1447 zip_info.next_in=compress_pixels;
1448 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1450 length=(size_t) ReadBlobMSBLong(image);
1451 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1454 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1456 } while (zip_info.avail_out != 0);
1457 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1458 quantum_type,pixels,exception);
1462 case RLECompression:
1464 for (x=0; x < (ssize_t) image->columns; x++)
1468 count=ReadBlob(image,packet_size,pixels);
1469 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
1472 if (image->storage_class == PseudoClass)
1473 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1476 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1477 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1478 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1479 if (image->colorspace == CMYKColorspace)
1480 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1482 if (image->alpha_trait == BlendPixelTrait)
1483 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1484 q+=GetPixelChannels(image);
1490 count=ReadBlob(image,packet_size*image->columns,pixels);
1491 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1492 quantum_type,pixels,exception);
1496 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1499 SetQuantumImageType(image,quantum_type);
1500 switch (image->compression)
1502 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1503 case BZipCompression:
1513 offset=SeekBlob(image,-((MagickOffsetType)
1514 bzip_info.avail_in),SEEK_CUR);
1516 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1518 code=BZ2_bzDecompressEnd(&bzip_info);
1524 #if defined(MAGICKCORE_LZMA_DELEGATE)
1525 case LZMACompression:
1530 code=lzma_code(&lzma_info,LZMA_FINISH);
1531 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1533 lzma_end(&lzma_info);
1537 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1538 case LZWCompression:
1539 case ZipCompression:
1549 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1552 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1554 code=inflateEnd(&zip_info);
1555 if (code != LZMA_OK)
1563 quantum_info=DestroyQuantumInfo(quantum_info);
1564 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1565 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1567 image=DestroyImageList(image);
1568 return((Image *) NULL);
1570 if (EOFBlob(image) != MagickFalse)
1572 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1577 Proceed to next image.
1579 if (image_info->number_scenes != 0)
1580 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1584 c=ReadBlobByte(image);
1585 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1589 Allocate next image structure.
1591 AcquireNextImage(image_info,image,exception);
1592 if (GetNextImageInList(image) == (Image *) NULL)
1594 image=DestroyImageList(image);
1595 return((Image *) NULL);
1597 image=SyncNextImageInList(image);
1598 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1599 GetBlobSize(image));
1600 if (status == MagickFalse)
1604 (void) CloseBlob(image);
1605 return(GetFirstImageInList(image));
1609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613 % R e g i s t e r M I F F I m a g e %
1617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1620 % supported formats. The properties include the image format tag, a method to
1621 % read and/or write the format, whether the format supports the saving of more
1622 % than one frame to the same file or blob, whether the format supports native
1623 % in-memory I/O, and a brief description of the format.
1625 % The format of the RegisterMIFFImage method is:
1627 % size_t RegisterMIFFImage(void)
1630 ModuleExport size_t RegisterMIFFImage(void)
1633 version[MaxTextExtent];
1639 #if defined(MagickImageCoderSignatureText)
1640 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1641 #if defined(ZLIB_VERSION)
1642 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1643 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1645 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1646 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1649 entry=SetMagickInfo("MIFF");
1650 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1651 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1652 entry->magick=(IsImageFormatHandler *) IsMIFF;
1653 entry->seekable_stream=MagickTrue;
1654 entry->description=ConstantString("Magick Image File Format");
1655 if (*version != '\0')
1656 entry->version=ConstantString(version);
1657 entry->module=ConstantString("MIFF");
1658 (void) RegisterMagickInfo(entry);
1659 return(MagickImageCoderSignature);
1663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1667 % U n r e g i s t e r M I F F I m a g e %
1671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1673 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1674 % from the list of supported formats.
1676 % The format of the UnregisterMIFFImage method is:
1678 % UnregisterMIFFImage(void)
1681 ModuleExport void UnregisterMIFFImage(void)
1683 (void) UnregisterMagickInfo("MIFF");
1687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1691 % W r i t e M I F F I m a g e %
1695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1697 % WriteMIFFImage() writes a MIFF image to a file.
1699 % The format of the WriteMIFFImage method is:
1701 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1702 % Image *image,ExceptionInfo *exception)
1704 % Compression code contributed by Kyle Shorter.
1706 % A description of each parameter follows:
1708 % o image_info: the image info.
1710 % o image: the image.
1712 % o exception: return any errors or warnings in this structure.
1716 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1717 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
1719 if (image->storage_class != DirectClass)
1724 value=(unsigned int) ClampToQuantum(pixel->index);
1725 switch (image->depth)
1729 *pixels++=(unsigned char) (value >> 24);
1730 *pixels++=(unsigned char) (value >> 16);
1733 *pixels++=(unsigned char) (value >> 8);
1736 *pixels++=(unsigned char) value;
1740 (void) ThrowMagickException(exception,GetMagickModule(),
1741 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1743 switch (image->depth)
1750 if (image->alpha_trait == BlendPixelTrait)
1752 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1753 pixels=PopLongPixel(MSBEndian,value,pixels);
1762 if (image->alpha_trait == BlendPixelTrait)
1764 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1765 pixels=PopShortPixel(MSBEndian,value,pixels);
1774 if (image->alpha_trait == BlendPixelTrait)
1776 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1778 pixels=PopCharPixel(value,pixels);
1783 (void) ThrowMagickException(exception,GetMagickModule(),
1784 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1786 *pixels++=(unsigned char) length;
1789 switch (image->depth)
1796 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1797 pixels=PopLongPixel(MSBEndian,value,pixels);
1798 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1800 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1801 pixels=PopLongPixel(MSBEndian,value,pixels);
1802 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1803 pixels=PopLongPixel(MSBEndian,value,pixels);
1805 if (image->colorspace == CMYKColorspace)
1807 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1808 pixels=PopLongPixel(MSBEndian,value,pixels);
1810 if (image->alpha_trait == BlendPixelTrait)
1812 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1813 pixels=PopLongPixel(MSBEndian,value,pixels);
1822 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1823 pixels=PopShortPixel(MSBEndian,value,pixels);
1824 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1826 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1827 pixels=PopShortPixel(MSBEndian,value,pixels);
1828 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1829 pixels=PopShortPixel(MSBEndian,value,pixels);
1831 if (image->colorspace == CMYKColorspace)
1833 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1834 pixels=PopShortPixel(MSBEndian,value,pixels);
1836 if (image->alpha_trait == BlendPixelTrait)
1838 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1839 pixels=PopShortPixel(MSBEndian,value,pixels);
1848 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1849 pixels=PopCharPixel(value,pixels);
1850 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1852 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1854 pixels=PopCharPixel(value,pixels);
1855 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1856 pixels=PopCharPixel(value,pixels);
1858 if (image->colorspace == CMYKColorspace)
1860 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1862 pixels=PopCharPixel(value,pixels);
1864 if (image->alpha_trait == BlendPixelTrait)
1866 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1868 pixels=PopCharPixel(value,pixels);
1873 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1874 "ImageDepthNotSupported","`%s'",image->filename);
1876 *pixels++=(unsigned char) length;
1880 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1881 Image *image,ExceptionInfo *exception)
1883 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1889 buffer[MaxTextExtent];
1898 #if defined(MAGICKCORE_LZMA_DELEGATE)
1903 initialize_lzma = LZMA_STREAM_INIT,
1938 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1944 Open output image file.
1946 assert(image_info != (const ImageInfo *) NULL);
1947 assert(image_info->signature == MagickSignature);
1948 assert(image != (Image *) NULL);
1949 assert(image->signature == MagickSignature);
1950 if (image->debug != MagickFalse)
1951 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1952 assert(exception != (ExceptionInfo *) NULL);
1953 assert(exception->signature == MagickSignature);
1954 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1955 if (status == MagickFalse)
1961 Allocate image pixels.
1963 if ((image->storage_class == PseudoClass) &&
1964 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1965 (void) SetImageStorageClass(image,DirectClass,exception);
1966 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1967 image->depth <= 32 ? 32UL : 64UL;
1968 if (IsImageGray(image,exception) == MagickFalse)
1971 sRGB masquerading as a grayscale image?
1973 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1974 (void) SetImageColorspace(image,sRGBColorspace,exception);
1977 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1978 (void) SetImageColorspace(image,GRAYColorspace,exception);
1979 quantum_info=AcquireQuantumInfo(image_info,image);
1980 if (quantum_info == (QuantumInfo *) NULL)
1981 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1982 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1983 (quantum_info->format == UndefinedQuantumFormat) &&
1984 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1986 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1987 if (status == MagickFalse)
1988 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1990 compression=UndefinedCompression;
1991 if (image_info->compression != UndefinedCompression)
1992 compression=image_info->compression;
1993 switch (compression)
1995 #if !defined(MAGICKCORE_LZMA_DELEGATE)
1996 case LZMACompression: compression=NoCompression; break;
1998 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1999 case LZWCompression:
2000 case ZipCompression: compression=NoCompression; break;
2002 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
2003 case BZipCompression: compression=NoCompression; break;
2005 case RLECompression:
2007 if (quantum_info->format == FloatingPointQuantumFormat)
2008 compression=NoCompression;
2009 GetPixelInfo(image,&target);
2015 packet_size=(size_t) (quantum_info->depth/8);
2016 if (image->storage_class == DirectClass)
2017 packet_size=(size_t) (3*quantum_info->depth/8);
2018 if (IsGrayColorspace(image->colorspace) != MagickFalse)
2019 packet_size=(size_t) (quantum_info->depth/8);
2020 if (image->alpha_trait == BlendPixelTrait)
2021 packet_size+=quantum_info->depth/8;
2022 if (image->colorspace == CMYKColorspace)
2023 packet_size+=quantum_info->depth/8;
2024 if (compression == RLECompression)
2026 length=image->columns;
2027 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2028 packet_size*image->columns));
2029 if ((compression == BZipCompression) || (compression == ZipCompression))
2030 if (length != (size_t) ((unsigned int) length))
2031 compression=NoCompression;
2032 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2033 sizeof(*compress_pixels));
2034 if (compress_pixels == (unsigned char *) NULL)
2035 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2039 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
2040 (void) FormatLocaleString(buffer,MaxTextExtent,
2041 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
2042 MagickClassOptions,image->storage_class),(double) image->colors,
2043 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
2044 image->alpha_trait));
2045 (void) WriteBlobString(image,buffer);
2046 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
2047 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
2049 (void) WriteBlobString(image,buffer);
2050 if (image->type != UndefinedType)
2052 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
2053 CommandOptionToMnemonic(MagickTypeOptions,image->type));
2054 (void) WriteBlobString(image,buffer);
2056 if (image->colorspace != UndefinedColorspace)
2058 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
2059 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
2060 (void) WriteBlobString(image,buffer);
2062 if (image->intensity != UndefinedPixelIntensityMethod)
2064 (void) FormatLocaleString(buffer,MaxTextExtent,"pixel-intensity=%s\n",
2065 CommandOptionToMnemonic(MagickPixelIntensityOptions,
2067 (void) WriteBlobString(image,buffer);
2069 if (image->endian != UndefinedEndian)
2071 (void) FormatLocaleString(buffer,MaxTextExtent,"endian=%s\n",
2072 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2073 (void) WriteBlobString(image,buffer);
2075 if (compression != UndefinedCompression)
2077 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
2078 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
2079 compression),(double) image->quality);
2080 (void) WriteBlobString(image,buffer);
2082 if (image->units != UndefinedResolution)
2084 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
2085 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
2086 (void) WriteBlobString(image,buffer);
2088 if ((image->resolution.x != 0) || (image->resolution.y != 0))
2090 (void) FormatLocaleString(buffer,MaxTextExtent,
2091 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
2092 (void) WriteBlobString(image,buffer);
2094 if ((image->page.width != 0) || (image->page.height != 0))
2096 (void) FormatLocaleString(buffer,MaxTextExtent,
2097 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2098 image->page.height,(double) image->page.x,(double) image->page.y);
2099 (void) WriteBlobString(image,buffer);
2102 if ((image->page.x != 0) || (image->page.y != 0))
2104 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2105 (long) image->page.x,(long) image->page.y);
2106 (void) WriteBlobString(image,buffer);
2108 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2110 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2111 (long) image->tile_offset.x,(long) image->tile_offset.y);
2112 (void) WriteBlobString(image,buffer);
2114 if ((GetNextImageInList(image) != (Image *) NULL) ||
2115 (GetPreviousImageInList(image) != (Image *) NULL))
2117 if (image->scene == 0)
2118 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2119 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2120 (double) image->delay,(double) image->ticks_per_second);
2122 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2123 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2124 image->scene,(double) image->iterations,(double) image->delay,
2125 (double) image->ticks_per_second);
2126 (void) WriteBlobString(image,buffer);
2130 if (image->scene != 0)
2132 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2133 (double) image->scene);
2134 (void) WriteBlobString(image,buffer);
2136 if (image->iterations != 0)
2138 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2139 (double) image->iterations);
2140 (void) WriteBlobString(image,buffer);
2142 if (image->delay != 0)
2144 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2145 (double) image->delay);
2146 (void) WriteBlobString(image,buffer);
2148 if (image->ticks_per_second != UndefinedTicksPerSecond)
2150 (void) FormatLocaleString(buffer,MaxTextExtent,
2151 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2152 (void) WriteBlobString(image,buffer);
2155 if (image->gravity != UndefinedGravity)
2157 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2158 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2159 (void) WriteBlobString(image,buffer);
2161 if (image->dispose != UndefinedDispose)
2163 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2164 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2165 (void) WriteBlobString(image,buffer);
2167 if (image->rendering_intent != UndefinedIntent)
2169 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2170 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2171 (void) WriteBlobString(image,buffer);
2173 if (image->gamma != 0.0)
2175 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2177 (void) WriteBlobString(image,buffer);
2179 if (image->chromaticity.white_point.x != 0.0)
2182 Note chomaticity points.
2184 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2185 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2186 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2187 image->chromaticity.green_primary.x,
2188 image->chromaticity.green_primary.y,
2189 image->chromaticity.blue_primary.x,
2190 image->chromaticity.blue_primary.y);
2191 (void) WriteBlobString(image,buffer);
2192 (void) FormatLocaleString(buffer,MaxTextExtent,
2193 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2194 image->chromaticity.white_point.y);
2195 (void) WriteBlobString(image,buffer);
2197 if (image->orientation != UndefinedOrientation)
2199 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2200 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2201 (void) WriteBlobString(image,buffer);
2203 if (image->profiles != (void *) NULL)
2212 Write image profiles.
2214 ResetImageProfileIterator(image);
2215 name=GetNextImageProfile(image);
2216 while (name != (const char *) NULL)
2218 profile=GetImageProfile(image,name);
2219 if (profile != (StringInfo *) NULL)
2221 (void) FormatLocaleString(buffer,MaxTextExtent,
2222 "profile:%s=%.20g\n",name,(double)
2223 GetStringInfoLength(profile));
2224 (void) WriteBlobString(image,buffer);
2226 name=GetNextImageProfile(image);
2229 if (image->montage != (char *) NULL)
2231 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2233 (void) WriteBlobString(image,buffer);
2235 if (quantum_info->format == FloatingPointQuantumFormat)
2236 (void) SetImageProperty(image,"quantum:format","floating-point",
2238 ResetImagePropertyIterator(image);
2239 property=GetNextImageProperty(image);
2240 while (property != (const char *) NULL)
2242 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2243 (void) WriteBlobString(image,buffer);
2244 value=GetImageProperty(image,property,exception);
2245 if (value != (const char *) NULL)
2250 length=strlen(value);
2251 for (i=0; i < (ssize_t) length; i++)
2252 if (isspace((int) ((unsigned char) value[i])) != 0)
2254 if ((i == (ssize_t) length) && (i != 0))
2255 (void) WriteBlob(image,length,(const unsigned char *) value);
2258 (void) WriteBlobByte(image,'{');
2259 if (strchr(value,'}') == (char *) NULL)
2260 (void) WriteBlob(image,length,(const unsigned char *) value);
2262 for (i=0; i < (ssize_t) length; i++)
2264 if (value[i] == (int) '}')
2265 (void) WriteBlobByte(image,'\\');
2266 (void) WriteBlobByte(image,value[i]);
2268 (void) WriteBlobByte(image,'}');
2271 (void) WriteBlobByte(image,'\n');
2272 property=GetNextImageProperty(image);
2274 (void) WriteBlobString(image,"\f\n:\032");
2275 if (image->montage != (char *) NULL)
2278 Write montage tile directory.
2280 if (image->directory != (char *) NULL)
2281 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2283 (void) WriteBlobByte(image,'\0');
2285 if (image->profiles != (void *) NULL)
2296 ResetImageProfileIterator(image);
2297 name=GetNextImageProfile(image);
2298 while (name != (const char *) NULL)
2300 profile=GetImageProfile(image,name);
2301 (void) WriteBlob(image,GetStringInfoLength(profile),
2302 GetStringInfoDatum(profile));
2303 name=GetNextImageProfile(image);
2306 if (image->storage_class == PseudoClass)
2318 packet_size=(size_t) (3*quantum_info->depth/8);
2319 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2320 packet_size*sizeof(*colormap));
2321 if (colormap == (unsigned char *) NULL)
2322 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2324 Write colormap to file.
2327 for (i=0; i < (ssize_t) image->colors; i++)
2329 switch (quantum_info->depth)
2332 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2335 register unsigned int
2338 pixel=ScaleQuantumToLong(image->colormap[i].red);
2339 q=PopLongPixel(MSBEndian,pixel,q);
2340 pixel=ScaleQuantumToLong(image->colormap[i].green);
2341 q=PopLongPixel(MSBEndian,pixel,q);
2342 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2343 q=PopLongPixel(MSBEndian,pixel,q);
2348 register unsigned short
2351 pixel=ScaleQuantumToShort(image->colormap[i].red);
2352 q=PopShortPixel(MSBEndian,pixel,q);
2353 pixel=ScaleQuantumToShort(image->colormap[i].green);
2354 q=PopShortPixel(MSBEndian,pixel,q);
2355 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2356 q=PopShortPixel(MSBEndian,pixel,q);
2361 register unsigned char
2364 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2365 q=PopCharPixel(pixel,q);
2366 pixel=(unsigned char) ScaleQuantumToChar(
2367 image->colormap[i].green);
2368 q=PopCharPixel(pixel,q);
2369 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2370 q=PopCharPixel(pixel,q);
2375 (void) WriteBlob(image,packet_size*image->colors,colormap);
2376 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2379 Write image pixels to file.
2382 switch (compression)
2384 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2385 case BZipCompression:
2390 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2391 bzip_info.bzalloc=AcquireBZIPMemory;
2392 bzip_info.bzfree=RelinquishBZIPMemory;
2393 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2394 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2395 9)),(int) image_info->verbose,0);
2401 #if defined(MAGICKCORE_LZMA_DELEGATE)
2402 case LZMACompression:
2407 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2408 allocator.alloc=AcquireLZMAMemory;
2409 allocator.free=RelinquishLZMAMemory;
2410 lzma_info=initialize_lzma;
2411 lzma_info.allocator=&allocator;
2412 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2413 if (code != LZMA_OK)
2418 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2419 case LZWCompression:
2420 case ZipCompression:
2425 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2426 zip_info.zalloc=AcquireZIPMemory;
2427 zip_info.zfree=RelinquishZIPMemory;
2428 code=deflateInit(&zip_info,(int) (image->quality ==
2429 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2438 quantum_type=GetQuantumType(image,exception);
2439 pixels=GetQuantumPixels(quantum_info);
2440 for (y=0; y < (ssize_t) image->rows; y++)
2442 register const Quantum
2448 if (status == MagickFalse)
2450 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2451 if (p == (const Quantum *) NULL)
2454 switch (compression)
2456 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2457 case BZipCompression:
2459 bzip_info.next_in=(char *) pixels;
2460 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2461 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2462 quantum_type,pixels,exception);
2468 bzip_info.next_out=(char *) compress_pixels;
2469 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2471 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2474 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2477 (void) WriteBlobMSBLong(image,(unsigned int) length);
2478 (void) WriteBlob(image,length,compress_pixels);
2480 } while (bzip_info.avail_in != 0);
2484 #if defined(MAGICKCORE_LZMA_DELEGATE)
2485 case LZMACompression:
2487 lzma_info.next_in=pixels;
2488 lzma_info.avail_in=packet_size*image->columns;
2489 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2490 quantum_type,pixels,exception);
2496 lzma_info.next_out=compress_pixels;
2497 lzma_info.avail_out=packet_size*image->columns;
2498 code=lzma_code(&lzma_info,LZMA_RUN);
2499 if (code != LZMA_OK)
2501 length=(size_t) (lzma_info.next_out-compress_pixels);
2504 (void) WriteBlobMSBLong(image,(unsigned int) length);
2505 (void) WriteBlob(image,length,compress_pixels);
2507 } while (lzma_info.avail_in != 0);
2511 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2512 case LZWCompression:
2513 case ZipCompression:
2515 zip_info.next_in=pixels;
2516 zip_info.avail_in=(uInt) (packet_size*image->columns);
2517 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2518 quantum_type,pixels,exception);
2524 zip_info.next_out=compress_pixels;
2525 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2526 code=deflate(&zip_info,Z_SYNC_FLUSH);
2529 length=(size_t) (zip_info.next_out-compress_pixels);
2532 (void) WriteBlobMSBLong(image,(unsigned int) length);
2533 (void) WriteBlob(image,length,compress_pixels);
2535 } while (zip_info.avail_in != 0);
2539 case RLECompression:
2542 GetPixelInfoPixel(image,p,&pixel);
2543 p+=GetPixelChannels(image);
2544 for (x=1; x < (ssize_t) image->columns; x++)
2546 GetPixelInfoPixel(image,p,&target);
2547 if ((length < 255) &&
2548 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2552 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2555 GetPixelInfoPixel(image,p,&pixel);
2556 p+=GetPixelChannels(image);
2558 q=PopRunlengthPacket(image,q,length,&pixel,exception);
2559 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2564 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2565 quantum_type,pixels,exception);
2566 (void) WriteBlob(image,packet_size*image->columns,pixels);
2570 if (image->previous == (Image *) NULL)
2572 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2574 if (status == MagickFalse)
2578 switch (compression)
2580 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2581 case BZipCompression:
2588 if (status == MagickFalse)
2590 bzip_info.next_out=(char *) compress_pixels;
2591 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2593 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2594 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2597 (void) WriteBlobMSBLong(image,(unsigned int) length);
2598 (void) WriteBlob(image,length,compress_pixels);
2600 if (code == BZ_STREAM_END)
2603 code=BZ2_bzCompressEnd(&bzip_info);
2609 #if defined(MAGICKCORE_LZMA_DELEGATE)
2610 case LZMACompression:
2617 if (status == MagickFalse)
2619 lzma_info.next_out=compress_pixels;
2620 lzma_info.avail_out=packet_size*image->columns;
2621 code=lzma_code(&lzma_info,LZMA_FINISH);
2622 length=(size_t) (lzma_info.next_out-compress_pixels);
2625 (void) WriteBlobMSBLong(image,(unsigned int) length);
2626 (void) WriteBlob(image,length,compress_pixels);
2628 if (code == LZMA_STREAM_END)
2631 lzma_end(&lzma_info);
2635 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2636 case LZWCompression:
2637 case ZipCompression:
2644 if (status == MagickFalse)
2646 zip_info.next_out=compress_pixels;
2647 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2648 code=deflate(&zip_info,Z_FINISH);
2649 length=(size_t) (zip_info.next_out-compress_pixels);
2652 (void) WriteBlobMSBLong(image,(unsigned int) length);
2653 (void) WriteBlob(image,length,compress_pixels);
2655 if (code == Z_STREAM_END)
2658 code=deflateEnd(&zip_info);
2667 quantum_info=DestroyQuantumInfo(quantum_info);
2668 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2669 if (GetNextImageInList(image) == (Image *) NULL)
2671 image=SyncNextImageInList(image);
2672 status=SetImageProgress(image,SaveImagesTag,scene++,
2673 GetImageListLength(image));
2674 if (status == MagickFalse)
2676 } while (image_info->adjoin != MagickFalse);
2677 (void) CloseBlob(image);