]> granicus.if.org Git - imagemagick/blobdiff - coders/miff.c
(no commit message)
[imagemagick] / coders / miff.c
index b4d4cd727670c9de8f5602ef57537cc2607c63f3..15bcb02d2726b68cdb5b721786446d75a26001af 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #include "magick/statistic.h"
 #include "magick/string_.h"
 #include "magick/string-private.h"
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+#include "bzlib.h"
+#endif
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+#include "lzma.h"
+#endif
 #if defined(MAGICKCORE_ZLIB_DELEGATE)
 #include "zlib.h"
 #endif
-#if defined(MAGICKCORE_BZLIB_DELEGATE)
-#include "bzlib.h"
+\f
+/*
+  Define declarations.
+*/
+#if !defined(LZMA_OK)
+#define LZMA_OK  0
 #endif
 \f
 /*
@@ -156,6 +166,14 @@ static void *AcquireBZIPMemory(void *context,int items,int size)
 }
 #endif
 
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
+{
+  (void) context;
+  return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
+}
+#endif
+
 #if defined(MAGICKCORE_ZLIB_DELEGATE)
 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
   unsigned int size)
@@ -244,7 +262,7 @@ static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
         }
         case 32:
         {
-          size_t
+          unsigned int
             quantum;
 
           if (image->matte != MagickFalse)
@@ -270,20 +288,20 @@ static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
         quantum;
 
       p=PushCharPixel(p,&quantum);
-      pixel->red=ScaleCharToQuantum(quantum);
+      SetRedPixelComponent(pixel,ScaleCharToQuantum(quantum));
       p=PushCharPixel(p,&quantum);
-      pixel->green=ScaleCharToQuantum(quantum);
+      SetGreenPixelComponent(pixel,ScaleCharToQuantum(quantum));
       p=PushCharPixel(p,&quantum);
-      pixel->blue=ScaleCharToQuantum(quantum);
+      SetBluePixelComponent(pixel,ScaleCharToQuantum(quantum));
       if (image->matte != MagickFalse)
         {
           p=PushCharPixel(p,&quantum);
-          pixel->opacity=ScaleCharToQuantum(quantum);
+          SetOpacityPixelComponent(pixel,ScaleCharToQuantum(quantum));
         }
       if (image->colorspace == CMYKColorspace)
         {
           p=PushCharPixel(p,&quantum);
-          *index=ScaleCharToQuantum(quantum);
+          SetBlackPixelComponent(index,ScaleCharToQuantum(quantum));
         }
       break;
     }
@@ -293,50 +311,52 @@ static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
         quantum;
 
       p=PushShortPixel(MSBEndian,p,&quantum);
-      pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH));
+      SetRedPixelComponent(pixel,quantum >> (image->depth-
+        MAGICKCORE_QUANTUM_DEPTH));
       p=PushShortPixel(MSBEndian,p,&quantum);
-      pixel->green=(Quantum) (quantum >> (image->depth-
+      SetGreenPixelComponent(pixel,quantum >> (image->depth-
         MAGICKCORE_QUANTUM_DEPTH));
       p=PushShortPixel(MSBEndian,p,&quantum);
-      pixel->blue=(Quantum) (quantum >> (image->depth-
+      SetBluePixelComponent(pixel,quantum >> (image->depth-
         MAGICKCORE_QUANTUM_DEPTH));
       if (image->matte != MagickFalse)
         {
           p=PushShortPixel(MSBEndian,p,&quantum);
-          pixel->opacity=(Quantum) (quantum >> (image->depth-
+          SetOpacityPixelComponent(pixel,quantum >> (image->depth-
             MAGICKCORE_QUANTUM_DEPTH));
         }
       if (image->colorspace == CMYKColorspace)
         {
           p=PushShortPixel(MSBEndian,p,&quantum);
-          *index=(IndexPacket) (quantum >> (image->depth-
+          SetBlackPixelComponent(index,quantum >> (image->depth-
             MAGICKCORE_QUANTUM_DEPTH));
         }
       break;
     }
     case 32:
     {
-      size_t
+      unsigned int
         quantum;
 
       p=PushLongPixel(MSBEndian,p,&quantum);
-      pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH));
+      SetRedPixelComponent(pixel,quantum >> (image->depth-
+        MAGICKCORE_QUANTUM_DEPTH));
       p=PushLongPixel(MSBEndian,p,&quantum);
-      pixel->green=(Quantum) (quantum >> (image->depth-
+      SetGreenPixelComponent(pixel,quantum >> (image->depth-
         MAGICKCORE_QUANTUM_DEPTH));
       p=PushLongPixel(MSBEndian,p,&quantum);
-      pixel->blue=(Quantum) (quantum >> (image->depth-
+      SetBluePixelComponent(pixel,quantum >> (image->depth-
         MAGICKCORE_QUANTUM_DEPTH));
       if (image->matte != MagickFalse)
         {
           p=PushLongPixel(MSBEndian,p,&quantum);
-          pixel->opacity=(Quantum) (quantum >> (image->depth-
+          SetOpacityPixelComponent(pixel,quantum >> (image->depth-
             MAGICKCORE_QUANTUM_DEPTH));
         }
       if (image->colorspace == CMYKColorspace)
         {
           p=PushLongPixel(MSBEndian,p,&quantum);
-          *index=(IndexPacket) (quantum >> (image->depth-
+          SetIndexPixelComponent(index,quantum >> (image->depth-
             MAGICKCORE_QUANTUM_DEPTH));
         }
       break;
@@ -348,16 +368,24 @@ static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
   *length=(size_t) (*p++)+1;
 }
 
-#if defined(MAGICKCORE_ZLIB_DELEGATE)
-static void RelinquishZIPMemory(voidpf context,voidpf memory)
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+static void RelinquishBZIPMemory(void *context,void *memory)
 {
   (void) context;
   memory=RelinquishMagickMemory(memory);
 }
 #endif
 
-#if defined(MAGICKCORE_BZLIB_DELEGATE)
-static void RelinquishBZIPMemory(void *context,void *memory)
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+static void RelinquishLZMAMemory(void *context,void *memory)
+{
+  (void) context;
+  memory=RelinquishMagickMemory(memory);
+}
+#endif
+
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+static void RelinquishZIPMemory(voidpf context,voidpf memory)
 {
   (void) context;
   memory=RelinquishMagickMemory(memory);
@@ -368,6 +396,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
   ExceptionInfo *exception)
 {
 #define BZipMaxExtent(x)  ((x)+((x)/100)+600)
+#define LZMAMaxExtent(x)  ((x)+((x)/3)+128)
 #define ZipMaxExtent(x)  ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
 
 #if defined(MAGICKCORE_BZLIB_DELEGATE)
@@ -399,12 +428,18 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
     c,
     code;
 
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+  lzma_stream
+    initialize_lzma = LZMA_STREAM_INIT,
+    lzma_info;
+
+  lzma_allocator
+    allocator;
+#endif
+
   LinkedListInfo
     *profiles;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
@@ -440,6 +475,9 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
   size_t
     colors;
 
+  ssize_t
+    y;
+
 #if defined(MAGICKCORE_ZLIB_DELEGATE)
   z_stream
     zip_info;
@@ -532,8 +570,6 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
             p=keyword;
             do
             {
-              if (isspace((int) ((unsigned char) c)) != 0)
-                break;
               if (c == (int) '=')
                 break;
               if ((size_t) (p-keyword) < (MaxTextExtent-1))
@@ -542,7 +578,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
             } while (c != EOF);
             *p='\0';
             p=options;
-            while (isspace((int) ((unsigned char) c)) != 0)
+            while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
               c=ReadBlobByte(image);
             if (c == (int) '=')
               {
@@ -616,7 +652,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       storage_class;
 
-                    storage_class=ParseMagickOption(MagickClassOptions,
+                    storage_class=ParseCommandOption(MagickClassOptions,
                       MagickFalse,options);
                     if (storage_class < 0)
                       break;
@@ -633,7 +669,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       colorspace;
 
-                    colorspace=ParseMagickOption(MagickColorspaceOptions,
+                    colorspace=ParseCommandOption(MagickColorspaceOptions,
                       MagickFalse,options);
                     if (colorspace < 0)
                       break;
@@ -645,7 +681,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       compression;
 
-                    compression=ParseMagickOption(MagickCompressOptions,
+                    compression=ParseCommandOption(MagickCompressOptions,
                       MagickFalse,options);
                     if (compression < 0)
                       break;
@@ -678,7 +714,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       dispose;
 
-                    dispose=ParseMagickOption(MagickDisposeOptions,MagickFalse,
+                    dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
                       options);
                     if (dispose < 0)
                       break;
@@ -696,7 +732,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       endian;
 
-                    endian=ParseMagickOption(MagickEndianOptions,MagickFalse,
+                    endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
                       options);
                     if (endian < 0)
                       break;
@@ -711,7 +747,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
               {
                 if (LocaleCompare(keyword,"gamma") == 0)
                   {
-                    image->gamma=StringToDouble(options);
+                    image->gamma=LocaleToDouble(options,(char **) NULL);
                     break;
                   }
                 if (LocaleCompare(keyword,"gravity") == 0)
@@ -719,7 +755,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       gravity;
 
-                    gravity=ParseMagickOption(MagickGravityOptions,MagickFalse,
+                    gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
                       options);
                     if (gravity < 0)
                       break;
@@ -763,7 +799,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       matte;
 
-                    matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
+                    matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
                       options);
                     if (matte < 0)
                       break;
@@ -792,7 +828,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       matte;
 
-                    matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
+                    matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
                       options);
                     if (matte < 0)
                       break;
@@ -804,7 +840,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       orientation;
 
-                    orientation=ParseMagickOption(MagickOrientationOptions,
+                    orientation=ParseCommandOption(MagickOrientationOptions,
                       MagickFalse,options);
                     if (orientation < 0)
                       break;
@@ -859,7 +895,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       format;
 
-                    format=ParseMagickOption(MagickQuantumFormatOptions,
+                    format=ParseCommandOption(MagickQuantumFormatOptions,
                       MagickFalse,options);
                     if (format < 0)
                       break;
@@ -887,7 +923,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       rendering_intent;
 
-                    rendering_intent=ParseMagickOption(MagickIntentOptions,
+                    rendering_intent=ParseCommandOption(MagickIntentOptions,
                       MagickFalse,options);
                     if (rendering_intent < 0)
                       break;
@@ -927,7 +963,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
               {
                 if (LocaleCompare(keyword,"ticks-per-second") == 0)
                   {
-                    image->ticks_per_second=StringToLong(options);
+                    image->ticks_per_second=(ssize_t) StringToLong(options);
                     break;
                   }
                 if (LocaleCompare(keyword,"tile-offset") == 0)
@@ -945,7 +981,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       type;
 
-                    type=ParseMagickOption(MagickTypeOptions,MagickFalse,
+                    type=ParseCommandOption(MagickTypeOptions,MagickFalse,
                       options);
                     if (type < 0)
                       break;
@@ -963,8 +999,8 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       units;
 
-                    units=ParseMagickOption(MagickResolutionOptions,MagickFalse,
-                      options);
+                    units=ParseCommandOption(MagickResolutionOptions,
+                      MagickFalse,options);
                     if (units < 0)
                       break;
                     image->units=(ResolutionType) units;
@@ -978,7 +1014,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
               {
                 if (LocaleCompare(keyword,"version") == 0)
                   {
-                    version=StringToDouble(options);
+                    version=LocaleToDouble(options,(char **) NULL);
                     break;
                   }
                 (void) SetImageProperty(image,keyword,options);
@@ -1074,6 +1110,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
 
               p=GetStringInfoDatum(profile);
               count=ReadBlob(image,GetStringInfoLength(profile),p);
+              (void) count;
             }
           name=(const char *) GetNextValueInLinkedList(profiles);
         }
@@ -1145,7 +1182,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
               }
               case 32:
               {
-                size_t
+                unsigned int
                   pixel;
 
                 for (i=0; i < (ssize_t) image->colors; i++)
@@ -1188,8 +1225,9 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
     if (image->compression == RLECompression)
       packet_size++;
     length=image->columns;
-    length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
-      packet_size*image->columns));
+    length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
+      LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
+      image->columns));
     compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
       sizeof(*compress_pixels));
     if (compress_pixels == (unsigned char *) NULL)
@@ -1218,6 +1256,61 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
         if (image->matte != MagickFalse)
           quantum_type=GrayAlphaQuantum;
       }
+    status=MagickTrue;
+    switch (image->compression)
+    {
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+      case BZipCompression:
+      {
+        (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
+        bzip_info.bzalloc=AcquireBZIPMemory;
+        bzip_info.bzfree=RelinquishBZIPMemory;
+        bzip_info.opaque=(void *) NULL;
+        code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
+          MagickFalse);
+        if (code != BZ_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+      case LZMACompression:
+      {
+        (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
+        allocator.alloc=AcquireLZMAMemory;
+        allocator.free=RelinquishLZMAMemory;
+        lzma_info=initialize_lzma;
+        lzma_info.allocator=(&allocator);
+        code=lzma_auto_decoder(&lzma_info,-1,0);
+        if (code != LZMA_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+      case LZWCompression:
+      case ZipCompression:
+      {
+        (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
+        zip_info.zalloc=AcquireZIPMemory;
+        zip_info.zfree=RelinquishZIPMemory;
+        zip_info.opaque=(voidpf) NULL;
+        code=inflateInit(&zip_info);
+        if (code != Z_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+      case RLECompression:
+      {
+        (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
+        pixel.opacity=(Quantum) TransparentOpacity;
+        index=(IndexPacket) 0;
+        break;
+      }
+      default:
+        break;
+    }
     pixels=GetQuantumPixels(quantum_info);
     index=(IndexPacket) 0;
     length=0;
@@ -1232,111 +1325,86 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
       register PixelPacket
         *restrict q;
 
+      if (status == MagickFalse)
+        break;
       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
       if (q == (PixelPacket *) NULL)
         break;
       indexes=GetAuthenticIndexQueue(image);
       switch (image->compression)
       {
-#if defined(MAGICKCORE_ZLIB_DELEGATE)
-        case LZWCompression:
-        case ZipCompression:
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+        case BZipCompression:
         {
-          if (y == 0)
-            {
-              zip_info.zalloc=AcquireZIPMemory;
-              zip_info.zfree=RelinquishZIPMemory;
-              zip_info.opaque=(voidpf) NULL;
-              code=inflateInit(&zip_info);
-              if (code >= 0)
-                status=MagickTrue;
-              zip_info.avail_in=0;
-            }
-          zip_info.next_out=pixels;
-          zip_info.avail_out=(uInt) (packet_size*image->columns);
+          bzip_info.next_out=(char *) pixels;
+          bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
           do
           {
-            if (zip_info.avail_in == 0)
+            if (bzip_info.avail_in == 0)
               {
-                zip_info.next_in=compress_pixels;
-                length=(size_t) ZipMaxExtent(packet_size*image->columns);
+                bzip_info.next_in=(char *) compress_pixels;
+                length=(size_t) BZipMaxExtent(packet_size*image->columns);
                 if (version != 0)
                   length=(size_t) ReadBlobMSBLong(image);
-                zip_info.avail_in=(unsigned int) ReadBlob(image,length,
-                  zip_info.next_in);
+                bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
+                  (unsigned char *) bzip_info.next_in);
               }
-            if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
+            if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
               break;
-          } while (zip_info.avail_out != 0);
-          if (y == (ssize_t) (image->rows-1))
-            {
-              if (version == 0)
-                {
-                  MagickOffsetType
-                    offset;
-
-                  offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
-                    SEEK_CUR);
-                  if (offset < 0)
-                    ThrowReaderException(CorruptImageError,
-                      "ImproperImageHeader");
-                }
-              code=inflateEnd(&zip_info);
-              if (code >= 0)
-                status=MagickTrue;
-            }
+          } while (bzip_info.avail_out != 0);
           (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           break;
         }
 #endif
-#if defined(MAGICKCORE_BZLIB_DELEGATE)
-        case BZipCompression:
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+        case LZMACompression:
         {
-          if (y == 0)
-            {
-              bzip_info.bzalloc=AcquireBZIPMemory;
-              bzip_info.bzfree=RelinquishBZIPMemory;
-              bzip_info.opaque=(void *) NULL;
-              code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
-                MagickFalse);
-              if (code >= 0)
-                status=MagickTrue;
-              bzip_info.avail_in=0;
-            }
-          bzip_info.next_out=(char *) pixels;
-          bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
+          lzma_info.next_out=pixels;
+          lzma_info.avail_out=packet_size*image->columns;
           do
           {
-            if (bzip_info.avail_in == 0)
+            if (lzma_info.avail_in == 0)
               {
-                bzip_info.next_in=(char *) compress_pixels;
-                length=(size_t) BZipMaxExtent(packet_size*image->columns);
+                lzma_info.next_in=compress_pixels;
+                length=(size_t) ReadBlobMSBLong(image);
+                lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
+                  (unsigned char *) lzma_info.next_in);
+              }
+            code=lzma_code(&lzma_info,LZMA_RUN);
+            if (code < 0)
+              {
+                status=MagickFalse;
+                break;
+              }
+            if (code == LZMA_STREAM_END)
+              break;
+          } while (lzma_info.avail_out != 0);
+          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+            quantum_type,pixels,exception);
+          break;
+        }
+#endif
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+        case LZWCompression:
+        case ZipCompression:
+        {
+          zip_info.next_out=pixels;
+          zip_info.avail_out=(uInt) (packet_size*image->columns);
+          do
+          {
+            if (zip_info.avail_in == 0)
+              {
+                zip_info.next_in=compress_pixels;
+                length=(size_t) ZipMaxExtent(packet_size*image->columns);
                 if (version != 0)
                   length=(size_t) ReadBlobMSBLong(image);
-                bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
-                  (unsigned char *) bzip_info.next_in);
+                zip_info.avail_in=(unsigned int) ReadBlob(image,length,
+                  zip_info.next_in);
               }
-            if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
+            if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
               break;
-          } while (bzip_info.avail_out != 0);
-          if (y == (ssize_t) (image->rows-1))
-            {
-              if (version == 0)
-                {
-                  MagickOffsetType
-                    offset;
-
-                  offset=SeekBlob(image,-((MagickOffsetType)
-                    bzip_info.avail_in),SEEK_CUR);
-                  if (offset < 0)
-                    ThrowReaderException(CorruptImageError,
-                      "ImproperImageHeader");
-                }
-              code=BZ2_bzDecompressEnd(&bzip_info);
-              if (code >= 0)
-                status=MagickTrue;
-            }
+          } while (zip_info.avail_out != 0);
           (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           break;
@@ -1344,12 +1412,6 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
 #endif
         case RLECompression:
         {
-          if (y == 0)
-            {
-              (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
-              pixel.opacity=(Quantum) TransparentOpacity;
-              index=(IndexPacket) 0;
-            }
           for (x=0; x < (ssize_t) image->columns; x++)
           {
             if (length == 0)
@@ -1360,8 +1422,12 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
             length--;
             if ((image->storage_class == PseudoClass) ||
                 (image->colorspace == CMYKColorspace))
-              indexes[x]=index;
-            *q++=pixel;
+              SetIndexPixelComponent(indexes+x,index);
+            SetRedPixelComponent(q,pixel.red);
+            SetGreenPixelComponent(q,pixel.green);
+            SetBluePixelComponent(q,pixel.blue);
+            SetOpacityPixelComponent(q,pixel.opacity);
+            q++;
           }
           break;
         }
@@ -1377,6 +1443,62 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
         break;
     }
     SetQuantumImageType(image,quantum_type);
+    switch (image->compression)
+    {
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+      case BZipCompression:
+      {
+        if (version == 0)
+          {
+            MagickOffsetType
+              offset;
+
+            offset=SeekBlob(image,-((MagickOffsetType)
+              bzip_info.avail_in),SEEK_CUR);
+            if (offset < 0)
+              ThrowReaderException(CorruptImageError,
+                "ImproperImageHeader");
+          }
+        code=BZ2_bzDecompressEnd(&bzip_info);
+        if (code != BZ_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+      case LZMACompression:
+      {
+        code=lzma_code(&lzma_info,LZMA_FINISH);
+        if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
+          status=MagickFalse;
+        lzma_end(&lzma_info);
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+      case LZWCompression:
+      case ZipCompression:
+      {
+        if (version == 0)
+          {
+            MagickOffsetType
+              offset;
+
+            offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
+              SEEK_CUR);
+            if (offset < 0)
+              ThrowReaderException(CorruptImageError,
+                "ImproperImageHeader");
+          }
+        code=inflateEnd(&zip_info);
+        if (code != LZMA_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+      default:
+        break;
+    }
     quantum_info=DestroyQuantumInfo(quantum_info);
     compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
     if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
@@ -1467,6 +1589,7 @@ ModuleExport size_t RegisterMIFFImage(void)
   entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
   entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
   entry->magick=(IsImageFormatHandler *) IsMIFF;
+  entry->seekable_stream=MagickTrue;
   entry->description=ConstantString("Magick Image File Format");
   if (*version != '\0')
     entry->version=ConstantString(version);
@@ -1554,7 +1677,7 @@ static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
       {
         case 32:
         {
-          size_t
+          unsigned int
             value;
 
           if (image->matte != MagickFalse)
@@ -1599,7 +1722,7 @@ static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
   {
     case 32:
     {
-      size_t
+      unsigned int
         value;
 
       value=ScaleQuantumToLong(pixel.red);
@@ -1698,8 +1821,14 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
   int
     code;
 
-  ssize_t
-    y;
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+  lzma_allocator
+    allocator;
+
+  lzma_stream
+    initialize_lzma = LZMA_STREAM_INIT,
+    lzma_info;
+#endif
 
   MagickBooleanType
     status;
@@ -1723,6 +1852,9 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     length,
     packet_size;
 
+  ssize_t
+    y;
+
   unsigned char
     *compress_pixels,
     *pixels,
@@ -1778,6 +1910,9 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
       compression=image_info->compression;
     switch (compression)
     {
+#if !defined(MAGICKCORE_ZLIB_DELEGATE)
+      case LZMACompression: compression=NoCompression; break;
+#endif
 #if !defined(MAGICKCORE_ZLIB_DELEGATE)
       case LZWCompression:
       case ZipCompression: compression=NoCompression; break;
@@ -1817,126 +1952,129 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
       Write MIFF header.
     */
     (void) WriteBlobString(image,"id=ImageMagick  version=1.0\n");
-    (void) FormatMagickString(buffer,MaxTextExtent,
-      "class=%s  colors=%lu  matte=%s\n",MagickOptionToMnemonic(
-      MagickClassOptions,image->storage_class),image->colors,
-      MagickOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
+    (void) FormatLocaleString(buffer,MaxTextExtent,
+      "class=%s  colors=%.20g  matte=%s\n",CommandOptionToMnemonic(
+      MagickClassOptions,image->storage_class),(double) image->colors,
+      CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
     (void) WriteBlobString(image,buffer);
-    (void) FormatMagickString(buffer,MaxTextExtent,"columns=%lu  rows=%lu  "
-      "depth=%lu\n",image->columns,image->rows,image->depth);
+    (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g  rows=%.20g  "
+      "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
+      image->depth);
     (void) WriteBlobString(image,buffer);
     if (image->type != UndefinedType)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
-          MagickOptionToMnemonic(MagickTypeOptions,image->type));
+        (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
+          CommandOptionToMnemonic(MagickTypeOptions,image->type));
         (void) WriteBlobString(image,buffer);
       }
     if (image->colorspace != UndefinedColorspace)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
-          MagickOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
+        (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
+          CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
         (void) WriteBlobString(image,buffer);
       }
     if (compression != UndefinedCompression)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"compression=%s  "
-          "quality=%lu\n",MagickOptionToMnemonic(MagickCompressOptions,
-          compression),image->quality);
+        (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s  "
+          "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
+          compression),(double) image->quality);
         (void) WriteBlobString(image,buffer);
       }
     if (image->units != UndefinedResolution)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n",
-          MagickOptionToMnemonic(MagickResolutionOptions,image->units));
+        (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
+          CommandOptionToMnemonic(MagickResolutionOptions,image->units));
         (void) WriteBlobString(image,buffer);
       }
     if ((image->x_resolution != 0) || (image->y_resolution != 0))
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,
+        (void) FormatLocaleString(buffer,MaxTextExtent,
           "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
         (void) WriteBlobString(image,buffer);
       }
     if ((image->page.width != 0) || (image->page.height != 0))
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"page=%lux%lu%+ld%+ld\n",
-          image->page.width,image->page.height,image->page.x,image->page.y);
+        (void) FormatLocaleString(buffer,MaxTextExtent,
+          "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
+          image->page.height,(double) image->page.x,(double) image->page.y);
         (void) WriteBlobString(image,buffer);
       }
     else
       if ((image->page.x != 0) || (image->page.y != 0))
         {
-          (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
-            image->page.x,image->page.y);
+          (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
+            (long) image->page.x,(long) image->page.y);
           (void) WriteBlobString(image,buffer);
         }
     if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
-          image->tile_offset.x,image->tile_offset.y);
+        (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
+          (long) image->tile_offset.x,(long) image->tile_offset.y);
         (void) WriteBlobString(image,buffer);
       }
     if ((GetNextImageInList(image) != (Image *) NULL) ||
         (GetPreviousImageInList(image) != (Image *) NULL))
       {
         if (image->scene == 0)
-          (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%lu  "
-            "delay=%lu  ticks-per-second=%lu\n",image->iterations,image->delay,
-             image->ticks_per_second);
+          (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g  "
+            "delay=%.20g  ticks-per-second=%.20g\n",(double) image->iterations,
+            (double) image->delay,(double) image->ticks_per_second);
         else
-          (void) FormatMagickString(buffer,MaxTextExtent,"scene=%lu  "
-            "iterations=%lu  delay=%lu  ticks-per-second=%lu\n",image->scene,
-            image->iterations,image->delay,image->ticks_per_second);
+          (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g  "
+            "iterations=%.20g  delay=%.20g  ticks-per-second=%.20g\n",(double)
+            image->scene,(double) image->iterations,(double) image->delay,
+            (double) image->ticks_per_second);
         (void) WriteBlobString(image,buffer);
       }
     else
       {
         if (image->scene != 0)
           {
-            (void) FormatMagickString(buffer,MaxTextExtent,"scene=%lu\n",
-              image->scene);
+            (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
+              (double) image->scene);
             (void) WriteBlobString(image,buffer);
           }
         if (image->iterations != 0)
           {
-            (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%lu\n",
-              image->iterations);
+            (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
+              (double) image->iterations);
             (void) WriteBlobString(image,buffer);
           }
         if (image->delay != 0)
           {
-            (void) FormatMagickString(buffer,MaxTextExtent,"delay=%lu\n",
-              image->delay);
+            (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
+              (double) image->delay);
             (void) WriteBlobString(image,buffer);
           }
         if (image->ticks_per_second != UndefinedTicksPerSecond)
           {
-            (void) FormatMagickString(buffer,MaxTextExtent,
-              "ticks-per-second=%lu\n",image->ticks_per_second);
+            (void) FormatLocaleString(buffer,MaxTextExtent,
+              "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
             (void) WriteBlobString(image,buffer);
           }
       }
     if (image->gravity != UndefinedGravity)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
-          MagickOptionToMnemonic(MagickGravityOptions,image->gravity));
+        (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
+          CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
         (void) WriteBlobString(image,buffer);
       }
     if (image->dispose != UndefinedDispose)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
-          MagickOptionToMnemonic(MagickDisposeOptions,image->dispose));
+        (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
+          CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
         (void) WriteBlobString(image,buffer);
       }
     if (image->rendering_intent != UndefinedIntent)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,
+        (void) FormatLocaleString(buffer,MaxTextExtent,
           "rendering-intent=%s\n",
-           MagickOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
+           CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
         (void) WriteBlobString(image,buffer);
       }
     if (image->gamma != 0.0)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n",
+        (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
           image->gamma);
         (void) WriteBlobString(image,buffer);
       }
@@ -1945,7 +2083,7 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
         /*
           Note chomaticity points.
         */
-        (void) FormatMagickString(buffer,MaxTextExtent,"red-primary=%g,"
+        (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
           "%g  green-primary=%g,%g  blue-primary=%g,%g\n",
           image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
           image->chromaticity.green_primary.x,
@@ -1953,15 +2091,15 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
           image->chromaticity.blue_primary.x,
           image->chromaticity.blue_primary.y);
         (void) WriteBlobString(image,buffer);
-        (void) FormatMagickString(buffer,MaxTextExtent,
+        (void) FormatLocaleString(buffer,MaxTextExtent,
           "white-point=%g,%g\n",image->chromaticity.white_point.x,
           image->chromaticity.white_point.y);
         (void) WriteBlobString(image,buffer);
       }
     if (image->orientation != UndefinedOrientation)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"orientation=%s\n",
-          MagickOptionToMnemonic(MagickOrientationOptions,image->orientation));
+        (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
+          CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
         (void) WriteBlobString(image,buffer);
       }
     if (image->profiles != (void *) NULL)
@@ -1982,8 +2120,9 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
           profile=GetImageProfile(image,name);
           if (profile != (StringInfo *) NULL)
             {
-              (void) FormatMagickString(buffer,MaxTextExtent,"profile:%s=%lu\n",
-                name,(size_t) GetStringInfoLength(profile));
+              (void) FormatLocaleString(buffer,MaxTextExtent,
+                "profile:%s=%.20g\n",name,(double)
+                GetStringInfoLength(profile));
               (void) WriteBlobString(image,buffer);
             }
           name=GetNextImageProfile(image);
@@ -1991,7 +2130,7 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
       }
     if (image->montage != (char *) NULL)
       {
-        (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n",
+        (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
           image->montage);
         (void) WriteBlobString(image,buffer);
       }
@@ -2001,7 +2140,7 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     property=GetNextImageProperty(image);
     while (property != (const char *) NULL)
     {
-      (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property);
+      (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
       (void) WriteBlobString(image,buffer);
       value=GetImageProperty(image,property);
       if (value != (const char *) NULL)
@@ -2079,7 +2218,7 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
               ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
             case 32:
             {
-              register size_t
+              register unsigned int
                 pixel;
 
               pixel=ScaleQuantumToLong(image->colormap[i].red);
@@ -2125,9 +2264,56 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     /*
       Write image pixels to file.
     */
+    status=MagickTrue;
+    switch (compression)
+    {
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+      case BZipCompression:
+      {
+        (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
+        bzip_info.bzalloc=AcquireBZIPMemory;
+        bzip_info.bzfree=RelinquishBZIPMemory;
+        code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
+          UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
+          9)),(int) image_info->verbose,0);
+        if (code != BZ_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+      case LZMACompression:
+      {
+        (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
+        allocator.alloc=AcquireLZMAMemory;
+        allocator.free=RelinquishLZMAMemory;
+        lzma_info=initialize_lzma;
+        lzma_info.allocator=&allocator;
+        code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
+        if (code != LZMA_OK)
+          status=MagickTrue;
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+      case LZWCompression:
+      case ZipCompression:
+      {
+        (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
+        zip_info.zalloc=AcquireZIPMemory;
+        zip_info.zfree=RelinquishZIPMemory;
+        code=deflateInit(&zip_info,(int) (image->quality ==
+          UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
+        if (code != Z_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+      default:
+        break;
+    }
     quantum_type=GetQuantumType(image,&image->exception);
     pixels=GetQuantumPixels(quantum_info);
-    status=MagickTrue;
     for (y=0; y < (ssize_t) image->rows; y++)
     {
       register const IndexPacket
@@ -2139,6 +2325,8 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
       register ssize_t
         x;
 
+      if (status == MagickFalse)
+        break;
       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
       if (p == (const PixelPacket *) NULL)
         break;
@@ -2146,115 +2334,77 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
       q=pixels;
       switch (compression)
       {
-#if defined(MAGICKCORE_ZLIB_DELEGATE)
-        case LZWCompression:
-        case ZipCompression:
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+        case BZipCompression:
         {
-          if (y == 0)
-            {
-              zip_info.zalloc=AcquireZIPMemory;
-              zip_info.zfree=RelinquishZIPMemory;
-              zip_info.opaque=(voidpf) NULL;
-              code=deflateInit(&zip_info,(int) (image->quality ==
-                UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
-                9)));
-              if (code >= 0)
-                status=MagickTrue;
-            }
-          zip_info.next_in=pixels;
-          zip_info.avail_in=(uInt) (packet_size*image->columns);
+          bzip_info.next_in=(char *) pixels;
+          bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
           (void) ExportQuantumPixels(image,(const CacheView *) NULL,
             quantum_info,quantum_type,pixels,&image->exception);
           do
           {
-            zip_info.next_out=compress_pixels;
-            zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
-            code=deflate(&zip_info,Z_SYNC_FLUSH);
-            if (code >= 0)
-              status=MagickTrue;
-            length=(size_t) (zip_info.next_out-compress_pixels);
+            bzip_info.next_out=(char *) compress_pixels;
+            bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
+              image->columns);
+            code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
+            if (code != BZ_OK)
+              status=MagickFalse;
+            length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
             if (length != 0)
               {
                 (void) WriteBlobMSBLong(image,(unsigned int) length);
                 (void) WriteBlob(image,length,compress_pixels);
               }
-          } while (zip_info.avail_in != 0);
-          if (y == (ssize_t) (image->rows-1))
-            {
-              for ( ; ; )
-              {
-                zip_info.next_out=compress_pixels;
-                zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
-                  image->columns);
-                code=deflate(&zip_info,Z_FINISH);
-                length=(size_t) (zip_info.next_out-compress_pixels);
-                if (length > 6)
-                  {
-                    (void) WriteBlobMSBLong(image,(unsigned int) length);
-                    (void) WriteBlob(image,length,compress_pixels);
-                  }
-                if (code == Z_STREAM_END)
-                  break;
-              }
-              status=deflateEnd(&zip_info) == 0 ? MagickTrue : MagickFalse;
-            }
+          } while (bzip_info.avail_in != 0);
           break;
         }
 #endif
-#if defined(MAGICKCORE_BZLIB_DELEGATE)
-        case BZipCompression:
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+        case LZMACompression:
         {
-          if (y == 0)
-            {
-              bzip_info.bzalloc=AcquireBZIPMemory;
-              bzip_info.bzfree=RelinquishBZIPMemory;
-              bzip_info.opaque=(void *) NULL;
-              code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
-                UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
-                9)),
-                (int) image_info->verbose,0);
-              if (code >= 0)
-                status=MagickTrue;
-            }
-          bzip_info.next_in=(char *) pixels;
-          bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
+          lzma_info.next_in=pixels;
+          lzma_info.avail_in=packet_size*image->columns;
           (void) ExportQuantumPixels(image,(const CacheView *) NULL,
             quantum_info,quantum_type,pixels,&image->exception);
           do
           {
-            bzip_info.next_out=(char *) compress_pixels;
-            bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
-              image->columns);
-            code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
-            if (code >= 0)
-              status=MagickTrue;
-            length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
+            lzma_info.next_out=compress_pixels;
+            lzma_info.avail_out=packet_size*image->columns;
+            code=lzma_code(&lzma_info,LZMA_RUN);
+            if (code != LZMA_OK)
+              status=MagickFalse;
+            length=(size_t) (lzma_info.next_out-compress_pixels);
             if (length != 0)
               {
                 (void) WriteBlobMSBLong(image,(unsigned int) length);
                 (void) WriteBlob(image,length,compress_pixels);
               }
-          } while (bzip_info.avail_in != 0);
-          if (y == (ssize_t) (image->rows-1))
-            {
-              for ( ; ; )
+          } while (lzma_info.avail_in != 0);
+          break;
+        }
+#endif
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+        case LZWCompression:
+        case ZipCompression:
+        {
+          zip_info.next_in=pixels;
+          zip_info.avail_in=(uInt) (packet_size*image->columns);
+          (void) ExportQuantumPixels(image,(const CacheView *) NULL,
+            quantum_info,quantum_type,pixels,&image->exception);
+          do
+          {
+            zip_info.next_out=compress_pixels;
+            zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
+            code=deflate(&zip_info,Z_SYNC_FLUSH);
+            if (code != Z_OK)
+              status=MagickFalse;
+            length=(size_t) (zip_info.next_out-compress_pixels);
+            if (length != 0)
               {
-                bzip_info.next_out=(char *) compress_pixels;
-                bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
-                  image->columns);
-                code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
-                length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
-                if (length != 0)
-                  {
-                    (void) WriteBlobMSBLong(image,(unsigned int) length);
-                    (void) WriteBlob(image,length,compress_pixels);
-                  }
-                if (code == BZ_STREAM_END)
-                  break;
+                (void) WriteBlobMSBLong(image,(unsigned int) length);
+                (void) WriteBlob(image,length,compress_pixels);
               }
-              status=BZ2_bzCompressEnd(&bzip_info) == 0 ? MagickTrue :
-                MagickFalse;
-            }
+          } while (zip_info.avail_in != 0);
           break;
         }
 #endif
@@ -2270,8 +2420,9 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
             if ((length < 255) && (x < (ssize_t) (image->columns-1)) &&
                 (IsColorEqual(p,&pixel) != MagickFalse) &&
                 ((image->matte == MagickFalse) ||
-                 (p->opacity == pixel.opacity)) &&
-                ((indexes == (IndexPacket *) NULL) || (index == indexes[x])))
+                 (GetOpacityPixelComponent(p) == pixel.opacity)) &&
+                ((indexes == (IndexPacket *) NULL) ||
+                 (index == GetIndexPixelComponent(indexes+x))))
               length++;
             else
               {
@@ -2281,7 +2432,7 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
               }
             pixel=(*p);
             if (indexes != (IndexPacket *) NULL)
-              index=indexes[x];
+              index=GetIndexPixelComponent(indexes+x);
             p++;
           }
           q=PopRunlengthPacket(image,q,length,pixel,index);
@@ -2296,8 +2447,99 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
           break;
         }
       }
-      if ((image->previous == (Image *) NULL) &&
-          (SetImageProgress(image,SaveImageTag,y,image->rows) == MagickFalse))
+      if (image->previous == (Image *) NULL)
+        {
+          status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+            image->rows);
+          if (status == MagickFalse)
+            break;
+        }
+    }
+    switch (compression)
+    {
+#if defined(MAGICKCORE_BZLIB_DELEGATE)
+      case BZipCompression:
+      {
+        for ( ; ; )
+        {
+          if (status == MagickFalse)
+            break;
+          bzip_info.next_out=(char *) compress_pixels;
+          bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
+            image->columns);
+          code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
+          if (code != BZ_OK)
+            status=MagickFalse;
+          length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
+          if (length != 0)
+            {
+              (void) WriteBlobMSBLong(image,(unsigned int) length);
+              (void) WriteBlob(image,length,compress_pixels);
+            }
+          if (code == BZ_STREAM_END)
+            break;
+        }
+        code=BZ2_bzCompressEnd(&bzip_info);
+        if (code != BZ_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_LZMA_DELEGATE)
+      case LZMACompression:
+      {
+        for ( ; ; )
+        {
+          if (status == MagickFalse)
+            break;
+          lzma_info.next_out=compress_pixels;
+          lzma_info.avail_out=packet_size*image->columns;
+          code=lzma_code(&lzma_info,LZMA_FINISH);
+          if (code != LZMA_OK)
+            status=MagickFalse;
+          length=(size_t) (lzma_info.next_out-compress_pixels);
+          if (length > 6)
+            {
+              (void) WriteBlobMSBLong(image,(unsigned int) length);
+              (void) WriteBlob(image,length,compress_pixels);
+            }
+          if (code == LZMA_STREAM_END)
+            break;
+        }
+        lzma_end(&lzma_info);
+        break;
+      }
+#endif
+#if defined(MAGICKCORE_ZLIB_DELEGATE)
+      case LZWCompression:
+      case ZipCompression:
+      {
+        for ( ; ; )
+        {
+          if (status == MagickFalse)
+            break;
+          zip_info.next_out=compress_pixels;
+          zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
+            image->columns);
+          code=deflate(&zip_info,Z_FINISH);
+          if (code != Z_OK)
+            status=MagickFalse;
+          length=(size_t) (zip_info.next_out-compress_pixels);
+          if (length > 6)
+            {
+              (void) WriteBlobMSBLong(image,(unsigned int) length);
+              (void) WriteBlob(image,length,compress_pixels);
+            }
+          if (code == Z_STREAM_END)
+            break;
+        }
+        code=deflateEnd(&zip_info);
+        if (code != Z_OK)
+          status=MagickFalse;
+        break;
+      }
+#endif
+      default:
         break;
     }
     quantum_info=DestroyQuantumInfo(quantum_info);