]> granicus.if.org Git - imagemagick/blobdiff - coders/pnm.c
(no commit message)
[imagemagick] / coders / pnm.c
index 9c31fa1e725f48852b9a4e51bf0d10e14d1c1da9..5d79b2d5d26d7af726f61580db27bccebe0d317f 100644 (file)
 %               Read/Write PBMPlus Portable Anymap Image Format               %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 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 declarations.
 */
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/cache.h"
-#include "magick/color.h"
-#include "magick/color-private.h"
-#include "magick/colorspace.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/module.h"
-#include "magick/monitor.h"
-#include "magick/monitor-private.h"
-#include "magick/pixel-private.h"
-#include "magick/property.h"
-#include "magick/quantum-private.h"
-#include "magick/static.h"
-#include "magick/statistic.h"
-#include "magick/string_.h"
-#include "magick/string-private.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/attribute.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/cache.h"
+#include "MagickCore/color.h"
+#include "MagickCore/color-private.h"
+#include "MagickCore/colorspace.h"
+#include "MagickCore/colorspace-private.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/module.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
+#include "MagickCore/pixel-accessor.h"
+#include "MagickCore/property.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/static.h"
+#include "MagickCore/statistic.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/string-private.h"
 \f
 /*
   Forward declarations.
 */
 static MagickBooleanType
-  WritePNMImage(const ImageInfo *,Image *);
+  WritePNMImage(const ImageInfo *,Image *,ExceptionInfo *);
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -134,81 +136,84 @@ static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
 %
 */
 
-static inline long ConstrainPixel(Image *image,const long offset,
-  const unsigned long extent)
+static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
+  const size_t extent,ExceptionInfo *exception)
 {
-  if ((offset < 0) || (offset > (long) extent))
+  if ((offset < 0) || (offset > (ssize_t) extent))
     {
-      (void) ThrowMagickException(&image->exception,GetMagickModule(),
-        CorruptImageError,"InvalidPixel","`%s'",image->filename);
+      (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
+        "InvalidPixel","`%s'",image->filename);
       return(0);
     }
   return(offset);
 }
 
-static unsigned long PNMInteger(Image *image,const unsigned int base)
+static void PNMComment(Image *image,ExceptionInfo *exception)
 {
-  char
-    *comment;
-
   int
     c;
 
+  char
+    *comment;
+
   register char
     *p;
 
   size_t
     extent;
 
-  unsigned long
+  /*
+    Read comment.
+  */
+  comment=AcquireString(GetImageProperty(image,"comment",exception));
+  extent=strlen(comment);
+  p=comment+strlen(comment);
+  for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
+  {
+    if ((size_t) (p-comment+1) >= extent)
+      {
+        extent<<=1;
+        comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
+          sizeof(*comment));
+        if (comment == (char *) NULL)
+          break;
+        p=comment+strlen(comment);
+      }
+    c=ReadBlobByte(image);
+    if (c != EOF)
+      {
+        *p=(char) c;
+        *(p+1)='\0';
+      }
+  }
+  if (comment == (char *) NULL)
+    return;
+  (void) SetImageProperty(image,"comment",comment,exception);
+  comment=DestroyString(comment);
+}
+
+static size_t PNMInteger(Image *image,const unsigned int base,
+  ExceptionInfo *exception)
+{
+  int
+    c;
+
+  size_t
     value;
 
   /*
     Skip any leading whitespace.
   */
-  extent=MaxTextExtent;
-  comment=(char *) NULL;
-  p=comment;
   do
   {
     c=ReadBlobByte(image);
     if (c == EOF)
       return(0);
     if (c == (int) '#')
-      {
-        /*
-          Read comment.
-        */
-        if (comment == (char *) NULL)
-          comment=AcquireString((char *) NULL);
-        p=comment+strlen(comment);
-        for ( ; (c != EOF) && (c != (int) '\n'); p++)
-        {
-          if ((size_t) (p-comment+1) >= extent)
-            {
-              extent<<=1;
-              comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
-                sizeof(*comment));
-              if (comment == (char *) NULL)
-                break;
-              p=comment+strlen(comment);
-            }
-          c=ReadBlobByte(image);
-          *p=(char) c;
-          *(p+1)='\0';
-        }
-        if (comment == (char *) NULL)
-          return(0);
-        continue;
-      }
+      PNMComment(image,exception);
   } while (isdigit(c) == MagickFalse);
-  if (comment != (char *) NULL)
-    {
-      (void) SetImageProperty(image,"comment",comment);
-      comment=DestroyString(comment);
-    }
   if (base == 2)
-    return((unsigned long) (c-(int) '0'));
+    return((size_t) (c-(int) '0'));
   /*
     Evaluate number.
   */
@@ -235,15 +240,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   Image
     *image;
 
-  long
-    row,
-    y;
-
   MagickBooleanType
     status;
 
-  Quantum
-    *scale;
+  QuantumAny
+    max_value;
 
   QuantumInfo
     *quantum_info;
@@ -251,22 +252,15 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   QuantumType
     quantum_type;
 
-  register long
-    i;
-
   size_t
+    depth,
     extent,
     packet_size;
 
   ssize_t
-    count;
-
-  unsigned long
-    depth,
-    max_value;
-
-  CacheView
-    *image_view;
+    count,
+    row,
+    y;
 
   /*
     Open image file.
@@ -278,7 +272,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  image=AcquireImage(image_info);
+  image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
     {
@@ -305,22 +299,22 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           PBM, PGM, PPM, and PNM.
         */
-        image->columns=PNMInteger(image,10);
-        image->rows=PNMInteger(image,10);
+        image->columns=PNMInteger(image,10,exception);
+        image->rows=PNMInteger(image,10,exception);
         if ((format == 'f') || (format == 'F'))
           {
             char
               scale[MaxTextExtent];
 
             (void) ReadBlobString(image,scale);
-            quantum_scale=StringToDouble(scale);
+            quantum_scale=StringToDouble(scale,(char **) NULL);
           }
         else
           {
             if ((format == '1') || (format == '4'))
               max_value=1;  /* bitmap */
             else
-              max_value=PNMInteger(image,10);
+              max_value=PNMInteger(image,10,exception);
           }
       }
     else
@@ -342,6 +336,16 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         {
           while (isspace((int) ((unsigned char) c)) != 0)
             c=ReadBlobByte(image);
+          if (c == '#')
+            {
+              /*
+                Comment.
+              */
+              PNMComment(image,exception);
+              c=ReadBlobByte(image);
+              while (isspace((int) ((unsigned char) c)) != 0)
+                c=ReadBlobByte(image);
+            }
           p=keyword;
           do
           {
@@ -367,6 +371,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           */
           if (LocaleCompare(keyword,"depth") == 0)
             packet_size=StringToUnsignedLong(value);
+          (void) packet_size;
           if (LocaleCompare(keyword,"height") == 0)
             image->rows=StringToUnsignedLong(value);
           if (LocaleCompare(keyword,"maxval") == 0)
@@ -374,34 +379,42 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (LocaleCompare(keyword,"TUPLTYPE") == 0)
             {
               if (LocaleCompare(value,"BLACKANDWHITE") == 0)
-                quantum_type=GrayQuantum;
+                {
+                  (void) SetImageColorspace(image,GRAYColorspace,exception);
+                  quantum_type=GrayQuantum;
+                }
               if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
                 {
+                  (void) SetImageColorspace(image,GRAYColorspace,exception);
+                  image->alpha_trait=BlendPixelTrait;
                   quantum_type=GrayAlphaQuantum;
-                  image->matte=MagickTrue;
                 }
               if (LocaleCompare(value,"GRAYSCALE") == 0)
-                quantum_type=GrayQuantum;
+                {
+                  quantum_type=GrayQuantum;
+                  (void) SetImageColorspace(image,GRAYColorspace,exception);
+                }
               if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
                 {
+                  (void) SetImageColorspace(image,GRAYColorspace,exception);
+                  image->alpha_trait=BlendPixelTrait;
                   quantum_type=GrayAlphaQuantum;
-                  image->matte=MagickTrue;
                 }
               if (LocaleCompare(value,"RGB_ALPHA") == 0)
                 {
+                  image->alpha_trait=BlendPixelTrait;
                   quantum_type=RGBAQuantum;
-                  image->matte=MagickTrue;
                 }
               if (LocaleCompare(value,"CMYK") == 0)
                 {
+                  (void) SetImageColorspace(image,CMYKColorspace,exception);
                   quantum_type=CMYKQuantum;
-                  image->colorspace=CMYKColorspace;
                 }
               if (LocaleCompare(value,"CMYK_ALPHA") == 0)
                 {
+                  (void) SetImageColorspace(image,CMYKColorspace,exception);
+                  image->alpha_trait=BlendPixelTrait;
                   quantum_type=CMYKAQuantum;
-                  image->colorspace=CMYKColorspace;
-                  image->matte=MagickTrue;
                 }
             }
           if (LocaleCompare(keyword,"width") == 0)
@@ -410,7 +423,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
     if ((image->columns == 0) || (image->rows == 0))
       ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
-    if (max_value >= 65536)
+    if (max_value > 4294967295)
       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     for (depth=1; GetQuantumRange(depth) < max_value; depth++) ;
     image->depth=depth;
@@ -429,29 +442,30 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PBM image to pixel packets.
         */
-        for (y=0; y < (long) image->rows; y++)
+        (void) SetImageColorspace(image,GRAYColorspace,exception);
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register long
+          register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             break;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
-            q->red=(Quantum) (PNMInteger(image,2) == 0 ? QuantumRange : 0);
-            q->green=q->red;
-            q->blue=q->red;
-            q++;
+            SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
+              QuantumRange : 0,q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -461,123 +475,82 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
       case '2':
       {
-        unsigned long
+        size_t
           intensity;
 
         /*
           Convert PGM image to pixel packets.
         */
-        scale=(Quantum *) NULL;
-        if (max_value != (1U*QuantumRange))
-          {
-            /*
-              Compute pixel scaling table.
-            */
-            scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
-              sizeof(*scale));
-            if (scale == (Quantum *) NULL)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-            for (i=0; i <= (long) max_value; i++)
-              scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
-          }
-        for (y=0; y < (long) image->rows; y++)
+        (void) SetImageColorspace(image,GRAYColorspace,exception);
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register long
+          register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             break;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
-            intensity=PNMInteger(image,10);
-            if (scale != (Quantum *) NULL)
-              intensity=(unsigned long) scale[ConstrainPixel(image,(long)
-                intensity,max_value)];
-            q->red=(Quantum) intensity;
-            q->green=q->red;
-            q->blue=q->red;
-            q++;
+            intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
+            SetPixelGray(image,intensity,q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
         }
         image->type=GrayscaleType;
-        if (scale != (Quantum *) NULL)
-          scale=(Quantum *) RelinquishMagickMemory(scale);
         break;
       }
       case '3':
       {
-        MagickPixelPacket
-          pixel;
-
         /*
           Convert PNM image to pixel packets.
         */
-        scale=(Quantum *) NULL;
-        if (max_value != (1U*QuantumRange))
-          {
-            /*
-              Compute pixel scaling table.
-            */
-            scale=(Quantum *) AcquireQuantumMemory((size_t) max_value+1UL,
-              sizeof(*scale));
-            if (scale == (Quantum *) NULL)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-            for (i=0; i <= (long) max_value; i++)
-              scale[i]=(Quantum) (((double) QuantumRange*i)/max_value+0.5);
-          }
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register long
+          register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             break;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
-            pixel.red=(MagickRealType) PNMInteger(image,10);
-            pixel.green=(MagickRealType) PNMInteger(image,10);
-            pixel.blue=(MagickRealType) PNMInteger(image,10);
-            if (scale != (Quantum *) NULL)
-              {
-                pixel.red=(MagickRealType) scale[ConstrainPixel(image,(long)
-                  pixel.red,max_value)];
-                pixel.green=(MagickRealType) scale[ConstrainPixel(image,(long)
-                  pixel.green,max_value)];
-                pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(long)
-                  pixel.blue,max_value)];
-              }
-            q->red=(Quantum) pixel.red;
-            q->green=(Quantum) pixel.green;
-            q->blue=(Quantum) pixel.blue;
-            q++;
+            double
+              pixel;
+
+            pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
+            SetPixelRed(image,pixel,q);
+            pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
+            SetPixelGreen(image,pixel,q);
+            pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
+            SetPixelBlue(image,pixel,q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
         }
-        if (scale != (Quantum *) NULL)
-          scale=(Quantum *) RelinquishMagickMemory(scale);
         break;
       }
       case '4':
@@ -585,6 +558,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PBM raw image to pixel packets.
         */
+        (void) SetImageColorspace(image,GRAYColorspace,exception);
         quantum_type=GrayQuantum;
         if (image->storage_class == PseudoClass)
           quantum_type=IndexQuantum;
@@ -593,23 +567,17 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         SetQuantumMinIsWhite(quantum_info,MagickTrue);
         extent=GetQuantumExtent(image,quantum_info,quantum_type);
-        image_view=AcquireCacheView(image);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
-#endif
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          long
-            offset;
-
           MagickBooleanType
             sync;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
-            count;
+            count,
+            offset;
 
           size_t
             length;
@@ -620,9 +588,6 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (status == MagickFalse)
             continue;
           pixels=GetQuantumPixels(quantum_info);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp critical (MagickCore_ReadPNMImage)
-#endif
           {
             count=ReadBlob(image,extent,pixels);
             if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
@@ -631,7 +596,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 MagickBooleanType
                   proceed;
 
-                proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
+                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                  row,image->rows);
                 if (proceed == MagickFalse)
                   status=MagickFalse;
               }
@@ -639,22 +605,20 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           if (count != (ssize_t) extent)
             status=MagickFalse;
-          q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
-            exception);
-          if (q == (PixelPacket *) NULL)
+          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
             }
-          length=ImportQuantumPixels(image,image_view,quantum_info,quantum_type,
-            pixels,exception);
+          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+            quantum_type,pixels,exception);
           if (length != extent)
             status=MagickFalse;
-          sync=SyncCacheViewAuthenticPixels(image_view,exception);
+          sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
             status=MagickFalse;
         }
-        image_view=DestroyCacheView(image_view);
         quantum_info=DestroyQuantumInfo(quantum_info);
         if (status == MagickFalse)
           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
@@ -663,41 +627,38 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
       case '5':
       {
-        QuantumAny
-          range;
-
         /*
           Convert PGM raw image to pixel packets.
         */
-        range=GetQuantumRange(image->depth);
+        (void) SetImageColorspace(image,GRAYColorspace,exception);
         quantum_type=GrayQuantum;
-        extent=(image->depth <= 8 ? 1 : 2)*image->columns;
+        if (image->depth <= 8)
+          extent=image->columns;
+        else
+          if (image->depth <= 16)
+            extent=2*image->columns;
+          else
+            extent=4*image->columns;
         quantum_info=AcquireQuantumInfo(image_info,image);
         if (quantum_info == (QuantumInfo *) NULL)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        image_view=AcquireCacheView(image);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
-#endif
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          long
-            offset;
-
           MagickBooleanType
             sync;
 
           register const unsigned char
-            *p;
+            *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
-            count;
+            count,
+            offset;
 
           unsigned char
             *pixels;
@@ -705,9 +666,6 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (status == MagickFalse)
             continue;
           pixels=GetQuantumPixels(quantum_info);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp critical (MagickCore_ReadPNMImage)
-#endif
           {
             count=ReadBlob(image,extent,pixels);
             if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
@@ -716,7 +674,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 MagickBooleanType
                   proceed;
 
-                proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
+                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                  row,image->rows);
                 if (proceed == MagickFalse)
                   status=MagickFalse;
               }
@@ -724,51 +683,67 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           if (count != (ssize_t) extent)
             status=MagickFalse;
-          q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
-            exception);
-          if (q == (PixelPacket *) NULL)
+          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
             }
           p=pixels;
-          if ((image->depth == 8) || (image->depth == 16))
-            (void) ImportQuantumPixels(image,image_view,quantum_info,
-              quantum_type,pixels,exception);
-          else
-            if (image->depth <= 8)
-              {
-                unsigned char
-                  pixel;
+          switch (image->depth)
+          {
+            case 8:
+            case 16:
+            case 32:
+            {
+              (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+                quantum_type,pixels,exception);
+              break;
+            }
+            default:
+            {
+              unsigned int
+                pixel;
 
-                for (x=0; x < (long) image->columns; x++)
+              if (image->depth <= 8)
                 {
-                  p=PushCharPixel(p,&pixel);
-                  q->red=ScaleAnyToQuantum(pixel,range);
-                  q->green=q->red;
-                  q->blue=q->red;
-                  q++;
-                }
-              }
-            else
-              {
-                unsigned short
-                  pixel;
+                  unsigned char
+                    pixel;
 
-                for (x=0; x < (long) image->columns; x++)
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    p=PushCharPixel(p,&pixel);
+                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    q+=GetPixelChannels(image);
+                  }
+                  break;
+                }
+              if (image->depth <= 16)
                 {
-                  p=PushShortPixel(MSBEndian,p,&pixel);
-                  q->red=ScaleAnyToQuantum(pixel,range);
-                  q->green=q->red;
-                  q->blue=q->red;
-                  q++;
+                  unsigned short
+                    pixel;
+
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    p=PushShortPixel(MSBEndian,p,&pixel);
+                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    q+=GetPixelChannels(image);
+                  }
+                  break;
                 }
+              for (x=0; x < (ssize_t) image->columns; x++)
+              {
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
+                q+=GetPixelChannels(image);
               }
-          sync=SyncCacheViewAuthenticPixels(image_view,exception);
+              break;
+            }
+          }
+          sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
             status=MagickFalse;
         }
-        image_view=DestroyCacheView(image_view);
         quantum_info=DestroyQuantumInfo(quantum_info);
         if (status == MagickFalse)
           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
@@ -777,48 +752,32 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
       case '6':
       {
-        ImageType
-          type;
-
-        QuantumAny
-          range;
-
         /*
           Convert PNM raster image to pixel packets.
         */
-        type=BilevelType;
         quantum_type=RGBQuantum;
         extent=3*(image->depth <= 8 ? 1 : 2)*image->columns;
-        range=GetQuantumRange(image->depth);
         quantum_info=AcquireQuantumInfo(image_info,image);
         if (quantum_info == (QuantumInfo *) NULL)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        image_view=AcquireCacheView(image);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp parallel for schedule(static,1) shared(row,status,type)
-#endif
-        for (y=0; y < (long) image->rows; y++)
+        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          long
-            offset;
-
           MagickBooleanType
             sync;
 
           register const unsigned char
-            *p;
+            *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
-            count;
-
-          size_t
-            length;
+            count,
+            offset;
 
           unsigned char
             *pixels;
@@ -826,9 +785,6 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (status == MagickFalse)
             continue;
           pixels=GetQuantumPixels(quantum_info);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp critical (MagickCore_ReadPNMImage)
-#endif
           {
             count=ReadBlob(image,extent,pixels);
             if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
@@ -837,7 +793,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 MagickBooleanType
                   proceed;
 
-                proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
+                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                  row,image->rows);
                 if (proceed == MagickFalse)
                   status=MagickFalse;
               }
@@ -845,102 +802,135 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           if (count != (ssize_t) extent)
             status=MagickFalse;
-          q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
-            exception);
-          if (q == (PixelPacket *) NULL)
+          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
             }
           p=pixels;
-          if ((image->depth == 8) || (image->depth == 16))
+          switch (image->depth)
+          {
+            case 8:
             {
-              length=ImportQuantumPixels(image,image_view,quantum_info,
-                quantum_type,pixels,exception);
-              if (length != extent)
-                status=MagickFalse;
-            }
-          else
-            if (image->depth <= 8)
+              for (x=0; x < (ssize_t) image->columns; x++)
               {
-                unsigned char
-                  pixel;
-
-                register PixelPacket
-                  *restrict r;
+                SetPixelRed(image,ScaleCharToQuantum(*p++),q);
+                SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
+                SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
+                SetPixelAlpha(image,OpaqueAlpha,q);
+                q+=GetPixelChannels(image);
+              }
+              break;
+            }
+            case 16:
+            {
+              unsigned short
+                pixel;
 
-                r=q;
-                for (x=0; x < (long) image->columns; x++)
-                {
-                  p=PushCharPixel(p,&pixel);
-                  r->red=ScaleAnyToQuantum(pixel,range);
-                  p=PushCharPixel(p,&pixel);
-                  r->green=ScaleAnyToQuantum(pixel,range);
-                  p=PushCharPixel(p,&pixel);
-                  r->blue=ScaleAnyToQuantum(pixel,range);
-                  r++;
-                }
+              for (x=0; x < (ssize_t) image->columns; x++)
+              {
+                p=PushShortPixel(MSBEndian,p,&pixel);
+                SetPixelRed(image,ScaleShortToQuantum(pixel),q);
+                p=PushShortPixel(MSBEndian,p,&pixel);
+                SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
+                p=PushShortPixel(MSBEndian,p,&pixel);
+                SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
+                SetPixelAlpha(image,OpaqueAlpha,q);
+                q+=GetPixelChannels(image);
               }
-            else
+              break;
+            }
+            case 32:
+            {
+              unsigned int
+                pixel;
+
+              for (x=0; x < (ssize_t) image->columns; x++)
               {
-                unsigned short
-                  pixel;
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelRed(image,ScaleLongToQuantum(pixel),q);
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelGreen(image,ScaleLongToQuantum(pixel),q);
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelBlue(image,ScaleLongToQuantum(pixel),q);
+                SetPixelAlpha(image,OpaqueAlpha,q);
+                q+=GetPixelChannels(image);
+              }
+              break;
+            }
+            default:
+            {
+              unsigned int
+                pixel;
 
-                register PixelPacket
-                  *restrict r;
+              if (image->depth <= 8)
+                {
+                  unsigned char
+                    pixel;
 
-                r=q;
-                for (x=0; x < (long) image->columns; x++)
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    p=PushCharPixel(p,&pixel);
+                    SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushCharPixel(p,&pixel);
+                    SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushCharPixel(p,&pixel);
+                    SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    q+=GetPixelChannels(image);
+                  }
+                  break;
+                }
+              if (image->depth <= 16)
                 {
-                  p=PushShortPixel(MSBEndian,p,&pixel);
-                  r->red=ScaleAnyToQuantum(pixel,range);
-                  p=PushShortPixel(MSBEndian,p,&pixel);
-                  r->green=ScaleAnyToQuantum(pixel,range);
-                  p=PushShortPixel(MSBEndian,p,&pixel);
-                  r->blue=ScaleAnyToQuantum(pixel,range);
-                  r++;
+                  unsigned short
+                    pixel;
+
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    p=PushShortPixel(MSBEndian,p,&pixel);
+                    SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushShortPixel(MSBEndian,p,&pixel);
+                    SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushShortPixel(MSBEndian,p,&pixel);
+                    SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    q+=GetPixelChannels(image);
+                  }
+                  break;
                 }
+              for (x=0; x < (ssize_t) image->columns; x++)
+              {
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                p=PushLongPixel(MSBEndian,p,&pixel);
+                SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                SetPixelAlpha(image,OpaqueAlpha,q);
+                q+=GetPixelChannels(image);
               }
-          if ((type == BilevelType) || (type == GrayscaleType))
-            for (x=0; x < (long) image->columns; x++)
-            {
-              if ((type == BilevelType) &&
-                  (IsMonochromePixel(q) == MagickFalse))
-                type=IsGrayPixel(q) == MagickFalse ? UndefinedType :
-                  GrayscaleType;
-              if ((type == GrayscaleType) && (IsGrayPixel(q) == MagickFalse))
-                type=UndefinedType;
-              if ((type != BilevelType) && (type != GrayscaleType))
-                break;
-              q++;
+              break;
             }
-          sync=SyncCacheViewAuthenticPixels(image_view,exception);
+          }
+          sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
             status=MagickFalse;
         }
-        image_view=DestroyCacheView(image_view);
         quantum_info=DestroyQuantumInfo(quantum_info);
         if (status == MagickFalse)
           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
-        if (type != UndefinedType)
-          image->type=type;
         break;
       }
       case '7':
       {
-        register IndexPacket
-          *indexes;
-
-        QuantumAny
-          range;
-
-        unsigned long
+        size_t
           channels;
 
         /*
           Convert PAM raster image to pixel packets.
         */
-        range=GetQuantumRange(image->depth);
         switch (quantum_type)
         {
           case GrayQuantum:
@@ -961,35 +951,35 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             break;
           }
         }
-        if (image->matte != MagickFalse)
+        if (image->alpha_trait == BlendPixelTrait)
           channels++;
-        extent=channels*(image->depth <= 8 ? 1 : 2)*image->columns;
+        if (image->depth <= 8)
+          extent=channels*image->columns;
+        else
+          if (image->depth <= 16)
+            extent=2*channels*image->columns;
+          else
+            extent=4*channels*image->columns;
         quantum_info=AcquireQuantumInfo(image_info,image);
         if (quantum_info == (QuantumInfo *) NULL)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        image_view=AcquireCacheView(image);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
-#endif
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          long
-            offset;
-
           MagickBooleanType
             sync;
 
           register const unsigned char
-            *p;
+            *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
-            count;
+            count,
+            offset;
 
           unsigned char
             *pixels;
@@ -997,9 +987,6 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (status == MagickFalse)
             continue;
           pixels=GetQuantumPixels(quantum_info);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp critical (MagickCore_ReadPNMImage)
-#endif
           {
             count=ReadBlob(image,extent,pixels);
             if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
@@ -1008,7 +995,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 MagickBooleanType
                   proceed;
 
-                proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
+                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                  row,image->rows);
                 if (proceed == MagickFalse)
                   status=MagickFalse;
               }
@@ -1016,173 +1004,246 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           if (count != (ssize_t) extent)
             status=MagickFalse;
-          q=QueueCacheViewAuthenticPixels(image_view,0,offset,image->columns,1,
-            exception);
-          if (q == (PixelPacket *) NULL)
+          q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
             }
-          indexes=GetCacheViewAuthenticIndexQueue(image_view);
           p=pixels;
-          if ((image->depth == 8) || (image->depth == 16))
-            (void) ImportQuantumPixels(image,image_view,quantum_info,
-              quantum_type,pixels,exception);
-          else
-            switch (quantum_type)
+          switch (image->depth)
+          {
+            case 8:
+            case 16:
+            case 32:
             {
-              case GrayQuantum:
-              case GrayAlphaQuantum:
+              (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+                quantum_type,pixels,exception);
+              break;
+            }
+            default:
+            {
+              switch (quantum_type)
               {
-                if (image->depth <= 8)
-                  {
-                    unsigned char
-                      pixel;
+                case GrayQuantum:
+                case GrayAlphaQuantum:
+                {
+                  unsigned int
+                    pixel;
 
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 8)
                     {
-                      p=PushCharPixel(p,&pixel);
-                      q->red=ScaleAnyToQuantum(pixel,range);
-                      q->green=q->red;
-                      q->blue=q->red;
-                      SetOpacitySample(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
-                        {
-                          p=PushCharPixel(p,&pixel);
-                          q->opacity=ScaleAnyToQuantum(pixel,range);
-                        }
-                      q++;
+                      unsigned char
+                        pixel;
+
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,OpaqueAlpha,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            p=PushCharPixel(p,&pixel);
+                            if (image->depth != 1)
+                              SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
+                                max_value),q);
+                            else
+                              SetPixelAlpha(image,QuantumRange-
+                                ScaleAnyToQuantum(pixel,max_value),q);
+                          }
+                        q+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  }
-                else
-                  {
-                    unsigned short
-                      pixel;
-
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 16)
                     {
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->red=ScaleAnyToQuantum(pixel,range);
-                      q->green=q->red;
-                      q->blue=q->red;
-                      SetOpacitySample(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
-                        {
-                          p=PushShortPixel(MSBEndian,p,&pixel);
-                          q->opacity=ScaleAnyToQuantum(pixel,range);
-                        }
-                      q++;
+                      unsigned short
+                        pixel;
+
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
+                        SetPixelAlpha(image,OpaqueAlpha,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            p=PushShortPixel(MSBEndian,p,&pixel);
+                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
+                              max_value),q);
+                          }
+                        q+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  }
-                break;
-              }
-              case CMYKQuantum:
-              case CMYKAQuantum:
-              {
-                if (image->depth <= 8)
+                  for (x=0; x < (ssize_t) image->columns; x++)
                   {
-                    unsigned char
-                      pixel;
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    if (image->alpha_trait == BlendPixelTrait)
+                      {
+                        p=PushLongPixel(MSBEndian,p,&pixel);
+                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                      }
+                    q+=GetPixelChannels(image);
+                  }
+                  break;
+                }
+                case CMYKQuantum:
+                case CMYKAQuantum:
+                {
+                  unsigned int
+                    pixel;
 
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 8)
                     {
-                      p=PushCharPixel(p,&pixel);
-                      q->red=ScaleAnyToQuantum(pixel,range);
-                      p=PushCharPixel(p,&pixel);
-                      q->green=ScaleAnyToQuantum(pixel,range);
-                      p=PushCharPixel(p,&pixel);
-                      q->blue=ScaleAnyToQuantum(pixel,range);
-                      p=PushCharPixel(p,&pixel);
-                      indexes[x]=ScaleAnyToQuantum(pixel,range);
-                      SetOpacitySample(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
-                        {
-                          p=PushCharPixel(p,&pixel);
-                          q->opacity=ScaleAnyToQuantum(pixel,range);
-                        }
-                      q++;
+                      unsigned char
+                        pixel;
+
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,OpaqueAlpha,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            p=PushCharPixel(p,&pixel);
+                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
+                              max_value),q);
+                          }
+                        q+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  }
-                else
-                  {
-                    unsigned short
-                      pixel;
-
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 16)
                     {
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->red=ScaleAnyToQuantum(pixel,range);
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->green=ScaleAnyToQuantum(pixel,range);
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->blue=ScaleAnyToQuantum(pixel,range);
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      indexes[x]=ScaleAnyToQuantum(pixel,range);
-                      SetOpacitySample(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
-                        {
-                          p=PushShortPixel(MSBEndian,p,&pixel);
-                          q->opacity=ScaleAnyToQuantum(pixel,range);
-                        }
-                      q++;
+                      unsigned short
+                        pixel;
+
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,OpaqueAlpha,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            p=PushShortPixel(MSBEndian,p,&pixel);
+                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
+                              max_value),q);
+                          }
+                        q+=GetPixelChannels(image);
+                      }
                     }
-                  }
-                break;
-              }
-              default:
-              {
-                if (image->depth <= 8)
+                  for (x=0; x < (ssize_t) image->columns; x++)
                   {
-                    unsigned char
-                      pixel;
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    if (image->alpha_trait == BlendPixelTrait)
+                      {
+                        p=PushLongPixel(MSBEndian,p,&pixel);
+                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                      }
+                    q+=GetPixelChannels(image);
+                  }
+                  break;
+                }
+                default:
+                {
+                  unsigned int
+                    pixel;
 
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 8)
                     {
-                      p=PushCharPixel(p,&pixel);
-                      q->red=ScaleAnyToQuantum(pixel,range);
-                      p=PushCharPixel(p,&pixel);
-                      q->green=ScaleAnyToQuantum(pixel,range);
-                      p=PushCharPixel(p,&pixel);
-                      q->blue=ScaleAnyToQuantum(pixel,range);
-                      SetOpacitySample(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
-                        {
-                          p=PushCharPixel(p,&pixel);
-                          q->opacity=ScaleAnyToQuantum(pixel,range);
-                        }
-                      q++;
+                      unsigned char
+                        pixel;
+
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushCharPixel(p,&pixel);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,OpaqueAlpha,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            p=PushCharPixel(p,&pixel);
+                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                          }
+                        q+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  }
-                else
-                  {
-                    unsigned short
-                      pixel;
-
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 16)
                     {
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->red=ScaleAnyToQuantum(pixel,range);
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->green=ScaleAnyToQuantum(pixel,range);
-                      p=PushShortPixel(MSBEndian,p,&pixel);
-                      q->blue=ScaleAnyToQuantum(pixel,range);
-                      SetOpacitySample(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
-                        {
-                          p=PushShortPixel(MSBEndian,p,&pixel);
-                          q->opacity=ScaleAnyToQuantum(pixel,range);
-                        }
-                      q++;
+                      unsigned short
+                        pixel;
+
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        p=PushShortPixel(MSBEndian,p,&pixel);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,OpaqueAlpha,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            p=PushShortPixel(MSBEndian,p,&pixel);
+                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
+                              max_value),q);
+                          }
+                        q+=GetPixelChannels(image);
+                      }
+                      break;
                     }
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    p=PushLongPixel(MSBEndian,p,&pixel);
+                    SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    if (image->alpha_trait == BlendPixelTrait)
+                      {
+                        p=PushLongPixel(MSBEndian,p,&pixel);
+                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                      }
+                    q+=GetPixelChannels(image);
                   }
-                break;
+                  break;
+                }
               }
             }
-          sync=SyncCacheViewAuthenticPixels(image_view,exception);
+          }
+          sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
             status=MagickFalse;
         }
-        image_view=DestroyCacheView(image_view);
         quantum_info=DestroyQuantumInfo(quantum_info);
         if (status == MagickFalse)
           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
@@ -1195,6 +1256,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PFM raster image to pixel packets.
         */
+        if (format == 'f')
+          (void) SetImageColorspace(image,GRAYColorspace,exception);
         quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
         image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
         image->depth=32;
@@ -1207,26 +1270,19 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
         if (status == MagickFalse)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        SetQuantumScale(quantum_info,(MagickRealType) QuantumRange*
-          fabs(quantum_scale));
+        SetQuantumScale(quantum_info,(double) QuantumRange*fabs(quantum_scale));
         extent=GetQuantumExtent(image,quantum_info,quantum_type);
-        image_view=AcquireCacheView(image);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp parallel for schedule(static,1) shared(row,status,quantum_type)
-#endif
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          long
-            offset;
-
           MagickBooleanType
             sync;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
-            count;
+            count,
+            offset;
 
           size_t
             length;
@@ -1237,9 +1293,6 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (status == MagickFalse)
             continue;
           pixels=GetQuantumPixels(quantum_info);
-#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 200505)
-  #pragma omp critical (MagickCore_ReadPNMImage)
-#endif
           {
             count=ReadBlob(image,extent,pixels);
             if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
@@ -1248,7 +1301,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 MagickBooleanType
                   proceed;
 
-                proceed=SetImageProgress(image,LoadImageTag,row,image->rows);
+                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                  row,image->rows);
                 if (proceed == MagickFalse)
                   status=MagickFalse;
               }
@@ -1256,22 +1310,21 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           if ((size_t) count != extent)
             status=MagickFalse;
-          q=QueueCacheViewAuthenticPixels(image_view,0,(long) (image->rows-
-            offset-1),image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
+            image->columns,1,exception);
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
             }
-          length=ImportQuantumPixels(image,image_view,quantum_info,quantum_type,
-            pixels,exception);
+          length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+            quantum_type,pixels,exception);
           if (length != extent)
             status=MagickFalse;
-          sync=SyncCacheViewAuthenticPixels(image_view,exception);
+          sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
             status=MagickFalse;
         }
-        image_view=DestroyCacheView(image_view);
         quantum_info=DestroyQuantumInfo(quantum_info);
         if (status == MagickFalse)
           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
@@ -1282,8 +1335,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     }
     if (EOFBlob(image) != MagickFalse)
-      (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
-        "UnexpectedEndOfFile","`%s'",image->filename);
+      {
+        (void) ThrowMagickException(exception,GetMagickModule(),
+          CorruptImageError,"UnexpectedEndOfFile","`%s'",image->filename);
+        break;
+      }
     /*
       Proceed to next image.
     */
@@ -1308,7 +1364,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Allocate next image structure.
         */
-        AcquireNextImage(image_info,image);
+        AcquireNextImage(image_info,image,exception);
         if (GetNextImageInList(image) == (Image *) NULL)
           {
             image=DestroyImageList(image);
@@ -1345,10 +1401,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
 %
 %  The format of the RegisterPNMImage method is:
 %
-%      unsigned long RegisterPNMImage(void)
+%      size_t RegisterPNMImage(void)
 %
 */
-ModuleExport unsigned long RegisterPNMImage(void)
+ModuleExport size_t RegisterPNMImage(void)
 {
   MagickInfo
     *entry;
@@ -1357,17 +1413,20 @@ ModuleExport unsigned long RegisterPNMImage(void)
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
   entry->description=ConstantString("Common 2-dimensional bitmap format");
+  entry->mime_type=ConstantString("image/x-portable-pixmap");
   entry->module=ConstantString("PNM");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("PBM");
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
   entry->description=ConstantString("Portable bitmap format (black and white)");
+  entry->mime_type=ConstantString("image/x-portable-bitmap");
   entry->module=ConstantString("PNM");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("PFM");
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
+  entry->endian_support=MagickTrue;
   entry->description=ConstantString("Portable float format");
   entry->module=ConstantString("PFM");
   (void) RegisterMagickInfo(entry);
@@ -1375,6 +1434,7 @@ ModuleExport unsigned long RegisterPNMImage(void)
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
   entry->description=ConstantString("Portable graymap format (gray scale)");
+  entry->mime_type=ConstantString("image/x-portable-greymap");
   entry->module=ConstantString("PNM");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("PNM");
@@ -1382,12 +1442,14 @@ ModuleExport unsigned long RegisterPNMImage(void)
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
   entry->magick=(IsImageFormatHandler *) IsPNM;
   entry->description=ConstantString("Portable anymap");
+  entry->mime_type=ConstantString("image/x-portable-pixmap");
   entry->module=ConstantString("PNM");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("PPM");
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
   entry->description=ConstantString("Portable pixmap format (color)");
+  entry->mime_type=ConstantString("image/x-portable-pixmap");
   entry->module=ConstantString("PNM");
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
@@ -1432,12 +1494,12 @@ ModuleExport void UnregisterPNMImage(void)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  Procedure WritePNMImage() writes an image to a file in the PNM rasterfile
-%  format.
+%  WritePNMImage() writes an image to a file in the PNM rasterfile format.
 %
 %  The format of the WritePNMImage method is:
 %
-%      MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
+%      MagickBooleanType WritePNMImage(const ImageInfo *image_info,
+%        Image *image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
@@ -1445,8 +1507,11 @@ ModuleExport void UnregisterPNMImage(void)
 %
 %    o image:  The image.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
-static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
+static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
+  ExceptionInfo *exception)
 {
   char
     buffer[MaxTextExtent],
@@ -1456,18 +1521,15 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
   const char
     *value;
 
-  IndexPacket
-    index;
-
-  long
-    y;
-
   MagickBooleanType
     status;
 
   MagickOffsetType
     scene;
 
+  Quantum
+    index;
+
   QuantumAny
     pixel;
 
@@ -1477,20 +1539,18 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
   QuantumType
     quantum_type;
 
-  register long
-    i;
-
   register unsigned char
     *pixels,
     *q;
 
-  ssize_t
-    count;
-
   size_t
     extent,
     packet_size;
 
+  ssize_t
+    count,
+    y;
+
   /*
     Open output image file.
   */
@@ -1500,18 +1560,24 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+  assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
   scene=0;
   do
   {
+    QuantumAny
+      max_value;
+
     /*
       Write PNM file header.
     */
     packet_size=3;
     quantum_type=RGBQuantum;
     (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
+    max_value=GetQuantumRange(image->depth);
     switch (magick[1])
     {
       case 'A':
@@ -1532,7 +1598,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       case 'f':
       {
         format='F';
-        if (IsGrayImage(image,&image->exception) != MagickFalse)
+        if (IsImageGray(image,exception) != MagickFalse)
           format='f';
         break;
       }
@@ -1548,12 +1614,12 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       case 'n':
       {
         if ((image_info->type != TrueColorType) &&
-            (IsGrayImage(image,&image->exception) != MagickFalse))
+            (IsImageGray(image,exception) != MagickFalse))
           {
             format='5';
             if (image_info->compression == NoCompression)
               format='2';
-            if (IsMonochromeImage(image,&image->exception) != MagickFalse)
+            if (IsImageMonochrome(image,exception) != MagickFalse)
               {
                 format='4';
                 if (image_info->compression == NoCompression)
@@ -1570,9 +1636,9 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         break;
       }
     }
-    (void) FormatMagickString(buffer,MaxTextExtent,"P%c\n",format);
+    (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
     (void) WriteBlobString(image,buffer);
-    value=GetImageProperty(image,"comment");
+    value=GetImageProperty(image,"comment",exception);
     if (value != (const char *) NULL)
       {
         register const char
@@ -1585,19 +1651,15 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         for (p=value; *p != '\0'; p++)
         {
           (void) WriteBlobByte(image,(unsigned char) *p);
-          if ((*p == '\r') && (*(p+1) != '\0'))
-            (void) WriteBlobByte(image,'#');
-          if ((*p == '\n') && (*(p+1) != '\0'))
+          if ((*p == '\n') || (*p == '\r'))
             (void) WriteBlobByte(image,'#');
         }
         (void) WriteBlobByte(image,'\n');
       }
     if (format != '7')
       {
-        if (image->colorspace != RGBColorspace)
-          (void) TransformImageColorspace(image,RGBColorspace);
-        (void) FormatMagickString(buffer,MaxTextExtent,"%lu %lu\n",
-          image->columns,image->rows);
+        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
+          (double) image->columns,(double) image->rows);
         (void) WriteBlobString(image,buffer);
       }
     else
@@ -1608,10 +1670,11 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           PAM header.
         */
-        (void) FormatMagickString(buffer,MaxTextExtent,
-          "WIDTH %lu\nHEIGHT %lu\n",image->columns,image->rows);
+        (void) FormatLocaleString(buffer,MaxTextExtent,
+          "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
+          image->rows);
         (void) WriteBlobString(image,buffer);
-        quantum_type=GetQuantumType(image,&image->exception);
+        quantum_type=GetQuantumType(image,exception);
         switch (quantum_type)
         {
           case CMYKQuantum:
@@ -1631,25 +1694,25 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
           default:
           {
             quantum_type=RGBQuantum;
-            if (image->matte != MagickFalse)
+            if (image->alpha_trait == BlendPixelTrait)
               quantum_type=RGBAQuantum;
             packet_size=3;
             (void) CopyMagickString(type,"RGB",MaxTextExtent);
             break;
           }
         }
-        if (image->matte != MagickFalse)
+        if (image->alpha_trait == BlendPixelTrait)
           {
             packet_size++;
             (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
           }
-        if (image->depth > 16)
-          image->depth=16;
-        (void) FormatMagickString(buffer,MaxTextExtent,
-          "DEPTH %lu\nMAXVAL %lu\n",(unsigned long) packet_size,(unsigned long)
-          GetQuantumRange(image->depth));
+        if (image->depth > 32)
+          image->depth=32;
+        (void) FormatLocaleString(buffer,MaxTextExtent,
+          "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
+          ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
-        (void) FormatMagickString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
+        (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
           type);
         (void) WriteBlobString(image,buffer);
       }
@@ -1666,26 +1729,22 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PBM image.
         */
+        (void) SetImageType(image,BilevelType,exception);
         q=pixels;
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const IndexPacket
-            *restrict indexes;
-
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
-          indexes=GetVirtualIndexQueue(image);
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
-            pixel=PixelIntensityToQuantum(p);
-            *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
+            *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
               '0' : '1');
             *q++=' ';
             if ((q-pixels+2) >= 80)
@@ -1693,13 +1752,13 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                 *q++='\n';
                 (void) WriteBlob(image,q-pixels,pixels);
                 q=pixels;
-                i=0;
               }
-            p++;
+            p+=GetPixelChannels(image);
           }
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1722,28 +1781,35 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         if (image->depth <= 8)
           (void) WriteBlobString(image,"255\n");
         else
-          (void) WriteBlobString(image,"65535\n");
+          if (image->depth <= 16)
+            (void) WriteBlobString(image,"65535\n");
+          else
+            (void) WriteBlobString(image,"4294967295\n");
         q=pixels;
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
-            index=PixelIntensityToQuantum(p);
+            index=ClampToQuantum(GetPixelLuma(image,p));
             if (image->depth <= 8)
-              count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
+              count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
                 ScaleQuantumToChar(index));
             else
-              count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,"%u ",
-                ScaleQuantumToShort(index));
+              if (image->depth <= 16)
+                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
+                  ScaleQuantumToShort(index));
+              else
+                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
+                  ScaleQuantumToLong(index));
             extent=(size_t) count;
             (void) strncpy((char *) q,buffer,extent);
             q+=extent;
@@ -1753,11 +1819,12 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                 (void) WriteBlob(image,q-pixels,pixels);
                 q=pixels;
               }
-            p++;
+            p+=GetPixelChannels(image);
           }
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1777,32 +1844,44 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PNM image.
         */
+        (void) TransformImageColorspace(image,sRGBColorspace,exception);
         if (image->depth <= 8)
           (void) WriteBlobString(image,"255\n");
         else
-          (void) WriteBlobString(image,"65535\n");
+          if (image->depth <= 16)
+            (void) WriteBlobString(image,"65535\n");
+          else
+            (void) WriteBlobString(image,"4294967295\n");
         q=pixels;
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
             if (image->depth <= 8)
-              count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
-                "%u %u %u ",ScaleQuantumToChar(GetRedSample(p)),
-                ScaleQuantumToChar(GetGreenSample(p)),ScaleQuantumToChar(GetBlueSample(p)));
+              count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+                "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
+                ScaleQuantumToChar(GetPixelGreen(image,p)),
+                ScaleQuantumToChar(GetPixelBlue(image,p)));
             else
-              count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
-                "%u %u %u ",ScaleQuantumToShort(GetRedSample(p)),
-                ScaleQuantumToShort(GetGreenSample(p)),ScaleQuantumToShort(GetBlueSample(p)));
+              if (image->depth <= 16)
+                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+                  "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
+                  ScaleQuantumToShort(GetPixelGreen(image,p)),
+                  ScaleQuantumToShort(GetPixelBlue(image,p)));
+              else
+                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+                  "%u %u %u ",ScaleQuantumToLong(GetPixelRed(image,p)),
+                  ScaleQuantumToLong(GetPixelGreen(image,p)),
+                  ScaleQuantumToLong(GetPixelBlue(image,p)));
             extent=(size_t) count;
             (void) strncpy((char *) q,buffer,extent);
             q+=extent;
@@ -1812,11 +1891,12 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                 (void) WriteBlob(image,q-pixels,pixels);
                 q=pixels;
               }
-            p++;
+            p+=GetPixelChannels(image);
           }
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1833,28 +1913,30 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PBM image.
         */
+        (void) SetImageType(image,BilevelType,exception);
         image->depth=1;
         quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
         if (quantum_info == (QuantumInfo *) NULL)
           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
         quantum_info->min_is_white=MagickTrue;
         pixels=GetQuantumPixels(quantum_info);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
-          extent=ExportQuantumPixels(image,(const CacheView *) NULL,
-            quantum_info,GrayQuantum,pixels,&image->exception);
+          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+            GrayQuantum,pixels,exception);
           count=WriteBlob(image,extent,pixels);
           if (count != (ssize_t) extent)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1864,16 +1946,13 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       }
       case '5':
       {
-        QuantumAny
-          range;
-
         /*
           Convert image to a PGM image.
         */
-        if (image->depth > 8)
-          image->depth=16;
-        (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
-          GetQuantumRange(image->depth));
+        if (image->depth > 32)
+          image->depth=32;
+        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
+          ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
         quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
         if (quantum_info == (QuantumInfo *) NULL)
@@ -1881,62 +1960,98 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         quantum_info->min_is_white=MagickTrue;
         pixels=GetQuantumPixels(quantum_info);
         extent=GetQuantumExtent(image,quantum_info,GrayQuantum);
-        range=GetQuantumRange(image->depth);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
           q=pixels;
-          if ((image->depth == 8) || (image->depth == 16))
-            extent=ExportQuantumPixels(image,(const CacheView *) NULL,
-              quantum_info,GrayQuantum,pixels,&image->exception);
-          else
+          switch (image->depth)
+          {
+            case 8:
+            case 16:
+            case 32:
+            {
+              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+                GrayQuantum,pixels,exception);
+              break;
+            }
+            default:
             {
               if (image->depth <= 8)
-                for (x=0; x < (long) image->columns; x++)
                 {
-                  if (IsGrayPixel(p) == MagickFalse)
-                    pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
-                  else
-                    {
-                      if (image->depth == 8)
-                        pixel=ScaleQuantumToChar(GetRedSample(p));
-                      else
-                        pixel=ScaleQuantumToAny(p->red,range);
-                    }
-                  q=PopCharPixel((unsigned char) pixel,q);
-                  p++;
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    if (IsPixelGray(image,p) == MagickFalse)
+                      pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
+                        image,p)),max_value);
+                    else
+                      {
+                        if (image->depth == 8)
+                          pixel=ScaleQuantumToChar(GetPixelRed(image,p));
+                        else
+                          pixel=ScaleQuantumToAny(GetPixelRed(image,p),
+                          max_value);
+                      }
+                    q=PopCharPixel((unsigned char) pixel,q);
+                    p+=GetPixelChannels(image);
+                  }
+                  extent=(size_t) (q-pixels);
+                  break;
                 }
-              else
-                for (x=0; x < (long) image->columns; x++)
+              if (image->depth <= 16)
                 {
-                  if (IsGrayPixel(p) == MagickFalse)
-                    pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
-                  else
-                    {
-                      if (image->depth == 16)
-                        pixel=ScaleQuantumToShort(GetRedSample(p));
-                      else
-                        pixel=ScaleQuantumToAny(p->red,range);
-                    }
-                  q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  p++;
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    if (IsPixelGray(image,p) == MagickFalse)
+                      pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
+                        p)),max_value);
+                    else
+                      {
+                        if (image->depth == 16)
+                          pixel=ScaleQuantumToShort(GetPixelRed(image,p));
+                        else
+                          pixel=ScaleQuantumToAny(GetPixelRed(image,p),
+                            max_value);
+                      }
+                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                    p+=GetPixelChannels(image);
+                  }
+                  extent=(size_t) (q-pixels);
+                  break;
                 }
+              for (x=0; x < (ssize_t) image->columns; x++)
+              {
+                if (IsPixelGray(image,p) == MagickFalse)
+                  pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,p)),
+                    max_value);
+                else
+                  {
+                    if (image->depth == 16)
+                      pixel=ScaleQuantumToLong(GetPixelRed(image,p));
+                    else
+                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                  }
+                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                p+=GetPixelChannels(image);
+              }
               extent=(size_t) (q-pixels);
+              break;
             }
+          }
           count=WriteBlob(image,extent,pixels);
           if (count != (ssize_t) extent)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1946,82 +2061,96 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       }
       case '6':
       {
-        QuantumAny
-          range;
-
         /*
           Convert image to a PNM image.
         */
-        if (image->depth > 8)
-          image->depth=16;
-        (void) FormatMagickString(buffer,MaxTextExtent,"%lu\n",(unsigned long)
-          GetQuantumRange(image->depth));
+        (void) TransformImageColorspace(image,sRGBColorspace,exception);
+        if (image->depth > 32)
+          image->depth=32;
+        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
+          ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
         quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
         if (quantum_info == (QuantumInfo *) NULL)
           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
         pixels=GetQuantumPixels(quantum_info);
         extent=GetQuantumExtent(image,quantum_info,quantum_type);
-        range=GetQuantumRange(image->depth);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
           q=pixels;
-          if ((image->depth == 8) || (image->depth == 16))
-            extent=ExportQuantumPixels(image,(const CacheView *) NULL,
-              quantum_info,quantum_type,pixels,&image->exception);
-          else
+          switch (image->depth)
+          {
+            case 8:
+            case 16:
+            case 32:
+            {
+              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+                quantum_type,pixels,exception);
+              break;
+            }
+            default:
             {
               if (image->depth <= 8)
-                for (x=0; x < (long) image->columns; x++)
                 {
-                  pixel=ScaleQuantumToAny(p->red,range);
-                  q=PopCharPixel((unsigned char) pixel,q);
-                  pixel=ScaleQuantumToAny(p->green,range);
-                  q=PopCharPixel((unsigned char) pixel,q);
-                  pixel=ScaleQuantumToAny(p->blue,range);
-                  q=PopCharPixel((unsigned char) pixel,q);
-                  if (image->matte != MagickFalse)
-                    {
-                      pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                        GetOpacitySample(p)),range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                    }
-                  p++;
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                    q=PopCharPixel((unsigned char) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
+                    q=PopCharPixel((unsigned char) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
+                    q=PopCharPixel((unsigned char) pixel,q);
+                    p+=GetPixelChannels(image);
+                  }
+                  extent=(size_t) (q-pixels);
+                  break;
                 }
-              else
-                for (x=0; x < (long) image->columns; x++)
+              if (image->depth <= 16)
                 {
-                  pixel=ScaleQuantumToAny(p->red,range);
-                  q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  pixel=ScaleQuantumToAny(p->green,range);
-                  q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  pixel=ScaleQuantumToAny(p->blue,range);
-                  q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  if (image->matte != MagickFalse)
-                    {
-                      pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                        GetOpacitySample(p)),range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                    }
-                  p++;
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
+                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
+                    q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                    p+=GetPixelChannels(image);
+                  }
+                  extent=(size_t) (q-pixels);
+                  break;
                 }
+              for (x=0; x < (ssize_t) image->columns; x++)
+              {
+                pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
+                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
+                q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                p+=GetPixelChannels(image);
+              }
               extent=(size_t) (q-pixels);
+              break;
             }
+          }
           count=WriteBlob(image,extent,pixels);
           if (count != (ssize_t) extent)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -2031,37 +2160,36 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       }
       case '7':
       {
-        QuantumAny
-          range;
-
         /*
           Convert image to a PAM.
         */
-        if (image->depth > 16)
-          image->depth=16;
+        if (image->depth > 32)
+          image->depth=32;
         quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
         pixels=GetQuantumPixels(quantum_info);
-        range=GetQuantumRange(image->depth);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const IndexPacket
-            *restrict indexes;
-
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          register long
+          register ssize_t
             x;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
-          indexes=GetVirtualIndexQueue(image);
           q=pixels;
-          if ((image->depth == 8) || (image->depth == 16))
-            extent=ExportQuantumPixels(image,(const CacheView *) NULL,
-              quantum_info,quantum_type,pixels,&image->exception);
-          else
+          switch (image->depth)
+          {
+            case 8:
+            case 16:
+            case 32:
+            {
+              extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+                quantum_type,pixels,exception);
+              break;
+            }
+            default:
             {
               switch (quantum_type)
               {
@@ -2069,123 +2197,203 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                 case GrayAlphaQuantum:
                 {
                   if (image->depth <= 8)
-                    for (x=0; x < (long) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      if (image->matte != MagickFalse)
-                        {
-                          pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
-                            range);
-                          q=PopCharPixel((unsigned char) pixel,q);
-                        }
-                      p++;
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
+                          image,p)),max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            pixel=(unsigned char) ScaleQuantumToAny(
+                              GetPixelAlpha(image,p),max_value);
+                            q=PopCharPixel((unsigned char) pixel,q);
+                          }
+                        p+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  else
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 16)
                     {
-                      pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      if (image->matte != MagickFalse)
-                        {
-                          pixel=(unsigned char) ScaleQuantumToAny(p->opacity,
-                            range);
-                          q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                        }
-                      p++;
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
+                          image,p)),max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            pixel=(unsigned char) ScaleQuantumToAny(
+                              GetPixelAlpha(image,p),max_value);
+                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                          }
+                        p+=GetPixelChannels(image);
+                      }
+                      break;
                     }
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
+                      p)),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    if (image->alpha_trait == BlendPixelTrait)
+                      {
+                        pixel=(unsigned char) ScaleQuantumToAny(
+                          GetPixelAlpha(image,p),max_value);
+                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                      }
+                    p+=GetPixelChannels(image);
+                  }
                   break;
                 }
                 case CMYKQuantum:
                 case CMYKAQuantum:
                 {
                   if (image->depth <= 8)
-                    for (x=0; x < (long) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(p->red,range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(p->green,range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(p->blue,range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(indexes[x],range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      if (image->matte != MagickFalse)
-                        {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacitySample(p)),range);
-                          q=PopCharPixel((unsigned char) pixel,q);
-                        }
-                      p++;
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
+                          max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
+                          max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
+                          max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
+                              max_value);
+                            q=PopCharPixel((unsigned char) pixel,q);
+                          }
+                        p+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  else
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 16)
                     {
-                      pixel=ScaleQuantumToAny(p->red,range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(p->green,range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(p->blue,range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(indexes[x],range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      if (image->matte != MagickFalse)
-                        {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacitySample(p)),range);
-                          q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                        }
-                      p++;
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
+                          max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
+                          max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
+                          max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
+                              max_value);
+                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                          }
+                        p+=GetPixelChannels(image);
+                      }
+                      break;
                     }
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelBlack(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    if (image->alpha_trait == BlendPixelTrait)
+                      {
+                        pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
+                          max_value);
+                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                      }
+                    p+=GetPixelChannels(image);
+                  }
                   break;
                 }
                 default:
                 {
                   if (image->depth <= 8)
-                    for (x=0; x < (long) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(p->red,range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(p->green,range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(p->blue,range);
-                      q=PopCharPixel((unsigned char) pixel,q);
-                      if (image->matte != MagickFalse)
-                        {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacitySample(p)),range);
-                          q=PopCharPixel((unsigned char) pixel,q);
-                        }
-                      p++;
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
+                          max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
+                          max_value);
+                        q=PopCharPixel((unsigned char) pixel,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
+                              max_value);
+                            q=PopCharPixel((unsigned char) pixel,q);
+                          }
+                        p+=GetPixelChannels(image);
+                      }
+                      break;
                     }
-                  else
-                    for (x=0; x < (long) image->columns; x++)
+                  if (image->depth <= 16)
                     {
-                      pixel=ScaleQuantumToAny(p->red,range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(p->green,range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(p->blue,range);
-                      q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      if (image->matte != MagickFalse)
-                        {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacitySample(p)),range);
-                          q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                        }
-                      p++;
+                      for (x=0; x < (ssize_t) image->columns; x++)
+                      {
+                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelGreen(image,p),
+                          max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
+                          max_value);
+                        q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                        if (image->alpha_trait == BlendPixelTrait)
+                          {
+                            pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
+                              max_value);
+                            q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
+                          }
+                        p+=GetPixelChannels(image);
+                      }
+                      break;
                     }
+                  for (x=0; x < (ssize_t) image->columns; x++)
+                  {
+                    pixel=ScaleQuantumToAny(GetPixelRed(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelGreen(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    pixel=ScaleQuantumToAny(GetPixelBlue(image,p),max_value);
+                    q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                    if (image->alpha_trait == BlendPixelTrait)
+                      {
+                        pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
+                          max_value);
+                        q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
+                      }
+                    p+=GetPixelChannels(image);
+                  }
                   break;
                 }
               }
               extent=(size_t) (q-pixels);
+              break;
             }
+          }
           count=WriteBlob(image,extent,pixels);
           if (count != (ssize_t) extent)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -2196,8 +2404,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       case 'F':
       case 'f':
       {
-        (void) WriteBlobString(image,image->endian != LSBEndian ? "1.0\n" :
-          "-1.0\n");
+        (void) WriteBlobString(image,image->endian == LSBEndian ? "-1.0\n" :
+          "1.0\n");
         image->depth=32;
         quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
         quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
@@ -2207,20 +2415,21 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         if (status == MagickFalse)
           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
         pixels=GetQuantumPixels(quantum_info);
-        for (y=(long) image->rows-1; y >= 0; y--)
+        for (y=(ssize_t) image->rows-1; y >= 0; y--)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
-          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-          if (p == (const PixelPacket *) NULL)
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
             break;
-          extent=ExportQuantumPixels(image,(const CacheView *) NULL,
-            quantum_info,quantum_type,pixels,&image->exception);
+          extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+            quantum_type,pixels,exception);
           (void) WriteBlob(image,extent,pixels);
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }