]> granicus.if.org Git - imagemagick/blobdiff - coders/miff.c
(no commit message)
[imagemagick] / coders / miff.c
index 365a47d89f582f78d5817a1c1626223d84b50971..12976b64f137354de1aadf72cea59db7ab87ae4c 100644 (file)
 #include "zlib.h"
 #endif
 \f
+/*
+  Define declarations.
+*/
+#if !defined(LZMA_OK)
+#define LZMA_OK  0
+#endif
+\f
 /*
   Forward declarations.
 */
@@ -159,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)
@@ -351,16 +366,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);
@@ -405,7 +428,11 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
 
 #if defined(MAGICKCORE_LZMA_DELEGATE)
   lzma_stream
-    lzma_info = LZMA_STREAM_INIT;
+    initialize_lzma = LZMA_STREAM_INIT,
+    lzma_info;
+
+  lzma_allocator
+    allocator;
 #endif
 
   LinkedListInfo
@@ -541,8 +568,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))
@@ -625,7 +650,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;
@@ -642,7 +667,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       colorspace;
 
-                    colorspace=ParseMagickOption(MagickColorspaceOptions,
+                    colorspace=ParseCommandOption(MagickColorspaceOptions,
                       MagickFalse,options);
                     if (colorspace < 0)
                       break;
@@ -654,7 +679,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       compression;
 
-                    compression=ParseMagickOption(MagickCompressOptions,
+                    compression=ParseCommandOption(MagickCompressOptions,
                       MagickFalse,options);
                     if (compression < 0)
                       break;
@@ -687,7 +712,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       dispose;
 
-                    dispose=ParseMagickOption(MagickDisposeOptions,MagickFalse,
+                    dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
                       options);
                     if (dispose < 0)
                       break;
@@ -705,7 +730,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       endian;
 
-                    endian=ParseMagickOption(MagickEndianOptions,MagickFalse,
+                    endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
                       options);
                     if (endian < 0)
                       break;
@@ -728,7 +753,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       gravity;
 
-                    gravity=ParseMagickOption(MagickGravityOptions,MagickFalse,
+                    gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
                       options);
                     if (gravity < 0)
                       break;
@@ -772,7 +797,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       matte;
 
-                    matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
+                    matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
                       options);
                     if (matte < 0)
                       break;
@@ -801,7 +826,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       matte;
 
-                    matte=ParseMagickOption(MagickBooleanOptions,MagickFalse,
+                    matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
                       options);
                     if (matte < 0)
                       break;
@@ -813,7 +838,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       orientation;
 
-                    orientation=ParseMagickOption(MagickOrientationOptions,
+                    orientation=ParseCommandOption(MagickOrientationOptions,
                       MagickFalse,options);
                     if (orientation < 0)
                       break;
@@ -868,7 +893,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       format;
 
-                    format=ParseMagickOption(MagickQuantumFormatOptions,
+                    format=ParseCommandOption(MagickQuantumFormatOptions,
                       MagickFalse,options);
                     if (format < 0)
                       break;
@@ -896,7 +921,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;
@@ -954,7 +979,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       type;
 
-                    type=ParseMagickOption(MagickTypeOptions,MagickFalse,
+                    type=ParseCommandOption(MagickTypeOptions,MagickFalse,
                       options);
                     if (type < 0)
                       break;
@@ -972,7 +997,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                     ssize_t
                       units;
 
-                    units=ParseMagickOption(MagickResolutionOptions,MagickFalse,
+                    units=ParseCommandOption(MagickResolutionOptions,MagickFalse,
                       options);
                     if (units < 0)
                       break;
@@ -1083,6 +1108,7 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
 
               p=GetStringInfoDatum(profile);
               count=ReadBlob(image,GetStringInfoLength(profile),p);
+              (void) count;
             }
           name=(const char *) GetNextValueInLinkedList(profiles);
         }
@@ -1228,6 +1254,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;
@@ -1242,6 +1323,8 @@ 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;
@@ -1251,17 +1334,6 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
 #if defined(MAGICKCORE_BZLIB_DELEGATE)
         case BZipCompression:
         {
-          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 != BZ_OK)
-                status=MagickFalse;
-              bzip_info.avail_in=0;
-            }
           bzip_info.next_out=(char *) pixels;
           bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
           do
@@ -1278,23 +1350,6 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
             if (BZ2_bzDecompress(&bzip_info) == BZ_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 != BZ_OK)
-                status=MagickFalse;
-            }
           (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           break;
@@ -1303,15 +1358,8 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
 #if defined(MAGICKCORE_LZMA_DELEGATE)
         case LZMACompression:
         {
-          if (y == 0)
-            {
-              code=lzma_auto_decoder(&lzma_info,-1,0);
-              if (code != LZMA_OK)
-                status=MagickFalse;
-              lzma_info.avail_in=0;
-            }
           lzma_info.next_out=pixels;
-          lzma_info.avail_out=(uInt) (packet_size*image->columns);
+          lzma_info.avail_out=packet_size*image->columns;
           do
           {
             if (lzma_info.avail_in == 0)
@@ -1322,11 +1370,14 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
                   (unsigned char *) lzma_info.next_in);
               }
             code=lzma_code(&lzma_info,LZMA_RUN);
-            if (code != LZMA_OK)
+            if (code < 0)
+              {
+                status=MagickFalse;
+                break;
+              }
+            if (code == LZMA_STREAM_END)
               break;
           } while (lzma_info.avail_out != 0);
-          if (y == (ssize_t) (image->rows-1))
-            lzma_end(&lzma_info);
           (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           break;
@@ -1336,16 +1387,6 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
         case LZWCompression:
         case ZipCompression:
         {
-          if (y == 0)
-            {
-              zip_info.zalloc=AcquireZIPMemory;
-              zip_info.zfree=RelinquishZIPMemory;
-              zip_info.opaque=(voidpf) NULL;
-              code=inflateInit(&zip_info);
-              if (code != Z_OK)
-                status=MagickFalse;
-              zip_info.avail_in=0;
-            }
           zip_info.next_out=pixels;
           zip_info.avail_out=(uInt) (packet_size*image->columns);
           do
@@ -1362,23 +1403,6 @@ static Image *ReadMIFFImage(const ImageInfo *image_info,
             if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_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 != LZMA_OK)
-                status=MagickFalse;
-            }
           (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           break;
@@ -1386,12 +1410,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)
@@ -1402,8 +1420,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;
         }
@@ -1419,6 +1441,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))
@@ -1509,6 +1587,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);
@@ -1741,8 +1820,12 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     code;
 
 #if defined(MAGICKCORE_LZMA_DELEGATE)
+  lzma_allocator
+    allocator;
+
   lzma_stream
-    lzma_info = LZMA_STREAM_INIT;
+    initialize_lzma = LZMA_STREAM_INIT,
+    lzma_info;
 #endif
 
   MagickBooleanType
@@ -1868,9 +1951,9 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     */
     (void) WriteBlobString(image,"id=ImageMagick  version=1.0\n");
     (void) FormatMagickString(buffer,MaxTextExtent,
-      "class=%s  colors=%.20g  matte=%s\n",MagickOptionToMnemonic(
+      "class=%s  colors=%.20g  matte=%s\n",CommandOptionToMnemonic(
       MagickClassOptions,image->storage_class),(double) image->colors,
-      MagickOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
+      CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
     (void) WriteBlobString(image,buffer);
     (void) FormatMagickString(buffer,MaxTextExtent,"columns=%.20g  rows=%.20g  "
       "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
@@ -1879,26 +1962,26 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     if (image->type != UndefinedType)
       {
         (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n",
-          MagickOptionToMnemonic(MagickTypeOptions,image->type));
+          CommandOptionToMnemonic(MagickTypeOptions,image->type));
         (void) WriteBlobString(image,buffer);
       }
     if (image->colorspace != UndefinedColorspace)
       {
         (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n",
-          MagickOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
+          CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
         (void) WriteBlobString(image,buffer);
       }
     if (compression != UndefinedCompression)
       {
         (void) FormatMagickString(buffer,MaxTextExtent,"compression=%s  "
-          "quality=%.20g\n",MagickOptionToMnemonic(MagickCompressOptions,
+          "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));
+          CommandOptionToMnemonic(MagickResolutionOptions,image->units));
         (void) WriteBlobString(image,buffer);
       }
     if ((image->x_resolution != 0) || (image->y_resolution != 0))
@@ -1971,20 +2054,20 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     if (image->gravity != UndefinedGravity)
       {
         (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n",
-          MagickOptionToMnemonic(MagickGravityOptions,image->gravity));
+          CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
         (void) WriteBlobString(image,buffer);
       }
     if (image->dispose != UndefinedDispose)
       {
         (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n",
-          MagickOptionToMnemonic(MagickDisposeOptions,image->dispose));
+          CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
         (void) WriteBlobString(image,buffer);
       }
     if (image->rendering_intent != UndefinedIntent)
       {
         (void) FormatMagickString(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)
@@ -2014,7 +2097,7 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
     if (image->orientation != UndefinedOrientation)
       {
         (void) FormatMagickString(buffer,MaxTextExtent,"orientation=%s\n",
-          MagickOptionToMnemonic(MagickOrientationOptions,image->orientation));
+          CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
         (void) WriteBlobString(image,buffer);
       }
     if (image->profiles != (void *) NULL)
@@ -2179,9 +2262,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
@@ -2193,6 +2323,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;
@@ -2203,17 +2335,6 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
 #if defined(MAGICKCORE_BZLIB_DELEGATE)
         case BZipCompression:
         {
-          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 != BZ_OK)
-                status=MagickFalse;
-            }
           bzip_info.next_in=(char *) pixels;
           bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
           (void) ExportQuantumPixels(image,(const CacheView *) NULL,
@@ -2233,44 +2354,14 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
                 (void) WriteBlob(image,length,compress_pixels);
               }
           } while (bzip_info.avail_in != 0);
-          if (y == (ssize_t) (image->rows-1))
-            {
-              for ( ; ; )
-              {
-                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:
         {
-          if (y == 0)
-            {
-              code=lzma_easy_encoder(&lzma_info,image->quality/10,
-                LZMA_CHECK_SHA256);
-              if (code != LZMA_OK)
-                status=MagickTrue;
-            }
           lzma_info.next_in=pixels;
-          lzma_info.avail_in=(uInt) (packet_size*image->columns);
+          lzma_info.avail_in=packet_size*image->columns;
           (void) ExportQuantumPixels(image,(const CacheView *) NULL,
             quantum_info,quantum_type,pixels,&image->exception);
           do
@@ -2287,26 +2378,6 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
                 (void) WriteBlob(image,length,compress_pixels);
               }
           } while (lzma_info.avail_in != 0);
-          if (y == (ssize_t) (image->rows-1))
-            {
-              for ( ; ; )
-              {
-                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
@@ -2314,17 +2385,6 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
         case LZWCompression:
         case ZipCompression:
         {
-          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 != Z_OK)
-                status=MagickFalse;
-            }
           zip_info.next_in=pixels;
           zip_info.avail_in=(uInt) (packet_size*image->columns);
           (void) ExportQuantumPixels(image,(const CacheView *) NULL,
@@ -2343,29 +2403,6 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
                 (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);
-                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
@@ -2381,8 +2418,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
               {
@@ -2392,7 +2430,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);
@@ -2415,6 +2453,93 @@ static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
             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);
     compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
     if (GetNextImageInList(image) == (Image *) NULL)