]> granicus.if.org Git - imagemagick/blobdiff - coders/pnm.c
...
[imagemagick] / coders / pnm.c
index 2df68020a665e8e0d558dda6f6cfa958ef086df2..21c0e895d34fdada2756129ac577c07193294167 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2017 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  %
@@ -135,7 +135,8 @@ static MagickBooleanType IsPNM(const unsigned char *magick,const size_t extent)
 %    o exception: return any errors or warnings in this structure.
 %
 */
-static void PNMComment(Image *image,ExceptionInfo *exception)
+
+static int PNMComment(Image *image,ExceptionInfo *exception)
 {
   int
     c;
@@ -153,14 +154,14 @@ static void PNMComment(Image *image,ExceptionInfo *exception)
     Read comment.
   */
   comment=AcquireString(GetImageProperty(image,"comment",exception));
-  extent=MaxTextExtent;
   p=comment+strlen(comment);
+  extent=strlen(comment)+MagickPathExtent;
   for (c='#'; (c != EOF) && (c != (int) '\n'); p++)
   {
     if ((size_t) (p-comment+1) >= extent)
       {
         extent<<=1;
-        comment=(char *) ResizeQuantumMemory(comment,extent+MaxTextExtent,
+        comment=(char *) ResizeQuantumMemory(comment,extent+MagickPathExtent,
           sizeof(*comment));
         if (comment == (char *) NULL)
           break;
@@ -174,9 +175,10 @@ static void PNMComment(Image *image,ExceptionInfo *exception)
       }
   }
   if (comment == (char *) NULL)
-    return;
+    return(c);
   (void) SetImageProperty(image,"comment",comment,exception);
   comment=DestroyString(comment);
+  return(c);
 }
 
 static unsigned int PNMInteger(Image *image,const unsigned int base,
@@ -197,7 +199,7 @@ static unsigned int PNMInteger(Image *image,const unsigned int base,
     if (c == EOF)
       return(0);
     if (c == (int) '#')
-      PNMComment(image,exception);
+      c=PNMComment(image,exception);
   } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
   if (base == 2)
     return((unsigned int) (c-(int) '0'));
@@ -205,18 +207,17 @@ static unsigned int PNMInteger(Image *image,const unsigned int base,
     Evaluate number.
   */
   value=0;
-  do
-  {
+  while (isdigit(c) != 0) {
     if (value > (unsigned int) (INT_MAX/10))
       break;
     value*=10;
-    if (value > (INT_MAX-c))
+    if (value > (unsigned int) (INT_MAX-(c-(int) '0')))
       break;
     value+=c-(int) '0';
     c=ReadBlobByte(image);
     if (c == EOF)
       return(0);
-  } while (isdigit(c) != 0);
+  }
   return(value);
 }
 
@@ -257,12 +258,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     Open image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   if (image_info->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
@@ -295,7 +296,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         if ((format == 'f') || (format == 'F'))
           {
             char
-              scale[MaxTextExtent];
+              scale[MagickPathExtent];
 
             (void) ReadBlobString(image,scale);
             quantum_scale=StringToDouble(scale,(char **) NULL);
@@ -311,8 +312,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     else
       {
         char
-          keyword[MaxTextExtent],
-          value[MaxTextExtent];
+          keyword[MagickPathExtent],
+          value[MagickPathExtent];
 
         int
           c;
@@ -332,7 +333,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               /*
                 Comment.
               */
-              PNMComment(image,exception);
+              c=PNMComment(image,exception);
               c=ReadBlobByte(image);
               while (isspace((int) ((unsigned char) c)) != 0)
                 c=ReadBlobByte(image);
@@ -340,7 +341,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           p=keyword;
           do
           {
-            if ((size_t) (p-keyword) < (MaxTextExtent-1))
+            if ((size_t) (p-keyword) < (MagickPathExtent-1))
               *p++=c;
             c=ReadBlobByte(image);
           } while (isalnum(c));
@@ -352,7 +353,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           p=value;
           while (isalnum(c) || (c == '_'))
           {
-            if ((size_t) (p-value) < (MaxTextExtent-1))
+            if ((size_t) (p-value) < (MagickPathExtent-1))
               *p++=c;
             c=ReadBlobByte(image);
           }
@@ -442,7 +443,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             x;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
           if (q == (Quantum *) NULL)
@@ -451,6 +452,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           {
             SetPixelGray(image,PNMInteger(image,2,exception) == 0 ?
               QuantumRange : 0,q);
+            if (EOFBlob(image) != MagickFalse)
+              break;
             q+=GetPixelChannels(image);
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
@@ -462,6 +465,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status == MagickFalse)
                 break;
             }
+          if (EOFBlob(image) != MagickFalse)
+            break;
         }
         image->type=BilevelType;
         break;
@@ -481,7 +486,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             x;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
           if (q == (Quantum *) NULL)
@@ -490,6 +495,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           {
             intensity=ScaleAnyToQuantum(PNMInteger(image,10,exception),
               max_value);
+            if (EOFBlob(image) != MagickFalse)
+              break;
             SetPixelGray(image,intensity,q);
             q+=GetPixelChannels(image);
           }
@@ -502,6 +509,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status == MagickFalse)
                 break;
             }
+          if (EOFBlob(image) != MagickFalse)
+            break;
         }
         image->type=GrayscaleType;
         break;
@@ -517,7 +526,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             x;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
           if (q == (Quantum *) NULL)
@@ -528,6 +537,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               pixel;
 
             pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
+            if (EOFBlob(image) != MagickFalse)
+              break;
             SetPixelRed(image,pixel,q);
             pixel=ScaleAnyToQuantum(PNMInteger(image,10,exception),max_value);
             SetPixelGreen(image,pixel,q);
@@ -544,6 +555,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status == MagickFalse)
                 break;
             }
+          if (EOFBlob(image) != MagickFalse)
+            break;
         }
         break;
       }
@@ -563,11 +576,14 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         extent=GetQuantumExtent(image,quantum_info,quantum_type);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
+          const unsigned char
+            *pixels;
+
           MagickBooleanType
             sync;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
@@ -576,29 +592,24 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           size_t
             length;
 
-          unsigned char
-            *pixels;
-
           if (status == MagickFalse)
             continue;
-          pixels=GetQuantumPixels(quantum_info);
-          {
-            count=ReadBlob(image,extent,pixels);
-            if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
-                (image->previous == (Image *) NULL))
-              {
-                MagickBooleanType
-                  proceed;
-
-                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
-                  row,image->rows);
-                if (proceed == MagickFalse)
-                  status=MagickFalse;
-              }
-            offset=row++;
-          }
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
             status=MagickFalse;
+          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
+              (image->previous == (Image *) NULL))
+            {
+              MagickBooleanType
+                proceed;
+
+              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                row,image->rows);
+              if (proceed == MagickFalse)
+                status=MagickFalse;
+            }
+          offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
             {
@@ -638,45 +649,43 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         for (y=0; y < (ssize_t) image->rows; y++)
         {
+          const unsigned char
+            *pixels;
+
           MagickBooleanType
             sync;
 
           register const unsigned char
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
             offset;
 
-          unsigned char
-            *pixels;
-
           if (status == MagickFalse)
             continue;
-          pixels=GetQuantumPixels(quantum_info);
-          {
-            count=ReadBlob(image,extent,pixels);
-            if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
-                (image->previous == (Image *) NULL))
-              {
-                MagickBooleanType
-                  proceed;
-
-                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
-                  row,image->rows);
-                if (proceed == MagickFalse)
-                  status=MagickFalse;
-              }
-            offset=row++;
-          }
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
             status=MagickFalse;
+          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
+              (image->previous == (Image *) NULL))
+            {
+              MagickBooleanType
+                proceed;
+
+              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                row,image->rows);
+              if (proceed == MagickFalse)
+                status=MagickFalse;
+            }
+          offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
             {
@@ -757,45 +766,43 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         (void) SetQuantumEndian(image,quantum_info,MSBEndian);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
+          const unsigned char
+            *pixels;
+
           MagickBooleanType
             sync;
 
           register const unsigned char
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
             offset;
 
-          unsigned char
-            *pixels;
-
           if (status == MagickFalse)
             continue;
-          pixels=GetQuantumPixels(quantum_info);
-          {
-            count=ReadBlob(image,extent,pixels);
-            if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
-                (image->previous == (Image *) NULL))
-              {
-                MagickBooleanType
-                  proceed;
-
-                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
-                  row,image->rows);
-                if (proceed == MagickFalse)
-                  status=MagickFalse;
-              }
-            offset=row++;
-          }
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
             status=MagickFalse;
+          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
+              (image->previous == (Image *) NULL))
+            {
+              MagickBooleanType
+                proceed;
+
+              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                row,image->rows);
+              if (proceed == MagickFalse)
+                status=MagickFalse;
+            }
+          offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
             {
@@ -945,7 +952,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             break;
           }
         }
-        if (image->alpha_trait == BlendPixelTrait)
+        if (image->alpha_trait != UndefinedPixelTrait)
           channels++;
         if (image->depth <= 8)
           extent=channels*image->columns;
@@ -959,45 +966,43 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         for (y=0; y < (ssize_t) image->rows; y++)
         {
+          const unsigned char
+            *pixels;
+
           MagickBooleanType
             sync;
 
           register const unsigned char
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
             offset;
 
-          unsigned char
-            *pixels;
-
           if (status == MagickFalse)
             continue;
-          pixels=GetQuantumPixels(quantum_info);
-          {
-            count=ReadBlob(image,extent,pixels);
-            if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
-                (image->previous == (Image *) NULL))
-              {
-                MagickBooleanType
-                  proceed;
-
-                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
-                  row,image->rows);
-                if (proceed == MagickFalse)
-                  status=MagickFalse;
-              }
-            offset=row++;
-          }
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
             status=MagickFalse;
+          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
+              (image->previous == (Image *) NULL))
+            {
+              MagickBooleanType
+                proceed;
+
+              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                row,image->rows);
+              if (proceed == MagickFalse)
+                status=MagickFalse;
+            }
+          offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
             {
@@ -1033,9 +1038,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                       for (x=0; x < (ssize_t) image->columns; x++)
                       {
                         p=PushCharPixel(p,&pixel);
-                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         SetPixelAlpha(image,OpaqueAlpha,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             p=PushCharPixel(p,&pixel);
                             if (image->depth != 1)
@@ -1060,7 +1066,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),
                           q);
                         SetPixelAlpha(image,OpaqueAlpha,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             p=PushShortPixel(MSBEndian,p,&pixel);
                             SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
@@ -1075,10 +1081,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     p=PushLongPixel(MSBEndian,p,&pixel);
                     SetPixelGray(image,ScaleAnyToQuantum(pixel,max_value),q);
                     SetPixelAlpha(image,OpaqueAlpha,q);
-                    if (image->alpha_trait == BlendPixelTrait)
+                    if (image->alpha_trait != UndefinedPixelTrait)
                       {
                         p=PushLongPixel(MSBEndian,p,&pixel);
-                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                       }
                     q+=GetPixelChannels(image);
                   }
@@ -1100,13 +1107,16 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         p=PushCharPixel(p,&pixel);
                         SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
                         p=PushCharPixel(p,&pixel);
-                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         p=PushCharPixel(p,&pixel);
-                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         p=PushCharPixel(p,&pixel);
-                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         SetPixelAlpha(image,OpaqueAlpha,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             p=PushCharPixel(p,&pixel);
                             SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
@@ -1126,13 +1136,16 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         p=PushShortPixel(MSBEndian,p,&pixel);
                         SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
                         p=PushShortPixel(MSBEndian,p,&pixel);
-                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         p=PushShortPixel(MSBEndian,p,&pixel);
-                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         p=PushShortPixel(MSBEndian,p,&pixel);
-                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         SetPixelAlpha(image,OpaqueAlpha,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             p=PushShortPixel(MSBEndian,p,&pixel);
                             SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
@@ -1152,10 +1165,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     p=PushLongPixel(MSBEndian,p,&pixel);
                     SetPixelBlack(image,ScaleAnyToQuantum(pixel,max_value),q);
                     SetPixelAlpha(image,OpaqueAlpha,q);
-                    if (image->alpha_trait == BlendPixelTrait)
+                    if (image->alpha_trait != UndefinedPixelTrait)
                       {
                         p=PushLongPixel(MSBEndian,p,&pixel);
-                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                       }
                     q+=GetPixelChannels(image);
                   }
@@ -1176,14 +1190,17 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         p=PushCharPixel(p,&pixel);
                         SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
                         p=PushCharPixel(p,&pixel);
-                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         p=PushCharPixel(p,&pixel);
-                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         SetPixelAlpha(image,OpaqueAlpha,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             p=PushCharPixel(p,&pixel);
-                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                            SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
+                              max_value),q);
                           }
                         q+=GetPixelChannels(image);
                       }
@@ -1199,11 +1216,13 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                         p=PushShortPixel(MSBEndian,p,&pixel);
                         SetPixelRed(image,ScaleAnyToQuantum(pixel,max_value),q);
                         p=PushShortPixel(MSBEndian,p,&pixel);
-                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelGreen(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         p=PushShortPixel(MSBEndian,p,&pixel);
-                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                         SetPixelAlpha(image,OpaqueAlpha,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             p=PushShortPixel(MSBEndian,p,&pixel);
                             SetPixelAlpha(image,ScaleAnyToQuantum(pixel,
@@ -1222,10 +1241,11 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
                     p=PushLongPixel(MSBEndian,p,&pixel);
                     SetPixelBlue(image,ScaleAnyToQuantum(pixel,max_value),q);
                     SetPixelAlpha(image,OpaqueAlpha,q);
-                    if (image->alpha_trait == BlendPixelTrait)
+                    if (image->alpha_trait != UndefinedPixelTrait)
                       {
                         p=PushLongPixel(MSBEndian,p,&pixel);
-                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),q);
+                        SetPixelAlpha(image,ScaleAnyToQuantum(pixel,max_value),
+                          q);
                       }
                     q+=GetPixelChannels(image);
                   }
@@ -1268,11 +1288,14 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
         extent=GetQuantumExtent(image,quantum_info,quantum_type);
         for (y=0; y < (ssize_t) image->rows; y++)
         {
+          const unsigned char
+            *pixels;
+
           MagickBooleanType
             sync;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
@@ -1281,29 +1304,24 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           size_t
             length;
 
-          unsigned char
-            *pixels;
-
           if (status == MagickFalse)
             continue;
-          pixels=GetQuantumPixels(quantum_info);
-          {
-            count=ReadBlob(image,extent,pixels);
-            if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
-                (image->previous == (Image *) NULL))
-              {
-                MagickBooleanType
-                  proceed;
-
-                proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
-                  row,image->rows);
-                if (proceed == MagickFalse)
-                  status=MagickFalse;
-              }
-            offset=row++;
-          }
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if ((size_t) count != extent)
             status=MagickFalse;
+          if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
+              (image->previous == (Image *) NULL))
+            {
+              MagickBooleanType
+                proceed;
+
+              proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
+                row,image->rows);
+              if (proceed == MagickFalse)
+                status=MagickFalse;
+            }
+          offset=row++;
           q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
             image->columns,1,exception);
           if (q == (Quantum *) NULL)
@@ -1347,9 +1365,9 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           Skip to end of line.
         */
         count=ReadBlob(image,1,(unsigned char *) &format);
-        if (count == 0)
+        if (count != 1)
           break;
-        if ((count != 0) && (format == 'P'))
+        if (format == 'P')
           break;
       } while (format != '\n');
     count=ReadBlob(image,1,(unsigned char *) &format);
@@ -1403,48 +1421,37 @@ ModuleExport size_t RegisterPNMImage(void)
   MagickInfo
     *entry;
 
-  entry=SetMagickInfo("PAM");
+  entry=AcquireMagickInfo("PNM","PAM","Common 2-dimensional bitmap format");
   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=AcquireMagickInfo("PNM","PBM",
+    "Portable bitmap format (black and white)");
   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=AcquireMagickInfo("PNM","PFM","Portable float format");
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   entry->encoder=(EncodeImageHandler *) WritePNMImage;
-  entry->endian_support=MagickTrue;
-  entry->description=ConstantString("Portable float format");
-  entry->module=ConstantString("PFM");
+  entry->flags|=CoderEndianSupportFlag;
   (void) RegisterMagickInfo(entry);
-  entry=SetMagickInfo("PGM");
+  entry=AcquireMagickInfo("PNM","PGM","Portable graymap format (gray scale)");
   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");
+  entry=AcquireMagickInfo("PNM","PNM","Portable anymap");
   entry->decoder=(DecodeImageHandler *) ReadPNMImage;
   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=AcquireMagickInfo("PNM","PPM","Portable pixmap format (color)");
   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);
 }
@@ -1508,9 +1515,9 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
   ExceptionInfo *exception)
 {
   char
-    buffer[MaxTextExtent],
+    buffer[MagickPathExtent],
     format,
-    magick[MaxTextExtent];
+    magick[MagickPathExtent];
 
   const char
     *value;
@@ -1549,13 +1556,13 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
     Open output image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
@@ -1570,7 +1577,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
     */
     packet_size=3;
     quantum_type=RGBQuantum;
-    (void) CopyMagickString(magick,image_info->magick,MaxTextExtent);
+    (void) CopyMagickString(magick,image_info->magick,MagickPathExtent);
     max_value=GetQuantumRange(image->depth);
     switch (magick[1])
     {
@@ -1592,7 +1599,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
       case 'f':
       {
         format='F';
-        if (IsImageGray(image,exception) != MagickFalse)
+        if (SetImageGray(image,exception) != MagickFalse)
           format='f';
         break;
       }
@@ -1608,12 +1615,12 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
       case 'n':
       {
         if ((image_info->type != TrueColorType) &&
-            (IsImageGray(image,exception) != MagickFalse))
+            (SetImageGray(image,exception) != MagickFalse))
           {
             format='5';
             if (image_info->compression == NoCompression)
               format='2';
-            if (IsImageMonochrome(image,exception) != MagickFalse)
+            if (SetImageMonochrome(image,exception) != MagickFalse)
               {
                 format='4';
                 if (image_info->compression == NoCompression)
@@ -1630,7 +1637,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         break;
       }
     }
-    (void) FormatLocaleString(buffer,MaxTextExtent,"P%c\n",format);
+    (void) FormatLocaleString(buffer,MagickPathExtent,"P%c\n",format);
     (void) WriteBlobString(image,buffer);
     value=GetImageProperty(image,"comment",exception);
     if (value != (const char *) NULL)
@@ -1652,19 +1659,19 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
       }
     if (format != '7')
       {
-        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n",
+        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n",
           (double) image->columns,(double) image->rows);
         (void) WriteBlobString(image,buffer);
       }
     else
       {
         char
-          type[MaxTextExtent];
+          type[MagickPathExtent];
 
         /*
           PAM header.
         */
-        (void) FormatLocaleString(buffer,MaxTextExtent,
+        (void) FormatLocaleString(buffer,MagickPathExtent,
           "WIDTH %.20g\nHEIGHT %.20g\n",(double) image->columns,(double)
           image->rows);
         (void) WriteBlobString(image,buffer);
@@ -1675,38 +1682,38 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
           case CMYKAQuantum:
           {
             packet_size=4;
-            (void) CopyMagickString(type,"CMYK",MaxTextExtent);
+            (void) CopyMagickString(type,"CMYK",MagickPathExtent);
             break;
           }
           case GrayQuantum:
           case GrayAlphaQuantum:
           {
             packet_size=1;
-            (void) CopyMagickString(type,"GRAYSCALE",MaxTextExtent);
+            (void) CopyMagickString(type,"GRAYSCALE",MagickPathExtent);
             break;
           }
           default:
           {
             quantum_type=RGBQuantum;
-            if (image->alpha_trait == BlendPixelTrait)
+            if (image->alpha_trait != UndefinedPixelTrait)
               quantum_type=RGBAQuantum;
             packet_size=3;
-            (void) CopyMagickString(type,"RGB",MaxTextExtent);
+            (void) CopyMagickString(type,"RGB",MagickPathExtent);
             break;
           }
         }
-        if (image->alpha_trait == BlendPixelTrait)
+        if (image->alpha_trait != UndefinedPixelTrait)
           {
             packet_size++;
-            (void) ConcatenateMagickString(type,"_ALPHA",MaxTextExtent);
+            (void) ConcatenateMagickString(type,"_ALPHA",MagickPathExtent);
           }
         if (image->depth > 32)
           image->depth=32;
-        (void) FormatLocaleString(buffer,MaxTextExtent,
+        (void) FormatLocaleString(buffer,MagickPathExtent,
           "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
           ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
-        (void) FormatLocaleString(buffer,MaxTextExtent,"TUPLTYPE %s\nENDHDR\n",
+        (void) FormatLocaleString(buffer,MagickPathExtent,"TUPLTYPE %s\nENDHDR\n",
           type);
         (void) WriteBlobString(image,buffer);
       }
@@ -1728,7 +1735,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
@@ -1741,7 +1748,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
             *q++=(unsigned char) (GetPixelLuma(image,p) >= (QuantumRange/2.0) ?
               '0' : '1');
             *q++=' ';
-            if ((q-pixels+2) >= 80)
+            if ((q-pixels+1) >= (ssize_t) sizeof(pixels))
               {
                 *q++='\n';
                 (void) WriteBlob(image,q-pixels,pixels);
@@ -1749,6 +1756,9 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
               }
             p+=GetPixelChannels(image);
           }
+          *q++='\n';
+          (void) WriteBlob(image,q-pixels,pixels);
+          q=pixels;
           if (image->previous == (Image *) NULL)
             {
               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
@@ -1783,7 +1793,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
@@ -1795,19 +1805,19 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
           {
             index=ClampToQuantum(GetPixelLuma(image,p));
             if (image->depth <= 8)
-              count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
+              count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,"%u ",
                 ScaleQuantumToChar(index));
             else
               if (image->depth <= 16)
-                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
-                  ScaleQuantumToShort(index));
+                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
+                  "%u ",ScaleQuantumToShort(index));
               else
-                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,"%u ",
-                  ScaleQuantumToLong(index));
+                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
+                  "%u ",ScaleQuantumToLong(index));
             extent=(size_t) count;
             (void) strncpy((char *) q,buffer,extent);
             q+=extent;
-            if ((q-pixels+extent) >= 80)
+            if ((q-pixels+extent+1) >= sizeof(pixels))
               {
                 *q++='\n';
                 (void) WriteBlob(image,q-pixels,pixels);
@@ -1815,6 +1825,9 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
               }
             p+=GetPixelChannels(image);
           }
+          *q++='\n';
+          (void) WriteBlob(image,q-pixels,pixels);
+          q=pixels;
           if (image->previous == (Image *) NULL)
             {
               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
@@ -1850,7 +1863,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
@@ -1861,25 +1874,25 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
           for (x=0; x < (ssize_t) image->columns; x++)
           {
             if (image->depth <= 8)
-              count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+              count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
                 "%u %u %u ",ScaleQuantumToChar(GetPixelRed(image,p)),
                 ScaleQuantumToChar(GetPixelGreen(image,p)),
                 ScaleQuantumToChar(GetPixelBlue(image,p)));
             else
               if (image->depth <= 16)
-                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
                   "%u %u %u ",ScaleQuantumToShort(GetPixelRed(image,p)),
                   ScaleQuantumToShort(GetPixelGreen(image,p)),
                   ScaleQuantumToShort(GetPixelBlue(image,p)));
               else
-                count=(ssize_t) FormatLocaleString(buffer,MaxTextExtent,
+                count=(ssize_t) FormatLocaleString(buffer,MagickPathExtent,
                   "%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;
-            if ((q-pixels+extent) >= 80)
+            if ((q-pixels+extent+1) >= sizeof(pixels))
               {
                 *q++='\n';
                 (void) WriteBlob(image,q-pixels,pixels);
@@ -1887,6 +1900,9 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
               }
             p+=GetPixelChannels(image);
           }
+          *q++='\n';
+          (void) WriteBlob(image,q-pixels,pixels);
+          q=pixels;
           if (image->previous == (Image *) NULL)
             {
               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
@@ -1918,7 +1934,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           p=GetVirtualPixels(image,0,y,image->columns,1,exception);
           if (p == (const Quantum *) NULL)
@@ -1946,7 +1962,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         */
         if (image->depth > 32)
           image->depth=32;
-        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
+        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
           ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
         quantum_info=AcquireQuantumInfo(image_info,image);
@@ -1959,7 +1975,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
@@ -2063,7 +2079,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         (void) TransformImageColorspace(image,sRGBColorspace,exception);
         if (image->depth > 32)
           image->depth=32;
-        (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double)
+        (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
           ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
         quantum_info=AcquireQuantumInfo(image_info,image);
@@ -2075,7 +2091,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
@@ -2169,7 +2185,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           register ssize_t
             x;
@@ -2202,7 +2218,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                         pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
                           image,p)),max_value);
                         q=PopCharPixel((unsigned char) pixel,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             pixel=(unsigned char) ScaleQuantumToAny(
                               GetPixelAlpha(image,p),max_value);
@@ -2219,7 +2235,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                         pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(
                           image,p)),max_value);
                         q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             pixel=(unsigned char) ScaleQuantumToAny(
                               GetPixelAlpha(image,p),max_value);
@@ -2234,7 +2250,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                     pixel=ScaleQuantumToAny(ClampToQuantum(GetPixelLuma(image,
                       p)),max_value);
                     q=PopLongPixel(MSBEndian,(unsigned int) pixel,q);
-                    if (image->alpha_trait == BlendPixelTrait)
+                    if (image->alpha_trait != UndefinedPixelTrait)
                       {
                         pixel=(unsigned char) ScaleQuantumToAny(
                           GetPixelAlpha(image,p),max_value);
@@ -2262,7 +2278,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                         pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
                           max_value);
                         q=PopCharPixel((unsigned char) pixel,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
                               max_value);
@@ -2287,7 +2303,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                         pixel=ScaleQuantumToAny(GetPixelBlack(image,p),
                           max_value);
                         q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
                               max_value);
@@ -2307,7 +2323,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                     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)
+                    if (image->alpha_trait != UndefinedPixelTrait)
                       {
                         pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
                           max_value);
@@ -2331,7 +2347,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                         pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
                           max_value);
                         q=PopCharPixel((unsigned char) pixel,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
                               max_value);
@@ -2353,7 +2369,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                         pixel=ScaleQuantumToAny(GetPixelBlue(image,p),
                           max_value);
                         q=PopShortPixel(MSBEndian,(unsigned short) pixel,q);
-                        if (image->alpha_trait == BlendPixelTrait)
+                        if (image->alpha_trait != UndefinedPixelTrait)
                           {
                             pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
                               max_value);
@@ -2371,7 +2387,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                     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)
+                    if (image->alpha_trait != UndefinedPixelTrait)
                       {
                         pixel=ScaleQuantumToAny(GetPixelAlpha(image,p),
                           max_value);
@@ -2410,7 +2426,6 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         quantum_info=AcquireQuantumInfo(image_info,image);
         if (quantum_info == (QuantumInfo *) NULL)
           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
-        (void) SetQuantumEndian(image,quantum_info,MSBEndian);
         status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
         if (status == MagickFalse)
           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
@@ -2418,7 +2433,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
         for (y=(ssize_t) image->rows-1; y >= 0; y--)
         {
           register const Quantum
-            *restrict p;
+            *magick_restrict p;
 
           p=GetVirtualPixels(image,0,y,image->columns,1,exception);
           if (p == (const Quantum *) NULL)