]> granicus.if.org Git - imagemagick/blobdiff - coders/pnm.c
https://github.com/ImageMagick/ImageMagick/issues/409
[imagemagick] / coders / pnm.c
index 21c0e895d34fdada2756129ac577c07193294167..9a1221d796bb731b0b273860828b835f6e7ad308 100644 (file)
@@ -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':
@@ -592,12 +593,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           size_t
             length;
 
-          if (status == MagickFalse)
-            continue;
           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))
             {
@@ -607,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;
       }
@@ -668,12 +662,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             count,
             offset;
 
-          if (status == MagickFalse)
-            continue;
           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))
             {
@@ -683,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)
           {
@@ -745,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;
       }
@@ -785,12 +772,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             count,
             offset;
 
-          if (status == MagickFalse)
-            continue;
           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))
             {
@@ -800,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)
           {
@@ -917,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':
@@ -985,12 +965,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
             count,
             offset;
 
-          if (status == MagickFalse)
-            continue;
           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))
             {
@@ -1000,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)
           {
@@ -1256,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;
       }
@@ -1304,12 +1277,10 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
           size_t
             length;
 
-          if (status == MagickFalse)
-            continue;
           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))
             {
@@ -1319,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(),
@@ -1690,6 +1658,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
           {
             packet_size=1;
             (void) CopyMagickString(type,"GRAYSCALE",MagickPathExtent);
+            if (IsImageMonochrome(image) != MagickFalse)
+              (void) CopyMagickString(type,"BLACKANDWHITE",MagickPathExtent);
             break;
           }
           default:
@@ -1713,8 +1683,8 @@ static MagickBooleanType WritePNMImage(const ImageInfo *image_info,Image *image,
           "DEPTH %.20g\nMAXVAL %.20g\n",(double) packet_size,(double)
           ((MagickOffsetType) GetQuantumRange(image->depth)));
         (void) WriteBlobString(image,buffer);
-        (void) FormatLocaleString(buffer,MagickPathExtent,"TUPLTYPE %s\nENDHDR\n",
-          type);
+        (void) FormatLocaleString(buffer,MagickPathExtent,
+          "TUPLTYPE %s\nENDHDR\n",type);
         (void) WriteBlobString(image,buffer);
       }
     /*