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 "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colormap.h"
50 #include "magick/colormap-private.h"
51 #include "magick/colorspace.h"
52 #include "magick/constitute.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/hashmap.h"
56 #include "magick/geometry.h"
57 #include "magick/image.h"
58 #include "magick/image-private.h"
59 #include "magick/list.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/module.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/option.h"
66 #include "magick/pixel.h"
67 #include "magick/profile.h"
68 #include "magick/property.h"
69 #include "magick/quantum-private.h"
70 #include "magick/static.h"
71 #include "magick/statistic.h"
72 #include "magick/string_.h"
73 #include "magick/string-private.h"
74 #if defined(MAGICKCORE_BZLIB_DELEGATE)
77 #if defined(MAGICKCORE_LZMA_DELEGATE)
80 #if defined(MAGICKCORE_ZLIB_DELEGATE)
87 static MagickBooleanType
88 WriteMIFFImage(const ImageInfo *,Image *);
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % IsMIFF() returns MagickTrue if the image format type, identified by the
102 % magick string, is MIFF.
104 % The format of the IsMIFF method is:
106 % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
108 % A description of each parameter follows:
110 % o magick: compare image format pattern against these bytes.
112 % o length: Specifies the length of the magick string.
115 static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
119 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 % R e a d M I F F I m a g e %
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
136 % memory necessary for the new Image structure and returns a pointer to the
139 % The format of the ReadMIFFImage method is:
141 % Image *ReadMIFFImage(const ImageInfo *image_info,
142 % ExceptionInfo *exception)
144 % Decompression code contributed by Kyle Shorter.
146 % A description of each parameter follows:
148 % o image_info: the image info.
150 % o exception: return any errors or warnings in this structure.
154 #if defined(MAGICKCORE_BZLIB_DELEGATE)
155 static void *AcquireBZIPMemory(void *context,int items,int size)
158 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
162 #if defined(MAGICKCORE_LZMA_DELEGATE)
163 static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
166 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
170 #if defined(MAGICKCORE_ZLIB_DELEGATE)
171 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
175 return((voidpf) AcquireQuantumMemory(items,size));
179 static inline size_t MagickMax(const size_t x,const size_t y)
186 static inline size_t MagickMin(const size_t x,const size_t y)
193 static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
194 size_t *length,PixelPacket *pixel,IndexPacket *index)
200 if (image->storage_class == PseudoClass)
202 *index=(IndexPacket) 0;
203 switch (image->depth)
207 *index=ConstrainColormapIndex(image,
208 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3));
214 *index=ConstrainColormapIndex(image,(*p << 8) | *(p+1));
220 *index=ConstrainColormapIndex(image,*p);
225 (void) ThrowMagickException(&image->exception,GetMagickModule(),
226 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
228 *pixel=image->colormap[(ssize_t) *index];
229 switch (image->depth)
236 if (image->matte != MagickFalse)
238 p=PushCharPixel(p,&quantum);
239 pixel->opacity=ScaleCharToQuantum(quantum);
248 if (image->matte != MagickFalse)
250 p=PushShortPixel(MSBEndian,p,&quantum);
251 pixel->opacity=(Quantum) (quantum >> (image->depth-
252 MAGICKCORE_QUANTUM_DEPTH));
261 if (image->matte != MagickFalse)
263 p=PushLongPixel(MSBEndian,p,&quantum);
264 pixel->opacity=(Quantum) (quantum >> (image->depth-
265 MAGICKCORE_QUANTUM_DEPTH));
270 (void) ThrowMagickException(&image->exception,GetMagickModule(),
271 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
273 *length=(size_t) (*p++)+1;
276 switch (image->depth)
283 p=PushCharPixel(p,&quantum);
284 pixel->red=ScaleCharToQuantum(quantum);
285 p=PushCharPixel(p,&quantum);
286 pixel->green=ScaleCharToQuantum(quantum);
287 p=PushCharPixel(p,&quantum);
288 pixel->blue=ScaleCharToQuantum(quantum);
289 if (image->matte != MagickFalse)
291 p=PushCharPixel(p,&quantum);
292 pixel->opacity=ScaleCharToQuantum(quantum);
294 if (image->colorspace == CMYKColorspace)
296 p=PushCharPixel(p,&quantum);
297 *index=ScaleCharToQuantum(quantum);
306 p=PushShortPixel(MSBEndian,p,&quantum);
307 pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH));
308 p=PushShortPixel(MSBEndian,p,&quantum);
309 pixel->green=(Quantum) (quantum >> (image->depth-
310 MAGICKCORE_QUANTUM_DEPTH));
311 p=PushShortPixel(MSBEndian,p,&quantum);
312 pixel->blue=(Quantum) (quantum >> (image->depth-
313 MAGICKCORE_QUANTUM_DEPTH));
314 if (image->matte != MagickFalse)
316 p=PushShortPixel(MSBEndian,p,&quantum);
317 pixel->opacity=(Quantum) (quantum >> (image->depth-
318 MAGICKCORE_QUANTUM_DEPTH));
320 if (image->colorspace == CMYKColorspace)
322 p=PushShortPixel(MSBEndian,p,&quantum);
323 *index=(IndexPacket) (quantum >> (image->depth-
324 MAGICKCORE_QUANTUM_DEPTH));
333 p=PushLongPixel(MSBEndian,p,&quantum);
334 pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH));
335 p=PushLongPixel(MSBEndian,p,&quantum);
336 pixel->green=(Quantum) (quantum >> (image->depth-
337 MAGICKCORE_QUANTUM_DEPTH));
338 p=PushLongPixel(MSBEndian,p,&quantum);
339 pixel->blue=(Quantum) (quantum >> (image->depth-
340 MAGICKCORE_QUANTUM_DEPTH));
341 if (image->matte != MagickFalse)
343 p=PushLongPixel(MSBEndian,p,&quantum);
344 pixel->opacity=(Quantum) (quantum >> (image->depth-
345 MAGICKCORE_QUANTUM_DEPTH));
347 if (image->colorspace == CMYKColorspace)
349 p=PushLongPixel(MSBEndian,p,&quantum);
350 *index=(IndexPacket) (quantum >> (image->depth-
351 MAGICKCORE_QUANTUM_DEPTH));
356 (void) ThrowMagickException(&image->exception,GetMagickModule(),
357 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
359 *length=(size_t) (*p++)+1;
362 #if defined(MAGICKCORE_BZLIB_DELEGATE)
363 static void RelinquishBZIPMemory(void *context,void *memory)
366 memory=RelinquishMagickMemory(memory);
370 #if defined(MAGICKCORE_LZMA_DELEGATE)
371 static void RelinquishLZMAMemory(void *context,void *memory)
374 memory=RelinquishMagickMemory(memory);
378 #if defined(MAGICKCORE_ZLIB_DELEGATE)
379 static void RelinquishZIPMemory(voidpf context,voidpf memory)
382 memory=RelinquishMagickMemory(memory);
386 static Image *ReadMIFFImage(const ImageInfo *image_info,
387 ExceptionInfo *exception)
389 #define BZipMaxExtent(x) ((x)+((x)/100)+600)
390 #define LZMAMaxExtent(x) ((x)+((x)/3)+128)
391 #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
393 #if defined(MAGICKCORE_BZLIB_DELEGATE)
400 keyword[MaxTextExtent],
422 #if defined(MAGICKCORE_LZMA_DELEGATE)
424 initialize_lzma = LZMA_STREAM_INIT,
472 #if defined(MAGICKCORE_ZLIB_DELEGATE)
480 assert(image_info != (const ImageInfo *) NULL);
481 assert(image_info->signature == MagickSignature);
482 if (image_info->debug != MagickFalse)
483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
484 image_info->filename);
485 assert(exception != (ExceptionInfo *) NULL);
486 assert(exception->signature == MagickSignature);
487 image=AcquireImage(image_info);
488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
491 image=DestroyImageList(image);
492 return((Image *) NULL);
495 Decode image header; header terminates one character beyond a ':'.
497 c=ReadBlobByte(image);
499 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
502 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
507 Decode image header; header terminates one character beyond a ':'.
509 length=MaxTextExtent;
510 options=AcquireString((char *) NULL);
511 quantum_format=UndefinedQuantumFormat;
512 profiles=(LinkedListInfo *) NULL;
515 image->compression=NoCompression;
516 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
527 Read comment-- any text between { }.
529 length=MaxTextExtent;
530 comment=AcquireString((char *) NULL);
531 for (p=comment; comment != (char *) NULL; p++)
533 c=ReadBlobByte(image);
534 if ((c == EOF) || (c == (int) '}'))
536 if ((size_t) (p-comment+1) >= length)
540 comment=(char *) ResizeQuantumMemory(comment,length+
541 MaxTextExtent,sizeof(*comment));
542 if (comment == (char *) NULL)
544 p=comment+strlen(comment);
548 if (comment == (char *) NULL)
549 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
551 (void) SetImageProperty(image,"comment",comment);
552 comment=DestroyString(comment);
553 c=ReadBlobByte(image);
556 if (isalnum(c) != MagickFalse)
564 if (isspace((int) ((unsigned char) c)) != 0)
568 if ((size_t) (p-keyword) < (MaxTextExtent-1))
570 c=ReadBlobByte(image);
574 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
575 c=ReadBlobByte(image);
579 Get the keyword value.
581 c=ReadBlobByte(image);
582 while ((c != (int) '}') && (c != EOF))
584 if ((size_t) (p-options+1) >= length)
588 options=(char *) ResizeQuantumMemory(options,length+
589 MaxTextExtent,sizeof(*options));
590 if (options == (char *) NULL)
592 p=options+strlen(options);
594 if (options == (char *) NULL)
595 ThrowReaderException(ResourceLimitError,
596 "MemoryAllocationFailed");
598 c=ReadBlobByte(image);
600 if (isspace((int) ((unsigned char) c)) != 0)
606 (void) CopyMagickString(options,options+1,MaxTextExtent);
608 Assign a value to the specified keyword.
615 if (LocaleCompare(keyword,"background-color") == 0)
617 (void) QueryColorDatabase(options,&image->background_color,
621 if (LocaleCompare(keyword,"blue-primary") == 0)
623 flags=ParseGeometry(options,&geometry_info);
624 image->chromaticity.blue_primary.x=geometry_info.rho;
625 image->chromaticity.blue_primary.y=geometry_info.sigma;
626 if ((flags & SigmaValue) == 0)
627 image->chromaticity.blue_primary.y=
628 image->chromaticity.blue_primary.x;
631 if (LocaleCompare(keyword,"border-color") == 0)
633 (void) QueryColorDatabase(options,&image->border_color,
637 (void) SetImageProperty(image,keyword,options);
643 if (LocaleCompare(keyword,"class") == 0)
648 storage_class=ParseMagickOption(MagickClassOptions,
649 MagickFalse,options);
650 if (storage_class < 0)
652 image->storage_class=(ClassType) storage_class;
655 if (LocaleCompare(keyword,"colors") == 0)
657 colors=StringToUnsignedLong(options);
660 if (LocaleCompare(keyword,"colorspace") == 0)
665 colorspace=ParseMagickOption(MagickColorspaceOptions,
666 MagickFalse,options);
669 image->colorspace=(ColorspaceType) colorspace;
672 if (LocaleCompare(keyword,"compression") == 0)
677 compression=ParseMagickOption(MagickCompressOptions,
678 MagickFalse,options);
681 image->compression=(CompressionType) compression;
684 if (LocaleCompare(keyword,"columns") == 0)
686 image->columns=StringToUnsignedLong(options);
689 (void) SetImageProperty(image,keyword,options);
695 if (LocaleCompare(keyword,"delay") == 0)
697 image->delay=StringToUnsignedLong(options);
700 if (LocaleCompare(keyword,"depth") == 0)
702 image->depth=StringToUnsignedLong(options);
705 if (LocaleCompare(keyword,"dispose") == 0)
710 dispose=ParseMagickOption(MagickDisposeOptions,MagickFalse,
714 image->dispose=(DisposeType) dispose;
717 (void) SetImageProperty(image,keyword,options);
723 if (LocaleCompare(keyword,"endian") == 0)
728 endian=ParseMagickOption(MagickEndianOptions,MagickFalse,
732 image->endian=(EndianType) endian;
735 (void) SetImageProperty(image,keyword,options);
741 if (LocaleCompare(keyword,"gamma") == 0)
743 image->gamma=StringToDouble(options);
746 if (LocaleCompare(keyword,"gravity") == 0)
751 gravity=ParseMagickOption(MagickGravityOptions,MagickFalse,
755 image->gravity=(GravityType) gravity;
758 if (LocaleCompare(keyword,"green-primary") == 0)
760 flags=ParseGeometry(options,&geometry_info);
761 image->chromaticity.green_primary.x=geometry_info.rho;
762 image->chromaticity.green_primary.y=geometry_info.sigma;
763 if ((flags & SigmaValue) == 0)
764 image->chromaticity.green_primary.y=
765 image->chromaticity.green_primary.x;
768 (void) SetImageProperty(image,keyword,options);
774 if (LocaleCompare(keyword,"id") == 0)
776 (void) CopyMagickString(id,options,MaxTextExtent);
779 if (LocaleCompare(keyword,"iterations") == 0)
781 image->iterations=StringToUnsignedLong(options);
784 (void) SetImageProperty(image,keyword,options);
790 if (LocaleCompare(keyword,"matte") == 0)
795 matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
799 image->matte=(MagickBooleanType) matte;
802 if (LocaleCompare(keyword,"matte-color") == 0)
804 (void) QueryColorDatabase(options,&image->matte_color,
808 if (LocaleCompare(keyword,"montage") == 0)
810 (void) CloneString(&image->montage,options);
813 (void) SetImageProperty(image,keyword,options);
819 if (LocaleCompare(keyword,"opaque") == 0)
824 matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
828 image->matte=(MagickBooleanType) matte;
831 if (LocaleCompare(keyword,"orientation") == 0)
836 orientation=ParseMagickOption(MagickOrientationOptions,
837 MagickFalse,options);
840 image->orientation=(OrientationType) orientation;
843 (void) SetImageProperty(image,keyword,options);
849 if (LocaleCompare(keyword,"page") == 0)
854 geometry=GetPageGeometry(options);
855 (void) ParseAbsoluteGeometry(geometry,&image->page);
856 geometry=DestroyString(geometry);
859 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
860 (LocaleNCompare(keyword,"profile-",8) == 0))
865 if (profiles == (LinkedListInfo *) NULL)
866 profiles=NewLinkedList(0);
867 (void) AppendValueToLinkedList(profiles,
868 AcquireString(keyword+8));
869 profile=AcquireStringInfo((size_t) StringToLong(options));
870 (void) SetImageProfile(image,keyword+8,profile);
871 profile=DestroyStringInfo(profile);
874 (void) SetImageProperty(image,keyword,options);
880 if (LocaleCompare(keyword,"quality") == 0)
882 image->quality=StringToUnsignedLong(options);
885 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
886 (LocaleCompare(keyword,"quantum:format") == 0))
891 format=ParseMagickOption(MagickQuantumFormatOptions,
892 MagickFalse,options);
895 quantum_format=(QuantumFormatType) format;
898 (void) SetImageProperty(image,keyword,options);
904 if (LocaleCompare(keyword,"red-primary") == 0)
906 flags=ParseGeometry(options,&geometry_info);
907 image->chromaticity.red_primary.x=geometry_info.rho;
908 image->chromaticity.red_primary.y=geometry_info.sigma;
909 if ((flags & SigmaValue) == 0)
910 image->chromaticity.red_primary.y=
911 image->chromaticity.red_primary.x;
914 if (LocaleCompare(keyword,"rendering-intent") == 0)
919 rendering_intent=ParseMagickOption(MagickIntentOptions,
920 MagickFalse,options);
921 if (rendering_intent < 0)
923 image->rendering_intent=(RenderingIntent) rendering_intent;
926 if (LocaleCompare(keyword,"resolution") == 0)
928 flags=ParseGeometry(options,&geometry_info);
929 image->x_resolution=geometry_info.rho;
930 image->y_resolution=geometry_info.sigma;
931 if ((flags & SigmaValue) == 0)
932 image->y_resolution=image->x_resolution;
935 if (LocaleCompare(keyword,"rows") == 0)
937 image->rows=StringToUnsignedLong(options);
940 (void) SetImageProperty(image,keyword,options);
946 if (LocaleCompare(keyword,"scene") == 0)
948 image->scene=StringToUnsignedLong(options);
951 (void) SetImageProperty(image,keyword,options);
957 if (LocaleCompare(keyword,"ticks-per-second") == 0)
959 image->ticks_per_second=(ssize_t) StringToLong(options);
962 if (LocaleCompare(keyword,"tile-offset") == 0)
967 geometry=GetPageGeometry(options);
968 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
969 geometry=DestroyString(geometry);
972 if (LocaleCompare(keyword,"type") == 0)
977 type=ParseMagickOption(MagickTypeOptions,MagickFalse,
981 image->type=(ImageType) type;
984 (void) SetImageProperty(image,keyword,options);
990 if (LocaleCompare(keyword,"units") == 0)
995 units=ParseMagickOption(MagickResolutionOptions,MagickFalse,
999 image->units=(ResolutionType) units;
1002 (void) SetImageProperty(image,keyword,options);
1008 if (LocaleCompare(keyword,"version") == 0)
1010 version=StringToDouble(options);
1013 (void) SetImageProperty(image,keyword,options);
1019 if (LocaleCompare(keyword,"white-point") == 0)
1021 flags=ParseGeometry(options,&geometry_info);
1022 image->chromaticity.white_point.x=geometry_info.rho;
1023 image->chromaticity.white_point.y=geometry_info.rho;
1024 if ((flags & SigmaValue) != 0)
1025 image->chromaticity.white_point.y=
1026 image->chromaticity.white_point.x;
1029 (void) SetImageProperty(image,keyword,options);
1034 (void) SetImageProperty(image,keyword,options);
1040 c=ReadBlobByte(image);
1041 while (isspace((int) ((unsigned char) c)) != 0)
1042 c=ReadBlobByte(image);
1044 options=DestroyString(options);
1045 (void) ReadBlobByte(image);
1047 Verify that required image information is defined.
1049 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1050 (image->storage_class == UndefinedClass) ||
1051 (image->columns == 0) || (image->rows == 0))
1052 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1053 if (image->montage != (char *) NULL)
1061 length=MaxTextExtent;
1062 image->directory=AcquireString((char *) NULL);
1067 if ((strlen(image->directory)+MaxTextExtent) >= length)
1070 Allocate more memory for the image directory.
1073 image->directory=(char *) ResizeQuantumMemory(image->directory,
1074 length+MaxTextExtent,sizeof(*image->directory));
1075 if (image->directory == (char *) NULL)
1076 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1077 p=image->directory+strlen(image->directory);
1079 c=ReadBlobByte(image);
1081 } while (c != (int) '\0');
1083 if (profiles != (LinkedListInfo *) NULL)
1092 Read image profiles.
1094 ResetLinkedListIterator(profiles);
1095 name=(const char *) GetNextValueInLinkedList(profiles);
1096 while (name != (const char *) NULL)
1098 profile=GetImageProfile(image,name);
1099 if (profile != (StringInfo *) NULL)
1101 register unsigned char
1104 p=GetStringInfoDatum(profile);
1105 count=ReadBlob(image,GetStringInfoLength(profile),p);
1107 name=(const char *) GetNextValueInLinkedList(profiles);
1109 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1111 image->depth=GetImageQuantumDepth(image,MagickFalse);
1112 if (image->storage_class == PseudoClass)
1115 Create image colormap.
1117 status=AcquireImageColormap(image,colors != 0 ? colors : 256);
1118 if (status == MagickFalse)
1119 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1129 Read image colormap from file.
1131 packet_size=(size_t) (3UL*image->depth/8UL);
1132 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1133 packet_size*sizeof(*colormap));
1134 if (colormap == (unsigned char *) NULL)
1135 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1136 count=ReadBlob(image,packet_size*image->colors,colormap);
1138 switch (image->depth)
1141 ThrowReaderException(CorruptImageError,
1142 "ImageDepthNotSupported");
1148 for (i=0; i < (ssize_t) image->colors; i++)
1150 p=PushCharPixel(p,&pixel);
1151 image->colormap[i].red=ScaleCharToQuantum(pixel);
1152 p=PushCharPixel(p,&pixel);
1153 image->colormap[i].green=ScaleCharToQuantum(pixel);
1154 p=PushCharPixel(p,&pixel);
1155 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1164 for (i=0; i < (ssize_t) image->colors; i++)
1166 p=PushShortPixel(MSBEndian,p,&pixel);
1167 image->colormap[i].red=ScaleShortToQuantum(pixel);
1168 p=PushShortPixel(MSBEndian,p,&pixel);
1169 image->colormap[i].green=ScaleShortToQuantum(pixel);
1170 p=PushShortPixel(MSBEndian,p,&pixel);
1171 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1180 for (i=0; i < (ssize_t) image->colors; i++)
1182 p=PushLongPixel(MSBEndian,p,&pixel);
1183 image->colormap[i].red=ScaleLongToQuantum(pixel);
1184 p=PushLongPixel(MSBEndian,p,&pixel);
1185 image->colormap[i].green=ScaleLongToQuantum(pixel);
1186 p=PushLongPixel(MSBEndian,p,&pixel);
1187 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1192 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1195 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1196 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1199 Allocate image pixels.
1201 quantum_info=AcquireQuantumInfo(image_info,image);
1202 if (quantum_info == (QuantumInfo *) NULL)
1203 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1204 if (quantum_format != UndefinedQuantumFormat)
1206 status=SetQuantumFormat(image,quantum_info,quantum_format);
1207 if (status == MagickFalse)
1208 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1210 packet_size=(size_t) (quantum_info->depth/8);
1211 if (image->storage_class == DirectClass)
1212 packet_size=(size_t) (3*quantum_info->depth/8);
1213 if (image->matte != MagickFalse)
1214 packet_size+=quantum_info->depth/8;
1215 if (image->colorspace == CMYKColorspace)
1216 packet_size+=quantum_info->depth/8;
1217 if (image->compression == RLECompression)
1219 length=image->columns;
1220 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1221 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1223 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1224 sizeof(*compress_pixels));
1225 if (compress_pixels == (unsigned char *) NULL)
1226 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1230 quantum_type=RGBQuantum;
1231 if (image->matte != MagickFalse)
1232 quantum_type=RGBAQuantum;
1233 if (image->colorspace == CMYKColorspace)
1235 quantum_type=CMYKQuantum;
1236 if (image->matte != MagickFalse)
1237 quantum_type=CMYKAQuantum;
1239 if (image->storage_class == PseudoClass)
1241 quantum_type=IndexQuantum;
1242 if (image->matte != MagickFalse)
1243 quantum_type=IndexAlphaQuantum;
1245 if (image->colorspace == GRAYColorspace)
1247 quantum_type=GrayQuantum;
1248 if (image->matte != MagickFalse)
1249 quantum_type=GrayAlphaQuantum;
1252 switch (image->compression)
1254 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1255 case BZipCompression:
1257 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1258 bzip_info.bzalloc=AcquireBZIPMemory;
1259 bzip_info.bzfree=RelinquishBZIPMemory;
1260 bzip_info.opaque=(void *) NULL;
1261 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1265 bzip_info.avail_in=0;
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)
1280 lzma_info.avail_in=0;
1284 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1285 case LZWCompression:
1286 case ZipCompression:
1288 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1289 zip_info.zalloc=AcquireZIPMemory;
1290 zip_info.zfree=RelinquishZIPMemory;
1291 zip_info.opaque=(voidpf) NULL;
1292 code=inflateInit(&zip_info);
1295 zip_info.avail_in=0;
1299 case RLECompression:
1301 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1302 pixel.opacity=(Quantum) TransparentOpacity;
1303 index=(IndexPacket) 0;
1309 pixels=GetQuantumPixels(quantum_info);
1310 index=(IndexPacket) 0;
1312 for (y=0; y < (ssize_t) image->rows; y++)
1314 register IndexPacket
1320 register PixelPacket
1323 if (status == MagickFalse)
1325 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1326 if (q == (PixelPacket *) NULL)
1328 indexes=GetAuthenticIndexQueue(image);
1329 switch (image->compression)
1331 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1332 case BZipCompression:
1334 bzip_info.next_out=(char *) pixels;
1335 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1338 if (bzip_info.avail_in == 0)
1340 bzip_info.next_in=(char *) compress_pixels;
1341 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1343 length=(size_t) ReadBlobMSBLong(image);
1344 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1345 (unsigned char *) bzip_info.next_in);
1347 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1349 } while (bzip_info.avail_out != 0);
1350 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1351 quantum_type,pixels,exception);
1355 #if defined(MAGICKCORE_LZMA_DELEGATE)
1356 case LZMACompression:
1358 lzma_info.next_out=pixels;
1359 lzma_info.avail_out=packet_size*image->columns;
1362 if (lzma_info.avail_in == 0)
1364 lzma_info.next_in=compress_pixels;
1365 length=(size_t) ReadBlobMSBLong(image);
1366 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1367 (unsigned char *) lzma_info.next_in);
1369 code=lzma_code(&lzma_info,LZMA_RUN);
1375 if (code == LZMA_STREAM_END)
1377 } while (lzma_info.avail_out != 0);
1378 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1379 quantum_type,pixels,exception);
1383 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1384 case LZWCompression:
1385 case ZipCompression:
1387 zip_info.next_out=pixels;
1388 zip_info.avail_out=(uInt) (packet_size*image->columns);
1391 if (zip_info.avail_in == 0)
1393 zip_info.next_in=compress_pixels;
1394 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1396 length=(size_t) ReadBlobMSBLong(image);
1397 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1400 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1402 } while (zip_info.avail_out != 0);
1403 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1404 quantum_type,pixels,exception);
1408 case RLECompression:
1410 for (x=0; x < (ssize_t) image->columns; x++)
1414 count=ReadBlob(image,packet_size,pixels);
1415 PushRunlengthPacket(image,pixels,&length,&pixel,&index);
1418 if ((image->storage_class == PseudoClass) ||
1419 (image->colorspace == CMYKColorspace))
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 lzma_end(&lzma_info);
1466 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1467 case LZWCompression:
1468 case ZipCompression:
1475 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1478 ThrowReaderException(CorruptImageError,
1479 "ImproperImageHeader");
1481 code=inflateEnd(&zip_info);
1482 if (code != LZMA_OK)
1490 quantum_info=DestroyQuantumInfo(quantum_info);
1491 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
1492 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
1494 image=DestroyImageList(image);
1495 return((Image *) NULL);
1497 if (EOFBlob(image) != MagickFalse)
1499 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1504 Proceed to next image.
1506 if (image_info->number_scenes != 0)
1507 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1511 c=ReadBlobByte(image);
1512 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1516 Allocate next image structure.
1518 AcquireNextImage(image_info,image);
1519 if (GetNextImageInList(image) == (Image *) NULL)
1521 image=DestroyImageList(image);
1522 return((Image *) NULL);
1524 image=SyncNextImageInList(image);
1525 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1526 GetBlobSize(image));
1527 if (status == MagickFalse)
1531 (void) CloseBlob(image);
1532 return(GetFirstImageInList(image));
1536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540 % R e g i s t e r M I F F I m a g e %
1544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546 % RegisterMIFFImage() adds properties for the MIFF image format to the list of
1547 % supported formats. The properties include the image format tag, a method to
1548 % read and/or write the format, whether the format supports the saving of more
1549 % than one frame to the same file or blob, whether the format supports native
1550 % in-memory I/O, and a brief description of the format.
1552 % The format of the RegisterMIFFImage method is:
1554 % size_t RegisterMIFFImage(void)
1557 ModuleExport size_t RegisterMIFFImage(void)
1560 version[MaxTextExtent];
1566 #if defined(MagickImageCoderSignatureText)
1567 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1568 #if defined(ZLIB_VERSION)
1569 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1570 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1572 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1573 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1576 entry=SetMagickInfo("MIFF");
1577 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1578 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1579 entry->magick=(IsImageFormatHandler *) IsMIFF;
1580 entry->description=ConstantString("Magick Image File Format");
1581 if (*version != '\0')
1582 entry->version=ConstantString(version);
1583 entry->module=ConstantString("MIFF");
1584 (void) RegisterMagickInfo(entry);
1585 return(MagickImageCoderSignature);
1589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1593 % U n r e g i s t e r M I F F I m a g e %
1597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1599 % UnregisterMIFFImage() removes format registrations made by the MIFF module
1600 % from the list of supported formats.
1602 % The format of the UnregisterMIFFImage method is:
1604 % UnregisterMIFFImage(void)
1607 ModuleExport void UnregisterMIFFImage(void)
1609 (void) UnregisterMagickInfo("MIFF");
1613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617 % W r i t e M I F F I m a g e %
1621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1623 % WriteMIFFImage() writes a MIFF image to a file.
1625 % The format of the WriteMIFFImage method is:
1627 % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1630 % Compression code contributed by Kyle Shorter.
1632 % A description of each parameter follows:
1634 % o image_info: the image info.
1636 % o image: the image.
1640 static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
1641 size_t length,PixelPacket pixel,IndexPacket index)
1643 if (image->storage_class != DirectClass)
1645 switch (image->depth)
1649 *pixels++=(unsigned char) ((size_t) index >> 24);
1650 *pixels++=(unsigned char) ((size_t) index >> 16);
1653 *pixels++=(unsigned char) ((size_t) index >> 8);
1656 *pixels++=(unsigned char) index;
1660 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1661 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1663 switch (image->depth)
1670 if (image->matte != MagickFalse)
1672 value=ScaleQuantumToLong(pixel.opacity);
1673 pixels=PopLongPixel(MSBEndian,value,pixels);
1682 if (image->matte != MagickFalse)
1684 value=ScaleQuantumToShort(pixel.opacity);
1685 pixels=PopShortPixel(MSBEndian,value,pixels);
1694 if (image->matte != MagickFalse)
1696 value=(unsigned char) ScaleQuantumToChar(pixel.opacity);
1697 pixels=PopCharPixel(value,pixels);
1702 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1703 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1705 *pixels++=(unsigned char) length;
1708 switch (image->depth)
1715 value=ScaleQuantumToLong(pixel.red);
1716 pixels=PopLongPixel(MSBEndian,value,pixels);
1717 value=ScaleQuantumToLong(pixel.green);
1718 pixels=PopLongPixel(MSBEndian,value,pixels);
1719 value=ScaleQuantumToLong(pixel.blue);
1720 pixels=PopLongPixel(MSBEndian,value,pixels);
1721 if (image->matte != MagickFalse)
1723 value=ScaleQuantumToLong(pixel.opacity);
1724 pixels=PopLongPixel(MSBEndian,value,pixels);
1726 if (image->colorspace == CMYKColorspace)
1728 value=ScaleQuantumToLong(index);
1729 pixels=PopLongPixel(MSBEndian,value,pixels);
1738 value=ScaleQuantumToShort(pixel.red);
1739 pixels=PopShortPixel(MSBEndian,value,pixels);
1740 value=ScaleQuantumToShort(pixel.green);
1741 pixels=PopShortPixel(MSBEndian,value,pixels);
1742 value=ScaleQuantumToShort(pixel.blue);
1743 pixels=PopShortPixel(MSBEndian,value,pixels);
1744 if (image->matte != MagickFalse)
1746 value=ScaleQuantumToShort(pixel.opacity);
1747 pixels=PopShortPixel(MSBEndian,value,pixels);
1749 if (image->colorspace == CMYKColorspace)
1751 value=ScaleQuantumToShort(index);
1752 pixels=PopShortPixel(MSBEndian,value,pixels);
1761 value=(unsigned char) ScaleQuantumToChar(pixel.red);
1762 pixels=PopCharPixel(value,pixels);
1763 value=(unsigned char) ScaleQuantumToChar(pixel.green);
1764 pixels=PopCharPixel(value,pixels);
1765 value=(unsigned char) ScaleQuantumToChar(pixel.blue);
1766 pixels=PopCharPixel(value,pixels);
1767 if (image->matte != MagickFalse)
1769 value=(unsigned char) ScaleQuantumToChar(pixel.opacity);
1770 pixels=PopCharPixel(value,pixels);
1772 if (image->colorspace == CMYKColorspace)
1774 value=(unsigned char) ScaleQuantumToChar(index);
1775 pixels=PopCharPixel(value,pixels);
1780 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1781 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1783 *pixels++=(unsigned char) length;
1787 static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
1790 #if defined(MAGICKCORE_BZLIB_DELEGATE)
1796 buffer[MaxTextExtent];
1811 #if defined(MAGICKCORE_LZMA_DELEGATE)
1816 initialize_lzma = LZMA_STREAM_INIT,
1850 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1856 Open output image file.
1858 assert(image_info != (const ImageInfo *) NULL);
1859 assert(image_info->signature == MagickSignature);
1860 assert(image != (Image *) NULL);
1861 assert(image->signature == MagickSignature);
1862 if (image->debug != MagickFalse)
1863 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1864 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1865 if (status == MagickFalse)
1872 Allocate image pixels.
1874 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1875 image->depth <= 32 ? 32UL : 64UL;
1876 quantum_info=AcquireQuantumInfo(image_info,image);
1877 if (quantum_info == (QuantumInfo *) NULL)
1878 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1879 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1880 (quantum_info->format == UndefinedQuantumFormat) &&
1881 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
1883 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1884 if (status == MagickFalse)
1885 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1887 if ((image->storage_class == PseudoClass) &&
1888 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1889 (void) SetImageStorageClass(image,DirectClass);
1890 if (IsGrayImage(image,&image->exception) != MagickFalse)
1892 image->storage_class=DirectClass;
1893 (void) SetImageColorspace(image,GRAYColorspace);
1895 compression=image->compression;
1896 if (image_info->compression != UndefinedCompression)
1897 compression=image_info->compression;
1898 switch (compression)
1900 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1901 case LZMACompression: compression=NoCompression; break;
1903 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
1904 case LZWCompression:
1905 case ZipCompression: compression=NoCompression; break;
1907 #if !defined(MAGICKCORE_BZLIB_DELEGATE)
1908 case BZipCompression: compression=NoCompression; break;
1910 case RLECompression:
1912 if (quantum_info->format == FloatingPointQuantumFormat)
1913 compression=NoCompression;
1919 packet_size=(size_t) (quantum_info->depth/8);
1920 if (image->storage_class == DirectClass)
1921 packet_size=(size_t) (3*quantum_info->depth/8);
1922 if (image->matte != MagickFalse)
1923 packet_size+=quantum_info->depth/8;
1924 if (image->colorspace == CMYKColorspace)
1925 packet_size+=quantum_info->depth/8;
1926 if (compression == RLECompression)
1928 length=image->columns;
1929 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1930 packet_size*image->columns));
1931 if ((compression == BZipCompression) || (compression == ZipCompression))
1932 if (length != (size_t) ((unsigned int) length))
1933 compression=NoCompression;
1934 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1935 sizeof(*compress_pixels));
1936 if (compress_pixels == (unsigned char *) NULL)
1937 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1941 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
1942 (void) FormatMagickString(buffer,MaxTextExtent,
1943 "class=%s colors=%.20g matte=%s\n",MagickOptionToMnemonic(
1944 MagickClassOptions,image->storage_class),(double) image->colors,
1945 MagickOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
1946 (void) WriteBlobString(image,buffer);
1947 (void) FormatMagickString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
1948 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1950 (void) WriteBlobString(image,buffer);
1951 if (image->type != UndefinedType)
1953 (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
1954 MagickOptionToMnemonic(MagickTypeOptions,image->type));
1955 (void) WriteBlobString(image,buffer);
1957 if (image->colorspace != UndefinedColorspace)
1959 (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
1960 MagickOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
1961 (void) WriteBlobString(image,buffer);
1963 if (compression != UndefinedCompression)
1965 (void) FormatMagickString(buffer,MaxTextExtent,"compression=%s "
1966 "quality=%.20g\n",MagickOptionToMnemonic(MagickCompressOptions,
1967 compression),(double) image->quality);
1968 (void) WriteBlobString(image,buffer);
1970 if (image->units != UndefinedResolution)
1972 (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n",
1973 MagickOptionToMnemonic(MagickResolutionOptions,image->units));
1974 (void) WriteBlobString(image,buffer);
1976 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1978 (void) FormatMagickString(buffer,MaxTextExtent,
1979 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
1980 (void) WriteBlobString(image,buffer);
1982 if ((image->page.width != 0) || (image->page.height != 0))
1984 (void) FormatMagickString(buffer,MaxTextExtent,
1985 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
1986 image->page.height,(double) image->page.x,(double) image->page.y);
1987 (void) WriteBlobString(image,buffer);
1990 if ((image->page.x != 0) || (image->page.y != 0))
1992 (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
1993 (long) image->page.x,(long) image->page.y);
1994 (void) WriteBlobString(image,buffer);
1996 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
1998 (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
1999 (long) image->tile_offset.x,(long) image->tile_offset.y);
2000 (void) WriteBlobString(image,buffer);
2002 if ((GetNextImageInList(image) != (Image *) NULL) ||
2003 (GetPreviousImageInList(image) != (Image *) NULL))
2005 if (image->scene == 0)
2006 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g "
2007 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2008 (double) image->delay,(double) image->ticks_per_second);
2010 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g "
2011 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2012 image->scene,(double) image->iterations,(double) image->delay,
2013 (double) image->ticks_per_second);
2014 (void) WriteBlobString(image,buffer);
2018 if (image->scene != 0)
2020 (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g\n",
2021 (double) image->scene);
2022 (void) WriteBlobString(image,buffer);
2024 if (image->iterations != 0)
2026 (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g\n",
2027 (double) image->iterations);
2028 (void) WriteBlobString(image,buffer);
2030 if (image->delay != 0)
2032 (void) FormatMagickString(buffer,MaxTextExtent,"delay=%.20g\n",
2033 (double) image->delay);
2034 (void) WriteBlobString(image,buffer);
2036 if (image->ticks_per_second != UndefinedTicksPerSecond)
2038 (void) FormatMagickString(buffer,MaxTextExtent,
2039 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
2040 (void) WriteBlobString(image,buffer);
2043 if (image->gravity != UndefinedGravity)
2045 (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
2046 MagickOptionToMnemonic(MagickGravityOptions,image->gravity));
2047 (void) WriteBlobString(image,buffer);
2049 if (image->dispose != UndefinedDispose)
2051 (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
2052 MagickOptionToMnemonic(MagickDisposeOptions,image->dispose));
2053 (void) WriteBlobString(image,buffer);
2055 if (image->rendering_intent != UndefinedIntent)
2057 (void) FormatMagickString(buffer,MaxTextExtent,
2058 "rendering-intent=%s\n",
2059 MagickOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
2060 (void) WriteBlobString(image,buffer);
2062 if (image->gamma != 0.0)
2064 (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n",
2066 (void) WriteBlobString(image,buffer);
2068 if (image->chromaticity.white_point.x != 0.0)
2071 Note chomaticity points.
2073 (void) FormatMagickString(buffer,MaxTextExtent,"red-primary=%g,"
2074 "%g green-primary=%g,%g blue-primary=%g,%g\n",
2075 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2076 image->chromaticity.green_primary.x,
2077 image->chromaticity.green_primary.y,
2078 image->chromaticity.blue_primary.x,
2079 image->chromaticity.blue_primary.y);
2080 (void) WriteBlobString(image,buffer);
2081 (void) FormatMagickString(buffer,MaxTextExtent,
2082 "white-point=%g,%g\n",image->chromaticity.white_point.x,
2083 image->chromaticity.white_point.y);
2084 (void) WriteBlobString(image,buffer);
2086 if (image->orientation != UndefinedOrientation)
2088 (void) FormatMagickString(buffer,MaxTextExtent,"orientation=%s\n",
2089 MagickOptionToMnemonic(MagickOrientationOptions,image->orientation));
2090 (void) WriteBlobString(image,buffer);
2092 if (image->profiles != (void *) NULL)
2101 Write image profiles.
2103 ResetImageProfileIterator(image);
2104 name=GetNextImageProfile(image);
2105 while (name != (const char *) NULL)
2107 profile=GetImageProfile(image,name);
2108 if (profile != (StringInfo *) NULL)
2110 (void) FormatMagickString(buffer,MaxTextExtent,
2111 "profile:%s=%.20g\n",name,(double)
2112 GetStringInfoLength(profile));
2113 (void) WriteBlobString(image,buffer);
2115 name=GetNextImageProfile(image);
2118 if (image->montage != (char *) NULL)
2120 (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n",
2122 (void) WriteBlobString(image,buffer);
2124 if (quantum_info->format == FloatingPointQuantumFormat)
2125 (void) SetImageProperty(image,"quantum:format","floating-point");
2126 ResetImagePropertyIterator(image);
2127 property=GetNextImageProperty(image);
2128 while (property != (const char *) NULL)
2130 (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property);
2131 (void) WriteBlobString(image,buffer);
2132 value=GetImageProperty(image,property);
2133 if (value != (const char *) NULL)
2135 for (i=0; i < (ssize_t) strlen(value); i++)
2136 if (isspace((int) ((unsigned char) value[i])) != 0)
2138 if (i <= (ssize_t) strlen(value))
2139 (void) WriteBlobByte(image,'{');
2140 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
2141 if (i <= (ssize_t) strlen(value))
2142 (void) WriteBlobByte(image,'}');
2144 (void) WriteBlobByte(image,'\n');
2145 property=GetNextImageProperty(image);
2147 (void) WriteBlobString(image,"\f\n:\032");
2148 if (image->montage != (char *) NULL)
2151 Write montage tile directory.
2153 if (image->directory != (char *) NULL)
2154 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2156 (void) WriteBlobByte(image,'\0');
2158 if (image->profiles != (void *) NULL)
2169 ResetImageProfileIterator(image);
2170 name=GetNextImageProfile(image);
2171 while (name != (const char *) NULL)
2173 profile=GetImageProfile(image,name);
2174 (void) WriteBlob(image,GetStringInfoLength(profile),
2175 GetStringInfoDatum(profile));
2176 name=GetNextImageProfile(image);
2179 if (image->storage_class == PseudoClass)
2191 packet_size=(size_t) (3*quantum_info->depth/8);
2192 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2193 packet_size*sizeof(*colormap));
2194 if (colormap == (unsigned char *) NULL)
2195 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2197 Write colormap to file.
2200 for (i=0; i < (ssize_t) image->colors; i++)
2202 switch (quantum_info->depth)
2205 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2208 register unsigned int
2211 pixel=ScaleQuantumToLong(image->colormap[i].red);
2212 q=PopLongPixel(MSBEndian,pixel,q);
2213 pixel=ScaleQuantumToLong(image->colormap[i].green);
2214 q=PopLongPixel(MSBEndian,pixel,q);
2215 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2216 q=PopLongPixel(MSBEndian,pixel,q);
2221 register unsigned short
2224 pixel=ScaleQuantumToShort(image->colormap[i].red);
2225 q=PopShortPixel(MSBEndian,pixel,q);
2226 pixel=ScaleQuantumToShort(image->colormap[i].green);
2227 q=PopShortPixel(MSBEndian,pixel,q);
2228 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2229 q=PopShortPixel(MSBEndian,pixel,q);
2234 register unsigned char
2237 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2238 q=PopCharPixel(pixel,q);
2239 pixel=(unsigned char) ScaleQuantumToChar(
2240 image->colormap[i].green);
2241 q=PopCharPixel(pixel,q);
2242 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2243 q=PopCharPixel(pixel,q);
2248 (void) WriteBlob(image,packet_size*image->colors,colormap);
2249 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2252 Write image pixels to file.
2255 switch (compression)
2257 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2258 case BZipCompression:
2260 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2261 bzip_info.bzalloc=AcquireBZIPMemory;
2262 bzip_info.bzfree=RelinquishBZIPMemory;
2263 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2264 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2265 9)),(int) image_info->verbose,0);
2271 #if defined(MAGICKCORE_LZMA_DELEGATE)
2272 case LZMACompression:
2274 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2275 allocator.alloc=AcquireLZMAMemory;
2276 allocator.free=RelinquishLZMAMemory;
2277 lzma_info=initialize_lzma;
2278 lzma_info.allocator=&allocator;
2279 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2280 if (code != LZMA_OK)
2285 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2286 case LZWCompression:
2287 case ZipCompression:
2289 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2290 zip_info.zalloc=AcquireZIPMemory;
2291 zip_info.zfree=RelinquishZIPMemory;
2292 code=deflateInit(&zip_info,(int) (image->quality ==
2293 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2302 quantum_type=GetQuantumType(image,&image->exception);
2303 pixels=GetQuantumPixels(quantum_info);
2304 for (y=0; y < (ssize_t) image->rows; y++)
2306 register const IndexPacket
2309 register const PixelPacket
2315 if (status == MagickFalse)
2317 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
2318 if (p == (const PixelPacket *) NULL)
2320 indexes=GetVirtualIndexQueue(image);
2322 switch (compression)
2324 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2325 case BZipCompression:
2327 bzip_info.next_in=(char *) pixels;
2328 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
2329 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2330 quantum_info,quantum_type,pixels,&image->exception);
2333 bzip_info.next_out=(char *) compress_pixels;
2334 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2336 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
2339 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2342 (void) WriteBlobMSBLong(image,(unsigned int) length);
2343 (void) WriteBlob(image,length,compress_pixels);
2345 } while (bzip_info.avail_in != 0);
2349 #if defined(MAGICKCORE_LZMA_DELEGATE)
2350 case LZMACompression:
2352 lzma_info.next_in=pixels;
2353 lzma_info.avail_in=packet_size*image->columns;
2354 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2355 quantum_info,quantum_type,pixels,&image->exception);
2358 lzma_info.next_out=compress_pixels;
2359 lzma_info.avail_out=packet_size*image->columns;
2360 code=lzma_code(&lzma_info,LZMA_RUN);
2361 if (code != LZMA_OK)
2363 length=(size_t) (lzma_info.next_out-compress_pixels);
2366 (void) WriteBlobMSBLong(image,(unsigned int) length);
2367 (void) WriteBlob(image,length,compress_pixels);
2369 } while (lzma_info.avail_in != 0);
2373 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2374 case LZWCompression:
2375 case ZipCompression:
2377 zip_info.next_in=pixels;
2378 zip_info.avail_in=(uInt) (packet_size*image->columns);
2379 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2380 quantum_info,quantum_type,pixels,&image->exception);
2383 zip_info.next_out=compress_pixels;
2384 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2385 code=deflate(&zip_info,Z_SYNC_FLUSH);
2388 length=(size_t) (zip_info.next_out-compress_pixels);
2391 (void) WriteBlobMSBLong(image,(unsigned int) length);
2392 (void) WriteBlob(image,length,compress_pixels);
2394 } while (zip_info.avail_in != 0);
2398 case RLECompression:
2401 index=(IndexPacket) 0;
2402 if (indexes != (IndexPacket *) NULL)
2405 for (x=0; x < (ssize_t) image->columns; x++)
2407 if ((length < 255) && (x < (ssize_t) (image->columns-1)) &&
2408 (IsColorEqual(p,&pixel) != MagickFalse) &&
2409 ((image->matte == MagickFalse) ||
2410 (p->opacity == pixel.opacity)) &&
2411 ((indexes == (IndexPacket *) NULL) || (index == indexes[x])))
2416 q=PopRunlengthPacket(image,q,length,pixel,index);
2420 if (indexes != (IndexPacket *) NULL)
2424 q=PopRunlengthPacket(image,q,length,pixel,index);
2425 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2430 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
2431 quantum_info,quantum_type,pixels,&image->exception);
2432 (void) WriteBlob(image,packet_size*image->columns,pixels);
2436 if (image->previous == (Image *) NULL)
2438 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2440 if (status == MagickFalse)
2444 switch (compression)
2446 #if defined(MAGICKCORE_BZLIB_DELEGATE)
2447 case BZipCompression:
2451 if (status == MagickFalse)
2453 bzip_info.next_out=(char *) compress_pixels;
2454 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2456 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 if (code != LZMA_OK)
2486 length=(size_t) (lzma_info.next_out-compress_pixels);
2489 (void) WriteBlobMSBLong(image,(unsigned int) length);
2490 (void) WriteBlob(image,length,compress_pixels);
2492 if (code == LZMA_STREAM_END)
2495 lzma_end(&lzma_info);
2499 #if defined(MAGICKCORE_ZLIB_DELEGATE)
2500 case LZWCompression:
2501 case ZipCompression:
2505 if (status == MagickFalse)
2507 zip_info.next_out=compress_pixels;
2508 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
2510 code=deflate(&zip_info,Z_FINISH);
2513 length=(size_t) (zip_info.next_out-compress_pixels);
2516 (void) WriteBlobMSBLong(image,(unsigned int) length);
2517 (void) WriteBlob(image,length,compress_pixels);
2519 if (code == Z_STREAM_END)
2522 code=deflateEnd(&zip_info);
2531 quantum_info=DestroyQuantumInfo(quantum_info);
2532 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2533 if (GetNextImageInList(image) == (Image *) NULL)
2535 image=SyncNextImageInList(image);
2536 status=SetImageProgress(image,SaveImagesTag,scene++,
2537 GetImageListLength(image));
2538 if (status == MagickFalse)
2540 } while (image_info->adjoin != MagickFalse);
2541 (void) CloseBlob(image);