2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M IIIII FFFFF FFFFF %
13 % Read/Write MIFF Image Format %
20 % Copyright 1999-2011 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/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/hashmap.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/option.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/profile.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/static.h"
72 #include "MagickCore/statistic.h"
73 #include "MagickCore/string_.h"
74 #include "MagickCore/string-private.h"
75 #if defined(MAGICKCORE_BZLIB_DELEGATE)
78 #if defined(MAGICKCORE_LZMA_DELEGATE)
81 #if defined(MAGICKCORE_ZLIB_DELEGATE)
95 static MagickBooleanType
96 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % IsMIFF() returns MagickTrue if the image format type, identified by the
110 % magick string, is MIFF.
112 % The format of the IsMIFF method is:
114 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
116 % A description of each parameter follows:
118 % o magick: compare image format pattern against these bytes.
120 % o length: Specifies the length of the magick string.
123 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
127 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 % R e a d M I F F I m a g e %
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
144 % memory necessary for the new Image structure and returns a pointer to the
147 % The format of the ReadMIFFImage method is:
149 % Image *ReadMIFFImage(const ImageInfo *image_info,
150 % ExceptionInfo *exception)
152 % Decompression code contributed by Kyle Shorter.
154 % A description of each parameter follows:
156 % o image_info: the image info.
158 % o exception: return any errors or warnings in this structure.
162 #if defined(MAGICKCORE_BZLIB_DELEGATE)
163 static void *AcquireBZIPMemory(void *context,int items,int size)
166 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
170 #if defined(MAGICKCORE_LZMA_DELEGATE)
171 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
174 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
178 #if defined(MAGICKCORE_ZLIB_DELEGATE)
179 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
183 return((voidpf) AcquireQuantumMemory(items,size));
187 static inline size_t MagickMax(const size_t x,const size_t y)
194 static inline size_t MagickMin(const size_t x,const size_t y)
201 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
202 size_t *length,PixelInfo *pixel)
208 if (image->storage_class == PseudoClass)
211 switch (image->depth)
215 pixel->index=ConstrainColormapIndex(image,
216 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3));
222 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1));
228 pixel->index=ConstrainColormapIndex(image,*p);
233 (void) ThrowMagickException(&image->exception,GetMagickModule(),
234 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
236 switch (image->depth)
243 if (image->matte != MagickFalse)
245 p=PushCharPixel(p,&quantum);
246 pixel->alpha=ScaleCharToQuantum(quantum);
255 if (image->matte != MagickFalse)
257 p=PushShortPixel(MSBEndian,p,&quantum);
258 pixel->alpha=(Quantum) (quantum >> (image->depth-
259 MAGICKCORE_QUANTUM_DEPTH));
268 if (image->matte != MagickFalse)
270 p=PushLongPixel(MSBEndian,p,&quantum);
271 pixel->alpha=(Quantum) (quantum >> (image->depth-
272 MAGICKCORE_QUANTUM_DEPTH));
277 (void) ThrowMagickException(&image->exception,GetMagickModule(),
278 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
280 *length=(size_t) (*p++)+1;
283 switch (image->depth)
290 p=PushCharPixel(p,&quantum);
291 pixel->red=ScaleCharToQuantum(quantum);
292 p=PushCharPixel(p,&quantum);
293 pixel->green=ScaleCharToQuantum(quantum);
294 p=PushCharPixel(p,&quantum);
295 pixel->blue=ScaleCharToQuantum(quantum);
296 if (image->colorspace == CMYKColorspace)
298 p=PushCharPixel(p,&quantum);
299 pixel->black=ScaleCharToQuantum(quantum);
301 if (image->matte != MagickFalse)
303 p=PushCharPixel(p,&quantum);
304 pixel->alpha=ScaleCharToQuantum(quantum);
313 p=PushShortPixel(MSBEndian,p,&quantum);
314 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
315 p=PushShortPixel(MSBEndian,p,&quantum);
316 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
317 p=PushShortPixel(MSBEndian,p,&quantum);
318 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
319 if (image->colorspace == CMYKColorspace)
321 p=PushShortPixel(MSBEndian,p,&quantum);
322 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
324 if (image->matte != MagickFalse)
326 p=PushShortPixel(MSBEndian,p,&quantum);
327 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
336 p=PushLongPixel(MSBEndian,p,&quantum);
337 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
338 p=PushLongPixel(MSBEndian,p,&quantum);
339 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
340 p=PushLongPixel(MSBEndian,p,&quantum);
341 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
342 if (image->colorspace == CMYKColorspace)
344 p=PushLongPixel(MSBEndian,p,&quantum);
345 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
347 if (image->matte != MagickFalse)
349 p=PushLongPixel(MSBEndian,p,&quantum);
350 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
355 (void) ThrowMagickException(&image->exception,GetMagickModule(),
356 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
358 *length=(size_t) (*p++)+1;
361 #if defined(MAGICKCORE_BZLIB_DELEGATE)
362 static void RelinquishBZIPMemory(void *context,void *memory)
365 memory=RelinquishMagickMemory(memory);
369 #if defined(MAGICKCORE_LZMA_DELEGATE)
370 static void RelinquishLZMAMemory(void *context,void *memory)
373 memory=RelinquishMagickMemory(memory);
377 #if defined(MAGICKCORE_ZLIB_DELEGATE)
378 static void RelinquishZIPMemory(voidpf context,voidpf memory)
381 memory=RelinquishMagickMemory(memory);
385 static Image *ReadMIFFImage(const ImageInfo *image_info,
386 ExceptionInfo *exception)
388 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
389 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
390 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
392 #if defined(MAGICKCORE_BZLIB_DELEGATE)
399 keyword[MaxTextExtent],
418 #if defined(MAGICKCORE_LZMA_DELEGATE)
420 initialize_lzma = LZMA_STREAM_INIT,
468 #if defined(MAGICKCORE_ZLIB_DELEGATE)
476 assert(image_info != (const ImageInfo *) NULL);
477 assert(image_info->signature == MagickSignature);
478 if (image_info->debug != MagickFalse)
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
480 image_info->filename);
481 assert(exception != (ExceptionInfo *) NULL);
482 assert(exception->signature == MagickSignature);
483 image=AcquireImage(image_info);
484 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
485 if (status == MagickFalse)
487 image=DestroyImageList(image);
488 return((Image *) NULL);
491 Decode image header; header terminates one character beyond a ':'.
493 c=ReadBlobByte(image);
495 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
498 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
503 Decode image header; header terminates one character beyond a ':'.
505 length=MaxTextExtent;
506 options=AcquireString((char *) NULL);
507 quantum_format=UndefinedQuantumFormat;
508 profiles=(LinkedListInfo *) NULL;
511 image->compression=NoCompression;
512 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
523 Read comment-- any text between { }.
525 length=MaxTextExtent;
526 comment=AcquireString((char *) NULL);
527 for (p=comment; comment != (char *) NULL; p++)
529 c=ReadBlobByte(image);
530 if ((c == EOF) || (c == (int) '}'))
532 if ((size_t) (p-comment+1) >= length)
536 comment=(char *) ResizeQuantumMemory(comment,length+
537 MaxTextExtent,sizeof(*comment));
538 if (comment == (char *) NULL)
540 p=comment+strlen(comment);
544 if (comment == (char *) NULL)
545 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
547 (void) SetImageProperty(image,"comment",comment);
548 comment=DestroyString(comment);
549 c=ReadBlobByte(image);
552 if (isalnum(c) != MagickFalse)
562 if ((size_t) (p-keyword) < (MaxTextExtent-1))
564 c=ReadBlobByte(image);
568 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
569 c=ReadBlobByte(image);
573 Get the keyword value.
575 c=ReadBlobByte(image);
576 while ((c != (int) '}') && (c != EOF))
578 if ((size_t) (p-options+1) >= length)
582 options=(char *) ResizeQuantumMemory(options,length+
583 MaxTextExtent,sizeof(*options));
584 if (options == (char *) NULL)
586 p=options+strlen(options);
588 if (options == (char *) NULL)
589 ThrowReaderException(ResourceLimitError,
590 "MemoryAllocationFailed");
592 c=ReadBlobByte(image);
594 if (isspace((int) ((unsigned char) c)) != 0)
600 (void) CopyMagickString(options,options+1,MaxTextExtent);
602 Assign a value to the specified keyword.
609 if (LocaleCompare(keyword,"background-color") == 0)
611 (void) QueryColorDatabase(options,&image->background_color,
615 if (LocaleCompare(keyword,"blue-primary") == 0)
617 flags=ParseGeometry(options,&geometry_info);
618 image->chromaticity.blue_primary.x=geometry_info.rho;
619 image->chromaticity.blue_primary.y=geometry_info.sigma;
620 if ((flags & SigmaValue) == 0)
621 image->chromaticity.blue_primary.y=
622 image->chromaticity.blue_primary.x;
625 if (LocaleCompare(keyword,"border-color") == 0)
627 (void) QueryColorDatabase(options,&image->border_color,
631 (void) SetImageProperty(image,keyword,options);
637 if (LocaleCompare(keyword,"class") == 0)
642 storage_class=ParseCommandOption(MagickClassOptions,
643 MagickFalse,options);
644 if (storage_class < 0)
646 image->storage_class=(ClassType) storage_class;
649 if (LocaleCompare(keyword,"colors") == 0)
651 colors=StringToUnsignedLong(options);
654 if (LocaleCompare(keyword,"colorspace") == 0)
659 colorspace=ParseCommandOption(MagickColorspaceOptions,
660 MagickFalse,options);
663 image->colorspace=(ColorspaceType) colorspace;
666 if (LocaleCompare(keyword,"compression") == 0)
671 compression=ParseCommandOption(MagickCompressOptions,
672 MagickFalse,options);
675 image->compression=(CompressionType) compression;
678 if (LocaleCompare(keyword,"columns") == 0)
680 image->columns=StringToUnsignedLong(options);
683 (void) SetImageProperty(image,keyword,options);
689 if (LocaleCompare(keyword,"delay") == 0)
691 image->delay=StringToUnsignedLong(options);
694 if (LocaleCompare(keyword,"depth") == 0)
696 image->depth=StringToUnsignedLong(options);
699 if (LocaleCompare(keyword,"dispose") == 0)
704 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
708 image->dispose=(DisposeType) dispose;
711 (void) SetImageProperty(image,keyword,options);
717 if (LocaleCompare(keyword,"endian") == 0)
722 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
726 image->endian=(EndianType) endian;
729 (void) SetImageProperty(image,keyword,options);
735 if (LocaleCompare(keyword,"gamma") == 0)
737 image->gamma=InterpretLocaleValue(options,(char **) NULL);
740 if (LocaleCompare(keyword,"gravity") == 0)
745 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
749 image->gravity=(GravityType) gravity;
752 if (LocaleCompare(keyword,"green-primary") == 0)
754 flags=ParseGeometry(options,&geometry_info);
755 image->chromaticity.green_primary.x=geometry_info.rho;
756 image->chromaticity.green_primary.y=geometry_info.sigma;
757 if ((flags & SigmaValue) == 0)
758 image->chromaticity.green_primary.y=
759 image->chromaticity.green_primary.x;
762 (void) SetImageProperty(image,keyword,options);
768 if (LocaleCompare(keyword,"id") == 0)
770 (void) CopyMagickString(id,options,MaxTextExtent);
773 if (LocaleCompare(keyword,"iterations") == 0)
775 image->iterations=StringToUnsignedLong(options);
778 (void) SetImageProperty(image,keyword,options);
784 if (LocaleCompare(keyword,"matte") == 0)
789 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
793 image->matte=(MagickBooleanType) matte;
796 if (LocaleCompare(keyword,"matte-color") == 0)
798 (void) QueryColorDatabase(options,&image->matte_color,
802 if (LocaleCompare(keyword,"montage") == 0)
804 (void) CloneString(&image->montage,options);
807 (void) SetImageProperty(image,keyword,options);
813 if (LocaleCompare(keyword,"opaque") == 0)
818 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
822 image->matte=(MagickBooleanType) matte;
825 if (LocaleCompare(keyword,"orientation") == 0)
830 orientation=ParseCommandOption(MagickOrientationOptions,
831 MagickFalse,options);
834 image->orientation=(OrientationType) orientation;
837 (void) SetImageProperty(image,keyword,options);
843 if (LocaleCompare(keyword,"page") == 0)
848 geometry=GetPageGeometry(options);
849 (void) ParseAbsoluteGeometry(geometry,&image->page);
850 geometry=DestroyString(geometry);
853 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
854 (LocaleNCompare(keyword,"profile-",8) == 0))
859 if (profiles == (LinkedListInfo *) NULL)
860 profiles=NewLinkedList(0);
861 (void) AppendValueToLinkedList(profiles,
862 AcquireString(keyword+8));
863 profile=BlobToStringInfo((const void *) NULL,(size_t)
864 StringToLong(options));
865 if (profile == (StringInfo *) NULL)
866 ThrowReaderException(ResourceLimitError,
867 "MemoryAllocationFailed");
868 (void) SetImageProfile(image,keyword+8,profile);
869 profile=DestroyStringInfo(profile);
872 (void) SetImageProperty(image,keyword,options);
878 if (LocaleCompare(keyword,"quality") == 0)
880 image->quality=StringToUnsignedLong(options);
883 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
884 (LocaleCompare(keyword,"quantum:format") == 0))
889 format=ParseCommandOption(MagickQuantumFormatOptions,
890 MagickFalse,options);
893 quantum_format=(QuantumFormatType) format;
896 (void) SetImageProperty(image,keyword,options);
902 if (LocaleCompare(keyword,"red-primary") == 0)
904 flags=ParseGeometry(options,&geometry_info);
905 image->chromaticity.red_primary.x=geometry_info.rho;
906 image->chromaticity.red_primary.y=geometry_info.sigma;
907 if ((flags & SigmaValue) == 0)
908 image->chromaticity.red_primary.y=
909 image->chromaticity.red_primary.x;
912 if (LocaleCompare(keyword,"rendering-intent") == 0)
917 rendering_intent=ParseCommandOption(MagickIntentOptions,
918 MagickFalse,options);
919 if (rendering_intent < 0)
921 image->rendering_intent=(RenderingIntent) rendering_intent;
924 if (LocaleCompare(keyword,"resolution") == 0)
926 flags=ParseGeometry(options,&geometry_info);
927 image->x_resolution=geometry_info.rho;
928 image->y_resolution=geometry_info.sigma;
929 if ((flags & SigmaValue) == 0)
930 image->y_resolution=image->x_resolution;
933 if (LocaleCompare(keyword,"rows") == 0)
935 image->rows=StringToUnsignedLong(options);
938 (void) SetImageProperty(image,keyword,options);
944 if (LocaleCompare(keyword,"scene") == 0)
946 image->scene=StringToUnsignedLong(options);
949 (void) SetImageProperty(image,keyword,options);
955 if (LocaleCompare(keyword,"ticks-per-second") == 0)
957 image->ticks_per_second=(ssize_t) StringToLong(options);
960 if (LocaleCompare(keyword,"tile-offset") == 0)
965 geometry=GetPageGeometry(options);
966 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
967 geometry=DestroyString(geometry);
970 if (LocaleCompare(keyword,"type") == 0)
975 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
979 image->type=(ImageType) type;
982 (void) SetImageProperty(image,keyword,options);
988 if (LocaleCompare(keyword,"units") == 0)
993 units=ParseCommandOption(MagickResolutionOptions,
994 MagickFalse,options);
997 image->units=(ResolutionType) units;
1000 (void) SetImageProperty(image,keyword,options);
1006 if (LocaleCompare(keyword,"version") == 0)
1008 version=InterpretLocaleValue(options,(char **) NULL);
1011 (void) SetImageProperty(image,keyword,options);
1017 if (LocaleCompare(keyword,"white-point") == 0)
1019 flags=ParseGeometry(options,&geometry_info);
1020 image->chromaticity.white_point.x=geometry_info.rho;
1021 image->chromaticity.white_point.y=geometry_info.rho;
1022 if ((flags & SigmaValue) != 0)
1023 image->chromaticity.white_point.y=
1024 image->chromaticity.white_point.x;
1027 (void) SetImageProperty(image,keyword,options);
1032 (void) SetImageProperty(image,keyword,options);
1038 c=ReadBlobByte(image);
1039 while (isspace((int) ((unsigned char) c)) != 0)
1040 c=ReadBlobByte(image);
1042 options=DestroyString(options);
1043 (void) ReadBlobByte(image);
1045 Verify that required image information is defined.
1047 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1048 (image->storage_class == UndefinedClass) ||
1049 (image->columns == 0) || (image->rows == 0))
1050 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1051 if (image->montage != (char *) NULL)
1059 length=MaxTextExtent;
1060 image->directory=AcquireString((char *) NULL);
1065 if ((strlen(image->directory)+MaxTextExtent) >= length)
1068 Allocate more memory for the image directory.
1071 image->directory=(char *) ResizeQuantumMemory(image->directory,
1072 length+MaxTextExtent,sizeof(*image->directory));
1073 if (image->directory == (char *) NULL)
1074 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1075 p=image->directory+strlen(image->directory);
1077 c=ReadBlobByte(image);
1079 } while (c != (int) '\0');
1081 if (profiles != (LinkedListInfo *) NULL)
1090 Read image profiles.
1092 ResetLinkedListIterator(profiles);
1093 name=(const char *) GetNextValueInLinkedList(profiles);
1094 while (name != (const char *) NULL)
1096 profile=GetImageProfile(image,name);
1097 if (profile != (StringInfo *) NULL)
1099 register unsigned char
1102 p=GetStringInfoDatum(profile);
1103 count=ReadBlob(image,GetStringInfoLength(profile),p);
1106 name=(const char *) GetNextValueInLinkedList(profiles);
1108 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1110 image->depth=GetImageQuantumDepth(image,MagickFalse);
1111 if (image->storage_class == PseudoClass)
1114 Create image colormap.
1116 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
1117 if (status == MagickFalse)
1118 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1128 Read image colormap from file.
1130 packet_size=(size_t) (3UL*image->depth/8UL);
1131 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1132 packet_size*sizeof(*colormap));
1133 if (colormap == (unsigned char *) NULL)
1134 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1135 count=ReadBlob(image,packet_size*image->colors,colormap);
1137 switch (image->depth)
1140 ThrowReaderException(CorruptImageError,
1141 "ImageDepthNotSupported");
1147 for (i=0; i < (ssize_t) image->colors; i++)
1149 p=PushCharPixel(p,&pixel);
1150 image->colormap[i].red=ScaleCharToQuantum(pixel);
1151 p=PushCharPixel(p,&pixel);
1152 image->colormap[i].green=ScaleCharToQuantum(pixel);
1153 p=PushCharPixel(p,&pixel);
1154 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1163 for (i=0; i < (ssize_t) image->colors; i++)
1165 p=PushShortPixel(MSBEndian,p,&pixel);
1166 image->colormap[i].red=ScaleShortToQuantum(pixel);
1167 p=PushShortPixel(MSBEndian,p,&pixel);
1168 image->colormap[i].green=ScaleShortToQuantum(pixel);
1169 p=PushShortPixel(MSBEndian,p,&pixel);
1170 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1179 for (i=0; i < (ssize_t) image->colors; i++)
1181 p=PushLongPixel(MSBEndian,p,&pixel);
1182 image->colormap[i].red=ScaleLongToQuantum(pixel);
1183 p=PushLongPixel(MSBEndian,p,&pixel);
1184 image->colormap[i].green=ScaleLongToQuantum(pixel);
1185 p=PushLongPixel(MSBEndian,p,&pixel);
1186 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1191 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1194 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1195 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1198 Allocate image pixels.
1200 quantum_info=AcquireQuantumInfo(image_info,image);
1201 if (quantum_info == (QuantumInfo *) NULL)
1202 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1203 if (quantum_format != UndefinedQuantumFormat)
1205 status=SetQuantumFormat(image,quantum_info,quantum_format);
1206 if (status == MagickFalse)
1207 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1209 packet_size=(size_t) (quantum_info->depth/8);
1210 if (image->storage_class == DirectClass)
1211 packet_size=(size_t) (3*quantum_info->depth/8);
1212 if (image->colorspace == GRAYColorspace)
1213 packet_size=quantum_info->depth/8;
1214 if (image->matte != MagickFalse)
1215 packet_size+=quantum_info->depth/8;
1216 if (image->colorspace == CMYKColorspace)
1217 packet_size+=quantum_info->depth/8;
1218 if (image->compression == RLECompression)
1220 length=image->columns;
1221 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1222 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1224 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1225 sizeof(*compress_pixels));
1226 if (compress_pixels == (unsigned char *) NULL)
1227 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1231 quantum_type=RGBQuantum;
1232 if (image->matte != MagickFalse)
1233 quantum_type=RGBAQuantum;
1234 if (image->colorspace == CMYKColorspace)
1236 quantum_type=CMYKQuantum;
1237 if (image->matte != MagickFalse)
1238 quantum_type=CMYKAQuantum;
1240 if (image->storage_class == PseudoClass)
1242 quantum_type=IndexQuantum;
1243 if (image->matte != MagickFalse)
1244 quantum_type=IndexAlphaQuantum;
1246 if (image->colorspace == GRAYColorspace)
1248 quantum_type=GrayQuantum;
1249 if (image->matte != MagickFalse)
1250 quantum_type=GrayAlphaQuantum;
1253 switch (image->compression)
1255 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1256 case BZipCompression:
1258 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1259 bzip_info.bzalloc=AcquireBZIPMemory;
1260 bzip_info.bzfree=RelinquishBZIPMemory;
1261 bzip_info.opaque=(void *) NULL;
1262 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1269 #if defined(MAGICKCORE_LZMA_DELEGATE)
1270 case LZMACompression:
1272 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1273 allocator.alloc=AcquireLZMAMemory;
1274 allocator.free=RelinquishLZMAMemory;
1275 lzma_info=initialize_lzma;
1276 lzma_info.allocator=(&allocator);
1277 code=lzma_auto_decoder(&lzma_info,-1,0);
1278 if (code != LZMA_OK)
1283 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1284 case LZWCompression:
1285 case ZipCompression:
1287 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1288 zip_info.zalloc=AcquireZIPMemory;
1289 zip_info.zfree=RelinquishZIPMemory;
1290 zip_info.opaque=(voidpf) NULL;
1291 code=inflateInit(&zip_info);
1297 case RLECompression:
1299 GetPixelInfo(image,&pixel);
1305 pixels=GetQuantumPixels(quantum_info);
1307 for (y=0; y < (ssize_t) image->rows; y++)
1315 if (status == MagickFalse)
1317 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1318 if (q == (Quantum *) NULL)
1320 switch (image->compression)
1322 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1323 case BZipCompression:
1325 bzip_info.next_out=(char *) pixels;
1326 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1329 if (bzip_info.avail_in == 0)
1331 bzip_info.next_in=(char *) compress_pixels;
1332 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1334 length=(size_t) ReadBlobMSBLong(image);
1335 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1336 (unsigned char *) bzip_info.next_in);
1338 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1340 } while (bzip_info.avail_out != 0);
1341 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1342 quantum_type,pixels,exception);
1346 #if defined(MAGICKCORE_LZMA_DELEGATE)
1347 case LZMACompression:
1349 lzma_info.next_out=pixels;
1350 lzma_info.avail_out=packet_size*image->columns;
1353 if (lzma_info.avail_in == 0)
1355 lzma_info.next_in=compress_pixels;
1356 length=(size_t) ReadBlobMSBLong(image);
1357 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1358 (unsigned char *) lzma_info.next_in);
1360 code=lzma_code(&lzma_info,LZMA_RUN);
1366 if (code == LZMA_STREAM_END)
1368 } while (lzma_info.avail_out != 0);
1369 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1370 quantum_type,pixels,exception);
1374 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1375 case LZWCompression:
1376 case ZipCompression:
1378 zip_info.next_out=pixels;
1379 zip_info.avail_out=(uInt) (packet_size*image->columns);
1382 if (zip_info.avail_in == 0)
1384 zip_info.next_in=compress_pixels;
1385 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1387 length=(size_t) ReadBlobMSBLong(image);
1388 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1391 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1393 } while (zip_info.avail_out != 0);
1394 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1395 quantum_type,pixels,exception);
1399 case RLECompression:
1401 for (x=0; x < (ssize_t) image->columns; x++)
1405 count=ReadBlob(image,packet_size,pixels);
1406 PushRunlengthPacket(image,pixels,&length,&pixel);
1409 if (image->storage_class == PseudoClass)
1410 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
1413 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1414 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1415 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
1416 if (image->colorspace == CMYKColorspace)
1417 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
1419 if (image->matte != MagickFalse)
1420 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
1421 q+=GetPixelChannels(image);
1427 count=ReadBlob(image,packet_size*image->columns,pixels);
1428 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1429 quantum_type,pixels,exception);
1433 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1436 SetQuantumImageType(image,quantum_type);
1437 switch (image->compression)
1439 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1440 case BZipCompression:
1447 offset=SeekBlob(image,-((MagickOffsetType)
1448 bzip_info.avail_in),SEEK_CUR);
1450 ThrowReaderException(CorruptImageError,
1451 "ImproperImageHeader");
1453 code=BZ2_bzDecompressEnd(&bzip_info);
1459 #if defined(MAGICKCORE_LZMA_DELEGATE)
1460 case LZMACompression:
1462 code=lzma_code(&lzma_info,LZMA_FINISH);
1463 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
1465 lzma_end(&lzma_info);
1469 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1470 case LZWCompression:
1471 case ZipCompression:
1478 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1481 ThrowReaderException(CorruptImageError,
1482 "ImproperImageHeader");
1484 code=inflateEnd(&zip_info);
1485 if (code != LZMA_OK)
1493 quantum_info=DestroyQuantumInfo(quantum_info);
1494 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1495 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1497 image=DestroyImageList(image);
1498 return((Image *) NULL);
1500 if (EOFBlob(image) != MagickFalse)
1502 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1507 Proceed to next image.
1509 if (image_info->number_scenes != 0)
1510 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1514 c=ReadBlobByte(image);
1515 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1519 Allocate next image structure.
1521 AcquireNextImage(image_info,image);
1522 if (GetNextImageInList(image) == (Image *) NULL)
1524 image=DestroyImageList(image);
1525 return((Image *) NULL);
1527 image=SyncNextImageInList(image);
1528 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1529 GetBlobSize(image));
1530 if (status == MagickFalse)
1534 (void) CloseBlob(image);
1535 return(GetFirstImageInList(image));
1539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1543 % R e g i s t e r M I F F I m a g e %
1547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1549 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1550 % supported formats. The properties include the image format tag, a method to
1551 % read and/or write the format, whether the format supports the saving of more
1552 % than one frame to the same file or blob, whether the format supports native
1553 % in-memory I/O, and a brief description of the format.
1555 % The format of the RegisterMIFFImage method is:
1557 % size_t RegisterMIFFImage(void)
1560 ModuleExport size_t RegisterMIFFImage(void)
1563 version[MaxTextExtent];
1569 #if defined(MagickImageCoderSignatureText)
1570 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1571 #if defined(ZLIB_VERSION)
1572 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1573 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1575 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1576 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1579 entry=SetMagickInfo("MIFF");
1580 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1581 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1582 entry->magick=(IsImageFormatHandler *) IsMIFF;
1583 entry->seekable_stream=MagickTrue;
1584 entry->description=ConstantString("Magick Image File Format");
1585 if (*version != '\0')
1586 entry->version=ConstantString(version);
1587 entry->module=ConstantString("MIFF");
1588 (void) RegisterMagickInfo(entry);
1589 return(MagickImageCoderSignature);
1593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597 % U n r e g i s t e r M I F F I m a g e %
1601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1604 % from the list of supported formats.
1606 % The format of the UnregisterMIFFImage method is:
1608 % UnregisterMIFFImage(void)
1611 ModuleExport void UnregisterMIFFImage(void)
1613 (void) UnregisterMagickInfo("MIFF");
1617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 % W r i t e M I F F I m a g e %
1625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 % WriteMIFFImage() writes a MIFF image to a file.
1629 % The format of the WriteMIFFImage method is:
1631 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1632 % Image *image,ExceptionInfo *exception)
1634 % Compression code contributed by Kyle Shorter.
1636 % A description of each parameter follows:
1638 % o image_info: the image info.
1640 % o image: the image.
1642 % o exception: return any errors or warnings in this structure.
1646 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1647 size_t length,PixelInfo *pixel)
1649 if (image->storage_class != DirectClass)
1651 switch (image->depth)
1655 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1656 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
1659 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
1662 *pixels++=(unsigned char) pixel->index;
1666 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1667 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1669 switch (image->depth)
1676 if (image->matte != MagickFalse)
1678 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1679 pixels=PopLongPixel(MSBEndian,value,pixels);
1688 if (image->matte != MagickFalse)
1690 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1691 pixels=PopShortPixel(MSBEndian,value,pixels);
1700 if (image->matte != MagickFalse)
1702 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1704 pixels=PopCharPixel(value,pixels);
1709 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1710 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1712 *pixels++=(unsigned char) length;
1715 switch (image->depth)
1722 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
1723 pixels=PopLongPixel(MSBEndian,value,pixels);
1724 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1725 pixels=PopLongPixel(MSBEndian,value,pixels);
1726 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1727 pixels=PopLongPixel(MSBEndian,value,pixels);
1728 if (image->colorspace == CMYKColorspace)
1730 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
1731 pixels=PopLongPixel(MSBEndian,value,pixels);
1733 if (image->matte != MagickFalse)
1735 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
1736 pixels=PopLongPixel(MSBEndian,value,pixels);
1745 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
1746 pixels=PopShortPixel(MSBEndian,value,pixels);
1747 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1748 pixels=PopShortPixel(MSBEndian,value,pixels);
1749 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1750 pixels=PopShortPixel(MSBEndian,value,pixels);
1751 if (image->colorspace == CMYKColorspace)
1753 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
1754 pixels=PopShortPixel(MSBEndian,value,pixels);
1756 if (image->matte != MagickFalse)
1758 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
1759 pixels=PopShortPixel(MSBEndian,value,pixels);
1768 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
1769 pixels=PopCharPixel(value,pixels);
1770 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->green));
1771 pixels=PopCharPixel(value,pixels);
1772 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1773 pixels=PopCharPixel(value,pixels);
1774 if (image->colorspace == CMYKColorspace)
1776 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1778 pixels=PopCharPixel(value,pixels);
1780 if (image->matte != MagickFalse)
1782 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1784 pixels=PopCharPixel(value,pixels);
1789 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1790 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1792 *pixels++=(unsigned char) length;
1796 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1797 Image *image,ExceptionInfo *exception)
1799 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1805 buffer[MaxTextExtent];
1817 #if defined(MAGICKCORE_LZMA_DELEGATE)
1822 initialize_lzma = LZMA_STREAM_INIT,
1857 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1863 Open output image file.
1865 assert(image_info != (const ImageInfo *) NULL);
1866 assert(image_info->signature == MagickSignature);
1867 assert(image != (Image *) NULL);
1868 assert(image->signature == MagickSignature);
1869 if (image->debug != MagickFalse)
1870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1871 assert(exception != (ExceptionInfo *) NULL);
1872 assert(exception->signature == MagickSignature);
1873 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1874 if (status == MagickFalse)
1881 Allocate image pixels.
1883 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1884 image->depth <= 32 ? 32UL : 64UL;
1885 quantum_info=AcquireQuantumInfo(image_info,image);
1886 if (quantum_info == (QuantumInfo *) NULL)
1887 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1888 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1889 (quantum_info->format == UndefinedQuantumFormat) &&
1890 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
1892 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1893 if (status == MagickFalse)
1894 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1896 if ((image->storage_class == PseudoClass) &&
1897 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1898 (void) SetImageStorageClass(image,DirectClass,exception);
1899 if ((image->colorspace != sRGBColorspace) &&
1900 (IsImageGray(image,exception) != MagickFalse))
1902 (void) SetImageStorageClass(image,DirectClass,exception);
1903 (void) SetImageColorspace(image,GRAYColorspace,exception);
1905 compression=image->compression;
1906 if (image_info->compression != UndefinedCompression)
1907 compression=image_info->compression;
1908 switch (compression)
1910 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1911 case LZMACompression: compression=NoCompression; break;
1913 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1914 case LZWCompression:
1915 case ZipCompression: compression=NoCompression; break;
1917 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1918 case BZipCompression: compression=NoCompression; break;
1920 case RLECompression:
1922 if (quantum_info->format == FloatingPointQuantumFormat)
1923 compression=NoCompression;
1924 GetPixelInfo(image,&target);
1930 packet_size=(size_t) (quantum_info->depth/8);
1931 if (image->storage_class == DirectClass)
1932 packet_size=(size_t) (3*quantum_info->depth/8);
1933 if ((image->colorspace != sRGBColorspace) &&
1934 (IsImageGray(image,exception) != MagickFalse))
1935 packet_size=(size_t) (quantum_info->depth/8);
1936 if (image->matte != MagickFalse)
1937 packet_size+=quantum_info->depth/8;
1938 if (image->colorspace == CMYKColorspace)
1939 packet_size+=quantum_info->depth/8;
1940 if (compression == RLECompression)
1942 length=image->columns;
1943 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1944 packet_size*image->columns));
1945 if ((compression == BZipCompression) || (compression == ZipCompression))
1946 if (length != (size_t) ((unsigned int) length))
1947 compression=NoCompression;
1948 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1949 sizeof(*compress_pixels));
1950 if (compress_pixels == (unsigned char *) NULL)
1951 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1955 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1956 (void) FormatLocaleString(buffer,MaxTextExtent,
1957 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
1958 MagickClassOptions,image->storage_class),(double) image->colors,
1959 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1960 (void) WriteBlobString(image,buffer);
1961 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1962 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1964 (void) WriteBlobString(image,buffer);
1965 if (image->type != UndefinedType)
1967 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
1968 CommandOptionToMnemonic(MagickTypeOptions,image->type));
1969 (void) WriteBlobString(image,buffer);
1971 if (image->colorspace != UndefinedColorspace)
1973 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
1974 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1975 (void) WriteBlobString(image,buffer);
1977 if (compression != UndefinedCompression)
1979 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
1980 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
1981 compression),(double) image->quality);
1982 (void) WriteBlobString(image,buffer);
1984 if (image->units != UndefinedResolution)
1986 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
1987 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
1988 (void) WriteBlobString(image,buffer);
1990 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1992 (void) FormatLocaleString(buffer,MaxTextExtent,
1993 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
1994 (void) WriteBlobString(image,buffer);
1996 if ((image->page.width != 0) || (image->page.height != 0))
1998 (void) FormatLocaleString(buffer,MaxTextExtent,
1999 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2000 image->page.height,(double) image->page.x,(double) image->page.y);
2001 (void) WriteBlobString(image,buffer);
2004 if ((image->page.x != 0) || (image->page.y != 0))
2006 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
2007 (long) image->page.x,(long) image->page.y);
2008 (void) WriteBlobString(image,buffer);
2010 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2012 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
2013 (long) image->tile_offset.x,(long) image->tile_offset.y);
2014 (void) WriteBlobString(image,buffer);
2016 if ((GetNextImageInList(image) != (Image *) NULL) ||
2017 (GetPreviousImageInList(image) != (Image *) NULL))
2019 if (image->scene == 0)
2020 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
2021 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2022 (double) image->delay,(double) image->ticks_per_second);
2024 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
2025 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2026 image->scene,(double) image->iterations,(double) image->delay,
2027 (double) image->ticks_per_second);
2028 (void) WriteBlobString(image,buffer);
2032 if (image->scene != 0)
2034 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
2035 (double) image->scene);
2036 (void) WriteBlobString(image,buffer);
2038 if (image->iterations != 0)
2040 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
2041 (double) image->iterations);
2042 (void) WriteBlobString(image,buffer);
2044 if (image->delay != 0)
2046 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
2047 (double) image->delay);
2048 (void) WriteBlobString(image,buffer);
2050 if (image->ticks_per_second != UndefinedTicksPerSecond)
2052 (void) FormatLocaleString(buffer,MaxTextExtent,
2053 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2054 (void) WriteBlobString(image,buffer);
2057 if (image->gravity != UndefinedGravity)
2059 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
2060 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
2061 (void) WriteBlobString(image,buffer);
2063 if (image->dispose != UndefinedDispose)
2065 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
2066 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
2067 (void) WriteBlobString(image,buffer);
2069 if (image->rendering_intent != UndefinedIntent)
2071 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2072 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2073 (void) WriteBlobString(image,buffer);
2075 if (image->gamma != 0.0)
2077 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
2079 (void) WriteBlobString(image,buffer);
2081 if (image->chromaticity.white_point.x != 0.0)
2084 Note chomaticity points.
2086 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
2087 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2088 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2089 image->chromaticity.green_primary.x,
2090 image->chromaticity.green_primary.y,
2091 image->chromaticity.blue_primary.x,
2092 image->chromaticity.blue_primary.y);
2093 (void) WriteBlobString(image,buffer);
2094 (void) FormatLocaleString(buffer,MaxTextExtent,
2095 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2096 image->chromaticity.white_point.y);
2097 (void) WriteBlobString(image,buffer);
2099 if (image->orientation != UndefinedOrientation)
2101 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
2102 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
2103 (void) WriteBlobString(image,buffer);
2105 if (image->profiles != (void *) NULL)
2114 Write image profiles.
2116 ResetImageProfileIterator(image);
2117 name=GetNextImageProfile(image);
2118 while (name != (const char *) NULL)
2120 profile=GetImageProfile(image,name);
2121 if (profile != (StringInfo *) NULL)
2123 (void) FormatLocaleString(buffer,MaxTextExtent,
2124 "profile:%s=%.20g\n",name,(double)
2125 GetStringInfoLength(profile));
2126 (void) WriteBlobString(image,buffer);
2128 name=GetNextImageProfile(image);
2131 if (image->montage != (char *) NULL)
2133 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
2135 (void) WriteBlobString(image,buffer);
2137 if (quantum_info->format == FloatingPointQuantumFormat)
2138 (void) SetImageProperty(image,"quantum:format","floating-point");
2139 ResetImagePropertyIterator(image);
2140 property=GetNextImageProperty(image);
2141 while (property != (const char *) NULL)
2143 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
2144 (void) WriteBlobString(image,buffer);
2145 value=GetImageProperty(image,property);
2146 if (value != (const char *) NULL)
2148 for (i=0; i < (ssize_t) strlen(value); i++)
2149 if (isspace((int) ((unsigned char) value[i])) != 0)
2151 if (i <= (ssize_t) strlen(value))
2152 (void) WriteBlobByte(image,'{');
2153 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2154 if (i <= (ssize_t) strlen(value))
2155 (void) WriteBlobByte(image,'}');
2157 (void) WriteBlobByte(image,'\n');
2158 property=GetNextImageProperty(image);
2160 (void) WriteBlobString(image,"\f\n:\032");
2161 if (image->montage != (char *) NULL)
2164 Write montage tile directory.
2166 if (image->directory != (char *) NULL)
2167 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2169 (void) WriteBlobByte(image,'\0');
2171 if (image->profiles != (void *) NULL)
2182 ResetImageProfileIterator(image);
2183 name=GetNextImageProfile(image);
2184 while (name != (const char *) NULL)
2186 profile=GetImageProfile(image,name);
2187 (void) WriteBlob(image,GetStringInfoLength(profile),
2188 GetStringInfoDatum(profile));
2189 name=GetNextImageProfile(image);
2192 if (image->storage_class == PseudoClass)
2204 packet_size=(size_t) (3*quantum_info->depth/8);
2205 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2206 packet_size*sizeof(*colormap));
2207 if (colormap == (unsigned char *) NULL)
2208 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2210 Write colormap to file.
2213 for (i=0; i < (ssize_t) image->colors; i++)
2215 switch (quantum_info->depth)
2218 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2221 register unsigned int
2224 pixel=ScaleQuantumToLong(image->colormap[i].red);
2225 q=PopLongPixel(MSBEndian,pixel,q);
2226 pixel=ScaleQuantumToLong(image->colormap[i].green);
2227 q=PopLongPixel(MSBEndian,pixel,q);
2228 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2229 q=PopLongPixel(MSBEndian,pixel,q);
2234 register unsigned short
2237 pixel=ScaleQuantumToShort(image->colormap[i].red);
2238 q=PopShortPixel(MSBEndian,pixel,q);
2239 pixel=ScaleQuantumToShort(image->colormap[i].green);
2240 q=PopShortPixel(MSBEndian,pixel,q);
2241 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2242 q=PopShortPixel(MSBEndian,pixel,q);
2247 register unsigned char
2250 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2251 q=PopCharPixel(pixel,q);
2252 pixel=(unsigned char) ScaleQuantumToChar(
2253 image->colormap[i].green);
2254 q=PopCharPixel(pixel,q);
2255 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2256 q=PopCharPixel(pixel,q);
2261 (void) WriteBlob(image,packet_size*image->colors,colormap);
2262 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2265 Write image pixels to file.
2268 switch (compression)
2270 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2271 case BZipCompression:
2273 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2274 bzip_info.bzalloc=AcquireBZIPMemory;
2275 bzip_info.bzfree=RelinquishBZIPMemory;
2276 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2277 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2278 9)),(int) image_info->verbose,0);
2284 #if defined(MAGICKCORE_LZMA_DELEGATE)
2285 case LZMACompression:
2287 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2288 allocator.alloc=AcquireLZMAMemory;
2289 allocator.free=RelinquishLZMAMemory;
2290 lzma_info=initialize_lzma;
2291 lzma_info.allocator=&allocator;
2292 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2293 if (code != LZMA_OK)
2298 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2299 case LZWCompression:
2300 case ZipCompression:
2302 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2303 zip_info.zalloc=AcquireZIPMemory;
2304 zip_info.zfree=RelinquishZIPMemory;
2305 code=deflateInit(&zip_info,(int) (image->quality ==
2306 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2315 quantum_type=GetQuantumType(image,exception);
2316 pixels=GetQuantumPixels(quantum_info);
2317 for (y=0; y < (ssize_t) image->rows; y++)
2319 register const Quantum
2325 if (status == MagickFalse)
2327 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2328 if (p == (const Quantum *) NULL)
2331 switch (compression)
2333 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2334 case BZipCompression:
2336 bzip_info.next_in=(char *) pixels;
2337 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2338 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2339 quantum_type,pixels,exception);
2342 bzip_info.next_out=(char *) compress_pixels;
2343 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2345 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2348 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2351 (void) WriteBlobMSBLong(image,(unsigned int) length);
2352 (void) WriteBlob(image,length,compress_pixels);
2354 } while (bzip_info.avail_in != 0);
2358 #if defined(MAGICKCORE_LZMA_DELEGATE)
2359 case LZMACompression:
2361 lzma_info.next_in=pixels;
2362 lzma_info.avail_in=packet_size*image->columns;
2363 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2364 quantum_type,pixels,exception);
2367 lzma_info.next_out=compress_pixels;
2368 lzma_info.avail_out=packet_size*image->columns;
2369 code=lzma_code(&lzma_info,LZMA_RUN);
2370 if (code != LZMA_OK)
2372 length=(size_t) (lzma_info.next_out-compress_pixels);
2375 (void) WriteBlobMSBLong(image,(unsigned int) length);
2376 (void) WriteBlob(image,length,compress_pixels);
2378 } while (lzma_info.avail_in != 0);
2382 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2383 case LZWCompression:
2384 case ZipCompression:
2386 zip_info.next_in=pixels;
2387 zip_info.avail_in=(uInt) (packet_size*image->columns);
2388 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2389 quantum_type,pixels,exception);
2392 zip_info.next_out=compress_pixels;
2393 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2394 code=deflate(&zip_info,Z_SYNC_FLUSH);
2397 length=(size_t) (zip_info.next_out-compress_pixels);
2400 (void) WriteBlobMSBLong(image,(unsigned int) length);
2401 (void) WriteBlob(image,length,compress_pixels);
2403 } while (zip_info.avail_in != 0);
2407 case RLECompression:
2410 SetPixelInfo(image,p,&pixel);
2411 p+=GetPixelChannels(image);
2412 for (x=1; x < (ssize_t) image->columns; x++)
2414 SetPixelInfo(image,p,&target);
2415 if ((length < 255) &&
2416 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
2420 q=PopRunlengthPacket(image,q,length,&pixel);
2423 SetPixelInfo(image,p,&pixel);
2424 p+=GetPixelChannels(image);
2426 q=PopRunlengthPacket(image,q,length,&pixel);
2427 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2432 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2433 quantum_type,pixels,exception);
2434 (void) WriteBlob(image,packet_size*image->columns,pixels);
2438 if (image->previous == (Image *) NULL)
2440 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2442 if (status == MagickFalse)
2446 switch (compression)
2448 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2449 case BZipCompression:
2453 if (status == MagickFalse)
2455 bzip_info.next_out=(char *) compress_pixels;
2456 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2458 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
2459 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2462 (void) WriteBlobMSBLong(image,(unsigned int) length);
2463 (void) WriteBlob(image,length,compress_pixels);
2465 if (code == BZ_STREAM_END)
2468 code=BZ2_bzCompressEnd(&bzip_info);
2474 #if defined(MAGICKCORE_LZMA_DELEGATE)
2475 case LZMACompression:
2479 if (status == MagickFalse)
2481 lzma_info.next_out=compress_pixels;
2482 lzma_info.avail_out=packet_size*image->columns;
2483 code=lzma_code(&lzma_info,LZMA_FINISH);
2484 length=(size_t) (lzma_info.next_out-compress_pixels);
2487 (void) WriteBlobMSBLong(image,(unsigned int) length);
2488 (void) WriteBlob(image,length,compress_pixels);
2490 if (code == LZMA_STREAM_END)
2493 lzma_end(&lzma_info);
2497 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2498 case LZWCompression:
2499 case ZipCompression:
2503 if (status == MagickFalse)
2505 zip_info.next_out=compress_pixels;
2506 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
2508 code=deflate(&zip_info,Z_FINISH);
2509 length=(size_t) (zip_info.next_out-compress_pixels);
2512 (void) WriteBlobMSBLong(image,(unsigned int) length);
2513 (void) WriteBlob(image,length,compress_pixels);
2515 if (code == Z_STREAM_END)
2518 code=deflateEnd(&zip_info);
2527 quantum_info=DestroyQuantumInfo(quantum_info);
2528 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2529 if (GetNextImageInList(image) == (Image *) NULL)
2531 image=SyncNextImageInList(image);
2532 status=SetImageProgress(image,SaveImagesTag,scene++,
2533 GetImageListLength(image));
2534 if (status == MagickFalse)
2536 } while (image_info->adjoin != MagickFalse);
2537 (void) CloseBlob(image);