]> granicus.if.org Git - imagemagick/blobdiff - coders/pnm.c
(no commit message)
[imagemagick] / coders / pnm.c
index b7981a2a735ae9ae8496de0907604761f4e9a438..edb28874ab1fdd7bac5595ab76213697b7a36f9a 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2013 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
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -135,18 +137,19 @@ static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
 */
 
 static inline ssize_t ConstrainPixel(Image *image,const ssize_t offset,
-  const size_t extent)
+  const size_t extent,ExceptionInfo *exception)
 {
   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 size_t PNMInteger(Image *image,const unsigned int base)
+static size_t PNMInteger(Image *image,const unsigned int base,
+  ExceptionInfo *exception)
 {
   char
     *comment;
@@ -192,8 +195,11 @@ static size_t PNMInteger(Image *image,const unsigned int base)
               p=comment+strlen(comment);
             }
           c=ReadBlobByte(image);
-          *p=(char) c;
-          *(p+1)='\0';
+          if (c != (int) '\n')
+            {
+              *p=(char) c;
+              *(p+1)='\0';
+            }
         }
         if (comment == (char *) NULL)
           return(0);
@@ -202,7 +208,7 @@ static size_t PNMInteger(Image *image,const unsigned int base)
   } while (isdigit(c) == MagickFalse);
   if (comment != (char *) NULL)
     {
-      (void) SetImageProperty(image,"comment",comment);
+      (void) SetImageProperty(image,"comment",comment,exception);
       comment=DestroyString(comment);
     }
   if (base == 2)
@@ -269,7 +275,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)
     {
@@ -296,22 +302,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
@@ -366,34 +372,42 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (LocaleCompare(keyword,"TUPLTYPE") == 0)
             {
               if (LocaleCompare(value,"BLACKANDWHITE") == 0)
-                quantum_type=GrayQuantum;
+                {
+                  SetImageColorspace(image,GRAYColorspace,exception);
+                  quantum_type=GrayQuantum;
+                }
               if (LocaleCompare(value,"BLACKANDWHITE_ALPHA") == 0)
                 {
+                  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;
+                  SetImageColorspace(image,GRAYColorspace,exception);
+                }
               if (LocaleCompare(value,"GRAYSCALE_ALPHA") == 0)
                 {
+                  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)
                 {
+                  SetImageColorspace(image,CMYKColorspace,exception);
                   quantum_type=CMYKQuantum;
-                  image->colorspace=CMYKColorspace;
                 }
               if (LocaleCompare(value,"CMYK_ALPHA") == 0)
                 {
+                  SetImageColorspace(image,CMYKColorspace,exception);
+                  image->alpha_trait=BlendPixelTrait;
                   quantum_type=CMYKAQuantum;
-                  image->colorspace=CMYKColorspace;
-                  image->matte=MagickTrue;
                 }
             }
           if (LocaleCompare(keyword,"width") == 0)
@@ -421,23 +435,23 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PBM image to pixel packets.
         */
+        SetImageColorspace(image,GRAYColorspace,exception);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           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 < (ssize_t) image->columns; x++)
           {
-            SetRedPixelComponent(q,PNMInteger(image,2) == 0 ? QuantumRange : 0);
-            SetGreenPixelComponent(q,GetRedPixelComponent(q));
-            SetBluePixelComponent(q,GetRedPixelComponent(q));
-            q++;
+            SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
+              QuantumRange : 0,q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
@@ -460,6 +474,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PGM image to pixel packets.
         */
+        SetImageColorspace(image,GRAYColorspace,exception);
         scale=(Quantum *) NULL;
         if (max_value != (1U*QuantumRange))
           {
@@ -478,22 +493,20 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           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 < (ssize_t) image->columns; x++)
           {
-            intensity=PNMInteger(image,10);
-            SetRedPixelComponent(q,intensity);
+            intensity=PNMInteger(image,10,exception);
+            SetPixelGray(image,intensity,q);
             if (scale != (Quantum *) NULL)
-              SetRedPixelComponent(q,scale[ConstrainPixel(image,(ssize_t)
-                intensity,max_value)]);
-            SetGreenPixelComponent(q,GetRedPixelComponent(q));
-            SetBluePixelComponent(q,GetRedPixelComponent(q));
-            q++;
+              SetPixelGray(image,scale[ConstrainPixel(image,(ssize_t) intensity,
+                max_value,exception)],q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
@@ -512,7 +525,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       }
       case '3':
       {
-        MagickPixelPacket
+        PixelInfo
           pixel;
 
         /*
@@ -536,30 +549,30 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           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 < (ssize_t) image->columns; x++)
           {
-            pixel.red=(MagickRealType) PNMInteger(image,10);
-            pixel.green=(MagickRealType) PNMInteger(image,10);
-            pixel.blue=(MagickRealType) PNMInteger(image,10);
+            pixel.red=(double) PNMInteger(image,10,exception);
+            pixel.green=(double) PNMInteger(image,10,exception);
+            pixel.blue=(double) PNMInteger(image,10,exception);
             if (scale != (Quantum *) NULL)
               {
-                pixel.red=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
-                  pixel.red,max_value)];
-                pixel.green=(MagickRealType) scale[ConstrainPixel(image,
-                  (ssize_t) pixel.green,max_value)];
-                pixel.blue=(MagickRealType) scale[ConstrainPixel(image,(ssize_t)
-                  pixel.blue,max_value)];
+                pixel.red=(double) scale[ConstrainPixel(image,(ssize_t)
+                  pixel.red,max_value,exception)];
+                pixel.green=(double) scale[ConstrainPixel(image,
+                  (ssize_t) pixel.green,max_value,exception)];
+                pixel.blue=(double) scale[ConstrainPixel(image,(ssize_t)
+                  pixel.blue,max_value,exception)];
               }
-            SetRedPixelComponent(q,pixel.red);
-            SetGreenPixelComponent(q,pixel.green);
-            SetBluePixelComponent(q,pixel.blue);
-            q++;
+            SetPixelRed(image,ClampToQuantum(pixel.red),q);
+            SetPixelGreen(image,ClampToQuantum(pixel.green),q);
+            SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
+            q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
             break;
@@ -580,6 +593,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PBM raw image to pixel packets.
         */
+        SetImageColorspace(image,GRAYColorspace,exception);
         quantum_type=GrayQuantum;
         if (image->storage_class == PseudoClass)
           quantum_type=IndexQuantum;
@@ -593,7 +607,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           MagickBooleanType
             sync;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
@@ -627,7 +641,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (count != (ssize_t) extent)
             status=MagickFalse;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
@@ -654,6 +668,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PGM raw image to pixel packets.
         */
+        SetImageColorspace(image,GRAYColorspace,exception);
         range=GetQuantumRange(image->depth);
         quantum_type=GrayQuantum;
         extent=(image->depth <= 8 ? 1 : 2)*image->columns;
@@ -671,7 +686,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
@@ -702,7 +717,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (count != (ssize_t) extent)
             status=MagickFalse;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
@@ -720,10 +735,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
                   p=PushCharPixel(p,&pixel);
-                  SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                  SetGreenPixelComponent(q,GetRedPixelComponent(q));
-                  SetBluePixelComponent(q,GetRedPixelComponent(q));
-                  q++;
+                  SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
+                  q+=GetPixelChannels(image);
                 }
               }
             else
@@ -734,10 +747,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
                   p=PushShortPixel(MSBEndian,p,&pixel);
-                  SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                  SetGreenPixelComponent(q,GetRedPixelComponent(q));
-                  SetBluePixelComponent(q,GetRedPixelComponent(q));
-                  q++;
+                  SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
+                  q+=GetPixelChannels(image);
                 }
               }
           sync=SyncAuthenticPixels(image,exception);
@@ -752,22 +763,19 @@ 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");
+        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           MagickBooleanType
@@ -779,7 +787,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
@@ -810,7 +818,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (count != (ssize_t) extent)
             status=MagickFalse;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
@@ -819,11 +827,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (image->depth == 8)
             for (x=0; x < (ssize_t) image->columns; x++)
             {
-              SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
-              SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
-              SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
-              q->opacity=OpaqueOpacity;
-              q++;
+              SetPixelRed(image,ScaleCharToQuantum(*p++),q);
+              SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
+              SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
+              SetPixelAlpha(image,OpaqueAlpha,q);
+              q+=GetPixelChannels(image);
             }
           else
             if (image->depth == 16)
@@ -834,13 +842,13 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
                   p=PushShortPixel(MSBEndian,p,&pixel);
-                  SetRedPixelComponent(q,ScaleShortToQuantum(pixel));
+                  SetPixelRed(image,ScaleShortToQuantum(pixel),q);
                   p=PushShortPixel(MSBEndian,p,&pixel);
-                  SetGreenPixelComponent(q,ScaleShortToQuantum(pixel));
+                  SetPixelGreen(image,ScaleShortToQuantum(pixel),q);
                   p=PushShortPixel(MSBEndian,p,&pixel);
-                  SetBluePixelComponent(q,ScaleShortToQuantum(pixel));
-                  SetOpacityPixelComponent(q,OpaqueOpacity);
-                  q++;
+                  SetPixelBlue(image,ScaleShortToQuantum(pixel),q);
+                  SetPixelAlpha(image,OpaqueAlpha,q);
+                  q+=GetPixelChannels(image);
                 }
               }
             else
@@ -852,13 +860,13 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   for (x=0; x < (ssize_t) image->columns; x++)
                   {
                     p=PushCharPixel(p,&pixel);
-                    SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                    SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
                     p=PushCharPixel(p,&pixel);
-                    SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                    SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
                     p=PushCharPixel(p,&pixel);
-                    SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                    SetOpacityPixelComponent(q,OpaqueOpacity);
-                    q++;
+                    SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    q+=GetPixelChannels(image);
                   }
                 }
               else
@@ -869,31 +877,15 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   for (x=0; x < (ssize_t) image->columns; x++)
                   {
                     p=PushShortPixel(MSBEndian,p,&pixel);
-                    SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                    SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
                     p=PushShortPixel(MSBEndian,p,&pixel);
-                    SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                    SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
                     p=PushShortPixel(MSBEndian,p,&pixel);
-                    SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                    SetOpacityPixelComponent(q,OpaqueOpacity);
-                    q++;
+                    SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
+                    SetPixelAlpha(image,OpaqueAlpha,q);
+                    q+=GetPixelChannels(image);
                   }
                 }
-          if ((type == BilevelType) || (type == GrayscaleType))
-            {
-              q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
-              for (x=0; x < (ssize_t) 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++;
-              }
-            }
           sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
             status=MagickFalse;
@@ -901,15 +893,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         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;
 
@@ -940,7 +927,7 @@ 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;
         quantum_info=AcquireQuantumInfo(image_info,image);
@@ -957,7 +944,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           register ssize_t
             x;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
@@ -988,12 +975,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           if (count != (ssize_t) extent)
             status=MagickFalse;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
             }
-          indexes=GetAuthenticIndexQueue(image);
           p=pixels;
           if ((image->depth == 8) || (image->depth == 16))
             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
@@ -1012,17 +998,14 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
                       p=PushCharPixel(p,&pixel);
-                      SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                      SetGreenPixelComponent(q,GetRedPixelComponent(q));
-                      SetBluePixelComponent(q,GetRedPixelComponent(q));
-                      SetOpacityPixelComponent(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
+                      SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
+                      SetPixelAlpha(image,OpaqueAlpha,q);
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           p=PushCharPixel(p,&pixel);
-                          SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
-                            range));
+                          SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
                         }
-                      q++;
+                      q+=GetPixelChannels(image);
                     }
                   }
                 else
@@ -1033,17 +1016,14 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                      SetGreenPixelComponent(q,GetRedPixelComponent(q));
-                      SetBluePixelComponent(q,GetRedPixelComponent(q));
-                      SetOpacityPixelComponent(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
+                      SetPixelGray(image,ScaleAnyToQuantum(pixel,range),q);
+                      SetPixelAlpha(image,OpaqueAlpha,q);
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           p=PushShortPixel(MSBEndian,p,&pixel);
-                          SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
-                            range));
+                          SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
                         }
-                      q++;
+                      q+=GetPixelChannels(image);
                     }
                   }
                 break;
@@ -1059,22 +1039,20 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
                       p=PushCharPixel(p,&pixel);
-                      SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushCharPixel(p,&pixel);
-                      SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushCharPixel(p,&pixel);
-                      SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushCharPixel(p,&pixel);
-                      SetIndexPixelComponent(indexes+x,ScaleAnyToQuantum(pixel,
-                        range));
-                      SetOpacityPixelComponent(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
+                      SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
+                      SetPixelAlpha(image,OpaqueAlpha,q);
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           p=PushCharPixel(p,&pixel);
-                          SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
-                            range));
+                          SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
                         }
-                      q++;
+                      q+=GetPixelChannels(image);
                     }
                   }
                 else
@@ -1085,22 +1063,20 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetIndexPixelComponent(indexes+x,ScaleAnyToQuantum(pixel,
-                        range));
-                      SetOpacityPixelComponent(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
+                      SetPixelBlack(image,ScaleAnyToQuantum(pixel,range),q);
+                      SetPixelAlpha(image,OpaqueAlpha,q);
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           p=PushShortPixel(MSBEndian,p,&pixel);
-                          SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
-                            range));
+                          SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
                         }
-                      q++;
+                      q+=GetPixelChannels(image);
                     }
                   }
                 break;
@@ -1115,19 +1091,18 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
                       p=PushCharPixel(p,&pixel);
-                      SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushCharPixel(p,&pixel);
-                      SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushCharPixel(p,&pixel);
-                      SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                      SetOpacityPixelComponent(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
+                      SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
+                      SetPixelAlpha(image,OpaqueAlpha,q);
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           p=PushCharPixel(p,&pixel);
-                          SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
-                            range));
+                          SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
                         }
-                      q++;
+                      q+=GetPixelChannels(image);
                     }
                   }
                 else
@@ -1138,19 +1113,18 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetRedPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelRed(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetGreenPixelComponent(q,ScaleAnyToQuantum(pixel,range));
+                      SetPixelGreen(image,ScaleAnyToQuantum(pixel,range),q);
                       p=PushShortPixel(MSBEndian,p,&pixel);
-                      SetBluePixelComponent(q,ScaleAnyToQuantum(pixel,range));
-                      SetOpacityPixelComponent(q,OpaqueOpacity);
-                      if (image->matte != MagickFalse)
+                      SetPixelBlue(image,ScaleAnyToQuantum(pixel,range),q);
+                      SetPixelAlpha(image,OpaqueAlpha,q);
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           p=PushShortPixel(MSBEndian,p,&pixel);
-                          SetOpacityPixelComponent(q,ScaleAnyToQuantum(pixel,
-                            range));
+                          SetPixelAlpha(image,ScaleAnyToQuantum(pixel,range),q);
                         }
-                      q++;
+                      q+=GetPixelChannels(image);
                     }
                   }
                 break;
@@ -1172,6 +1146,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         /*
           Convert PFM raster image to pixel packets.
         */
+        if (format == 'f')
+          SetImageColorspace(image,GRAYColorspace,exception);
         quantum_type=format == 'f' ? GrayQuantum : RGBQuantum;
         image->endian=quantum_scale < 0.0 ? LSBEndian : MSBEndian;
         image->depth=32;
@@ -1184,15 +1160,14 @@ 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);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           MagickBooleanType
             sync;
 
-          register PixelPacket
+          register Quantum
             *restrict q;
 
           ssize_t
@@ -1227,7 +1202,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             status=MagickFalse;
           q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
             image->columns,1,exception);
-          if (q == (PixelPacket *) NULL)
+          if (q == (Quantum *) NULL)
             {
               status=MagickFalse;
               continue;
@@ -1250,8 +1225,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.
     */
@@ -1276,7 +1254,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);
@@ -1405,7 +1383,8 @@ ModuleExport void UnregisterPNMImage(void)
 %
 %  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.
 %
@@ -1413,8 +1392,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],
@@ -1424,15 +1406,15 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
   const char
     *value;
 
-  IndexPacket
-    index;
-
   MagickBooleanType
     status;
 
   MagickOffsetType
     scene;
 
+  Quantum
+    index;
+
   QuantumAny
     pixel;
 
@@ -1463,7 +1445,9 @@ 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;
@@ -1495,7 +1479,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;
       }
@@ -1511,12 +1495,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)
@@ -1533,9 +1517,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
@@ -1557,9 +1541,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
       }
     if (format != '7')
       {
-        if (image->colorspace != RGBColorspace)
-          (void) TransformImageColorspace(image,RGBColorspace);
-        (void) FormatMagickString(buffer,MaxTextExtent,"%.20g %.20g\n",
+        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
           (double) image->columns,(double) image->rows);
         (void) WriteBlobString(image,buffer);
       }
@@ -1571,11 +1553,11 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           PAM header.
         */
-        (void) FormatMagickString(buffer,MaxTextExtent,
+        (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:
@@ -1595,25 +1577,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,
+        (void) FormatLocaleString(buffer,MaxTextExtent,
           "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
-          GetQuantumRange(image->depth));
+          ((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);
       }
@@ -1630,21 +1612,23 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PBM image.
         */
+        if (IsImageGray(image,exception) == MagickFalse)
+          (void) TransformImageColorspace(image,GRAYColorspace,exception);
         q=pixels;
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
           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 < (ssize_t) image->columns; x++)
           {
-            pixel=PixelIntensityToQuantum(p);
+            pixel=GetPixelIntensity(image,p);
             *q++=(unsigned char) (pixel >= (Quantum) (QuantumRange/2) ?
               '0' : '1');
             *q++=' ';
@@ -1654,7 +1638,7 @@ 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)
             {
@@ -1679,6 +1663,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PGM image.
         */
+        if (IsImageGray(image,exception) == MagickFalse)
+          (void) TransformImageColorspace(image,GRAYColorspace,exception);
         if (image->depth <= 8)
           (void) WriteBlobString(image,"255\n");
         else
@@ -1686,23 +1672,23 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         q=pixels;
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
           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 < (ssize_t) image->columns; x++)
           {
-            index=PixelIntensityToQuantum(p);
+            index=GetPixelIntensity(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 ",
+              count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
                 ScaleQuantumToShort(index));
             extent=(size_t) count;
             (void) strncpy((char *) q,buffer,extent);
@@ -1713,7 +1699,7 @@ 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)
             {
@@ -1738,6 +1724,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PNM image.
         */
+        if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
+          (void) TransformImageColorspace(image,sRGBColorspace,exception);
         if (image->depth <= 8)
           (void) WriteBlobString(image,"255\n");
         else
@@ -1745,27 +1733,27 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         q=pixels;
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
           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 < (ssize_t) image->columns; x++)
           {
             if (image->depth <= 8)
-              count=(ssize_t) FormatMagickString(buffer,MaxTextExtent,
-                "%u %u %u ",ScaleQuantumToChar(GetRedPixelComponent(p)),
-                ScaleQuantumToChar(GetGreenPixelComponent(p)),
-                ScaleQuantumToChar(GetBluePixelComponent(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(GetRedPixelComponent(p)),
-                ScaleQuantumToShort(GetGreenPixelComponent(p)),
-                ScaleQuantumToShort(GetBluePixelComponent(p)));
+              count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+                "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
+                ScaleQuantumToShort(GetPixelGreen(image,p)),
+                ScaleQuantumToShort(GetPixelBlue(image,p)));
             extent=(size_t) count;
             (void) strncpy((char *) q,buffer,extent);
             q+=extent;
@@ -1775,7 +1763,7 @@ 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)
             {
@@ -1797,6 +1785,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PBM image.
         */
+        if (IsImageGray(image,exception) == MagickFalse)
+          (void) TransformImageColorspace(image,GRAYColorspace,exception);
         image->depth=1;
         quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
         if (quantum_info == (QuantumInfo *) NULL)
@@ -1805,14 +1795,14 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         pixels=GetQuantumPixels(quantum_info);
         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;
@@ -1835,10 +1825,12 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PGM image.
         */
+        if (IsImageGray(image,exception) == MagickFalse)
+          (void) TransformImageColorspace(image,GRAYColorspace,exception);
         if (image->depth > 8)
           image->depth=16;
-        (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
-          GetQuantumRange(image->depth));
+        (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)
@@ -1849,50 +1841,50 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         range=GetQuantumRange(image->depth);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
           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);
+            extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+              GrayQuantum,pixels,exception);
           else
             {
               if (image->depth <= 8)
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
-                  if (IsGrayPixel(p) == MagickFalse)
-                    pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
+                  if (IsPixelGray(image,p) == MagickFalse)
+                    pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
                   else
                     {
                       if (image->depth == 8)
-                        pixel=ScaleQuantumToChar(GetRedPixelComponent(p));
+                        pixel=ScaleQuantumToChar(GetPixelRed(image,p));
                       else
-                        pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                     }
                   q=PopCharPixel((unsigned char) pixel,q);
-                  p++;
+                  p+=GetPixelChannels(image);
                 }
               else
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
-                  if (IsGrayPixel(p) == MagickFalse)
-                    pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
+                  if (IsPixelGray(image,p) == MagickFalse)
+                    pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
                   else
                     {
                       if (image->depth == 16)
-                        pixel=ScaleQuantumToShort(GetRedPixelComponent(p));
+                        pixel=ScaleQuantumToShort(GetPixelRed(image,p));
                       else
-                        pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                        pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                     }
                   q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  p++;
+                  p+=GetPixelChannels(image);
                 }
               extent=(size_t) (q-pixels);
             }
@@ -1918,55 +1910,58 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         /*
           Convert image to a PNM image.
         */
+        if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
+          (void) TransformImageColorspace(image,sRGBColorspace,exception);
         if (image->depth > 8)
           image->depth=16;
-        (void) FormatMagickString(buffer,MaxTextExtent,"%.20g\n",(double)
-          GetQuantumRange(image->depth));
+        (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 < (ssize_t) image->rows; y++)
         {
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
           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);
+            extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+              quantum_type,pixels,exception);
           else
             {
               if (image->depth <= 8)
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
-                  pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                  pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                   q=PopCharPixel((unsigned char) pixel,q);
-                  pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
+                  pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
                   q=PopCharPixel((unsigned char) pixel,q);
-                  pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
+                  pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
                   q=PopCharPixel((unsigned char) pixel,q);
-                  p++;
+                  p+=GetPixelChannels(image);
                 }
               else
                 for (x=0; x < (ssize_t) image->columns; x++)
                 {
-                  pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                  pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                   q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
+                  pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
                   q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
+                  pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
                   q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                  p++;
+                  p+=GetPixelChannels(image);
                 }
               extent=(size_t) (q-pixels);
             }
@@ -1999,23 +1994,19 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         range=GetQuantumRange(image->depth);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register const IndexPacket
-            *restrict indexes;
-
-          register const PixelPacket
+          register const Quantum
             *restrict p;
 
           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);
+            extent=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
+              quantum_type,pixels,exception);
           else
             {
               switch (quantum_type)
@@ -2026,28 +2017,28 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                   if (image->depth <= 8)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      if (image->matte != MagickFalse)
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           pixel=(unsigned char) ScaleQuantumToAny(
-                            GetOpacityPixelComponent(p),range);
+                            GetPixelAlpha(image,p),range);
                           q=PopCharPixel((unsigned char) pixel,q);
                         }
-                      p++;
+                      p+=GetPixelChannels(image);
                     }
                   else
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(PixelIntensityToQuantum(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelIntensity(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      if (image->matte != MagickFalse)
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
                           pixel=(unsigned char) ScaleQuantumToAny(
-                            GetOpacityPixelComponent(p),range);
+                            GetPixelAlpha(image,p),range);
                           q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
                         }
-                      p++;
+                      p+=GetPixelChannels(image);
                     }
                   break;
                 }
@@ -2057,42 +2048,38 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                   if (image->depth <= 8)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(
-                        GetIndexPixelComponent(indexes+x),range);
+                      pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      if (image->matte != MagickFalse)
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacityPixelComponent(p)),range);
+                          pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
                           q=PopCharPixel((unsigned char) pixel,q);
                         }
-                      p++;
+                      p+=GetPixelChannels(image);
                     }
                   else
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(
-                        GetIndexPixelComponent(indexes+x),range);
+                      pixel=ScaleQuantumToAny(GetPixelBlack(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      if (image->matte != MagickFalse)
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacityPixelComponent(p)),range);
+                          pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
                           q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
                         }
-                      p++;
+                      p+=GetPixelChannels(image);
                     }
                   break;
                 }
@@ -2101,36 +2088,34 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
                   if (image->depth <= 8)
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
                       q=PopCharPixel((unsigned char) pixel,q);
-                      if (image->matte != MagickFalse)
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacityPixelComponent(p)),range);
+                          pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
                           q=PopCharPixel((unsigned char) pixel,q);
                         }
-                      p++;
+                      p+=GetPixelChannels(image);
                     }
                   else
                     for (x=0; x < (ssize_t) image->columns; x++)
                     {
-                      pixel=ScaleQuantumToAny(GetRedPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelRed(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(GetGreenPixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelGreen(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      pixel=ScaleQuantumToAny(GetBluePixelComponent(p),range);
+                      pixel=ScaleQuantumToAny(GetPixelBlue(image,p),range);
                       q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                      if (image->matte != MagickFalse)
+                      if (image->alpha_trait == BlendPixelTrait)
                         {
-                          pixel=ScaleQuantumToAny((Quantum) (QuantumRange-
-                            GetOpacityPixelComponent(p)),range);
+                          pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),range);
                           q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
                         }
-                      p++;
+                      p+=GetPixelChannels(image);
                     }
                   break;
                 }
@@ -2154,8 +2139,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);
@@ -2167,14 +2152,14 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image)
         pixels=GetQuantumPixels(quantum_info);
         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)
             {