]> granicus.if.org Git - imagemagick/blobdiff - coders/icon.c
...
[imagemagick] / coders / icon.c
index f0a21745c205611b473a1bdf7369ecc850145f82..7df403acac82c1f1d46ccfc8f4c4147a31879d08 100644 (file)
 %                   Read Microsoft Windows Icon Format                        %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2018 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  %
 %  obtain a copy of the License at                                            %
 %                                                                             %
-%    http://www.imagemagick.org/script/license.php                            %
+%    https://www.imagemagick.org/script/license.php                           %
 %                                                                             %
 %  Unless required by applicable law or agreed to in writing, software        %
 %  distributed under the License is distributed on an "AS IS" BASIS,          %
   Include declarations.
 */
 #include "MagickCore/studio.h"
+#include "MagickCore/artifact.h"
 #include "MagickCore/blob.h"
 #include "MagickCore/blob-private.h"
 #include "MagickCore/cache.h"
 #include "MagickCore/colormap.h"
 #include "MagickCore/colorspace.h"
+#include "MagickCore/colorspace-private.h"
 #include "MagickCore/exception.h"
 #include "MagickCore/exception-private.h"
 #include "MagickCore/image.h"
@@ -55,7 +57,8 @@
 #include "MagickCore/memory_.h"
 #include "MagickCore/monitor.h"
 #include "MagickCore/monitor-private.h"
-#include "MagickCore/nt-feature.h"
+#include "MagickCore/nt-base-private.h"
+#include "MagickCore/option.h"
 #include "MagickCore/pixel-accessor.h"
 #include "MagickCore/quantize.h"
 #include "MagickCore/quantum-private.h"
@@ -139,8 +142,73 @@ typedef struct _IconInfo
 /*
   Forward declaractions.
 */
+static Image
+  *AutoResizeImage(const Image *,const char *,MagickOffsetType *,
+    ExceptionInfo *);
+
 static MagickBooleanType
   WriteICONImage(const ImageInfo *,Image *,ExceptionInfo *);
+
+Image *AutoResizeImage(const Image *image,const char *option,
+  MagickOffsetType *count,ExceptionInfo *exception)
+{
+  #define MAX_SIZES 16
+
+  char
+    *q;
+
+  const char
+    *p;
+
+  Image
+    *resized,
+    *images;
+
+  register ssize_t
+    i;
+
+  size_t
+    sizes[MAX_SIZES]={256,192,128,96,64,48,40,32,24,16};
+
+  images=NULL;
+  *count=0;
+  i=0;
+  p=option;
+  while (*p != '\0' && i < MAX_SIZES)
+  {
+    size_t
+      size;
+
+    while ((isspace((int) ((unsigned char) *p)) != 0))
+      p++;
+
+    size=(size_t)strtol(p,&q,10);
+    if ((p == q) || (size < 16) || (size > 256))
+      return((Image *) NULL);
+
+    p=q;
+    sizes[i++]=size;
+
+    while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
+      p++;
+  }
+
+  if (i==0)
+    i=10;
+  *count=i;
+  for (i=0; i < *count; i++)
+  {
+    resized=ResizeImage(image,sizes[i],sizes[i],image->filter,exception);
+    if (resized == (Image *) NULL)
+      return(DestroyImageList(images));
+
+    if (images == (Image *) NULL)
+      images=resized;
+    else
+      AppendImageToList(&images,resized);
+  }
+  return(images);
+}
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -184,6 +252,9 @@ static Image *ReadICONImage(const ImageInfo *image_info,
   MagickBooleanType
     status;
 
+  MagickSizeType
+    extent;
+
   register ssize_t
     i,
     x;
@@ -198,7 +269,6 @@ static Image *ReadICONImage(const ImageInfo *image_info,
     bit,
     byte,
     bytes_per_line,
-    length,
     one,
     scanline_pad;
 
@@ -207,17 +277,14 @@ static Image *ReadICONImage(const ImageInfo *image_info,
     offset,
     y;
 
-  unsigned char
-    *pixels;
-
   /*
     Open image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
@@ -232,6 +299,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
       ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
       (icon_file.count > MaxIcons))
     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  extent=0;
   for (i=0; i < icon_file.count; i++)
   {
     icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
@@ -243,7 +311,12 @@ static Image *ReadICONImage(const ImageInfo *image_info,
       ReadBlobLSBShort(image);
     icon_file.directory[i].size=ReadBlobLSBLong(image);
     icon_file.directory[i].offset=ReadBlobLSBLong(image);
+    if (EOFBlob(image) != MagickFalse)
+      break;
+    extent=MagickMax(extent,icon_file.directory[i].size);
   }
+  if ((EOFBlob(image) != MagickFalse) || (extent > GetBlobSize(image)))
+    ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
   one=1;
   for (i=0; i < icon_file.count; i++)
   {
@@ -255,104 +328,18 @@ static Image *ReadICONImage(const ImageInfo *image_info,
     if (offset < 0)
       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     icon_info.size=ReadBlobLSBLong(image);
-    icon_info.width=(unsigned char) ((int) ReadBlobLSBLong(image));
-    icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
+    icon_info.width=(unsigned char) ReadBlobLSBSignedLong(image);
+    icon_info.height=(unsigned char) (ReadBlobLSBSignedLong(image)/2);
     icon_info.planes=ReadBlobLSBShort(image);
     icon_info.bits_per_pixel=ReadBlobLSBShort(image);
-    if (icon_info.bits_per_pixel > 32)
-      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-    icon_info.compression=ReadBlobLSBLong(image);
-    icon_info.image_size=ReadBlobLSBLong(image);
-    icon_info.x_pixels=ReadBlobLSBLong(image);
-    icon_info.y_pixels=ReadBlobLSBLong(image);
-    icon_info.number_colors=ReadBlobLSBLong(image);
-    icon_info.colors_important=ReadBlobLSBLong(image);
-    image->matte=MagickTrue;
-    image->columns=(size_t) icon_file.directory[i].width;
-    if ((ssize_t) image->columns > icon_info.width)
-      image->columns=(size_t) icon_info.width;
-    if (image->columns == 0)
-      image->columns=256;
-    image->rows=(size_t) icon_file.directory[i].height;
-    if ((ssize_t) image->rows > icon_info.height)
-      image->rows=(size_t) icon_info.height;
-    if (image->rows == 0)
-      image->rows=256;
-    image->depth=icon_info.bits_per_pixel;
-    if (image->debug != MagickFalse)
-      {
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          " scene    = %.20g",(double) i);
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "   size   = %.20g",(double) icon_info.size);
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "   width  = %.20g",(double) icon_file.directory[i].width);
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "   height = %.20g",(double) icon_file.directory[i].height);
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "   colors = %.20g",(double ) icon_info.number_colors);
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "   planes = %.20g",(double) icon_info.planes);
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "   bpp    = %.20g",(double) icon_info.bits_per_pixel);
-      }
-  if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
-    {
-      image->storage_class=PseudoClass;
-      image->colors=icon_info.number_colors;
-      if (image->colors == 0)
-        image->colors=one << icon_info.bits_per_pixel;
-    }
-  if (image->storage_class == PseudoClass)
-    {
-      register ssize_t
-        i;
-
-      size_t
-        number_colors,
-        one;
-
-      unsigned char
-        *icon_colormap;
-
-      /*
-        Read Icon raster colormap.
-      */
-      one=1;
-      number_colors=one << icon_info.bits_per_pixel;
-      if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
-        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-      icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
-        image->colors,4UL*sizeof(*icon_colormap));
-      if (icon_colormap == (unsigned char *) NULL)
-        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-      count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
-      if (count != (ssize_t) (4*image->colors))
-        ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
-      p=icon_colormap;
-      for (i=0; i < (ssize_t) image->colors; i++)
+    if (EOFBlob(image) != MagickFalse)
       {
-        image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
-        image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
-        image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
-        p++;
-      }
-      icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
-    }
-    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
-      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
+        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
+          image->filename);
         break;
-    length=icon_file.directory[i].size-40;
-    pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
-    if (pixels == (unsigned char *) NULL)
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-    count=ReadBlob(image,length,pixels);
-    if (count != (ssize_t) length)
-      {
-        pixels=(unsigned char *) RelinquishMagickMemory(pixels);
-        ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
       }
-    if (memcmp(pixels,"\211PNG\r\n\032\n",8) == 0)
+    if (((icon_info.planes == 18505) && (icon_info.bits_per_pixel == 21060)) || 
+        (icon_info.size == 0x474e5089))
       {
         Image
           *icon_image;
@@ -360,37 +347,148 @@ static Image *ReadICONImage(const ImageInfo *image_info,
         ImageInfo
           *read_info;
 
+        size_t
+          length;
+
+        unsigned char
+          *png;
+
         /*
           Icon image encoded as a compressed PNG image.
         */
+        length=icon_file.directory[i].size;
+        if ((length < 16) || (~length < 16))
+          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+        png=(unsigned char *) AcquireQuantumMemory(length+16,sizeof(*png));
+        if (png == (unsigned char *) NULL)
+          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+        (void) memcpy(png,"\211PNG\r\n\032\n\000\000\000\015",12);
+        png[12]=(unsigned char) icon_info.planes;
+        png[13]=(unsigned char) (icon_info.planes >> 8);
+        png[14]=(unsigned char) icon_info.bits_per_pixel;
+        png[15]=(unsigned char) (icon_info.bits_per_pixel >> 8);
+        count=ReadBlob(image,length,png+16);
+        if (count != (ssize_t) length)
+          {
+            png=(unsigned char *) RelinquishMagickMemory(png);
+            ThrowReaderException(CorruptImageError,
+                "InsufficientImageDataInFile");
+          }
         read_info=CloneImageInfo(image_info);
-        (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
-        icon_image=BlobToImage(read_info,pixels,length,exception);
+        (void) CopyMagickString(read_info->magick,"PNG",MagickPathExtent);
+        icon_image=BlobToImage(read_info,png,length+16,exception);
         read_info=DestroyImageInfo(read_info);
-        pixels=(unsigned char *) RelinquishMagickMemory(pixels);
+        png=(unsigned char *) RelinquishMagickMemory(png);
         if (icon_image == (Image *) NULL)
-          {
-            image=DestroyImageList(image);
-            return((Image *) NULL);
-          }
+          return(DestroyImageList(image));
         DestroyBlob(icon_image);
         icon_image->blob=ReferenceBlob(image->blob);
         ReplaceImageInList(&image,icon_image);
       }
     else
       {
-        register unsigned char
-          *p;
+        if (icon_info.bits_per_pixel > 32)
+          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+        icon_info.compression=ReadBlobLSBLong(image);
+        icon_info.image_size=ReadBlobLSBLong(image);
+        icon_info.x_pixels=ReadBlobLSBLong(image);
+        icon_info.y_pixels=ReadBlobLSBLong(image);
+        icon_info.number_colors=ReadBlobLSBLong(image);
+        if (icon_info.number_colors > GetBlobSize(image))
+          ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
+        icon_info.colors_important=ReadBlobLSBLong(image);
+        image->alpha_trait=BlendPixelTrait;
+        image->columns=(size_t) icon_file.directory[i].width;
+        if ((ssize_t) image->columns > icon_info.width)
+          image->columns=(size_t) icon_info.width;
+        if (image->columns == 0)
+          image->columns=256;
+        image->rows=(size_t) icon_file.directory[i].height;
+        if ((ssize_t) image->rows > icon_info.height)
+          image->rows=(size_t) icon_info.height;
+        if (image->rows == 0)
+          image->rows=256;
+        image->depth=icon_info.bits_per_pixel;
+        if (image->depth > 16)
+          image->depth=8;
+        if (image->debug != MagickFalse)
+          {
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              " scene    = %.20g",(double) i);
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              "   size   = %.20g",(double) icon_info.size);
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              "   width  = %.20g",(double) icon_file.directory[i].width);
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              "   height = %.20g",(double) icon_file.directory[i].height);
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              "   colors = %.20g",(double ) icon_info.number_colors);
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              "   planes = %.20g",(double) icon_info.planes);
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+              "   bpp    = %.20g",(double) icon_info.bits_per_pixel);
+          }
+      if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16U))
+        {
+          image->storage_class=PseudoClass;
+          image->colors=icon_info.number_colors;
+          if ((image->colors == 0) || (image->colors > 256))
+            image->colors=one << icon_info.bits_per_pixel;
+        }
+      if (image->storage_class == PseudoClass)
+        {
+          register ssize_t
+            j;
+
+          unsigned char
+            *icon_colormap;
 
+          /*
+            Read Icon raster colormap.
+          */
+          if (image->colors > GetBlobSize(image))
+            ThrowReaderException(CorruptImageError,
+              "InsufficientImageDataInFile");
+          if (AcquireImageColormap(image,image->colors,exception) ==
+              MagickFalse)
+            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
+            image->colors,4UL*sizeof(*icon_colormap));
+          if (icon_colormap == (unsigned char *) NULL)
+            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
+          if (count != (ssize_t) (4*image->colors))
+            {
+              icon_colormap=(unsigned char *) RelinquishMagickMemory(
+                icon_colormap);
+              ThrowReaderException(CorruptImageError,
+                "InsufficientImageDataInFile");
+            }
+          p=icon_colormap;
+          for (j=0; j < (ssize_t) image->colors; j++)
+          {
+            image->colormap[j].blue=(Quantum) ScaleCharToQuantum(*p++);
+            image->colormap[j].green=(Quantum) ScaleCharToQuantum(*p++);
+            image->colormap[j].red=(Quantum) ScaleCharToQuantum(*p++);
+            p++;
+          }
+          icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
+        }
         /*
           Convert Icon raster image to pixel packets.
         */
+        if ((image_info->ping != MagickFalse) &&
+            (image_info->number_scenes != 0))
+          if (image->scene >= (image_info->scene+image_info->number_scenes-1))
+            break;
+        status=SetImageExtent(image,image->columns,image->rows,exception);
+        if (status == MagickFalse)
+          return(DestroyImageList(image));
         bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
           ~31) >> 3;
         (void) bytes_per_line;
         scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
           (image->columns*icon_info.bits_per_pixel)) >> 3;
-        p=pixels;
         switch (icon_info.bits_per_pixel)
         {
           case 1:
@@ -405,7 +503,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 break;
               for (x=0; x < (ssize_t) (image->columns-7); x+=8)
               {
-                byte=(size_t) *p++;
+                byte=(size_t) ReadBlobByte(image);
                 for (bit=0; bit < 8; bit++)
                 {
                   SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
@@ -415,7 +513,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
               }
               if ((image->columns % 8) != 0)
                 {
-                  byte=(size_t) *p++;
+                  byte=(size_t) ReadBlobByte(image);
                   for (bit=0; bit < (image->columns % 8); bit++)
                   {
                     SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
@@ -424,7 +522,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                   }
                 }
               for (x=0; x < (ssize_t) scanline_pad; x++)
-                (void) *p++;
+                (void) ReadBlobByte(image);
               if (SyncAuthenticPixels(image,exception) == MagickFalse)
                 break;
               if (image->previous == (Image *) NULL)
@@ -449,7 +547,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 break;
               for (x=0; x < ((ssize_t) image->columns-1); x+=2)
               {
-                byte=(size_t) *p++;
+                byte=(size_t) ReadBlobByte(image);
                 SetPixelIndex(image,((byte >> 4) & 0xf),q);
                 q+=GetPixelChannels(image);
                 SetPixelIndex(image,((byte) & 0xf),q);
@@ -457,12 +555,12 @@ static Image *ReadICONImage(const ImageInfo *image_info,
               }
               if ((image->columns % 2) != 0)
                 {
-                  byte=(size_t) *p++;
+                  byte=(size_t) ReadBlobByte(image);
                   SetPixelIndex(image,((byte >> 4) & 0xf),q);
                   q+=GetPixelChannels(image);
                 }
               for (x=0; x < (ssize_t) scanline_pad; x++)
-                (void) *p++;
+                (void) ReadBlobByte(image);
               if (SyncAuthenticPixels(image,exception) == MagickFalse)
                 break;
               if (image->previous == (Image *) NULL)
@@ -487,12 +585,12 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 break;
               for (x=0; x < (ssize_t) image->columns; x++)
               {
-                byte=(size_t) *p++;
-                SetPixelIndex(image,byte,q);
+                byte=(size_t) ReadBlobByte(image);
+                SetPixelIndex(image,(Quantum) byte,q);
                 q+=GetPixelChannels(image);
               }
               for (x=0; x < (ssize_t) scanline_pad; x++)
-                (void) *p++;
+                (void) ReadBlobByte(image);
               if (SyncAuthenticPixels(image,exception) == MagickFalse)
                 break;
               if (image->previous == (Image *) NULL)
@@ -517,13 +615,13 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 break;
               for (x=0; x < (ssize_t) image->columns; x++)
               {
-                byte=(size_t) *p++;
-                byte|=(size_t) (*p++ << 8);
-                SetPixelIndex(image,byte,q);
+                byte=(size_t) ReadBlobByte(image);
+                byte|=((size_t) ReadBlobByte(image) << 8);
+                SetPixelIndex(image,(Quantum) byte,q);
                 q+=GetPixelChannels(image);
               }
               for (x=0; x < (ssize_t) scanline_pad; x++)
-                (void) *p++;
+                (void) ReadBlobByte(image);
               if (SyncAuthenticPixels(image,exception) == MagickFalse)
                 break;
               if (image->previous == (Image *) NULL)
@@ -549,16 +647,20 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 break;
               for (x=0; x < (ssize_t) image->columns; x++)
               {
-                SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
-                SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
-                SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
+                SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
+                  ReadBlobByte(image)),q);
+                SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
+                  ReadBlobByte(image)),q);
+                SetPixelRed(image,ScaleCharToQuantum((unsigned char)
+                  ReadBlobByte(image)),q);
                 if (icon_info.bits_per_pixel == 32)
-                  SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
+                  SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
+                    ReadBlobByte(image)),q);
                 q+=GetPixelChannels(image);
               }
               if (icon_info.bits_per_pixel == 24)
                 for (x=0; x < (ssize_t) scanline_pad; x++)
-                  (void) *p++;
+                  (void) ReadBlobByte(image);
               if (SyncAuthenticPixels(image,exception) == MagickFalse)
                 break;
               if (image->previous == (Image *) NULL)
@@ -574,8 +676,9 @@ static Image *ReadICONImage(const ImageInfo *image_info,
           default:
             ThrowReaderException(CorruptImageError,"ImproperImageHeader");
         }
-        if (image_info->ping == MagickFalse)
-          (void) SyncImage(image);
+        if ((image_info->ping == MagickFalse) &&
+            (icon_info.bits_per_pixel <= 16))
+          (void) SyncImage(image,exception);
         if (icon_info.bits_per_pixel != 32)
           {
             /*
@@ -589,7 +692,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 break;
               for (x=0; x < ((ssize_t) image->columns-7); x+=8)
               {
-                byte=(size_t) *p++;
+                byte=(size_t) ReadBlobByte(image);
                 for (bit=0; bit < 8; bit++)
                 {
                   SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
@@ -599,7 +702,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
               }
               if ((image->columns % 8) != 0)
                 {
-                  byte=(size_t) *p++;
+                  byte=(size_t) ReadBlobByte(image);
                   for (bit=0; bit < (image->columns % 8); bit++)
                   {
                     SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
@@ -609,7 +712,7 @@ static Image *ReadICONImage(const ImageInfo *image_info,
                 }
               if ((image->columns % 32) != 0)
                 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
-                  (void) *p++;
+                  (void) ReadBlobByte(image);
               if (SyncAuthenticPixels(image,exception) == MagickFalse)
                 break;
             }
@@ -621,7 +724,6 @@ static Image *ReadICONImage(const ImageInfo *image_info,
             break;
           }
       }
-    pixels=(unsigned char *) RelinquishMagickMemory(pixels);
     /*
       Proceed to next image.
     */
@@ -636,8 +738,8 @@ static Image *ReadICONImage(const ImageInfo *image_info,
         AcquireNextImage(image_info,image,exception);
         if (GetNextImageInList(image) == (Image *) NULL)
           {
-            image=DestroyImageList(image);
-            return((Image *) NULL);
+            status=MagickFalse;
+            break;
           }
         image=SyncNextImageInList(image);
         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
@@ -647,6 +749,8 @@ static Image *ReadICONImage(const ImageInfo *image_info,
       }
   }
   (void) CloseBlob(image);
+  if (status == MagickFalse)
+    return(DestroyImageList(image));
   return(GetFirstImageInList(image));
 }
 \f
@@ -678,29 +782,25 @@ ModuleExport size_t RegisterICONImage(void)
   MagickInfo
     *entry;
 
-  entry=SetMagickInfo("CUR");
+  entry=AcquireMagickInfo("ICON","CUR","Microsoft icon");
   entry->decoder=(DecodeImageHandler *) ReadICONImage;
   entry->encoder=(EncodeImageHandler *) WriteICONImage;
-  entry->adjoin=MagickFalse;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString("Microsoft icon");
-  entry->module=ConstantString("CUR");
+  entry->flags^=CoderAdjoinFlag;
+  entry->flags|=CoderDecoderSeekableStreamFlag;
+  entry->flags|=CoderEncoderSeekableStreamFlag;
   (void) RegisterMagickInfo(entry);
-  entry=SetMagickInfo("ICO");
+  entry=AcquireMagickInfo("ICON","ICO","Microsoft icon");
   entry->decoder=(DecodeImageHandler *) ReadICONImage;
   entry->encoder=(EncodeImageHandler *) WriteICONImage;
-  entry->adjoin=MagickTrue;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString("Microsoft icon");
-  entry->module=ConstantString("ICON");
+  entry->flags|=CoderDecoderSeekableStreamFlag;
+  entry->flags|=CoderEncoderSeekableStreamFlag;
   (void) RegisterMagickInfo(entry);
-  entry=SetMagickInfo("ICON");
+  entry=AcquireMagickInfo("ICON","ICON","Microsoft icon");
   entry->decoder=(DecodeImageHandler *) ReadICONImage;
   entry->encoder=(EncodeImageHandler *) WriteICONImage;
-  entry->adjoin=MagickFalse;
-  entry->seekable_stream=MagickTrue;
-  entry->description=ConstantString("Microsoft icon");
-  entry->module=ConstantString("ICON");
+  entry->flags^=CoderAdjoinFlag;
+  entry->flags|=CoderDecoderSeekableStreamFlag;
+  entry->flags|=CoderEncoderSeekableStreamFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -746,6 +846,10 @@ ModuleExport void UnregisterICONImage(void)
 %  image format, version 3 for Windows or (if the image has a matte channel)
 %  version 4.
 %
+%  It encodes any subimage as a compressed PNG image ("BI_PNG)", only when its
+%  dimensions are 256x256 and image->compression is undefined or is defined as
+%  ZipCompression.
+%
 %  The format of the WriteICONImage method is:
 %
 %      MagickBooleanType WriteICONImage(const ImageInfo *image_info,
@@ -763,6 +867,9 @@ ModuleExport void UnregisterICONImage(void)
 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
   Image *image,ExceptionInfo *exception)
 {
+  const char
+    *option;
+
   IconFile
     icon_file;
 
@@ -770,6 +877,7 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
     icon_info;
 
   Image
+    *images,
     *next;
   
   MagickBooleanType
@@ -791,48 +899,58 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
 
   size_t
     bytes_per_line,
+    imageListLength,
     scanline_pad;
 
   ssize_t
     y;
 
   unsigned char
-    bit,
-    byte,
     *pixels;
 
   /*
     Open output image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
-  scene=0;
-  next=image;
-  do
-  {
-    if ((image->columns > 256L) || (image->rows > 256L))
-      ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
-    scene++;
-    next=SyncNextImageInList(next);
-  } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
+  images=(Image *) NULL;
+  option=GetImageOption(image_info,"icon:auto-resize");
+  if (option != (const char *) NULL)
+    {
+      images=AutoResizeImage(image,option,&scene,exception);
+      if (images == (Image *) NULL)
+        ThrowWriterException(ImageError,"InvalidDimensions");
+    }
+  else
+    {
+      scene=0;
+      next=image;
+      do
+      {
+        if ((image->columns > 256L) || (image->rows > 256L))
+          ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
+        scene++;
+        next=SyncNextImageInList(next);
+      } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
+    }
   /*
     Dump out a ICON header template to be properly initialized later.
   */
   (void) WriteBlobLSBShort(image,0);
   (void) WriteBlobLSBShort(image,1);
   (void) WriteBlobLSBShort(image,(unsigned char) scene);
-  (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
-  (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
+  (void) memset(&icon_file,0,sizeof(icon_file));
+  (void) memset(&icon_info,0,sizeof(icon_info));
   scene=0;
-  next=image;
+  next=(images != (Image *) NULL) ? images : image;
   do
   {
     (void) WriteBlobByte(image,icon_file.directory[scene].width);
@@ -849,10 +967,13 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
     next=SyncNextImageInList(next);
   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
   scene=0;
-  next=image;
+  next=(images != (Image *) NULL) ? images : image;
+  imageListLength=GetImageListLength(image);
   do
   {
-    if ((next->columns == 256) && (next->rows == 256))
+    if ((next->columns > 255L) && (next->rows > 255L) &&
+        ((next->compression == UndefinedCompression) ||
+        (next->compression == ZipCompression)))
       {
         Image
           *write_image;
@@ -866,20 +987,31 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
         unsigned char
           *png;
 
-        /*
-          Icon image encoded as a compressed PNG image.
-        */
         write_image=CloneImage(next,0,0,MagickTrue,exception);
         if (write_image == (Image *) NULL)
-          return(MagickFalse);
+          {
+            images=DestroyImageList(images);
+            return(MagickFalse);
+          }
         write_info=CloneImageInfo(image_info);
-        (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
+        (void) CopyMagickString(write_info->magick,"PNG",MagickPathExtent);
+        length=0;
+
+        /* Don't write any ancillary chunks except for gAMA */
+        (void) SetImageArtifact(write_image,"png:include-chunk","none,gama");
+
+        /* Only write PNG32 formatted PNG (32-bit RGBA), 8 bits per channel */
+        (void) SetImageArtifact(write_image,"png:format","png32");
+
         png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
           exception);
-        write_image=DestroyImage(write_image);
+        write_image=DestroyImageList(write_image);
         write_info=DestroyImageInfo(write_info);
         if (png == (unsigned char *) NULL)
-          return(MagickFalse);
+          {
+            images=DestroyImageList(images);
+            return(MagickFalse);
+          }
         icon_file.directory[scene].width=0;
         icon_file.directory[scene].height=0;
         icon_file.directory[scene].colors=0;
@@ -896,8 +1028,7 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
         /*
           Initialize ICON raster file header.
         */
-        if (next->colorspace != RGBColorspace)
-          (void) TransformImageColorspace(next,RGBColorspace);
+        (void) TransformImageColorspace(next,sRGBColorspace,exception);
         icon_info.file_size=14+12+28;
         icon_info.offset_bits=icon_info.file_size;
         icon_info.compression=BI_RGB;
@@ -938,9 +1069,6 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
               }
             else
               {
-                size_t
-                  one;
-
                 one=1;
                 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
                 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
@@ -967,14 +1095,14 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
           case UndefinedResolution:
           case PixelsPerInchResolution:
           {
-            icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
-            icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
+            icon_info.x_pixels=(size_t) (100.0*next->resolution.x/2.54);
+            icon_info.y_pixels=(size_t) (100.0*next->resolution.y/2.54);
             break;
           }
           case PixelsPerCentimeterResolution:
           {
-            icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
-            icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
+            icon_info.x_pixels=(size_t) (100.0*next->resolution.x);
+            icon_info.y_pixels=(size_t) (100.0*next->resolution.y);
             break;
           }
         }
@@ -985,8 +1113,11 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
         pixels=(unsigned char *) AcquireQuantumMemory((size_t)
           icon_info.image_size,sizeof(*pixels));
         if (pixels == (unsigned char *) NULL)
-          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
-        (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
+          {
+            images=DestroyImageList(images);
+            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+          }
+        (void) memset(pixels,0,(size_t) icon_info.image_size);
         switch (icon_info.bits_per_pixel)
         {
           case 1:
@@ -1113,7 +1244,7 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
                 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
                 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
                 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
-                if (next->matte == MagickFalse)
+                if (next->alpha_trait == UndefinedPixelTrait)
                   *q++=ScaleQuantumToChar(QuantumRange);
                 else
                   *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
@@ -1166,7 +1297,11 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
             icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
               (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
             if (icon_colormap == (unsigned char *) NULL)
-              ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+              {
+                images=DestroyImageList(images);
+                ThrowWriterException(ResourceLimitError,
+                  "MemoryAllocationFailed");
+              }
             q=icon_colormap;
             for (i=0; i < (ssize_t) next->colors; i++)
             {
@@ -1195,6 +1330,10 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
         scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
         for (y=((ssize_t) next->rows - 1); y >= 0; y--)
         {
+          unsigned char
+            bit,
+            byte;
+
           p=GetVirtualPixels(next,0,y,next->columns,1,exception);
           if (p == (const Quantum *) NULL)
             break;
@@ -1203,7 +1342,7 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
           for (x=0; x < (ssize_t) next->columns; x++)
           {
             byte<<=1;
-            if ((next->matte == MagickTrue) &&
+            if ((next->alpha_trait != UndefinedPixelTrait) &&
                 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
               byte|=0x01;
             bit++;
@@ -1223,11 +1362,10 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
       }
     if (GetNextImageInList(next) == (Image *) NULL)
       break;
-    next=SyncNextImageInList(next);
-    status=SetImageProgress(next,SaveImagesTag,scene++,
-      GetImageListLength(next));
+    status=SetImageProgress(next,SaveImagesTag,scene++,imageListLength);
     if (status == MagickFalse)
       break;
+    next=SyncNextImageInList(next);
   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
   offset=SeekBlob(image,0,SEEK_SET);
   (void) offset;
@@ -1235,7 +1373,7 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
   (void) WriteBlobLSBShort(image,1);
   (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
   scene=0;
-  next=image;
+  next=(images != (Image *) NULL) ? images : image;
   do
   {
     (void) WriteBlobByte(image,icon_file.directory[scene].width);
@@ -1252,5 +1390,6 @@ static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
     next=SyncNextImageInList(next);
   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
   (void) CloseBlob(image);
+  images=DestroyImageList(images);
   return(MagickTrue);
 }