]> granicus.if.org Git - imagemagick/blobdiff - coders/pnm.c
Fixed memory leak reported in #456.
[imagemagick] / coders / pnm.c
index 6a2a6f1da34245387e2d2bb476e32c74f1504e2e..8f417cb18557a07ea9fa0bb93a8dab02273a0fde 100644 (file)
 %                                 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  %
 %  obtain a copy of the License at                                            %
 %                                                                             %
-%    http://www.imagemagick.org/script/license.php                            %
+%    https://www.imagemagick.org/script/license.php                           %
 %                                                                             %
 %  Unless required by applicable law or agreed to in writing, software        %
 %  distributed under the License is distributed on an "AS IS" BASIS,          %
@@ -155,13 +155,13 @@ static int PNMComment(Image *image,ExceptionInfo *exception)
   */
   comment=AcquireString(GetImageProperty(image,"comment",exception));
   p=comment+strlen(comment);
-  extent=strlen(comment)+MaxTextExtent;
+  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;
@@ -258,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)
@@ -296,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);
@@ -312,8 +312,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
     else
       {
         char
-          keyword[MaxTextExtent],
-          value[MaxTextExtent];
+          keyword[MagickPathExtent],
+          value[MagickPathExtent];
 
         int
           c;
@@ -341,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));
@@ -353,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);
           }
@@ -429,6 +429,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       Convert PNM pixels to runextent-encoded MIFF packets.
     */
     row=0;
+    y=0;
     switch (format)
     {
       case '1':
@@ -443,7 +444,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)
@@ -452,6 +453,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)
@@ -463,6 +466,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status == MagickFalse)
                 break;
             }
+          if (EOFBlob(image) != MagickFalse)
+            break;
         }
         image->type=BilevelType;
         break;
@@ -482,7 +487,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)
@@ -491,6 +496,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);
           }
@@ -503,6 +510,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status == MagickFalse)
                 break;
             }
+          if (EOFBlob(image) != MagickFalse)
+            break;
         }
         image->type=GrayscaleType;
         break;
@@ -518,7 +527,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)
@@ -529,6 +538,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);
@@ -545,6 +556,8 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (status == MagickFalse)
                 break;
             }
+          if (EOFBlob(image) != MagickFalse)
+            break;
         }
         break;
       }
@@ -564,14 +577,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 void
+          const unsigned char
             *pixels;
 
           MagickBooleanType
             sync;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
@@ -580,12 +593,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           size_t
             length;
 
-          if (status == MagickFalse)
-            continue;
-          pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
-            &count);
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
-            status=MagickFalse;
+            break;
           if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
               (image->previous == (Image *) NULL))
             {
@@ -595,26 +606,21 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
                 row,image->rows);
               if (proceed == MagickFalse)
-                status=MagickFalse;
+                break;
             }
           offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
-            {
-              status=MagickFalse;
-              continue;
-            }
+            break;
           length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           if (length != extent)
-            status=MagickFalse;
+            break;
           sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
-            status=MagickFalse;
+            break;
         }
         quantum_info=DestroyQuantumInfo(quantum_info);
-        if (status == MagickFalse)
-          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
         SetQuantumImageType(image,quantum_type);
         break;
       }
@@ -637,31 +643,29 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          const void
+          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;
 
-          if (status == MagickFalse)
-            continue;
-          pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
-            &count);
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
-            status=MagickFalse;
+            break;
           if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
               (image->previous == (Image *) NULL))
             {
@@ -671,15 +675,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
                 row,image->rows);
               if (proceed == MagickFalse)
-                status=MagickFalse;
+                break;
             }
           offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
-            {
-              status=MagickFalse;
-              continue;
-            }
+            break;
           p=pixels;
           switch (image->depth)
           {
@@ -733,11 +734,9 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
-            status=MagickFalse;
+            break;
         }
         quantum_info=DestroyQuantumInfo(quantum_info);
-        if (status == MagickFalse)
-          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
         SetQuantumImageType(image,quantum_type);
         break;
       }
@@ -754,31 +753,29 @@ 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 void
+          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;
 
-          if (status == MagickFalse)
-            continue;
-          pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
-            &count);
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
-            status=MagickFalse;
+            break;
           if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
               (image->previous == (Image *) NULL))
             {
@@ -788,15 +785,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
                 row,image->rows);
               if (proceed == MagickFalse)
-                status=MagickFalse;
+                break;
             }
           offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
-            {
-              status=MagickFalse;
-              continue;
-            }
+            break;
           p=pixels;
           switch (image->depth)
           {
@@ -905,11 +899,9 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
-            status=MagickFalse;
+            break;
         }
         quantum_info=DestroyQuantumInfo(quantum_info);
-        if (status == MagickFalse)
-          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
         break;
       }
       case '7':
@@ -954,31 +946,29 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
         for (y=0; y < (ssize_t) image->rows; y++)
         {
-          const void
+          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;
 
-          if (status == MagickFalse)
-            continue;
-          pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
-            &count);
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if (count != (ssize_t) extent)
-            status=MagickFalse;
+            break;
           if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
               (image->previous == (Image *) NULL))
             {
@@ -988,15 +978,12 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
                 row,image->rows);
               if (proceed == MagickFalse)
-                status=MagickFalse;
+                break;
             }
           offset=row++;
           q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
           if (q == (Quantum *) NULL)
-            {
-              status=MagickFalse;
-              continue;
-            }
+            break;
           p=pixels;
           switch (image->depth)
           {
@@ -1244,11 +1231,9 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           }
           sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
-            status=MagickFalse;
+            break;
         }
         quantum_info=DestroyQuantumInfo(quantum_info);
-        if (status == MagickFalse)
-          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
         SetQuantumImageType(image,quantum_type);
         break;
       }
@@ -1276,14 +1261,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 void
+          const unsigned char
             *pixels;
 
           MagickBooleanType
             sync;
 
           register Quantum
-            *restrict q;
+            *magick_restrict q;
 
           ssize_t
             count,
@@ -1292,12 +1277,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           size_t
             length;
 
-          if (status == MagickFalse)
-            continue;
-          pixels=ReadBlobStream(image,extent,GetQuantumPixels(quantum_info),
-            &count);
+          pixels=(unsigned char *) ReadBlobStream(image,extent,
+            GetQuantumPixels(quantum_info),&count);
           if ((size_t) count != extent)
-            status=MagickFalse;
+            break;
           if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
               (image->previous == (Image *) NULL))
             {
@@ -1307,33 +1290,30 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
               proceed=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
                 row,image->rows);
               if (proceed == MagickFalse)
-                status=MagickFalse;
+                break;
             }
           offset=row++;
           q=QueueAuthenticPixels(image,0,(ssize_t) (image->rows-offset-1),
             image->columns,1,exception);
           if (q == (Quantum *) NULL)
-            {
-              status=MagickFalse;
-              continue;
-            }
+            break;
           length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
             quantum_type,pixels,exception);
           if (length != extent)
-            status=MagickFalse;
+            break;
           sync=SyncAuthenticPixels(image,exception);
           if (sync == MagickFalse)
-            status=MagickFalse;
+            break;
         }
         quantum_info=DestroyQuantumInfo(quantum_info);
-        if (status == MagickFalse)
-          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
         SetQuantumImageType(image,quantum_type);
         break;
       }
       default:
         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
     }
+    if (y < (ssize_t) image->rows)
+      ThrowReaderException(CorruptImageError,"UnableToReadImageData");
     if (EOFBlob(image) != MagickFalse)
       {
         (void) ThrowMagickException(exception,GetMagickModule(),
@@ -1409,48 +1389,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->flags|=CoderEndianSupportFlag;
-  entry->description=ConstantString("Portable float format");
-  entry->module=ConstantString("PFM");
   (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);
 }
@@ -1514,9 +1483,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;
@@ -1555,13 +1524,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);
@@ -1576,7 +1545,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])
     {
@@ -1636,7 +1605,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)
@@ -1658,19 +1627,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);
@@ -1681,14 +1650,16 @@ 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);
+            if (IsImageMonochrome(image) != MagickFalse)
+              (void) CopyMagickString(type,"BLACKANDWHITE",MagickPathExtent);
             break;
           }
           default:
@@ -1697,23 +1668,23 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
             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 != 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",
-          type);
+        (void) FormatLocaleString(buffer,MagickPathExtent,
+          "TUPLTYPE %s\nENDHDR\n",type);
         (void) WriteBlobString(image,buffer);
       }
     /*
@@ -1734,7 +1705,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;
@@ -1747,7 +1718,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);
@@ -1755,6 +1726,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,
@@ -1789,7 +1763,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;
@@ -1801,19 +1775,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);
@@ -1821,6 +1795,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,
@@ -1856,7 +1833,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;
@@ -1867,25 +1844,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);
@@ -1893,6 +1870,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,
@@ -1924,7 +1904,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)
@@ -1952,7 +1932,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);
@@ -1965,7 +1945,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;
@@ -1999,7 +1979,7 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
                           pixel=ScaleQuantumToChar(GetPixelRed(image,p));
                         else
                           pixel=ScaleQuantumToAny(GetPixelRed(image,p),
-                          max_value);
+                            max_value);
                       }
                     q=PopCharPixel((unsigned char) pixel,q);
                     p+=GetPixelChannels(image);
@@ -2069,7 +2049,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);
@@ -2081,7 +2061,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;
@@ -2175,7 +2155,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;
@@ -2416,7 +2396,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");
@@ -2424,7 +2403,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)