Skip to content

Commit b7f3b3b

Browse files
committed
Added custom end of file check to the pnm encoder to allow a missing final newline
1 parent 485c42c commit b7f3b3b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

coders/pnm.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,24 @@ static unsigned int PNMInteger(Image *image,CommentInfo *comment_info,
219219
}
220220
c=ReadBlobByte(image);
221221
if (c == EOF)
222-
return(0);
222+
return(value);
223223
}
224224
if (c == (int) '#')
225225
c=PNMComment(image,comment_info);
226226
return(value);
227227
}
228228

229+
static inline MagickBooleanType PNMEOFBlob(const Image *image,ssize_t x,ssize_t y)
230+
{
231+
if (EOFBlob(image) == MagickFalse)
232+
return(MagickFalse);
233+
if (x < (ssize_t) image->columns-1)
234+
return(MagickTrue);
235+
if (y != (ssize_t) image->rows-1)
236+
return(MagickTrue);
237+
return(MagickFalse);
238+
}
239+
229240
static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
230241
{
231242
#define ThrowPNMException(exception,message) \
@@ -497,7 +508,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
497508
{
498509
SetPixelRed(q,PNMInteger(image,&comment_info,2) == 0 ?
499510
QuantumRange : 0);
500-
if (EOFBlob(image) != MagickFalse)
511+
if (PNMEOFBlob(image,x,y) != MagickFalse)
501512
break;
502513
SetPixelGreen(q,GetPixelRed(q));
503514
SetPixelBlue(q,GetPixelRed(q));
@@ -512,7 +523,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
512523
if (status == MagickFalse)
513524
break;
514525
}
515-
if (EOFBlob(image) != MagickFalse)
526+
if (PNMEOFBlob(image,x,y) != MagickFalse)
516527
break;
517528
}
518529
image->type=BilevelType;
@@ -542,7 +553,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
542553
{
543554
intensity=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10),
544555
max_value);
545-
if (EOFBlob(image) != MagickFalse)
556+
if (PNMEOFBlob(image,x,y) != MagickFalse)
546557
break;
547558
SetPixelRed(q,intensity);
548559
SetPixelGreen(q,GetPixelRed(q));
@@ -558,7 +569,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
558569
if (status == MagickFalse)
559570
break;
560571
}
561-
if (EOFBlob(image) != MagickFalse)
572+
if (PNMEOFBlob(image,x,y) != MagickFalse)
562573
break;
563574
}
564575
image->type=GrayscaleType;
@@ -587,7 +598,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
587598

588599
pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10),
589600
max_value);
590-
if (EOFBlob(image) != MagickFalse)
601+
if (PNMEOFBlob(image,x,y) != MagickFalse)
591602
break;
592603
SetPixelRed(q,pixel);
593604
pixel=ScaleAnyToQuantum(PNMInteger(image,&comment_info,10),
@@ -607,7 +618,7 @@ static Image *ReadPNMImage(const ImageInfo *image_info,ExceptionInfo *exception)
607618
if (status == MagickFalse)
608619
break;
609620
}
610-
if (EOFBlob(image) != MagickFalse)
621+
if (PNMEOFBlob(image,x,y) != MagickFalse)
611622
break;
612623
}
613624
break;

0 commit comments

Comments
 (0)